Skip to content

Commit 332e5f3

Browse files
authored
MRG: Merge pull request #14 from octue/update-attribute-handling
Update attribute handling
2 parents 17331e3 + 58c8de6 commit 332e5f3

File tree

4 files changed

+67
-15
lines changed

4 files changed

+67
-15
lines changed

functions/event_handler/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def handle_event(cloud_event):
6262
"sender_sdk_version": attributes.pop("sender_sdk_version"),
6363
"recipient": attributes.pop("recipient"),
6464
"question_uuid": attributes.pop("question_uuid"),
65-
"parent_question_uuid": attributes.pop("parent_question_uuid"),
65+
"parent_question_uuid": attributes.pop("parent_question_uuid", None),
6666
"originator_question_uuid": attributes.pop("originator_question_uuid"),
6767
# Backend-specific metadata.
6868
"backend": BACKEND,

poetry.lock

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "twined-gcp"
3-
version = "0.7.0"
3+
version = "0.7.1"
44
description = ""
55
authors = [
66
{name = "Marcus Lugg", email = "[email protected]"}

tests/test_event_handler.py

+57-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
"question_uuid": QUESTION_UUID,
2525
"parent_question_uuid": "1d897229-155d-498d-b6ae-21960fab3754",
2626
"originator_question_uuid": "fb6cf9a3-84fb-45ce-a4da-0d2257bec319",
27-
"forward_logs": True,
27+
"retry_count": "0",
28+
"forward_logs": "1",
29+
"save_diagnostics": "SAVE_DIAGNOSTICS_ON_CRASH",
30+
"cpus": "1",
31+
"memory": "2Gi",
32+
"ephemeral_storage": "256Mi",
2833
}
2934

3035

@@ -75,9 +80,16 @@ def test_store_pub_sub_event_in_bigquery(self):
7580
"datetime": "2024-04-11T09:26:39.144818",
7681
"uuid": "c8bda9fa-f072-4330-92b1-96920d06b28d",
7782
"kind": "heart",
78-
"event": {"some": "data"},
83+
"event": {
84+
"some": "data",
85+
},
7986
"other_attributes": {
80-
"forward_logs": True,
87+
"retry_count": "0",
88+
"forward_logs": "1",
89+
"save_diagnostics": "SAVE_DIAGNOSTICS_ON_CRASH",
90+
"cpus": "1",
91+
"memory": "2Gi",
92+
"ephemeral_storage": "256Mi",
8193
},
8294
"parent": "octue/parent-test-service:5.6.3",
8395
"originator": "octue/ancestor-test-service:5.6.3",
@@ -93,13 +105,51 @@ def test_store_pub_sub_event_in_bigquery(self):
93105
},
94106
)
95107

108+
def test_store_pub_sub_event_in_bigquery_with_no_parent_question_uuid(self):
109+
"""Test that an event with no `parent_question_uuid` attribute is handled correctly."""
110+
attributes = copy.copy(EVENT_ATTRIBUTES)
111+
attributes.pop("parent_question_uuid")
112+
113+
cloud_event = MockCloudEvent(
114+
data={
115+
"message": {
116+
"data": base64.b64encode(b'{"kind": "heart", "some": "data"}'),
117+
"attributes": attributes,
118+
"messageId": "1234",
119+
}
120+
}
121+
)
122+
123+
mock_big_query_client = MockBigQueryClient()
124+
125+
with patch("functions.event_handler.main.BigQueryClient", return_value=mock_big_query_client):
126+
handle_event(cloud_event)
127+
128+
self.assertIsNone(mock_big_query_client.inserted_rows[0][0]["parent_question_uuid"])
129+
96130
def test_question_is_dispatched_to_kueue(self):
97131
"""Test that questions are dispatched to Kueue correctly."""
132+
event_attributes = {
133+
"datetime": "2024-04-11T09:26:39.144818",
134+
"uuid": "c8bda9fa-f072-4330-92b1-96920d06b28d",
135+
"parent": "octue/parent-test-service:5.6.3",
136+
"originator": "octue/ancestor-test-service:5.6.3",
137+
"sender": "octue/test-service:5.6.3",
138+
"sender_type": "PARENT",
139+
"sender_sdk_version": "1.0.3",
140+
"recipient": SRUID,
141+
"question_uuid": QUESTION_UUID,
142+
"parent_question_uuid": "1d897229-155d-498d-b6ae-21960fab3754",
143+
"originator_question_uuid": "fb6cf9a3-84fb-45ce-a4da-0d2257bec319",
144+
"retry_count": "0",
145+
"forward_logs": "1",
146+
}
147+
98148
cloud_event = MockCloudEvent(
99149
data={
100150
"message": {
101151
"data": base64.b64encode(b'{"kind": "question", "input_values": {"some": "data"}}'),
102-
"attributes": copy.copy(EVENT_ATTRIBUTES),
152+
"attributes": copy.copy(event_attributes),
103153
"messageId": "1234",
104154
}
105155
}
@@ -118,11 +168,13 @@ def test_question_is_dispatched_to_kueue(self):
118168
self.assertEqual(container.name, job.metadata.name)
119169
self.assertEqual(container.image, f"some-artifact-registry-url/{SRUID}")
120170
self.assertEqual(container.command, ["octue", "question", "ask", "local"])
171+
172+
# Check the default resource requirements are used.
121173
self.assertEqual(container.resources, {"requests": {"cpu": 1, "ephemeral-storage": "1Gi", "memory": "500Mi"}})
122174

123175
self.assertEqual(
124176
container.args,
125-
["--attributes", json.dumps(EVENT_ATTRIBUTES), "--input-values", '{"some": "data"}'],
177+
["--attributes", json.dumps(event_attributes), "--input-values", '{"some": "data"}'],
126178
)
127179

128180
environment_variables = [variable.to_dict() for variable in container.env]

0 commit comments

Comments
 (0)