From 7e04106ab4fbcce893c077cb8f5b95ac6f9712ad Mon Sep 17 00:00:00 2001 From: Stanislav Hejny Date: Mon, 2 Jun 2025 12:11:07 +0100 Subject: [PATCH] Issue #23 - add unit test for new code from fixes identified after running test for BRAG collections --- tests/adapter/test_backpressure_handler.py | 2 +- tests/adapter/test_pydantic_serializer.py | 96 +++++++++++++++++++++- 2 files changed, 96 insertions(+), 2 deletions(-) diff --git a/tests/adapter/test_backpressure_handler.py b/tests/adapter/test_backpressure_handler.py index 73482e0..42b7219 100644 --- a/tests/adapter/test_backpressure_handler.py +++ b/tests/adapter/test_backpressure_handler.py @@ -284,7 +284,7 @@ class TestBackpressureHandler: assert handler.config == mock_config assert handler.swarm_service_id == "clever-amqp-service" assert handler.swarm_task_id == "python-adapter" - assert handler.parallel_workers == 1 + assert handler.parallel_workers == mock_config.backpressure.threshold_threads assert handler.average_cpu_usage is None def test_increase_parallel_executions(self, mock_channel, mock_loop, mock_config): diff --git a/tests/adapter/test_pydantic_serializer.py b/tests/adapter/test_pydantic_serializer.py index af2b9ff..266536a 100644 --- a/tests/adapter/test_pydantic_serializer.py +++ b/tests/adapter/test_pydantic_serializer.py @@ -1,4 +1,5 @@ -from datetime import datetime +import json +from datetime import date, datetime from unittest import TestCase from uuid import UUID @@ -7,6 +8,7 @@ from pydantic import BaseModel from amqp.adapter.pydantic_serializer import ( deserialize_object, parse_url_query, + python_type_to_json, serialize_object, ) @@ -130,3 +132,95 @@ class PydanticSerializerTest(TestCase): ) self.assertEqual(path, "/api/v0/unstructured/863e4d9f-4612-4173-8d0a-3de34cc7a741") self.assertEqual(args, {}) + + +class TestPythonTypeToJson: + """ + Test cases for the python_type_to_json function. + """ + + def test_basic_types(self): + """Test serialization of basic Python types.""" + # Test dictionary + data = {"key": "value", "number": 42} + result = python_type_to_json(data) + assert json.loads(result) == data + + # Test list + data = [1, 2, 3, "four"] + result = python_type_to_json(data) + assert json.loads(result) == data + + # Test tuple + data = (1, 2, 3) + result = python_type_to_json(data) + assert json.loads(result) == [1, 2, 3] # Tuples are converted to lists + + # Test set + data = {1, 2, 3} + result = python_type_to_json(data) + assert set(json.loads(result)) == data # Sets are converted to lists + + def test_datetime_handling(self): + """Test serialization of datetime objects.""" + # Test datetime + test_datetime = datetime(2024, 1, 1, 12, 0, 0) + data = {"timestamp": test_datetime} + result = python_type_to_json(data) + assert json.loads(result) == {"timestamp": test_datetime.isoformat()} + + # Test date + test_date = date(2024, 1, 1) + data = {"date": test_date} + result = python_type_to_json(data) + assert json.loads(result) == {"date": test_date.isoformat()} + + def test_nested_structures(self): + """Test serialization of nested data structures.""" + data = { + "list_of_datetimes": [datetime(2024, 1, 1), datetime(2024, 1, 2)], + "dict_with_date": {"date": date(2024, 1, 1)}, + "mixed": {"set": {1, 2, 3}, "tuple": (1, 2, 3), "datetime": datetime(2024, 1, 1)}, + } + result = python_type_to_json(data) + parsed = json.loads(result) + assert parsed["list_of_datetimes"] == [d.isoformat() for d in data["list_of_datetimes"]] + assert parsed["dict_with_date"]["date"] == data["dict_with_date"]["date"].isoformat() + assert set(parsed["mixed"]["set"]) == data["mixed"]["set"] + assert parsed["mixed"]["tuple"] == list(data["mixed"]["tuple"]) + assert parsed["mixed"]["datetime"] == data["mixed"]["datetime"].isoformat() + + def test_custom_object_serialization(self): + """Test serialization of custom objects with __dict__ attribute.""" + + class TestObject: + def __init__(self, value): + self.value = value + self.timestamp = datetime(2024, 1, 1) + + obj = TestObject("test") + data = {"obj": obj} + result = python_type_to_json(data) + parsed = json.loads(result) + assert parsed["obj"]["value"] == "test" + assert parsed["obj"]["timestamp"] == obj.timestamp.isoformat() + + def test_empty_structures(self): + """Test serialization of empty data structures.""" + # Empty dict + assert python_type_to_json({}) == "{}" + + # Empty list + assert python_type_to_json([]) == "[]" + + # Empty set + assert python_type_to_json(set()) == "[]" + + # Empty tuple + assert python_type_to_json(()) == "[]" + + def test_unicode_handling(self): + """Test handling of Unicode characters.""" + data = {"text": "Hello, 世界!"} + result = python_type_to_json(data) + assert json.loads(result) == data # ensure_ascii=False should preserve Unicode