Skip to content

Commit cc859fc

Browse files
committed
add unit tests for schema
1 parent cad0809 commit cc859fc

File tree

4 files changed

+120
-5
lines changed

4 files changed

+120
-5
lines changed

pytest.ini

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[pytest]
2+
filterwarnings =
3+
# deprecation warning from attrdict dependency
4+
ignore:Using or importing the ABCs:DeprecationWarning

tests/test_schema.py

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import pytest
2+
3+
import marshmallow
4+
from marshmallow_dataclass import dataclass
5+
6+
from thinq2 import schema
7+
8+
9+
def test_base_schema_ignores_unknown_fields():
10+
model = schema.BaseSchema().load(dict(foo="bar"))
11+
assert model == {}
12+
13+
14+
def test_controller_can_be_constructed_from_dict(Controller, valid_data):
15+
controller = Controller(valid_data)
16+
assert isinstance(controller, Controller)
17+
18+
19+
def test_controller_can_be_constructed_from_kwargs(Controller, valid_data):
20+
controller = Controller(**valid_data)
21+
assert isinstance(controller, Controller)
22+
23+
24+
def test_controller_can_be_constructed_from_model(Controller, Model, valid_data):
25+
model = Model(**valid_data)
26+
controller = Controller(model)
27+
assert isinstance(controller, Controller)
28+
29+
30+
def test_controller_as_dict_equals_model_data(Controller, valid_data):
31+
controller = Controller(valid_data)
32+
assert vars(controller) == valid_data
33+
34+
35+
def test_controller_constructor_styles_are_equivalent(Controller, Model, valid_data):
36+
controller_from_dict = Controller(valid_data)
37+
controller_from_kwargs = Controller(**valid_data)
38+
controller_from_model = Controller(Model(**valid_data))
39+
assert vars(controller_from_dict) == vars(controller_from_kwargs)
40+
assert vars(controller_from_dict) == vars(controller_from_model)
41+
42+
43+
def test_controller_instances_with_identical_data_are_not_equal(Controller, valid_data):
44+
controller_a = Controller(valid_data)
45+
controller_b = Controller(valid_data)
46+
assert controller_a != controller_b
47+
48+
49+
def test_initialzer_decorator_sets_default_value(Controller, data_with_missing_quux):
50+
quux = 43
51+
52+
class QuuxedController(Controller):
53+
@schema.initializer
54+
def quux(self):
55+
return quux
56+
57+
controller = QuuxedController(**data_with_missing_quux)
58+
assert controller.quux == quux
59+
60+
61+
def test_initialzer_ignored_when_value_supplied(Controller, data_with_missing_quux):
62+
quux = 43
63+
data = {**data_with_missing_quux, **dict(quux=quux + 1)}
64+
65+
class QuuxedController(Controller):
66+
@schema.initializer
67+
def quux(self):
68+
return quux
69+
70+
controller = QuuxedController(data)
71+
assert controller.quux != quux
72+
73+
74+
# XXX - need proper test of child controllers for nested fields
75+
def test_child_controller_decoration(Controller, valid_data):
76+
pass
77+
78+
79+
@pytest.fixture
80+
def Model():
81+
@dataclass
82+
class Model:
83+
foo: str
84+
quux: int
85+
86+
return Model
87+
88+
89+
@pytest.fixture
90+
def Controller(Model):
91+
@schema.controller(Model)
92+
class Controller:
93+
pass
94+
95+
return Controller
96+
97+
98+
@pytest.fixture
99+
def valid_data():
100+
return dict(foo="bar", quux=42)
101+
102+
103+
@pytest.fixture
104+
def data_with_extra_field():
105+
return dict(foo="bar", quux=42, boz=True)
106+
107+
108+
@pytest.fixture
109+
def data_with_missing_quux():
110+
return dict(foo="bar")
111+
112+
113+
@pytest.fixture
114+
def data_with_wrong_quux_type():
115+
return dict(foo="bar", quux="42")

tests/test_thinqtt_python.py

-5
This file was deleted.

thinq2/schema.py

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def load(cls, data):
8585
def __dict__(self):
8686
return schema.dump(self._data)
8787

88+
8889
# XXX - should throw attribute exception if attr not in schema
8990
def __getattr__(self, attr):
9091
if not attr.startswith("_"):

0 commit comments

Comments
 (0)