-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathserializable_test.py
More file actions
102 lines (75 loc) · 2.54 KB
/
serializable_test.py
File metadata and controls
102 lines (75 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from dataclasses import dataclass
from test.unit.base import ClientBaseCase
from typing import Optional
from linode_api4 import Base, JSONObject, Property
class JSONObjectTest(ClientBaseCase):
def test_serialize_optional(self):
@dataclass
class Foo(JSONObject):
always_include = {"foo"}
foo: Optional[str] = None
bar: Optional[str] = None
baz: str = None
foo = Foo().dict
assert foo["foo"] is None
assert "bar" not in foo
assert foo["baz"] is None
foo = Foo(foo="test", bar="test2", baz="test3").dict
assert foo["foo"] == "test"
assert foo["bar"] == "test2"
assert foo["baz"] == "test3"
def test_serialize_optional_include_None(self):
@dataclass
class Foo(JSONObject):
include_none_values = True
foo: Optional[str] = None
bar: Optional[str] = None
baz: str = None
foo = Foo().dict
assert foo["foo"] is None
assert foo["bar"] is None
assert foo["baz"] is None
foo = Foo(foo="test", bar="test2", baz="test3").dict
assert foo["foo"] == "test"
assert foo["bar"] == "test2"
assert foo["baz"] == "test3"
def test_serialize_put_class(self):
"""
Ensures that the JSONObject put_class ClassVar functions as expected.
"""
@dataclass
class SubStructOptions(JSONObject):
test1: Optional[str] = None
@dataclass
class SubStruct(JSONObject):
put_class = SubStructOptions
test1: str = ""
test2: int = 0
class Model(Base):
api_endpoint = "/foo/bar"
properties = {
"id": Property(identifier=True),
"substruct": Property(mutable=True, json_object=SubStruct),
}
mock_response = {
"id": 123,
"substruct": {
"test1": "abc",
"test2": 321,
},
}
with self.mock_get(mock_response) as mock:
obj = self.client.load(Model, 123)
assert mock.called
assert obj.id == 123
assert obj.substruct.test1 == "abc"
assert obj.substruct.test2 == 321
obj.substruct.test1 = "cba"
with self.mock_put(mock_response) as mock:
obj.save()
assert mock.called
assert mock.call_data == {
"substruct": {
"test1": "cba",
}
}