Skip to content

Commit 755f043

Browse files
committed
add serialization unit tests
1 parent b665830 commit 755f043

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import pytest
2+
3+
from bayesflow.utils.serialization import deserialize, serializable, serialize
4+
5+
6+
@serializable
7+
class Foo:
8+
@classmethod
9+
def from_config(cls, config, custom_objects=None):
10+
return cls(**deserialize(config, custom_objects=custom_objects))
11+
12+
def get_config(self):
13+
return {}
14+
15+
16+
@serializable
17+
class Bar:
18+
@classmethod
19+
def from_config(cls, config, custom_objects=None):
20+
return cls(**deserialize(config, custom_objects=custom_objects))
21+
22+
def get_config(self):
23+
return {}
24+
25+
26+
@pytest.mark.parametrize("obj", [1, "1", True, False, None, ...])
27+
def test_primitives(obj):
28+
assert deserialize(serialize(obj)) == obj
29+
30+
31+
@pytest.mark.parametrize(
32+
"obj",
33+
[
34+
[1, 2, 3, "a", "b", None, True, ...],
35+
{"a": 1, "b": 2},
36+
],
37+
)
38+
def test_collections_of_primitives(obj):
39+
assert deserialize(serialize(obj)) == obj
40+
41+
42+
@pytest.mark.parametrize(
43+
"obj",
44+
[
45+
int,
46+
list,
47+
],
48+
)
49+
def test_builtin_types(obj):
50+
assert deserialize(serialize(obj)) is obj
51+
52+
53+
def test_custom_object():
54+
instance = Foo()
55+
56+
assert isinstance(deserialize(serialize(instance)), Foo)
57+
58+
59+
def test_custom_type():
60+
assert deserialize(serialize(Foo)) is Foo
61+
62+
63+
@pytest.mark.parametrize(
64+
"obj",
65+
[
66+
[int, list, tuple, bool],
67+
{"a": int, "b": list, "c": tuple, "d": bool},
68+
],
69+
)
70+
def test_collections_of_builtin_types(obj):
71+
assert deserialize(serialize(obj)) == obj
72+
73+
74+
@pytest.mark.parametrize(
75+
"obj",
76+
[
77+
[Foo, Bar],
78+
{"a": Foo, "b": Bar},
79+
],
80+
)
81+
def test_collection_of_custom_types(obj):
82+
assert deserialize(serialize(obj)) == obj

0 commit comments

Comments
 (0)