Skip to content

Commit ebd0057

Browse files
Rewrite serdesertest/ to pytest (#316)
1 parent a5f4df7 commit ebd0057

File tree

58 files changed

+3633
-4407
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3633
-4407
lines changed
Lines changed: 107 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,114 @@
1-
import unittest
1+
import json
2+
import re
3+
4+
import pytest
5+
26
from conductor.client.http.models.action import Action
37
from conductor.client.http.models.start_workflow import StartWorkflow
48
from conductor.client.http.models.task_details import TaskDetails
59
from conductor.client.http.models.terminate_workflow import TerminateWorkflow
6-
from conductor.client.http.models.update_workflow_variables import UpdateWorkflowVariables
10+
from conductor.client.http.models.update_workflow_variables import (
11+
UpdateWorkflowVariables,
12+
)
713
from tests.serdesertest.util.serdeser_json_resolver_utility import JsonTemplateResolver
8-
import json
9-
import re
10-
11-
12-
class TestActionSerDes(unittest.TestCase):
13-
def setUp(self):
14-
# Load the JSON template
15-
self.server_json_str = JsonTemplateResolver.get_json_string("EventHandler.Action")
16-
self.server_json = json.loads(self.server_json_str)
17-
18-
def _camel_to_snake(self, name):
19-
"""Convert camelCase to snake_case"""
20-
# Insert underscore before uppercase letters and convert to lowercase
21-
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
22-
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
23-
24-
def _create_model_object(self, model_class, json_data):
25-
"""Generic method to create model objects with proper camelCase to snake_case conversion"""
26-
if not json_data:
27-
return None
28-
29-
# Create an instance of the model class
30-
obj = model_class()
31-
32-
# Iterate through the JSON data and set attributes on the model object
33-
for key, value in json_data.items():
34-
# Convert camelCase to snake_case
35-
snake_key = self._camel_to_snake(key)
36-
37-
# Try to set the attribute if it exists on the model
38-
if hasattr(obj, snake_key):
39-
setattr(obj, snake_key, value)
40-
41-
return obj
42-
43-
def test_action_serdes(self):
44-
# 1. Test deserialization of server JSON to SDK model
45-
action_obj = Action(
46-
action=self.server_json.get("action"),
47-
start_workflow=self._create_model_object(StartWorkflow, self.server_json.get("start_workflow")),
48-
complete_task=self._create_model_object(TaskDetails, self.server_json.get("complete_task")),
49-
fail_task=self._create_model_object(TaskDetails, self.server_json.get("fail_task")),
50-
expand_inline_json=self.server_json.get("expandInlineJSON"),
51-
terminate_workflow=self._create_model_object(TerminateWorkflow, self.server_json.get("terminate_workflow")),
52-
update_workflow_variables=self._create_model_object(UpdateWorkflowVariables,
53-
self.server_json.get("update_workflow_variables"))
54-
)
55-
56-
# 2. Verify all fields are properly populated
57-
self.assertEqual(self.server_json.get("action"), action_obj.action)
58-
59-
# Check if start_workflow exists in the JSON
60-
if "start_workflow" in self.server_json:
61-
self.assertIsNotNone(action_obj.start_workflow)
62-
63-
# Check if complete_task exists in the JSON
64-
if "complete_task" in self.server_json:
65-
self.assertIsNotNone(action_obj.complete_task)
66-
67-
# Check if fail_task exists in the JSON
68-
if "fail_task" in self.server_json:
69-
self.assertIsNotNone(action_obj.fail_task)
70-
71-
# Check if expandInlineJSON exists in the JSON
72-
if "expandInlineJSON" in self.server_json:
73-
self.assertEqual(self.server_json.get("expandInlineJSON"), action_obj.expand_inline_json)
74-
75-
# Check if terminate_workflow exists in the JSON
76-
if "terminate_workflow" in self.server_json:
77-
self.assertIsNotNone(action_obj.terminate_workflow)
78-
79-
# Check if update_workflow_variables exists in the JSON
80-
if "update_workflow_variables" in self.server_json:
81-
self.assertIsNotNone(action_obj.update_workflow_variables)
82-
83-
# 3. Verify the action enum value is valid
84-
allowed_values = ["start_workflow", "complete_task", "fail_task", "terminate_workflow",
85-
"update_workflow_variables"]
86-
self.assertIn(action_obj.action, allowed_values)
87-
88-
# 4. Test serialization back to JSON
89-
result_json = action_obj.to_dict()
90-
91-
# 5. Verify the result JSON has the same keys and values as the original
92-
for key in self.server_json:
93-
if key == "expandInlineJSON":
94-
# Handle camelCase to snake_case conversion
95-
self.assertEqual(self.server_json[key], result_json["expand_inline_json"])
96-
elif key in ["terminate_workflow", "start_workflow", "complete_task", "fail_task",
97-
"update_workflow_variables"]:
98-
# For nested objects, verify they exist in result_json
99-
if self.server_json[key] is not None:
100-
self.assertIsNotNone(result_json[key])
101-
102-
# For terminate_workflow, check specific camelCase fields
103-
if key == "terminate_workflow" and key in result_json:
104-
term_json = self.server_json[key]
105-
result_term = result_json[key]
106-
107-
# Maps the expected field names from server JSON to the result JSON
108-
if "workflowId" in term_json and "workflowId" in result_term:
109-
self.assertEqual(term_json["workflowId"], result_term["workflowId"])
110-
if "terminationReason" in term_json and "terminationReason" in result_term:
111-
self.assertEqual(term_json["terminationReason"], result_term["terminationReason"])
112-
113-
# For update_workflow_variables, check specific camelCase fields
114-
if key == "update_workflow_variables" and key in result_json:
115-
update_json = self.server_json[key]
116-
result_update = result_json[key]
117-
118-
# Maps the expected field names from server JSON to the result JSON
119-
if "workflowId" in update_json and "workflowId" in result_update:
120-
self.assertEqual(update_json["workflowId"], result_update["workflowId"])
121-
if "variables" in update_json and "variables" in result_update:
122-
self.assertEqual(update_json["variables"], result_update["variables"])
123-
if "appendArray" in update_json and "appendArray" in result_update:
124-
self.assertEqual(update_json["appendArray"], result_update["appendArray"])
125-
elif key in result_json:
126-
# Direct comparison for keys that match
127-
self.assertEqual(self.server_json[key], result_json[key])
12814

12915

130-
if __name__ == '__main__':
131-
unittest.main()
16+
def camel_to_snake(name):
17+
s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
18+
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()
19+
20+
21+
def create_model_object(model_class, json_data):
22+
if not json_data:
23+
return None
24+
obj = model_class()
25+
for key, value in json_data.items():
26+
snake_key = camel_to_snake(key)
27+
if hasattr(obj, snake_key):
28+
setattr(obj, snake_key, value)
29+
return obj
30+
31+
32+
@pytest.fixture
33+
def server_json():
34+
return json.loads(JsonTemplateResolver.get_json_string("EventHandler.Action"))
35+
36+
37+
def test_action_serdes(server_json):
38+
action_obj = Action(
39+
action=server_json.get("action"),
40+
start_workflow=create_model_object(
41+
StartWorkflow, server_json.get("start_workflow")
42+
),
43+
complete_task=create_model_object(
44+
TaskDetails, server_json.get("complete_task")
45+
),
46+
fail_task=create_model_object(TaskDetails, server_json.get("fail_task")),
47+
expand_inline_json=server_json.get("expandInlineJSON"),
48+
terminate_workflow=create_model_object(
49+
TerminateWorkflow, server_json.get("terminate_workflow")
50+
),
51+
update_workflow_variables=create_model_object(
52+
UpdateWorkflowVariables, server_json.get("update_workflow_variables")
53+
),
54+
)
55+
assert server_json.get("action") == action_obj.action
56+
if "start_workflow" in server_json:
57+
assert action_obj.start_workflow is not None
58+
if "complete_task" in server_json:
59+
assert action_obj.complete_task is not None
60+
if "fail_task" in server_json:
61+
assert action_obj.fail_task is not None
62+
if "expandInlineJSON" in server_json:
63+
assert server_json.get("expandInlineJSON") == action_obj.expand_inline_json
64+
if "terminate_workflow" in server_json:
65+
assert action_obj.terminate_workflow is not None
66+
if "update_workflow_variables" in server_json:
67+
assert action_obj.update_workflow_variables is not None
68+
allowed_values = [
69+
"start_workflow",
70+
"complete_task",
71+
"fail_task",
72+
"terminate_workflow",
73+
"update_workflow_variables",
74+
]
75+
assert action_obj.action in allowed_values
76+
result_json = action_obj.to_dict()
77+
for key in server_json:
78+
if key == "expandInlineJSON":
79+
assert server_json[key] == result_json["expand_inline_json"]
80+
elif key in [
81+
"terminate_workflow",
82+
"start_workflow",
83+
"complete_task",
84+
"fail_task",
85+
"update_workflow_variables",
86+
]:
87+
if server_json[key] is not None:
88+
assert result_json[key] is not None
89+
if key == "terminate_workflow" and key in result_json:
90+
term_json = server_json[key]
91+
result_term = result_json[key]
92+
if "workflowId" in term_json and "workflowId" in result_term:
93+
assert term_json["workflowId"] == result_term["workflowId"]
94+
if (
95+
"terminationReason" in term_json
96+
and "terminationReason" in result_term
97+
):
98+
assert (
99+
term_json["terminationReason"]
100+
== result_term["terminationReason"]
101+
)
102+
if key == "update_workflow_variables" and key in result_json:
103+
update_json = server_json[key]
104+
result_update = result_json[key]
105+
if "workflowId" in update_json and "workflowId" in result_update:
106+
assert update_json["workflowId"] == result_update["workflowId"]
107+
if "variables" in update_json and "variables" in result_update:
108+
assert update_json["variables"] == result_update["variables"]
109+
if "appendArray" in update_json and "appendArray" in result_update:
110+
assert (
111+
update_json["appendArray"] == result_update["appendArray"]
112+
)
113+
elif key in result_json:
114+
assert server_json[key] == result_json[key]
Lines changed: 31 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,36 @@
1-
import unittest
21
import json
3-
from tests.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-
)
292

30-
# Verify model is properly initialized
31-
self.assertIsNotNone(auth_request, "Deserialized object should not be null")
3+
import pytest
324

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-
)
5+
from conductor.client.http.models.authorization_request import AuthorizationRequest
6+
from tests.serdesertest.util.serdeser_json_resolver_utility import JsonTemplateResolver
617

628

63-
if __name__ == '__main__':
64-
unittest.main()
9+
@pytest.fixture
10+
def server_json():
11+
return json.loads(JsonTemplateResolver.get_json_string("AuthorizationRequest"))
12+
13+
14+
def test_serialization_deserialization(server_json):
15+
auth_request = AuthorizationRequest(
16+
subject=server_json.get("subject"),
17+
target=server_json.get("target"),
18+
access=server_json.get("access"),
19+
)
20+
assert auth_request is not None, "Deserialized object should not be null"
21+
assert auth_request.access is not None, "Access list should not be null"
22+
assert all(
23+
access in ["CREATE", "READ", "UPDATE", "DELETE", "EXECUTE"]
24+
for access in auth_request.access
25+
)
26+
assert auth_request.subject is not None, "Subject should not be null"
27+
assert auth_request.target is not None, "Target should not be null"
28+
result_dict = auth_request.to_dict()
29+
assert set(server_json.keys()) == set(
30+
result_dict.keys()
31+
), "Serialized JSON should have the same keys as the original"
32+
original_json_normalized = json.dumps(server_json, sort_keys=True)
33+
result_json_normalized = json.dumps(result_dict, sort_keys=True)
34+
assert (
35+
original_json_normalized == result_json_normalized
36+
), "Serialized JSON should match the original SERVER_JSON"

0 commit comments

Comments
 (0)