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

Commit

Permalink
test_write_additional_data_value now supports pendulum datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
eran-av committed Sep 24, 2024
1 parent 3278fe8 commit 5438a74
Showing 1 changed file with 94 additions and 27 deletions.
121 changes: 94 additions & 27 deletions tests/unit/test_json_serialization_writer.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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()
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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()
Expand All @@ -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(
Expand All @@ -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():
Expand All @@ -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():
Expand All @@ -246,30 +259,84 @@ 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]
}]
}
}
)

# 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

0 comments on commit 5438a74

Please sign in to comment.