Skip to content

Commit 263e187

Browse files
author
Joel Collins
committed
Unit tests for server types
1 parent cab443b commit 263e187

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

tests/test_server_types.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from labthings.server import types
2+
3+
4+
def test_make_primative():
5+
from fractions import Fraction
6+
7+
assert types.make_primative(Fraction(5, 2)) == 2.5
8+
9+
10+
def test_value_to_field():
11+
from labthings.server import fields
12+
13+
# Test arrays of data
14+
d1 = [1, 2, 3]
15+
gen_field = types.value_to_field(d1)
16+
expected_field = fields.List(fields.Int())
17+
18+
assert gen_field._serialize(d1, None, None) == expected_field._serialize(
19+
d1, None, None
20+
)
21+
22+
# Test single values
23+
d2 = "String"
24+
gen_field_2 = types.value_to_field(d2)
25+
expected_field_2 = fields.String(example="String")
26+
27+
assert gen_field_2._serialize(d2, None, None) == expected_field_2._serialize(
28+
d2, None, None
29+
)
30+
31+
32+
def test_data_dict_to_schema():
33+
from labthings.server import fields
34+
from fractions import Fraction
35+
from marshmallow import Schema
36+
37+
d = {
38+
"val1": Fraction(5, 2),
39+
"map1": {
40+
"subval1": "Hello",
41+
"subval2": False,
42+
},
43+
"val2": 5,
44+
"val3": [1, 2, 3, 4],
45+
"val4": range(1, 5),
46+
}
47+
48+
gen_schema_dict = types.data_dict_to_schema(d)
49+
expected_schema_dict = {
50+
"val1": fields.Float(),
51+
"map1": {
52+
"subval1": fields.String(),
53+
"subval2": fields.Boolean(),
54+
},
55+
"val2": fields.Integer(),
56+
"val3": fields.List(fields.Int()),
57+
"val4": fields.List(fields.Int()),
58+
}
59+
60+
gen_schema = Schema.from_dict(gen_schema_dict)()
61+
expected_schema = Schema.from_dict(expected_schema_dict)()
62+
63+
assert gen_schema.dump(d) == expected_schema.dump(d)

0 commit comments

Comments
 (0)