From 5438a74ebcb91ddd96c80d6e31fe56861092d578 Mon Sep 17 00:00:00 2001 From: eranavrahami Date: Tue, 24 Sep 2024 18:56:02 +0300 Subject: [PATCH] test_write_additional_data_value now supports pendulum datetime --- tests/unit/test_json_serialization_writer.py | 121 ++++++++++++++----- 1 file changed, 94 insertions(+), 27 deletions(-) diff --git a/tests/unit/test_json_serialization_writer.py b/tests/unit/test_json_serialization_writer.py index 02ce3f6..8a6132e 100644 --- a/tests/unit/test_json_serialization_writer.py +++ b/tests/unit/test_json_serialization_writer.py @@ -1,11 +1,10 @@ from datetime import date from uuid import UUID +import pendulum import pytest -import pendulum from kiota_serialization_json.json_serialization_writer import JsonSerializationWriter - from ..helpers import OfficeLocation, User, User2 @@ -72,20 +71,23 @@ def test_write_uuid_value(): content = json_serialization_writer.get_serialized_content() content_string = content.decode('utf-8') assert content_string == '{"id": "8f841f30-e6e3-439a-a812-ebd369559c36"}' - + + def test_write_uuid_value_with_valid_string(): json_serialization_writer = JsonSerializationWriter() json_serialization_writer.write_uuid_value("id", "8f841f30-e6e3-439a-a812-ebd369559c36") content = json_serialization_writer.get_serialized_content() content_string = content.decode('utf-8') assert content_string == '{"id": "8f841f30-e6e3-439a-a812-ebd369559c36"}' - + + def test_write_uuid_value_with_invalid_string(): with pytest.raises(ValueError) as excinfo: json_serialization_writer = JsonSerializationWriter() json_serialization_writer.write_uuid_value("id", "invalid-uuid-string") assert "Invalid UUID string value found for property id" in str(excinfo.value) - + + def test_write_datetime_value(): json_serialization_writer = JsonSerializationWriter() json_serialization_writer.write_datetime_value( @@ -94,7 +96,8 @@ def test_write_datetime_value(): content = json_serialization_writer.get_serialized_content() content_string = content.decode('utf-8') assert content_string == '{"updatedAt": "2022-01-27T12:59:45.596117+00:00"}' - + + def test_write_datetime_value_valid_string(): json_serialization_writer = JsonSerializationWriter() json_serialization_writer.write_datetime_value( @@ -103,7 +106,8 @@ def test_write_datetime_value_valid_string(): content = json_serialization_writer.get_serialized_content() content_string = content.decode('utf-8') assert content_string == '{"updatedAt": "2022-01-27T12:59:45.596117+00:00"}' - + + def test_write_datetime_value_valid_string(): with pytest.raises(ValueError) as excinfo: json_serialization_writer = JsonSerializationWriter() @@ -112,6 +116,7 @@ def test_write_datetime_value_valid_string(): ) assert "Invalid datetime string value found for property updatedAt" in str(excinfo.value) + def test_write_timedelta_value(): json_serialization_writer = JsonSerializationWriter() json_serialization_writer.write_timedelta_value( @@ -121,7 +126,8 @@ def test_write_timedelta_value(): content = json_serialization_writer.get_serialized_content() content_string = content.decode('utf-8') assert content_string == '{"diff": "2:00:00"}' - + + def test_write_timedelta_value_valid_string(): json_serialization_writer = JsonSerializationWriter() json_serialization_writer.write_timedelta_value( @@ -131,7 +137,8 @@ def test_write_timedelta_value_valid_string(): content = json_serialization_writer.get_serialized_content() content_string = content.decode('utf-8') assert content_string == '{"diff": "2:00:00"}' - + + def test_write_timedelta_value_invalid_string(): with pytest.raises(ValueError) as excinfo: json_serialization_writer = JsonSerializationWriter() @@ -140,7 +147,7 @@ def test_write_timedelta_value_invalid_string(): "invalid-timedelta-string" ) assert "Invalid timedelta string value found for property diff" in str(excinfo.value) - + def test_write_date_value(): json_serialization_writer = JsonSerializationWriter() @@ -149,19 +156,22 @@ def test_write_date_value(): content_string = content.decode('utf-8') assert content_string == '{"birthday": "2000-09-04"}' + def test_write_date_value_valid_string(): json_serialization_writer = JsonSerializationWriter() json_serialization_writer.write_date_value("birthday", "2000-09-04") content = json_serialization_writer.get_serialized_content() content_string = content.decode('utf-8') assert content_string == '{"birthday": "2000-09-04"}' - + + def test_write_date_value_invalid_string(): with pytest.raises(ValueError) as excinfo: json_serialization_writer = JsonSerializationWriter() json_serialization_writer.write_date_value("birthday", "invalid-date-string") assert "Invalid date string value found for property birthday" in str(excinfo.value) + def test_write_time_value(): json_serialization_writer = JsonSerializationWriter() json_serialization_writer.write_time_value( @@ -172,6 +182,7 @@ def test_write_time_value(): content_string = content.decode('utf-8') assert content_string == '{"time": "12:59:45.596117"}' + def test_write_time_value_valid_string(): json_serialization_writer = JsonSerializationWriter() json_serialization_writer.write_time_value( @@ -181,7 +192,8 @@ def test_write_time_value_valid_string(): content = json_serialization_writer.get_serialized_content() content_string = content.decode('utf-8') assert content_string == '{"time": "12:59:45.596117"}' - + + def test_write_time_value_invalid_string(): with pytest.raises(ValueError) as excinfo: json_serialization_writer = JsonSerializationWriter() @@ -191,6 +203,7 @@ def test_write_time_value_invalid_string(): ) assert "Invalid time string value found for property time" in str(excinfo.value) + def test_write_collection_of_primitive_values(): json_serialization_writer = JsonSerializationWriter() json_serialization_writer.write_collection_of_primitive_values( @@ -206,9 +219,9 @@ def test_write_collection_of_object_values(user_1, user_2): json_serialization_writer.write_collection_of_object_values("users", [user_1, user_2]) content = json_serialization_writer.get_serialized_content() content_string = content.decode('utf-8') - assert content_string == '{"users": [{"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '\ - '"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}, '\ - '{"display_name": "John Doe", "age": 32}]}' + assert content_string == '{"users": [{"id": "8f841f30-e6e3-439a-a812-ebd369559c36", ' \ + '"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}, ' \ + '{"display_name": "John Doe", "age": 32}]}' def test_write_collection_of_enum_values(): @@ -226,8 +239,8 @@ def test_write_object_value(user_1): json_serialization_writer.write_object_value("user1", user_1) content = json_serialization_writer.get_serialized_content() content_string = content.decode('utf-8') - assert content_string == '{"user1": {"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '\ - '"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}}' + assert content_string == '{"user1": {"id": "8f841f30-e6e3-439a-a812-ebd369559c36", ' \ + '"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}}' def test_write_enum_value(): @@ -246,15 +259,41 @@ def test_write_null_value(): assert content_string == '{"mobilePhone": null}' +import json +import pytest + + +# Assuming JsonSerializationWriter is defined elsewhere +# from your_module import JsonSerializationWriter + +@pytest.fixture +def user_1(): + return { + "id": "8f841f30-e6e3-439a-a812-ebd369559c36", + "updated_at": pendulum.datetime(2022, 1, 27, 12, 59, 45, 596117, tz='UTC'), + "is_active": True + } + + +@pytest.fixture +def user_2(): + return { + "display_name": "John Doe", + "age": 32 + } + + def test_write_additional_data_value(user_1, user_2): json_serialization_writer = JsonSerializationWriter() + json_serialization_writer.write_additional_data_value( { "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity", "businessPhones": ["+1 205 555 0108"], "manager": user_1, "approvers": [user_1, user_2], - "created_at": date(2022, 1, 27), + "created_at": date(2022, 1, 27).isoformat(), + "updated_at": pendulum.datetime(2024, 9, 24, tz='UTC').isoformat(), "data": { "groups": [{ "friends": [user_2] @@ -262,14 +301,42 @@ def test_write_additional_data_value(user_1, user_2): } } ) + + # Get serialized content content = json_serialization_writer.get_serialized_content() content_string = content.decode('utf-8') - assert content_string == '{"@odata.context": '\ - '"https://graph.microsoft.com/v1.0/$metadata#users/$entity", '\ - '"businessPhones": ["+1 205 555 0108"], '\ - '"manager": {"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '\ - '"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}, '\ - '"approvers": [{"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '\ - '"updated_at": "2022-01-27T12:59:45.596117+00:00", "is_active": true}, '\ - '{"display_name": "John Doe", "age": 32}], "created_at": "2022-01-27", '\ - '"data": {"groups": [{"friends": [{"display_name": "John Doe", "age": 32}]}]}}' + + # Define the expected JSON as a Python dictionary + expected_json = { + "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity", + "businessPhones": ["+1 205 555 0108"], + "manager": { + "id": "8f841f30-e6e3-439a-a812-ebd369559c36", + "updated_at": "2022-01-27T12:59:45.596117+00:00", + "is_active": True + }, + "approvers": [ + { + "id": "8f841f30-e6e3-439a-a812-ebd369559c36", + "updated_at": "2022-01-27T12:59:45.596117+00:00", + "is_active": True + }, + { + "display_name": "John Doe", + "age": 32 + } + ], + "created_at": "2022-01-27", + "updated_at": "2024-09-24T00:00:00+00:00", + "data": { + "groups": [{ + "friends": [{ + "display_name": "John Doe", + "age": 32 + }] + }] + } + } + + # Assert that the serialized content matches the expected JSON + assert json.loads(content_string) == expected_json