Skip to content

Commit c2400c2

Browse files
committed
add serialization tests
1 parent ce3129e commit c2400c2

File tree

4 files changed

+105
-50
lines changed

4 files changed

+105
-50
lines changed

servicestack/clients.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import base64
2-
import datetime
32
import decimal
43
import inspect
54
import json

tests/test_client.py

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,55 @@ def create_EchoComplexTypes():
7575
)
7676

7777

78+
def assert_AllTypes(test: unittest.TestCase, dto: AllTypes):
79+
# print(type(dto))
80+
# print(vars(dto))
81+
test.assertEqual(dto.id, 1)
82+
test.assertEqual(dto.byte, 2)
83+
test.assertEqual(dto.short, 3)
84+
test.assertEqual(dto.int_, 4)
85+
test.assertEqual(dto.long, 5)
86+
test.assertEqual(dto.u_short, 6)
87+
test.assertEqual(dto.u_int, 7)
88+
test.assertEqual(dto.u_long, 8)
89+
test.assertEqual(dto.float_, 1.1)
90+
test.assertEqual(dto.double, 2.2)
91+
test.assertEqual(dto.decimal, 3.0)
92+
test.assertEqual(dto.string, "string")
93+
test.assertEqual(dto.date_time, datetime(2001, 1, 1, tzinfo=timezone.utc))
94+
test.assertEqual(dto.date_time_offset, datetime(2001, 1, 1, tzinfo=timezone.utc))
95+
test.assertEqual(dto.time_span, timedelta(hours=1))
96+
test.assertEqual(dto.guid, "ea762009b66c410b9bf5ce21ad519249")
97+
test.assertListEqual(dto.string_list, ["A", "B", "C"])
98+
test.assertListEqual(dto.string_array, ["D", "E", "F"])
99+
test.assertDictEqual(dto.string_map, {"A": "D", "B": "E", "C": "F"})
100+
test.assertDictEqual(dto.int_string_map, {1: "A", 2: "B", 3: "C"})
101+
test.assertEqual(dto.sub_type.id, 1)
102+
test.assertEqual(dto.sub_type.name, "name")
103+
104+
105+
def assert_AllCollectionTypes(test: unittest.TestCase, dto: AllCollectionTypes):
106+
test.assertListEqual(dto.int_array, [1, 2, 3])
107+
test.assertListEqual(dto.int_list, [1, 2, 3])
108+
test.assertListEqual(dto.string_array, ["A", "B", "C"])
109+
test.assertListEqual(dto.string_list, ["D", "E", "F"])
110+
test.assertEqual(dto.byte_array, b'ABC')
111+
test.assertEqual(len(dto.poco_array), 1)
112+
test.assertEqual(dto.poco_array[0].name, "pocoArray")
113+
test.assertEqual(len(dto.poco_lookup), 1)
114+
poco_lookup_values = dto.poco_lookup["A"]
115+
test.assertEqual(len(poco_lookup_values), 2)
116+
test.assertEqual(poco_lookup_values[0].name, "B")
117+
test.assertEqual(poco_lookup_values[1].name, "C")
118+
test.assertEqual(len(dto.poco_lookup_map), 1)
119+
poco_lookup_map_values = dto.poco_lookup_map["A"]
120+
test.assertEqual(len(poco_lookup_map_values), 1)
121+
poco_lookup_mapa_list = poco_lookup_map_values[0]
122+
test.assertEqual(len(poco_lookup_mapa_list), 2)
123+
test.assertEqual(poco_lookup_mapa_list["B"].name, "C")
124+
test.assertEqual(poco_lookup_mapa_list["D"].name, "E")
125+
126+
78127
client = create_test_client()
79128

80129

@@ -86,52 +135,10 @@ def assert_HelloAllTypesResponse(self, dto: HelloAllTypesResponse):
86135
self.assert_AllTypes(dto.all_types)
87136
self.assert_AllCollectionTypes(dto.all_collection_types)
88137

89-
def assert_AllTypes(self, dto: AllTypes):
90-
# print(type(dto))
91-
# print(vars(dto))
92-
self.assertEqual(dto.id, 1)
93-
self.assertEqual(dto.byte, 2)
94-
self.assertEqual(dto.short, 3)
95-
self.assertEqual(dto.int_, 4)
96-
self.assertEqual(dto.long, 5)
97-
self.assertEqual(dto.u_short, 6)
98-
self.assertEqual(dto.u_int, 7)
99-
self.assertEqual(dto.u_long, 8)
100-
self.assertEqual(dto.float_, 1.1)
101-
self.assertEqual(dto.double, 2.2)
102-
self.assertEqual(dto.decimal, 3.0)
103-
self.assertEqual(dto.string, "string")
104-
self.assertEqual(dto.date_time, datetime(2001, 1, 1, tzinfo=timezone.utc))
105-
self.assertEqual(dto.date_time_offset, datetime(2001, 1, 1, tzinfo=timezone.utc))
106-
self.assertEqual(dto.time_span, timedelta(hours=1))
107-
self.assertEqual(dto.guid, "ea762009b66c410b9bf5ce21ad519249")
108-
self.assertListEqual(dto.string_list, ["A", "B", "C"])
109-
self.assertListEqual(dto.string_array, ["D", "E", "F"])
110-
self.assertDictEqual(dto.string_map, {"A": "D", "B": "E", "C": "F"})
111-
self.assertDictEqual(dto.int_string_map, {1: "A", 2: "B", 3: "C"})
112-
self.assertEqual(dto.sub_type.id, 1)
113-
self.assertEqual(dto.sub_type.name, "name")
138+
def assert_AllTypes(self, dto: AllTypes): assert_AllTypes(self, dto)
114139

115140
def assert_AllCollectionTypes(self, dto: AllCollectionTypes):
116-
self.assertListEqual(dto.int_array, [1, 2, 3])
117-
self.assertListEqual(dto.int_list, [1, 2, 3])
118-
self.assertListEqual(dto.string_array, ["A", "B", "C"])
119-
self.assertListEqual(dto.string_list, ["D", "E", "F"])
120-
self.assertEqual(dto.byte_array, b'ABC')
121-
self.assertEqual(len(dto.poco_array), 1)
122-
self.assertEqual(dto.poco_array[0].name, "pocoArray")
123-
self.assertEqual(len(dto.poco_lookup), 1)
124-
poco_lookup_values = dto.poco_lookup["A"]
125-
self.assertEqual(len(poco_lookup_values), 2)
126-
self.assertEqual(poco_lookup_values[0].name, "B")
127-
self.assertEqual(poco_lookup_values[1].name, "C")
128-
self.assertEqual(len(dto.poco_lookup_map), 1)
129-
poco_lookup_map_values = dto.poco_lookup_map["A"]
130-
self.assertEqual(len(poco_lookup_map_values), 1)
131-
poco_lookup_mapa_list = poco_lookup_map_values[0]
132-
self.assertEqual(len(poco_lookup_mapa_list), 2)
133-
self.assertEqual(poco_lookup_mapa_list["B"].name, "C")
134-
self.assertEqual(poco_lookup_mapa_list["D"].name, "E")
141+
assert_AllCollectionTypes(self, dto)
135142

136143
def assert_EchoComplexTypes(self, dto: EchoComplexTypes):
137144
self.assertEqual(dto.sub_type.id, 1)

tests/test_client_auth.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"""
33

44
import unittest
5-
from datetime import datetime
65

76
from servicestack import WebServiceException
87
from .config import *
@@ -92,7 +91,7 @@ def test_can_use_on_authentication_required_to_fetch_token_after_expired_token(s
9291
]
9392

9493
create_expired_jwt = create_jwt()
95-
create_expired_jwt.jwt_expiry = datetime(2000, 1, 1)
94+
create_expired_jwt.jwt_expiry = datetime.datetime(2000, 1, 1)
9695
expired_jwt: CreateJwtResponse = client.post(create_expired_jwt)
9796

9897
client.bearer_token = expired_jwt.token
@@ -109,7 +108,7 @@ def test_can_use_refresh_token_to_fetch_token_after_expired_token(self):
109108
client.set_credentials(None, None)
110109

111110
create_expired_jwt = create_jwt()
112-
create_expired_jwt.jwt_expiry = datetime(2000, 1, 1)
111+
create_expired_jwt.jwt_expiry = datetime.datetime(2000, 1, 1)
113112
expired_jwt: CreateJwtResponse = client.post(create_expired_jwt)
114113

115114
client.bearer_token = expired_jwt.token
@@ -125,7 +124,7 @@ def test_can_reauthenticate_after_an_auto_refresh_access_token(self):
125124
refresh_token = auth_response.refresh_token
126125

127126
create_expired_jwt = create_jwt()
128-
create_expired_jwt.jwt_expiry = datetime(2000, 1, 1)
127+
create_expired_jwt.jwt_expiry = datetime.datetime(2000, 1, 1)
129128
expired_jwt: CreateJwtResponse = client.post(create_expired_jwt)
130129
bearer_token = expired_jwt.token
131130

@@ -183,7 +182,7 @@ def test_expires_refresh_token_throws_RefreshTokenException(self):
183182
client = create_test_client()
184183

185184
create_expired_jwt = create_jwt()
186-
create_expired_jwt.jwt_expiry = datetime(2000, 1, 1)
185+
create_expired_jwt.jwt_expiry = datetime.datetime(2000, 1, 1)
187186
expired_jwt: CreateJwtResponse = client.post(create_expired_jwt)
188187
client.refresh_token = expired_jwt.token
189188

tests/test_serialization.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""Serialization Tests
2+
"""
3+
import unittest
4+
5+
from servicestack.clients import append_querystring
6+
from servicestack.utils import *
7+
from tests.config import *
8+
from .dtos import *
9+
from tests.test_client import create_AllTypes, create_AllCollectionTypes, \
10+
assert_AllTypes, assert_AllCollectionTypes
11+
12+
JSON_ALL_TYPES = '{"id":1,"byte":2,"short":3,"int":4,"long":5,"uShort":6,"uInt":7,"uLong":8,"float":1.1,"double":2.2,"decimal":3.0,"string":"string","dateTime":"\/Date(978307200000)\/","timeSpan":"PT1H","dateTimeOffset":"\/Date(978307200000)\/","guid":"ea762009b66c410b9bf5ce21ad519249","char":"c","keyValuePair":{},"stringList":["A","B","C"],"stringArray":["D","E","F"],"stringMap":{"A":"D","B":"E","C":"F"},"intStringMap":{"1":"A","2":"B","3":"C"},"subType":{"id":1,"name":"name"}}'
13+
JSON_ALL_COLLECTION_TYPES = '{"intArray":[1,2,3],"intList":[4,5,6],"stringArray":["A","B","C"],"stringList":["D","E","F"],byteArray:"QUJD","pocoArray":[{"name":"pocoArray"}],"pocoList":[{"name":"pocoList"}],"pocoLookup":{"A":[{"name":"B"},{"name":"C"}]},"pocoLookupMap":{"A":[{"B":{"name":"C"}},{"D":{"name":"E"}}]}}'
14+
15+
16+
class TestSerialization(unittest.TestCase):
17+
18+
def test_does_serialize_AllTypes(self):
19+
dto = create_AllTypes()
20+
json_str = to_json(dto)
21+
json_obj = from_json(AllTypes, json_str)
22+
assert_AllTypes(self, json_obj)
23+
24+
def test_does_deserialize_AllTypes(self):
25+
json_obj = from_json(AllTypes, JSON_ALL_TYPES)
26+
assert_AllTypes(self, json_obj)
27+
28+
def test_does_serialize_AllCollectionTypes(self):
29+
dto = create_AllCollectionTypes()
30+
json_str = to_json(dto)
31+
json_obj = from_json(AllCollectionTypes, json_str)
32+
assert_AllCollectionTypes(self, json_obj)
33+
34+
def test_does_serialize_HelloAllTypes(self):
35+
dto = HelloAllTypes(
36+
name="HelloAllTypes",
37+
all_types=create_AllTypes(),
38+
all_collection_types=create_AllCollectionTypes())
39+
json_str = to_json(dto)
40+
json_obj: HelloAllTypes = from_json(HelloAllTypes, json_str)
41+
self.assertEqual(dto.name, json_obj.name)
42+
assert_AllTypes(self, json_obj.all_types)
43+
assert_AllCollectionTypes(self, json_obj.all_collection_types)
44+
45+
def test_does_serialize_inheritance(self):
46+
dto = HelloWithInheritance(name="foo", id=1)
47+
json_str = to_json(dto)
48+
json_obj: HelloWithInheritance = from_json(HelloWithInheritance, json_str)
49+
self.assertEqual(json_obj.name, "foo")
50+
self.assertEqual(json_obj.id, 1)

0 commit comments

Comments
 (0)