Skip to content

Commit

Permalink
TST: Update tests to use new stored events format
Browse files Browse the repository at this point in the history
skipci
  • Loading branch information
cortadocodes committed Jun 26, 2024
1 parent 39a2303 commit 038f624
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_asynchronous_question(self):
self.assertIsNone(answer)

# Wait for question to complete.
time.sleep(10)
time.sleep(12)

events = get_events(
table_id="octue_sdk_python_test_dataset.service-events",
Expand Down
2 changes: 1 addition & 1 deletion tests/cloud/emulators/test_child_emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def error_run_function(*args, **kwargs):
subscription, _ = parent.ask(service_id=child.id, input_values={})
parent.wait_for_answer(subscription=subscription)

child_emulator = ChildEmulator(events=parent.received_events)
child_emulator = ChildEmulator(events=[event["event"] for event in parent.received_events])

with self.assertRaises(OSError):
child_emulator.ask(input_values={})
Expand Down
70 changes: 50 additions & 20 deletions tests/cloud/pub_sub/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_timeout(self):
event_handler = GoogleCloudPubSubEventHandler(
subscription=self.subscription,
recipient=self.parent,
event_handlers={"test": lambda message: None, "finish-test": lambda message: message},
event_handlers={"test": lambda event, attributes: None, "finish-test": lambda event, attributes: event},
schema={},
)

Expand All @@ -55,7 +55,10 @@ def test_in_order_messages_are_handled_in_order(self):
event_handler = GoogleCloudPubSubEventHandler(
subscription=self.subscription,
recipient=self.parent,
event_handlers={"test": lambda message: None, "finish-test": lambda message: "This is the result."},
event_handlers={
"test": lambda event, attributes: None,
"finish-test": lambda event, attributes: "This is the result.",
},
schema={},
)

Expand Down Expand Up @@ -98,7 +101,7 @@ def test_in_order_messages_are_handled_in_order(self):
self.assertEqual(result, "This is the result.")

self.assertEqual(
event_handler.handled_events,
[event["event"] for event in event_handler.handled_events],
[
{"kind": "test", "order": 0},
{"kind": "test", "order": 1},
Expand All @@ -112,7 +115,10 @@ def test_out_of_order_messages_are_handled_in_order(self):
event_handler = GoogleCloudPubSubEventHandler(
subscription=self.subscription,
recipient=self.parent,
event_handlers={"test": lambda message: None, "finish-test": lambda message: "This is the result."},
event_handlers={
"test": lambda event, attributes: None,
"finish-test": lambda event, attributes: "This is the result.",
},
schema={},
)

Expand Down Expand Up @@ -156,7 +162,7 @@ def test_out_of_order_messages_are_handled_in_order(self):
self.assertEqual(result, "This is the result.")

self.assertEqual(
event_handler.handled_events,
[event["event"] for event in event_handler.handled_events],
[
{"kind": "test", "order": 0},
{"kind": "test", "order": 1},
Expand All @@ -172,7 +178,10 @@ def test_out_of_order_messages_with_end_message_first_are_handled_in_order(self)
event_handler = GoogleCloudPubSubEventHandler(
subscription=self.subscription,
recipient=self.parent,
event_handlers={"test": lambda message: None, "finish-test": lambda message: "This is the result."},
event_handlers={
"test": lambda event, attributes: None,
"finish-test": lambda event, attributes: "This is the result.",
},
schema={},
)

Expand Down Expand Up @@ -216,7 +225,7 @@ def test_out_of_order_messages_with_end_message_first_are_handled_in_order(self)
self.assertEqual(result, "This is the result.")

self.assertEqual(
event_handler.handled_events,
[event["event"] for event in event_handler.handled_events],
[
{"kind": "test", "order": 0},
{"kind": "test", "order": 1},
Expand All @@ -230,7 +239,10 @@ def test_no_timeout(self):
event_handler = GoogleCloudPubSubEventHandler(
subscription=self.subscription,
recipient=self.parent,
event_handlers={"test": lambda message: None, "finish-test": lambda message: "This is the result."},
event_handlers={
"test": lambda event, attributes: None,
"finish-test": lambda event, attributes: "This is the result.",
},
schema={},
)

Expand Down Expand Up @@ -269,7 +281,7 @@ def test_no_timeout(self):

self.assertEqual(result, "This is the result.")
self.assertEqual(
event_handler.handled_events,
[event["event"] for event in event_handler.handled_events],
[{"kind": "test", "order": 0}, {"kind": "test", "order": 1}, {"kind": "finish-test", "order": 2}],
)

Expand Down Expand Up @@ -395,7 +407,10 @@ def test_missing_messages_at_start_can_be_skipped(self):
event_handler = GoogleCloudPubSubEventHandler(
subscription=self.subscription,
recipient=self.parent,
event_handlers={"test": lambda message: None, "finish-test": lambda message: "This is the result."},
event_handlers={
"test": lambda event, attributes: None,
"finish-test": lambda event, attributes: "This is the result.",
},
schema={},
skip_missing_events_after=0,
)
Expand Down Expand Up @@ -447,7 +462,7 @@ def test_missing_messages_at_start_can_be_skipped(self):

self.assertEqual(result, "This is the result.")
self.assertEqual(
event_handler.handled_events,
[event["event"] for event in event_handler.handled_events],
[
{"kind": "test", "order": 2},
{"kind": "test", "order": 3},
Expand All @@ -461,7 +476,10 @@ def test_missing_messages_in_middle_can_skipped(self):
event_handler = GoogleCloudPubSubEventHandler(
subscription=self.subscription,
recipient=self.parent,
event_handlers={"test": lambda message: None, "finish-test": lambda message: "This is the result."},
event_handlers={
"test": lambda event, attributes: None,
"finish-test": lambda event, attributes: "This is the result.",
},
schema={},
skip_missing_events_after=0,
)
Expand Down Expand Up @@ -524,7 +542,7 @@ def test_missing_messages_in_middle_can_skipped(self):

# Check that all the non-missing messages were handled.
self.assertEqual(
event_handler.handled_events,
[event["event"] for event in event_handler.handled_events],
[
{"kind": "test", "order": 0},
{"kind": "test", "order": 1},
Expand All @@ -538,7 +556,10 @@ def test_multiple_blocks_of_missing_messages_in_middle_can_skipped(self):
event_handler = GoogleCloudPubSubEventHandler(
subscription=self.subscription,
recipient=self.parent,
event_handlers={"test": lambda message: None, "finish-test": lambda message: "This is the result."},
event_handlers={
"test": lambda event, attributes: None,
"finish-test": lambda event, attributes: "This is the result.",
},
schema={},
skip_missing_events_after=0,
)
Expand Down Expand Up @@ -642,7 +663,7 @@ def test_multiple_blocks_of_missing_messages_in_middle_can_skipped(self):

# Check that all the non-missing messages were handled.
self.assertEqual(
event_handler.handled_events,
[event["event"] for event in event_handler.handled_events],
[
{"kind": "test", "order": 0},
{"kind": "test", "order": 1},
Expand All @@ -660,7 +681,10 @@ def test_all_messages_missing_apart_from_result(self):
event_handler = GoogleCloudPubSubEventHandler(
subscription=self.subscription,
recipient=self.parent,
event_handlers={"test": lambda message: None, "finish-test": lambda message: "This is the result."},
event_handlers={
"test": lambda event, attributes: None,
"finish-test": lambda event, attributes: "This is the result.",
},
schema={},
skip_missing_events_after=0,
)
Expand Down Expand Up @@ -691,7 +715,7 @@ def test_all_messages_missing_apart_from_result(self):
logging_context.output[0],
)
# Check that the result message was handled.
self.assertEqual(event_handler.handled_events, [{"kind": "finish-test", "order": 1000}])
self.assertEqual(event_handler.handled_events[0]["event"], {"kind": "finish-test", "order": 1000})


class TestPullAndEnqueueAvailableMessages(BaseTestCase):
Expand Down Expand Up @@ -726,7 +750,10 @@ def test_pull_and_enqueue_available_events(self):
event_handler = GoogleCloudPubSubEventHandler(
subscription=self.subscription,
recipient=self.parent,
event_handlers={"test": lambda message: None, "finish-test": lambda message: "This is the result."},
event_handlers={
"test": lambda event, attributes: None,
"finish-test": lambda event, attributes: "This is the result.",
},
schema={},
)

Expand Down Expand Up @@ -754,14 +781,17 @@ def test_pull_and_enqueue_available_events(self):
]

event_handler._pull_and_enqueue_available_events(timeout=10)
self.assertEqual(event_handler.waiting_events, {0: mock_message})
self.assertEqual(event_handler.waiting_events[0][0], mock_message)

def test_timeout_error_raised_if_result_message_not_received_in_time(self):
"""Test that a timeout error is raised if a result message is not received in time."""
event_handler = GoogleCloudPubSubEventHandler(
subscription=self.subscription,
recipient=self.parent,
event_handlers={"test": lambda message: None, "finish-test": lambda message: "This is the result."},
event_handlers={
"test": lambda event, attributes: None,
"finish-test": lambda event, attributes: "This is the result.",
},
)

event_handler.child_sdk_version = "0.1.3"
Expand Down
24 changes: 12 additions & 12 deletions tests/cloud/pub_sub/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -691,11 +691,11 @@ def test_child_messages_can_be_recorded_by_parent(self):
parent.wait_for_answer(subscription)

# Check that the child's messages have been recorded by the parent.
self.assertEqual(parent.received_events[0]["kind"], "delivery_acknowledgement")
self.assertEqual(parent.received_events[1]["kind"], "log_record")
self.assertEqual(parent.received_events[2]["kind"], "log_record")
self.assertEqual(parent.received_events[3]["kind"], "log_record")
self.assertEqual(parent.received_events[4], {"kind": "result", "output_values": "Hello! It worked!"})
self.assertEqual(parent.received_events[0]["event"]["kind"], "delivery_acknowledgement")
self.assertEqual(parent.received_events[1]["event"]["kind"], "log_record")
self.assertEqual(parent.received_events[2]["event"]["kind"], "log_record")
self.assertEqual(parent.received_events[3]["event"]["kind"], "log_record")
self.assertEqual(parent.received_events[4]["event"], {"kind": "result", "output_values": "Hello! It worked!"})

def test_child_exception_message_can_be_recorded_by_parent(self):
"""Test that the parent can record exceptions raised by the child."""
Expand All @@ -708,9 +708,9 @@ def test_child_exception_message_can_be_recorded_by_parent(self):
parent.wait_for_answer(subscription)

# Check that the child's messages have been recorded by the parent.
self.assertEqual(parent.received_events[0]["kind"], "delivery_acknowledgement")
self.assertEqual(parent.received_events[1]["kind"], "exception")
self.assertIn("Oh no.", parent.received_events[1]["exception_message"])
self.assertEqual(parent.received_events[0]["event"]["kind"], "delivery_acknowledgement")
self.assertEqual(parent.received_events[1]["event"]["kind"], "exception")
self.assertIn("Oh no.", parent.received_events[1]["event"]["exception_message"])

def test_child_sends_heartbeat_messages_at_expected_regular_intervals(self):
"""Test that children send heartbeat messages at the expected regular intervals."""
Expand All @@ -737,11 +737,11 @@ def run_function(*args, **kwargs):

parent.wait_for_answer(subscription)

self.assertEqual(parent.received_events[1]["kind"], "heartbeat")
self.assertEqual(parent.received_events[2]["kind"], "heartbeat")
self.assertEqual(parent.received_events[1]["event"]["kind"], "heartbeat")
self.assertEqual(parent.received_events[2]["event"]["kind"], "heartbeat")

first_heartbeat_time = datetime.datetime.fromisoformat(parent.received_events[1]["datetime"])
second_heartbeat_time = datetime.datetime.fromisoformat(parent.received_events[2]["datetime"])
first_heartbeat_time = datetime.datetime.fromisoformat(parent.received_events[1]["event"]["datetime"])
second_heartbeat_time = datetime.datetime.fromisoformat(parent.received_events[2]["event"]["datetime"])

self.assertAlmostEqual(
second_heartbeat_time - first_heartbeat_time,
Expand Down
10 changes: 5 additions & 5 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,10 @@ def app(analysis):
self.assertEqual(questions[1]["id"], f"octue/yet-another-child:{MOCK_SERVICE_REVISION_TAG}")
self.assertEqual(questions[1]["input_values"], "miaow")

self.assertEqual(questions[1]["events"][1]["kind"], "exception")
self.assertEqual(questions[1]["events"][1]["exception_type"], "ValueError")
self.assertEqual(questions[1]["events"][1]["event"]["kind"], "exception")
self.assertEqual(questions[1]["events"][1]["event"]["exception_type"], "ValueError")
self.assertEqual(
questions[1]["events"][1]["exception_message"],
questions[1]["events"][1]["event"]["exception_message"],
f"Error in <MockService('octue/yet-another-child:{MOCK_SERVICE_REVISION_TAG}')>: Deliberately raised for "
f"testing.",
)
Expand Down Expand Up @@ -863,14 +863,14 @@ def app(analysis):
self.assertEqual(questions[0]["key"], "my-child")
self.assertEqual(questions[0]["id"], f"octue/a-child:{MOCK_SERVICE_REVISION_TAG}")
self.assertEqual(questions[0]["input_values"], [1, 2, 3, 4])
self.assertEqual(questions[0]["events"][1]["output_values"], [1, 4, 9, 16])
self.assertEqual(questions[0]["events"][1]["event"]["output_values"], [1, 4, 9, 16])
self.assertEqual(len(questions[0]["events"]), 2)

# Second question.
self.assertEqual(questions[1]["key"], "another-child")
self.assertEqual(questions[1]["id"], f"octue/another-child:{MOCK_SERVICE_REVISION_TAG}")
self.assertEqual(questions[1]["input_values"], "miaow")
self.assertEqual(questions[1]["events"][1]["output_values"], "woof")
self.assertEqual(questions[1]["events"][1]["event"]["output_values"], "woof")

# This should be 4 but log messages aren't currently being handled by the child emulator correctly.
self.assertEqual(len(questions[1]["events"]), 2)

0 comments on commit 038f624

Please sign in to comment.