Skip to content

Commit

Permalink
Merge pull request #385 from Acurisu/json_serialization_minor_refacto…
Browse files Browse the repository at this point in the history
…ring

remove unused code/imports as well as some reformatting
  • Loading branch information
andrueastman authored Oct 16, 2024
2 parents e523b7f + b300212 commit 70c6c2c
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import warnings
from datetime import date, datetime, time, timedelta
from enum import Enum
from typing import Any, Callable, Dict, Generic, List, Optional, TypeVar, Union
from typing import Any, Callable, List, Optional, TypeVar
from uuid import UUID

import pendulum
Expand Down
1 change: 0 additions & 1 deletion packages/serialization/json/tests/helpers/union_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from typing import Any, Callable, Dict, List, Optional

from kiota_abstractions.serialization import Parsable, ParseNode, SerializationWriter
from kiota_abstractions.serialization.parse_node_helper import ParseNodeHelper

from . import User, User2

Expand Down
3 changes: 1 addition & 2 deletions packages/serialization/json/tests/helpers/user2.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from __future__ import annotations

from dataclasses import dataclass, field
from datetime import date, datetime
from typing import Any, Callable, Dict, List, Optional, TypeVar
from typing import Any, Callable, Dict, Optional, TypeVar

from kiota_abstractions.serialization import Parsable, ParseNode, SerializationWriter

Expand Down
88 changes: 35 additions & 53 deletions packages/serialization/json/tests/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,45 @@
import json

import pytest

url: str = "https://graph.microsoft.com/v1.0/$metadata#users/$entity"


@pytest.fixture
def user1_json():
return '{"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '\
'"office_location": "dunhill", '\
'"updated_at": "2021-07-29T03:07:25Z", '\
'"birthday": "2000-09-04", '\
'"business_phones": ["+1 205 555 0108"], '\
'"mobile_phone": null, '\
'"is_active": true, '\
'"additional_data": {"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity", '\
'"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}], '\
'"data": {'\
'"groups": ['\
'{"friends": [{"display_name": "John Doe", "age": 32}]}'\
']'\
'}}}'\


@pytest.fixture
def user2_json():

return '{"id": 2, "display_name": "MOD Administrator", "age": 32, "gpa": 3.9}'
return (
'{"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '
'"office_location": "dunhill", '
'"updated_at": "2021-07-29T03:07:25Z", '
'"birthday": "2000-09-04", '
'"business_phones": ["+1 205 555 0108"], '
'"mobile_phone": null, '
'"is_active": true, '
'"additional_data": {"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users/$entity", '
'"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}], '
'"data": {'
'"groups": ['
'{"friends": [{"display_name": "John Doe", "age": 32}]}'
"]"
"}}}"
)


@pytest.fixture
def users_json():
return '[{"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '\
'"office_location": "dunhill", '\
'"updated_at": "2021-07-29T03:07:25Z", '\
'"birthday": "2000-09-04", '\
'"business_phones": ["+1 205 555 0108"], '\
'"mobile_phone": null, '\
'"is_active": true}, '\
'{"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '\
'"office_location": "dunhill", '\
'"updated_at": "2021-07-29T03:07:25Z", '\
'"birthday": "2000-09-04", '\
'"business_phones": ["+1 205 555 0108"], '\
'"mobile_phone": null, '\
'"is_active": true}]'


@pytest.fixture
def sample_entity_json():

entity_json = json.dumps(
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#$entity",
"id": "8f841f30-e6e3-439a-a812-ebd369559c36"
}
return (
'[{"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '
'"office_location": "dunhill", '
'"updated_at": "2021-07-29T03:07:25Z", '
'"birthday": "2000-09-04", '
'"business_phones": ["+1 205 555 0108"], '
'"mobile_phone": null, '
'"is_active": true}, '
'{"id": "8f841f30-e6e3-439a-a812-ebd369559c36", '
'"office_location": "dunhill", '
'"updated_at": "2021-07-29T03:07:25Z", '
'"birthday": "2000-09-04", '
'"business_phones": ["+1 205 555 0108"], '
'"mobile_phone": null, '
'"is_active": true}]'
)
return entity_json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import json
from uuid import UUID

import pytest

from kiota_serialization_json.json_parse_node import JsonParseNode
from kiota_serialization_json.json_serialization_writer import JsonSerializationWriter

Expand Down
39 changes: 20 additions & 19 deletions packages/serialization/json/tests/unit/test_json_parse_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from kiota_serialization_json.json_parse_node import JsonParseNode
from ..helpers import OfficeLocation, User

url: str = "https://graph.microsoft.com/v1.0/$metadata#users/$entity"


def test_get_str_value():
parse_node = JsonParseNode("Diego Siciliani")
Expand Down Expand Up @@ -42,7 +40,7 @@ def test_get_float_value_from_float():
@pytest.mark.parametrize("value", [0, 10, 100])
def test_get_float_value(value: int):
"""
Consider an OpenAPI Specification using the type: number and format: float or double
Consider an OpenAPI Specification using the type: number and format: float or double
Note: The OpenAPI Specification also allows for the use of the type: integer and format: int32 or int64
Consider an API with Price data [0, 0.5, 1, 1.5, 2] and so on
Expand Down Expand Up @@ -70,30 +68,30 @@ def test_get_datetime_value_returns_none_with_invalid_str(value: str):


def test_get_datetime_value():
parse_node = JsonParseNode('2022-01-27T12:59:45.596117')
parse_node = JsonParseNode("2022-01-27T12:59:45.596117")
result = parse_node.get_datetime_value()
assert isinstance(result, datetime)


def test_get_date_value():
parse_node = JsonParseNode('2015-04-20')
parse_node = JsonParseNode("2015-04-20")
result = parse_node.get_date_value()
assert isinstance(result, date)
assert str(result) == '2015-04-20'
assert str(result) == "2015-04-20"


def test_get_time_value():
parse_node = JsonParseNode('12:59:45.596117')
parse_node = JsonParseNode("12:59:45.596117")
result = parse_node.get_time_value()
assert isinstance(result, time)
assert str(result) == '12:59:45.596117'
assert str(result) == "12:59:45.596117"


def test_get_timedelta_value():
parse_node = JsonParseNode('PT30S')
parse_node = JsonParseNode("PT30S")
result = parse_node.get_timedelta_value()
assert isinstance(result, timedelta)
assert str(result) == '0:00:30'
assert str(result) == "0:00:30"


def test_get_collection_of_primitive_values():
Expand All @@ -109,7 +107,7 @@ def test_get_collection_of_primitive_values_no_type():


def test_get_bytes_value():
parse_node = JsonParseNode('U2Ftd2VsIGlzIHRoZSBiZXN0')
parse_node = JsonParseNode("U2Ftd2VsIGlzIHRoZSBiZXN0")
result = parse_node.get_bytes_value()
assert isinstance(result, bytes)

Expand Down Expand Up @@ -159,26 +157,29 @@ def test_get_object_value(user1_json):
assert result.business_phones == ["+1 205 555 0108"]
assert result.is_active is True
assert result.mobile_phone is None
assert result.additional_data["additional_data"][
"@odata.context"] == "https://graph.microsoft.com/v1.0/$metadata#users/$entity"
assert (
result.additional_data["additional_data"]["@odata.context"] ==
"https://graph.microsoft.com/v1.0/$metadata#users/$entity"
)
assert result.additional_data["additional_data"]["manager"] == {
"id": UUID('8f841f30-e6e3-439a-a812-ebd369559c36'),
"id": UUID("8f841f30-e6e3-439a-a812-ebd369559c36"),
"updated_at":
DateTime(2022, 1, 27, 12, 59, 45, 596117, tzinfo=FixedTimezone(0, name="+00:00")),
"is_active": True
"is_active": True,
}
assert result.additional_data["additional_data"]["approvers"] == [
{
"id":
UUID('8f841f30-e6e3-439a-a812-ebd369559c36'),
UUID("8f841f30-e6e3-439a-a812-ebd369559c36"),
"updated_at":
DateTime(2022, 1, 27, 12, 59, 45, 596117, tzinfo=FixedTimezone(0, name="+00:00")),
"is_active":
True
}, {
True,
},
{
"display_name": "John Doe",
"age": 32
}
},
]
assert result.additional_data["additional_data"]["data"] == {
"groups": [{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ def test_get_root_parse_node(sample_json_string):


def test_get_root_parse_node_no_content_type(sample_json_string):
with pytest.raises(Exception) as e_info:
with pytest.raises(Exception):
factory = JsonParseNodeFactory()
sample_json_string_bytes = sample_json_string.encode('utf-8')
root = factory.get_root_parse_node('', sample_json_string_bytes)
factory.get_root_parse_node('', sample_json_string_bytes)


def test_get_root_parse_node_unsupported_content_type(sample_json_string):
with pytest.raises(Exception) as e_info:
with pytest.raises(Exception):
factory = JsonParseNodeFactory()
sample_json_string_bytes = sample_json_string.encode('utf-8')
root = factory.get_root_parse_node('application/xml', sample_json_string_bytes)
factory.get_root_parse_node('application/xml', sample_json_string_bytes)


def test_get_root_parse_node_empty_json():
with pytest.raises(TypeError) as e_info:
with pytest.raises(TypeError):
factory = JsonParseNodeFactory()
sample_string_bytes = ''.encode('utf-8')
root = factory.get_root_parse_node('application/json', sample_string_bytes)
factory.get_root_parse_node('application/json', sample_string_bytes)


def test_get_valid_content_type():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ def test_get_serialization_writer():


def test_get_serialization_writer_no_content_type():
with pytest.raises(TypeError) as e_info:
with pytest.raises(TypeError):
factory = JsonSerializationWriterFactory()
factory.get_serialization_writer('')


def test_get_serialization_writer_unsupported_content_type():
with pytest.raises(Exception) as e_info:
with pytest.raises(Exception):
factory = JsonSerializationWriterFactory()
factory.get_serialization_writer('application/xml')

Expand Down
2 changes: 0 additions & 2 deletions packages/serialization/json/tests/unit/test_union_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import json
from uuid import UUID

import pytest

from kiota_serialization_json.json_parse_node import JsonParseNode
from kiota_serialization_json.json_serialization_writer import JsonSerializationWriter

Expand Down

0 comments on commit 70c6c2c

Please sign in to comment.