|
a |
|
b/tests/utils/test_bindings.py |
|
|
1 |
import pytest |
|
|
2 |
from confit import validate_arguments |
|
|
3 |
from confit.errors import ConfitValidationError |
|
|
4 |
|
|
|
5 |
from edsnlp.utils.bindings import BINDING_GETTERS, BINDING_SETTERS, AttributesArg |
|
|
6 |
|
|
|
7 |
|
|
|
8 |
def test_qualifier_validation(): |
|
|
9 |
@validate_arguments |
|
|
10 |
def fn(arg: AttributesArg): |
|
|
11 |
return arg |
|
|
12 |
|
|
|
13 |
assert fn("_.negated") == {"_.negated": True} |
|
|
14 |
assert fn(["_.negated", "_.event"]) == {"_.negated": True, "_.event": True} |
|
|
15 |
assert fn({"_.negated": True, "_.event": "DATE"}) == { |
|
|
16 |
"_.negated": True, |
|
|
17 |
"_.event": ["DATE"], |
|
|
18 |
} |
|
|
19 |
|
|
|
20 |
callback = lambda x: x # noqa: E731 |
|
|
21 |
|
|
|
22 |
assert fn(callback) is callback |
|
|
23 |
|
|
|
24 |
with pytest.raises(ConfitValidationError): |
|
|
25 |
fn(1) |
|
|
26 |
|
|
|
27 |
with pytest.raises(ConfitValidationError): |
|
|
28 |
fn({"_.negated": 1}) |
|
|
29 |
|
|
|
30 |
|
|
|
31 |
def test_bindings(): |
|
|
32 |
class custom: |
|
|
33 |
def __init__(self, value): |
|
|
34 |
self.value = value |
|
|
35 |
|
|
|
36 |
obj = custom([custom(1), custom(2)]) |
|
|
37 |
assert BINDING_GETTERS["value[0].value"](obj) == 1 |
|
|
38 |
assert BINDING_GETTERS[("value[0].value", 1)](obj) is True |
|
|
39 |
BINDING_SETTERS[("value[1].value", 3)](obj) |
|
|
40 |
assert obj.value[1].value == 3 |