11from datetime import date
22from uuid import UUID
33
4+ import pendulum
45import pytest
56
6- import pendulum
77from kiota_serialization_json .json_serialization_writer import JsonSerializationWriter
8-
98from ..helpers import OfficeLocation , User , User2
109
1110
@@ -72,20 +71,23 @@ def test_write_uuid_value():
7271 content = json_serialization_writer .get_serialized_content ()
7372 content_string = content .decode ('utf-8' )
7473 assert content_string == '{"id": "8f841f30-e6e3-439a-a812-ebd369559c36"}'
75-
74+
75+
7676def test_write_uuid_value_with_valid_string ():
7777 json_serialization_writer = JsonSerializationWriter ()
7878 json_serialization_writer .write_uuid_value ("id" , "8f841f30-e6e3-439a-a812-ebd369559c36" )
7979 content = json_serialization_writer .get_serialized_content ()
8080 content_string = content .decode ('utf-8' )
8181 assert content_string == '{"id": "8f841f30-e6e3-439a-a812-ebd369559c36"}'
82-
82+
83+
8384def test_write_uuid_value_with_invalid_string ():
8485 with pytest .raises (ValueError ) as excinfo :
8586 json_serialization_writer = JsonSerializationWriter ()
8687 json_serialization_writer .write_uuid_value ("id" , "invalid-uuid-string" )
8788 assert "Invalid UUID string value found for property id" in str (excinfo .value )
88-
89+
90+
8991def test_write_datetime_value ():
9092 json_serialization_writer = JsonSerializationWriter ()
9193 json_serialization_writer .write_datetime_value (
@@ -94,7 +96,8 @@ def test_write_datetime_value():
9496 content = json_serialization_writer .get_serialized_content ()
9597 content_string = content .decode ('utf-8' )
9698 assert content_string == '{"updatedAt": "2022-01-27T12:59:45.596117+00:00"}'
97-
99+
100+
98101def test_write_datetime_value_valid_string ():
99102 json_serialization_writer = JsonSerializationWriter ()
100103 json_serialization_writer .write_datetime_value (
@@ -103,7 +106,8 @@ def test_write_datetime_value_valid_string():
103106 content = json_serialization_writer .get_serialized_content ()
104107 content_string = content .decode ('utf-8' )
105108 assert content_string == '{"updatedAt": "2022-01-27T12:59:45.596117+00:00"}'
106-
109+
110+
107111def test_write_datetime_value_valid_string ():
108112 with pytest .raises (ValueError ) as excinfo :
109113 json_serialization_writer = JsonSerializationWriter ()
@@ -112,6 +116,7 @@ def test_write_datetime_value_valid_string():
112116 )
113117 assert "Invalid datetime string value found for property updatedAt" in str (excinfo .value )
114118
119+
115120def test_write_timedelta_value ():
116121 json_serialization_writer = JsonSerializationWriter ()
117122 json_serialization_writer .write_timedelta_value (
@@ -121,7 +126,8 @@ def test_write_timedelta_value():
121126 content = json_serialization_writer .get_serialized_content ()
122127 content_string = content .decode ('utf-8' )
123128 assert content_string == '{"diff": "2:00:00"}'
124-
129+
130+
125131def test_write_timedelta_value_valid_string ():
126132 json_serialization_writer = JsonSerializationWriter ()
127133 json_serialization_writer .write_timedelta_value (
@@ -131,7 +137,8 @@ def test_write_timedelta_value_valid_string():
131137 content = json_serialization_writer .get_serialized_content ()
132138 content_string = content .decode ('utf-8' )
133139 assert content_string == '{"diff": "2:00:00"}'
134-
140+
141+
135142def test_write_timedelta_value_invalid_string ():
136143 with pytest .raises (ValueError ) as excinfo :
137144 json_serialization_writer = JsonSerializationWriter ()
@@ -140,7 +147,7 @@ def test_write_timedelta_value_invalid_string():
140147 "invalid-timedelta-string"
141148 )
142149 assert "Invalid timedelta string value found for property diff" in str (excinfo .value )
143-
150+
144151
145152def test_write_date_value ():
146153 json_serialization_writer = JsonSerializationWriter ()
@@ -149,19 +156,22 @@ def test_write_date_value():
149156 content_string = content .decode ('utf-8' )
150157 assert content_string == '{"birthday": "2000-09-04"}'
151158
159+
152160def test_write_date_value_valid_string ():
153161 json_serialization_writer = JsonSerializationWriter ()
154162 json_serialization_writer .write_date_value ("birthday" , "2000-09-04" )
155163 content = json_serialization_writer .get_serialized_content ()
156164 content_string = content .decode ('utf-8' )
157165 assert content_string == '{"birthday": "2000-09-04"}'
158-
166+
167+
159168def test_write_date_value_invalid_string ():
160169 with pytest .raises (ValueError ) as excinfo :
161170 json_serialization_writer = JsonSerializationWriter ()
162171 json_serialization_writer .write_date_value ("birthday" , "invalid-date-string" )
163172 assert "Invalid date string value found for property birthday" in str (excinfo .value )
164173
174+
165175def test_write_time_value ():
166176 json_serialization_writer = JsonSerializationWriter ()
167177 json_serialization_writer .write_time_value (
@@ -172,6 +182,7 @@ def test_write_time_value():
172182 content_string = content .decode ('utf-8' )
173183 assert content_string == '{"time": "12:59:45.596117"}'
174184
185+
175186def test_write_time_value_valid_string ():
176187 json_serialization_writer = JsonSerializationWriter ()
177188 json_serialization_writer .write_time_value (
@@ -181,7 +192,8 @@ def test_write_time_value_valid_string():
181192 content = json_serialization_writer .get_serialized_content ()
182193 content_string = content .decode ('utf-8' )
183194 assert content_string == '{"time": "12:59:45.596117"}'
184-
195+
196+
185197def test_write_time_value_invalid_string ():
186198 with pytest .raises (ValueError ) as excinfo :
187199 json_serialization_writer = JsonSerializationWriter ()
@@ -191,6 +203,7 @@ def test_write_time_value_invalid_string():
191203 )
192204 assert "Invalid time string value found for property time" in str (excinfo .value )
193205
206+
194207def test_write_collection_of_primitive_values ():
195208 json_serialization_writer = JsonSerializationWriter ()
196209 json_serialization_writer .write_collection_of_primitive_values (
@@ -206,9 +219,9 @@ def test_write_collection_of_object_values(user_1, user_2):
206219 json_serialization_writer .write_collection_of_object_values ("users" , [user_1 , user_2 ])
207220 content = json_serialization_writer .get_serialized_content ()
208221 content_string = content .decode ('utf-8' )
209- assert content_string == '{"users": [{"id": "8f841f30-e6e3-439a-a812-ebd369559c36", ' \
210- '"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}, ' \
211- '{"display_name": "John Doe", "age": 32}]}'
222+ assert content_string == '{"users": [{"id": "8f841f30-e6e3-439a-a812-ebd369559c36", ' \
223+ '"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}, ' \
224+ '{"display_name": "John Doe", "age": 32}]}'
212225
213226
214227def test_write_collection_of_enum_values ():
@@ -226,8 +239,8 @@ def test_write_object_value(user_1):
226239 json_serialization_writer .write_object_value ("user1" , user_1 )
227240 content = json_serialization_writer .get_serialized_content ()
228241 content_string = content .decode ('utf-8' )
229- assert content_string == '{"user1": {"id": "8f841f30-e6e3-439a-a812-ebd369559c36", ' \
230- '"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}}'
242+ assert content_string == '{"user1": {"id": "8f841f30-e6e3-439a-a812-ebd369559c36", ' \
243+ '"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}}'
231244
232245
233246def test_write_enum_value ():
@@ -246,30 +259,84 @@ def test_write_null_value():
246259 assert content_string == '{"mobilePhone": null}'
247260
248261
262+ import json
263+ import pytest
264+
265+
266+ # Assuming JsonSerializationWriter is defined elsewhere
267+ # from your_module import JsonSerializationWriter
268+
269+ @pytest .fixture
270+ def user_1 ():
271+ return {
272+ "id" : "8f841f30-e6e3-439a-a812-ebd369559c36" ,
273+ "updated_at" : pendulum .datetime (2022 , 1 , 27 , 12 , 59 , 45 , 596117 , tz = 'UTC' ),
274+ "is_active" : True
275+ }
276+
277+
278+ @pytest .fixture
279+ def user_2 ():
280+ return {
281+ "display_name" : "John Doe" ,
282+ "age" : 32
283+ }
284+
285+
249286def test_write_additional_data_value (user_1 , user_2 ):
250287 json_serialization_writer = JsonSerializationWriter ()
288+
251289 json_serialization_writer .write_additional_data_value (
252290 {
253291 "@odata.context" : "https://graph.microsoft.com/v1.0/$metadata#users/$entity" ,
254292 "businessPhones" : ["+1 205 555 0108" ],
255293 "manager" : user_1 ,
256294 "approvers" : [user_1 , user_2 ],
257- "created_at" : date (2022 , 1 , 27 ),
295+ "created_at" : date (2022 , 1 , 27 ).isoformat (),
296+ "updated_at" : pendulum .datetime (2024 , 9 , 24 , tz = 'UTC' ).isoformat (),
258297 "data" : {
259298 "groups" : [{
260299 "friends" : [user_2 ]
261300 }]
262301 }
263302 }
264303 )
304+
305+ # Get serialized content
265306 content = json_serialization_writer .get_serialized_content ()
266307 content_string = content .decode ('utf-8' )
267- assert content_string == '{"@odata.context": ' \
268- '"https://graph.microsoft.com/v1.0/$metadata#users/$entity", ' \
269- '"businessPhones": ["+1 205 555 0108"], ' \
270- '"manager": {"id": "8f841f30-e6e3-439a-a812-ebd369559c36", ' \
271- '"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}, ' \
272- '"approvers": [{"id": "8f841f30-e6e3-439a-a812-ebd369559c36", ' \
273- '"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}, ' \
274- '{"display_name": "John Doe", "age": 32}], "created_at": "2022-01-27", ' \
275- '"data": {"groups": [{"friends": [{"display_name": "John Doe", "age": 32}]}]}}'
308+
309+ # Define the expected JSON as a Python dictionary
310+ expected_json = {
311+ "@odata.context" : "https://graph.microsoft.com/v1.0/$metadata#users/$entity" ,
312+ "businessPhones" : ["+1 205 555 0108" ],
313+ "manager" : {
314+ "id" : "8f841f30-e6e3-439a-a812-ebd369559c36" ,
315+ "updated_at" : "2022-01-27T12:59:45.596117+00:00" ,
316+ "is_active" : True
317+ },
318+ "approvers" : [
319+ {
320+ "id" : "8f841f30-e6e3-439a-a812-ebd369559c36" ,
321+ "updated_at" : "2022-01-27T12:59:45.596117+00:00" ,
322+ "is_active" : True
323+ },
324+ {
325+ "display_name" : "John Doe" ,
326+ "age" : 32
327+ }
328+ ],
329+ "created_at" : "2022-01-27" ,
330+ "updated_at" : "2024-09-24T00:00:00+00:00" ,
331+ "data" : {
332+ "groups" : [{
333+ "friends" : [{
334+ "display_name" : "John Doe" ,
335+ "age" : 32
336+ }]
337+ }]
338+ }
339+ }
340+
341+ # Assert that the serialized content matches the expected JSON
342+ assert json .loads (content_string ) == expected_json
0 commit comments