Skip to content

Commit c4b2a92

Browse files
committed
Serialisation deserialisation tests for python sdk
1 parent 4b4b5ba commit c4b2a92

11 files changed

Lines changed: 2221 additions & 1 deletion

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ requests >= 2.31.0
55
typing-extensions >= 4.2.0
66
astor >= 0.8.1
77
shortuuid >= 1.0.11
8-
dacite >= 1.8.1
8+
dacite >= 1.8.1
9+
deprecated>=1.2.13

tests/serdesertest/__init__.py

Whitespace-only changes.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import unittest
2+
import json
3+
from serdesertest.util.serdeser_json_resolver_utility import JsonTemplateResolver
4+
5+
# Import the classes being tested
6+
from conductor.client.http.models.authorization_request import AuthorizationRequest
7+
8+
9+
class TestAuthorizationRequestSerDes(unittest.TestCase):
10+
"""
11+
Unit tests for serialization and deserialization of AuthorizationRequest model.
12+
"""
13+
14+
def setUp(self):
15+
"""Set up test fixtures."""
16+
# Load the template JSON for testing
17+
self.server_json_str = JsonTemplateResolver.get_json_string("AuthorizationRequest")
18+
self.server_json = json.loads(self.server_json_str)
19+
20+
def test_serialization_deserialization(self):
21+
"""Test complete serialization and deserialization process."""
22+
# Create the authorization request object directly from JSON
23+
# The model's __init__ should handle the nested objects
24+
auth_request = AuthorizationRequest(
25+
subject=self.server_json.get('subject'),
26+
target=self.server_json.get('target'),
27+
access=self.server_json.get('access')
28+
)
29+
30+
# Verify model is properly initialized
31+
self.assertIsNotNone(auth_request, "Deserialized object should not be null")
32+
33+
# Verify access list
34+
self.assertIsNotNone(auth_request.access, "Access list should not be null")
35+
self.assertTrue(all(access in ["CREATE", "READ", "UPDATE", "DELETE", "EXECUTE"]
36+
for access in auth_request.access))
37+
38+
# Verify subject and target are present
39+
self.assertIsNotNone(auth_request.subject, "Subject should not be null")
40+
self.assertIsNotNone(auth_request.target, "Target should not be null")
41+
42+
# Serialize back to dictionary
43+
result_dict = auth_request.to_dict()
44+
45+
# Verify structure matches the original
46+
self.assertEqual(
47+
set(self.server_json.keys()),
48+
set(result_dict.keys()),
49+
"Serialized JSON should have the same keys as the original"
50+
)
51+
52+
# Convert both to JSON strings and compare (similar to objectMapper.readTree)
53+
original_json_normalized = json.dumps(self.server_json, sort_keys=True)
54+
result_json_normalized = json.dumps(result_dict, sort_keys=True)
55+
56+
self.assertEqual(
57+
original_json_normalized,
58+
result_json_normalized,
59+
"Serialized JSON should match the original SERVER_JSON"
60+
)
61+
62+
63+
if __name__ == '__main__':
64+
unittest.main()
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import unittest
2+
import json
3+
4+
from conductor.client.http.models import BulkResponse
5+
from serdesertest.util.serdeser_json_resolver_utility import JsonTemplateResolver
6+
7+
8+
class TestBulkResponseSerDeser(unittest.TestCase):
9+
"""Test serialization and deserialization for BulkResponse class"""
10+
11+
def setUp(self):
12+
"""Set up test fixtures"""
13+
# Load test data from template
14+
self.server_json_str = JsonTemplateResolver.get_json_string("BulkResponse")
15+
# Parse into dictionary for comparisons
16+
self.server_json_dict = json.loads(self.server_json_str)
17+
18+
def test_bulk_response_serialization_deserialization(self):
19+
"""Comprehensive test for serialization and deserialization of BulkResponse"""
20+
# 1. Deserialize JSON into model object
21+
bulk_response = BulkResponse(
22+
bulk_error_results=self.server_json_dict['bulkErrorResults'],
23+
bulk_successful_results=self.server_json_dict['bulkSuccessfulResults']
24+
)
25+
26+
# 2. Verify BulkResponse object properties and types
27+
self.assertIsInstance(bulk_response, BulkResponse)
28+
self.assertIsInstance(bulk_response.bulk_error_results, dict)
29+
self.assertIsInstance(bulk_response.bulk_successful_results, list)
30+
31+
# 3. Validate content of properties
32+
for key, value in bulk_response.bulk_error_results.items():
33+
self.assertIsInstance(key, str)
34+
self.assertIsInstance(value, str)
35+
36+
# Validate the structure of items in bulk_successful_results
37+
# This adapts to the actual structure found in the template
38+
for item in bulk_response.bulk_successful_results:
39+
# If the items are dictionaries with 'value' keys
40+
if isinstance(item, dict) and 'value' in item:
41+
self.assertIsInstance(item['value'], str)
42+
# If the items are strings
43+
elif isinstance(item, str):
44+
pass
45+
else:
46+
self.fail(f"Unexpected item type in bulk_successful_results: {type(item)}")
47+
48+
# 4. Verify values match original source
49+
self.assertEqual(bulk_response.bulk_error_results, self.server_json_dict['bulkErrorResults'])
50+
self.assertEqual(bulk_response.bulk_successful_results, self.server_json_dict['bulkSuccessfulResults'])
51+
52+
# 5. Test serialization back to dictionary
53+
result_dict = bulk_response.to_dict()
54+
self.assertIn('bulk_error_results', result_dict)
55+
self.assertIn('bulk_successful_results', result_dict)
56+
self.assertEqual(result_dict['bulk_error_results'], self.server_json_dict['bulkErrorResults'])
57+
self.assertEqual(result_dict['bulk_successful_results'], self.server_json_dict['bulkSuccessfulResults'])
58+
59+
# 6. Test serialization to JSON-compatible dictionary (with camelCase keys)
60+
json_compatible_dict = {
61+
'bulkErrorResults': result_dict['bulk_error_results'],
62+
'bulkSuccessfulResults': result_dict['bulk_successful_results']
63+
}
64+
65+
# 7. Normalize dictionaries for comparison (handles differences in ordering)
66+
normalized_original = json.loads(json.dumps(self.server_json_dict, sort_keys=True))
67+
normalized_result = json.loads(json.dumps(json_compatible_dict, sort_keys=True))
68+
self.assertEqual(normalized_original, normalized_result)
69+
70+
# 8. Test full serialization/deserialization round trip
71+
bulk_response_2 = BulkResponse(
72+
bulk_error_results=result_dict['bulk_error_results'],
73+
bulk_successful_results=result_dict['bulk_successful_results']
74+
)
75+
self.assertEqual(bulk_response.bulk_error_results, bulk_response_2.bulk_error_results)
76+
self.assertEqual(bulk_response.bulk_successful_results, bulk_response_2.bulk_successful_results)
77+
78+
# 9. Test with missing fields
79+
bulk_response_errors_only = BulkResponse(
80+
bulk_error_results={"id1": "error1"}
81+
)
82+
self.assertEqual(bulk_response_errors_only.bulk_error_results, {"id1": "error1"})
83+
self.assertIsNone(bulk_response_errors_only.bulk_successful_results)
84+
85+
# Create a structure similar to what's in the template
86+
sample_successful_result = [{"value": "success1"}]
87+
bulk_response_success_only = BulkResponse(
88+
bulk_successful_results=sample_successful_result
89+
)
90+
self.assertIsNone(bulk_response_success_only.bulk_error_results)
91+
self.assertEqual(bulk_response_success_only.bulk_successful_results, sample_successful_result)
92+
93+
# 10. Test with empty fields
94+
bulk_response_empty = BulkResponse(
95+
bulk_error_results={},
96+
bulk_successful_results=[]
97+
)
98+
self.assertEqual(bulk_response_empty.bulk_error_results, {})
99+
self.assertEqual(bulk_response_empty.bulk_successful_results, [])
100+
101+
102+
if __name__ == '__main__':
103+
unittest.main()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import unittest
2+
import json
3+
4+
from conductor.client.http.models import ConductorApplication
5+
from serdesertest.util.serdeser_json_resolver_utility import JsonTemplateResolver
6+
7+
8+
9+
class TestConductorApplicationSerialization(unittest.TestCase):
10+
"""Test case for ConductorApplication serialization and deserialization."""
11+
12+
def setUp(self):
13+
"""Set up test fixtures before each test."""
14+
# Load JSON template from the resolver utility
15+
self.server_json_str = JsonTemplateResolver.get_json_string("ConductorApplication")
16+
self.server_json = json.loads(self.server_json_str)
17+
18+
def test_serialization_deserialization(self):
19+
"""Test that validates the serialization and deserialization of ConductorApplication model."""
20+
21+
# Step 1: Deserialize server JSON into SDK model object
22+
# Create model object using constructor with fields from the JSON
23+
conductor_app = ConductorApplication(
24+
id=self.server_json.get('id'),
25+
name=self.server_json.get('name'),
26+
created_by=self.server_json.get('createdBy')
27+
)
28+
29+
# Step 2: Verify all fields are correctly populated
30+
self.assertEqual(conductor_app.id, self.server_json.get('id'))
31+
self.assertEqual(conductor_app.name, self.server_json.get('name'))
32+
self.assertEqual(conductor_app.created_by, self.server_json.get('createdBy'))
33+
34+
# Step 3: Serialize the model back to JSON
35+
serialized_json = conductor_app.to_dict()
36+
37+
# Step 4: Verify the serialized JSON matches the original
38+
# Note: Field names in serialized_json will be in snake_case
39+
self.assertEqual(serialized_json.get('id'), self.server_json.get('id'))
40+
self.assertEqual(serialized_json.get('name'), self.server_json.get('name'))
41+
# Handle the camelCase to snake_case transformation
42+
self.assertEqual(serialized_json.get('created_by'), self.server_json.get('createdBy'))
43+
44+
45+
if __name__ == '__main__':
46+
unittest.main()
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import json
2+
import unittest
3+
4+
from conductor.client.http.models import ConductorUser, Role, Group
5+
from serdesertest.util.serdeser_json_resolver_utility import JsonTemplateResolver
6+
7+
8+
class TestConductorUserSerDeSer(unittest.TestCase):
9+
"""Test serialization and deserialization of ConductorUser."""
10+
11+
def setUp(self):
12+
# Load JSON template using the utility
13+
self.server_json_str = JsonTemplateResolver.get_json_string("ConductorUser")
14+
self.server_json = json.loads(self.server_json_str)
15+
16+
def test_conductor_user_serde(self):
17+
"""Test that ConductorUser can be deserialized from server JSON and serialized back without data loss."""
18+
19+
# 1. Deserialize server JSON into ConductorUser object
20+
conductor_user = ConductorUser()
21+
conductor_user_dict = self.server_json
22+
23+
# Set attributes from deserialized JSON
24+
if 'id' in conductor_user_dict:
25+
conductor_user.id = conductor_user_dict['id']
26+
if 'name' in conductor_user_dict:
27+
conductor_user.name = conductor_user_dict['name']
28+
if 'roles' in conductor_user_dict:
29+
# Assuming Role has a from_dict method or similar
30+
roles_list = []
31+
for role_data in conductor_user_dict['roles']:
32+
role = Role() # Create a Role object based on your actual implementation
33+
# Set Role properties here
34+
roles_list.append(role)
35+
conductor_user.roles = roles_list
36+
if 'groups' in conductor_user_dict:
37+
# Assuming Group has a from_dict method or similar
38+
groups_list = []
39+
for group_data in conductor_user_dict['groups']:
40+
group = Group() # Create a Group object based on your actual implementation
41+
# Set Group properties here
42+
groups_list.append(group)
43+
conductor_user.groups = groups_list
44+
if 'uuid' in conductor_user_dict:
45+
conductor_user.uuid = conductor_user_dict['uuid']
46+
if 'applicationUser' in conductor_user_dict:
47+
conductor_user.application_user = conductor_user_dict['applicationUser']
48+
if 'encryptedId' in conductor_user_dict:
49+
conductor_user.encrypted_id = conductor_user_dict['encryptedId']
50+
if 'encryptedIdDisplayValue' in conductor_user_dict:
51+
conductor_user.encrypted_id_display_value = conductor_user_dict['encryptedIdDisplayValue']
52+
53+
# 2. Verify all fields are properly populated
54+
expected_id = self.server_json.get('id', None)
55+
self.assertEqual(conductor_user.id, expected_id)
56+
57+
expected_name = self.server_json.get('name', None)
58+
self.assertEqual(conductor_user.name, expected_name)
59+
60+
# Verify lists
61+
if 'roles' in self.server_json:
62+
self.assertEqual(len(conductor_user.roles), len(self.server_json['roles']))
63+
64+
if 'groups' in self.server_json:
65+
self.assertEqual(len(conductor_user.groups), len(self.server_json['groups']))
66+
67+
expected_uuid = self.server_json.get('uuid', None)
68+
self.assertEqual(conductor_user.uuid, expected_uuid)
69+
70+
expected_app_user = self.server_json.get('applicationUser', None)
71+
self.assertEqual(conductor_user.application_user, expected_app_user)
72+
73+
expected_encrypted_id = self.server_json.get('encryptedId', None)
74+
self.assertEqual(conductor_user.encrypted_id, expected_encrypted_id)
75+
76+
expected_encrypted_id_display = self.server_json.get('encryptedIdDisplayValue', None)
77+
self.assertEqual(conductor_user.encrypted_id_display_value, expected_encrypted_id_display)
78+
79+
# 3. Serialize the object back to JSON
80+
serialized_json = conductor_user.to_dict()
81+
82+
# 4. Verify the serialized JSON matches the original
83+
# Handle camelCase to snake_case transformations
84+
if 'applicationUser' in self.server_json:
85+
self.assertEqual(serialized_json['application_user'], self.server_json['applicationUser'])
86+
if 'encryptedId' in self.server_json:
87+
self.assertEqual(serialized_json['encrypted_id'], self.server_json['encryptedId'])
88+
if 'encryptedIdDisplayValue' in self.server_json:
89+
self.assertEqual(serialized_json['encrypted_id_display_value'], self.server_json['encryptedIdDisplayValue'])
90+
91+
# Check common fields that don't need transformation
92+
for field in ['id', 'name', 'uuid']:
93+
if field in self.server_json:
94+
self.assertEqual(serialized_json[field], self.server_json[field])
95+
96+
# Check lists length
97+
if 'roles' in self.server_json:
98+
self.assertEqual(len(serialized_json['roles']), len(self.server_json['roles']))
99+
if 'groups' in self.server_json:
100+
self.assertEqual(len(serialized_json['groups']), len(self.server_json['groups']))
101+
102+
103+
if __name__ == '__main__':
104+
unittest.main()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import unittest
2+
import json
3+
4+
from conductor.client.http.models.correlation_ids_search_request import CorrelationIdsSearchRequest
5+
from serdesertest.util.serdeser_json_resolver_utility import JsonTemplateResolver
6+
7+
class TestCorrelationIdsSearchRequest(unittest.TestCase):
8+
"""Test case for CorrelationIdsSearchRequest class."""
9+
10+
def setUp(self):
11+
"""Set up test fixtures."""
12+
# Load the JSON template for CorrelationIdsSearchRequest
13+
self.server_json_str = JsonTemplateResolver.get_json_string("CorrelationIdsSearchRequest")
14+
self.server_json = json.loads(self.server_json_str)
15+
16+
# Convert camelCase to snake_case for initialization
17+
self.python_format_json = {}
18+
for key, value in self.server_json.items():
19+
# Use the attribute_map to find the Python property name
20+
python_key = next((k for k, v in CorrelationIdsSearchRequest.attribute_map.items() if v == key), key)
21+
self.python_format_json[python_key] = value
22+
23+
def test_serdeser_correlation_ids_search_request(self):
24+
"""Test serialization and deserialization of CorrelationIdsSearchRequest."""
25+
# 1. Server JSON can be correctly deserialized into SDK model object
26+
model_obj = CorrelationIdsSearchRequest(**self.python_format_json)
27+
28+
# 2. All fields are properly populated during deserialization
29+
# Check correlation_ids (list[str])
30+
self.assertIsNotNone(model_obj.correlation_ids)
31+
self.assertIsInstance(model_obj.correlation_ids, list)
32+
for item in model_obj.correlation_ids:
33+
self.assertIsInstance(item, str)
34+
35+
# Check workflow_names (list[str])
36+
self.assertIsNotNone(model_obj.workflow_names)
37+
self.assertIsInstance(model_obj.workflow_names, list)
38+
for item in model_obj.workflow_names:
39+
self.assertIsInstance(item, str)
40+
41+
# 3. The SDK model can be serialized back to JSON
42+
serialized_dict = model_obj.to_dict()
43+
44+
# 4. The resulting JSON matches the original
45+
# Convert serialized dict keys to camelCase for comparison
46+
json_dict = {}
47+
for attr, value in serialized_dict.items():
48+
if attr in model_obj.attribute_map:
49+
json_dict[model_obj.attribute_map[attr]] = value
50+
else:
51+
json_dict[attr] = value
52+
53+
# Compare with original JSON
54+
self.assertEqual(self.server_json, json_dict)
55+
56+
57+
if __name__ == '__main__':
58+
unittest.main()

0 commit comments

Comments
 (0)