Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

Commit 6ab9150

Browse files
committed
reformat
1 parent 27fa193 commit 6ab9150

File tree

2 files changed

+79
-46
lines changed

2 files changed

+79
-46
lines changed

kiota_serialization_json/json_serialization_writer.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def __init__(self) -> None:
2323
self.value: Any = None
2424

2525
self._on_start_object_serialization: Optional[Callable[[Parsable, SerializationWriter],
26-
None]] = None
26+
None]] = None
2727
self._on_before_object_serialization: Optional[Callable[[Parsable], None]] = None
2828
self._on_after_object_serialization: Optional[Callable[[Parsable], None]] = None
2929

@@ -79,7 +79,7 @@ def write_uuid_value(self, key: Optional[str], value: Optional[UUID]) -> None:
7979
"""Writes the specified uuid value to the stream with an optional given key.
8080
Args:
8181
key (Optional[str]): The key to be used for the written value. May be null.
82-
value (Optional[UUId]): The uuid value to be written.
82+
value (Optional[UUID]): The uuid value to be written.
8383
"""
8484
if isinstance(value, UUID):
8585
if key:
@@ -106,9 +106,9 @@ def write_datetime_value(self, key: Optional[str], value: Optional[datetime]) ->
106106
"""
107107
if isinstance(value, datetime):
108108
if key:
109-
self.writer[key] = str(value.isoformat())
109+
self.writer[key] = value.isoformat()
110110
else:
111-
self.value = str(value.isoformat())
111+
self.value = value.isoformat()
112112
elif isinstance(value, str):
113113
try:
114114
pendulum.parse(value)
@@ -238,7 +238,7 @@ def write_collection_of_enum_values(
238238
"""Writes the specified collection of enum values to the stream with an optional given key.
239239
Args:
240240
key (Optional[str]): The key to be used for the written value. May be null.
241-
values Optional[List[Enum]): The enum values to be written.
241+
values (Optional[List[Enum]]): The enum values to be written.
242242
"""
243243
if isinstance(values, list):
244244
result = []
@@ -359,7 +359,7 @@ def __write_dict_value(self, key: Optional[str], value: Dict[str, Any]) -> None:
359359
def write_additional_data_value(self, value: Dict[str, Any]) -> None:
360360
"""Writes the specified additional data to the stream.
361361
Args:
362-
value (Dict[str, Any]): he additional data to be written.
362+
value (Dict[str, Any]): The additional data to be written.
363363
"""
364364
if isinstance(value, dict):
365365
for key, val in value.items():
@@ -389,35 +389,35 @@ def get_serialized_content(self) -> bytes:
389389
def on_before_object_serialization(self) -> Optional[Callable[[Parsable], None]]:
390390
"""Gets the callback called before the object gets serialized.
391391
Returns:
392-
Optional[Callable[[Parsable], None]]:the callback called before the object
392+
Optional[Callable[[Parsable], None]]: The callback called before the object
393393
gets serialized.
394394
"""
395395
return self._on_before_object_serialization
396396

397397
@on_before_object_serialization.setter
398398
def on_before_object_serialization(self, value: Optional[Callable[[Parsable], None]]) -> None:
399-
"""Sets the callback called before the objects gets serialized.
399+
"""Sets the callback called before the objects get serialized.
400400
Args:
401-
value (Optional[Callable[[Parsable], None]]): the callback called before the objects
402-
gets serialized.
401+
value (Optional[Callable[[Parsable], None]]): The callback called before the objects
402+
get serialized.
403403
"""
404404
self._on_before_object_serialization = value
405405

406406
@property
407407
def on_after_object_serialization(self) -> Optional[Callable[[Parsable], None]]:
408408
"""Gets the callback called after the object gets serialized.
409409
Returns:
410-
Optional[Optional[Callable[[Parsable], None]]]: the callback called after the object
410+
Optional[Callable[[Parsable], None]]: The callback called after the object
411411
gets serialized.
412412
"""
413413
return self._on_after_object_serialization
414414

415415
@on_after_object_serialization.setter
416416
def on_after_object_serialization(self, value: Optional[Callable[[Parsable], None]]) -> None:
417-
"""Sets the callback called after the objects gets serialized.
417+
"""Sets the callback called after the objects get serialized.
418418
Args:
419-
value (Optional[Callable[[Parsable], None]]): the callback called after the objects
420-
gets serialized.
419+
value (Optional[Callable[[Parsable], None]]): The callback called after the objects
420+
get serialized.
421421
"""
422422
self._on_after_object_serialization = value
423423

@@ -427,7 +427,7 @@ def on_start_object_serialization(
427427
) -> Optional[Callable[[Parsable, SerializationWriter], None]]:
428428
"""Gets the callback called right after the serialization process starts.
429429
Returns:
430-
Optional[Callable[[Parsable, SerializationWriter], None]]: the callback called
430+
Optional[Callable[[Parsable, SerializationWriter], None]]: The callback called
431431
right after the serialization process starts.
432432
"""
433433
return self._on_start_object_serialization
@@ -438,7 +438,7 @@ def on_start_object_serialization(
438438
) -> None:
439439
"""Sets the callback called right after the serialization process starts.
440440
Args:
441-
value (Optional[Callable[[Parsable, SerializationWriter], None]]): the callback
441+
value (Optional[Callable[[Parsable, SerializationWriter], None]]): The callback
442442
called right after the serialization process starts.
443443
"""
444444
self._on_start_object_serialization = value
@@ -471,7 +471,10 @@ def write_any_value(self, key: Optional[str], value: Any) -> Any:
471471
self.write_collection_of_object_values(key, value)
472472
elif all(isinstance(x, Enum) for x in value):
473473
self.write_collection_of_enum_values(key, value)
474-
elif all(any(isinstance(x, primitive_type) for primitive_type in PRIMITIVE_TYPES) for x in value):
474+
elif all(
475+
any(isinstance(x, primitive_type) for primitive_type in PRIMITIVE_TYPES)
476+
for x in value
477+
):
475478
self.write_collection_of_primitive_values(key, value)
476479
elif all(isinstance(x, dict) for x in value):
477480
self.__write_collection_of_dict_values(key, value)

tests/unit/test_json_serialization_writer.py

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from uuid import UUID
33

44
import pytest
5-
65
import pendulum
76
from kiota_serialization_json.json_serialization_writer import JsonSerializationWriter
87

@@ -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+
7676
def 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+
8384
def 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+
8991
def 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+
98101
def test_write_datetime_value_valid_string():
99102
json_serialization_writer = JsonSerializationWriter()
100103
json_serialization_writer.write_datetime_value(
@@ -103,25 +106,31 @@ 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-
107-
def test_write_datetime_value_valid_string():
109+
110+
111+
def test_write_datetime_value_invalid_string():
108112
with pytest.raises(ValueError) as excinfo:
109113
json_serialization_writer = JsonSerializationWriter()
110114
json_serialization_writer.write_datetime_value(
111115
"updatedAt", "invalid-datetime-string"
112116
)
113117
assert "Invalid datetime string value found for property updatedAt" in str(excinfo.value)
114118

119+
115120
def test_write_timedelta_value():
116121
json_serialization_writer = JsonSerializationWriter()
117122
json_serialization_writer.write_timedelta_value(
118123
"diff",
119-
(pendulum.parse('2022-01-27T12:59:45.596117') - pendulum.parse('2022-01-27T10:59:45.596117')).as_timedelta()
124+
(
125+
pendulum.parse('2022-01-27T12:59:45.596117') -
126+
pendulum.parse('2022-01-27T10:59:45.596117')
127+
).as_timedelta()
120128
)
121129
content = json_serialization_writer.get_serialized_content()
122130
content_string = content.decode('utf-8')
123131
assert content_string == '{"diff": "2:00:00"}'
124-
132+
133+
125134
def test_write_timedelta_value_valid_string():
126135
json_serialization_writer = JsonSerializationWriter()
127136
json_serialization_writer.write_timedelta_value(
@@ -131,7 +140,8 @@ def test_write_timedelta_value_valid_string():
131140
content = json_serialization_writer.get_serialized_content()
132141
content_string = content.decode('utf-8')
133142
assert content_string == '{"diff": "2:00:00"}'
134-
143+
144+
135145
def test_write_timedelta_value_invalid_string():
136146
with pytest.raises(ValueError) as excinfo:
137147
json_serialization_writer = JsonSerializationWriter()
@@ -140,7 +150,7 @@ def test_write_timedelta_value_invalid_string():
140150
"invalid-timedelta-string"
141151
)
142152
assert "Invalid timedelta string value found for property diff" in str(excinfo.value)
143-
153+
144154

145155
def test_write_date_value():
146156
json_serialization_writer = JsonSerializationWriter()
@@ -149,19 +159,22 @@ def test_write_date_value():
149159
content_string = content.decode('utf-8')
150160
assert content_string == '{"birthday": "2000-09-04"}'
151161

162+
152163
def test_write_date_value_valid_string():
153164
json_serialization_writer = JsonSerializationWriter()
154165
json_serialization_writer.write_date_value("birthday", "2000-09-04")
155166
content = json_serialization_writer.get_serialized_content()
156167
content_string = content.decode('utf-8')
157168
assert content_string == '{"birthday": "2000-09-04"}'
158-
169+
170+
159171
def test_write_date_value_invalid_string():
160172
with pytest.raises(ValueError) as excinfo:
161173
json_serialization_writer = JsonSerializationWriter()
162174
json_serialization_writer.write_date_value("birthday", "invalid-date-string")
163175
assert "Invalid date string value found for property birthday" in str(excinfo.value)
164176

177+
165178
def test_write_time_value():
166179
json_serialization_writer = JsonSerializationWriter()
167180
json_serialization_writer.write_time_value(
@@ -172,6 +185,7 @@ def test_write_time_value():
172185
content_string = content.decode('utf-8')
173186
assert content_string == '{"time": "12:59:45.596117"}'
174187

188+
175189
def test_write_time_value_valid_string():
176190
json_serialization_writer = JsonSerializationWriter()
177191
json_serialization_writer.write_time_value(
@@ -181,7 +195,8 @@ def test_write_time_value_valid_string():
181195
content = json_serialization_writer.get_serialized_content()
182196
content_string = content.decode('utf-8')
183197
assert content_string == '{"time": "12:59:45.596117"}'
184-
198+
199+
185200
def test_write_time_value_invalid_string():
186201
with pytest.raises(ValueError) as excinfo:
187202
json_serialization_writer = JsonSerializationWriter()
@@ -191,6 +206,7 @@ def test_write_time_value_invalid_string():
191206
)
192207
assert "Invalid time string value found for property time" in str(excinfo.value)
193208

209+
194210
def test_write_collection_of_primitive_values():
195211
json_serialization_writer = JsonSerializationWriter()
196212
json_serialization_writer.write_collection_of_primitive_values(
@@ -206,9 +222,14 @@ def test_write_collection_of_object_values(user_1, user_2):
206222
json_serialization_writer.write_collection_of_object_values("users", [user_1, user_2])
207223
content = json_serialization_writer.get_serialized_content()
208224
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}]}'
225+
expected = (
226+
'{"users": ['
227+
'{"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '
228+
'"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}, '
229+
'{"display_name": "John Doe", "age": 32}'
230+
']}'
231+
)
232+
assert content_string == expected
212233

213234

214235
def test_write_collection_of_enum_values():
@@ -226,8 +247,13 @@ def test_write_object_value(user_1):
226247
json_serialization_writer.write_object_value("user1", user_1)
227248
content = json_serialization_writer.get_serialized_content()
228249
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}}'
250+
expected = (
251+
'{"user1": {'
252+
'"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '
253+
'"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true'
254+
'}}'
255+
)
256+
assert content_string == expected
231257

232258

233259
def test_write_enum_value():
@@ -265,14 +291,18 @@ def test_write_additional_data_value(user_1, user_2):
265291
)
266292
content = json_serialization_writer.get_serialized_content()
267293
content_string = content.decode('utf-8')
268-
assert content_string == '{"@odata.context": '\
269-
'"https://graph.microsoft.com/v1.0/$metadata#users/$entity", '\
270-
'"businessPhones": ["+1 205 555 0108"], '\
271-
'"manager": {"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '\
272-
'"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}, '\
273-
'"approvers": [{"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '\
274-
'"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}, '\
275-
'{"display_name": "John Doe", "age": 32}], '\
276-
'"created_at": "2022-01-27", '\
277-
'"updated_at": "2024-09-24T00:00:00", '\
278-
'"data": {"groups": [{"friends": [{"display_name": "John Doe", "age": 32}]}]}}'
294+
expected = (
295+
'{"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity", '
296+
'"businessPhones": ["+1 205 555 0108"], '
297+
'"manager": {"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '
298+
'"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}, '
299+
'"approvers": ['
300+
'{"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '
301+
'"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}, '
302+
'{"display_name": "John Doe", "age": 32}'
303+
'], '
304+
'"created_at": "2022-01-27", '
305+
'"updated_at": "2024-09-24T00:00:00", '
306+
'"data": {"groups": [{"friends": [{"display_name": "John Doe", "age": 32}]}]}}'
307+
)
308+
assert content_string == expected

0 commit comments

Comments
 (0)