gts-agency-python-sdk 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agency_sdk/__init__.py +0 -0
- agency_sdk/auth_hooks.py +55 -0
- agency_sdk/client.py +212 -0
- agency_sdk/credentials.py +54 -0
- agency_sdk/delegates/__init__.py +0 -0
- agency_sdk/delegates/annotations_client.py +495 -0
- agency_sdk/delegates/annotations_dto.py +200 -0
- agency_sdk/delegates/base_client.py +126 -0
- agency_sdk/delegates/datasets_client.py +109 -0
- agency_sdk/delegates/datasets_dto.py +81 -0
- agency_sdk/delegates/datasource_client.py +44 -0
- agency_sdk/delegates/datasource_dto.py +107 -0
- agency_sdk/delegates/files_client.py +184 -0
- agency_sdk/delegates/files_dto.py +36 -0
- agency_sdk/delegates/gateway_client.py +81 -0
- agency_sdk/delegates/gateway_dto.py +37 -0
- agency_sdk/delegates/ontology_client.py +203 -0
- agency_sdk/delegates/ontology_dto.py +221 -0
- agency_sdk/delegates/prompts_client.py +56 -0
- agency_sdk/delegates/rules_client.py +33 -0
- agency_sdk/delegates/rules_dto.py +108 -0
- agency_sdk/delegates/session_client.py +95 -0
- agency_sdk/delegates/session_dto.py +94 -0
- agency_sdk/delegates/session_templates_client.py +23 -0
- agency_sdk/delegates/session_templates_dto.py +27 -0
- agency_sdk/delegates/session_vault_client.py +135 -0
- agency_sdk/delegates/session_vault_dto.py +75 -0
- agency_sdk/delegates/work_queue_client.py +230 -0
- agency_sdk/delegates/work_queue_dto.py +127 -0
- agency_sdk/domain.py +140 -0
- agency_sdk/observability/__init__.py +60 -0
- agency_sdk/observability/auth.py +15 -0
- agency_sdk/observability/bootstrap.py +344 -0
- agency_sdk/test/__init__.py +0 -0
- agency_sdk/test/conftest.py +106 -0
- agency_sdk/test/test_annotations_client.py +599 -0
- agency_sdk/test/test_annotations_dto.py +299 -0
- agency_sdk/test/test_base_client.py +153 -0
- agency_sdk/test/test_client_facade.py +105 -0
- agency_sdk/test/test_credentials.py +67 -0
- agency_sdk/test/test_files_client.py +264 -0
- agency_sdk/test/test_files_dto.py +93 -0
- agency_sdk/test/test_gateway_dto.py +70 -0
- agency_sdk/test/test_gateway_facade.py +229 -0
- agency_sdk/test/test_gateway_openai.py +76 -0
- agency_sdk/test/test_observability_auth.py +125 -0
- agency_sdk/test/test_observability_facade_threadsafety.py +50 -0
- agency_sdk/test/test_observability_langfuse.py +76 -0
- agency_sdk/test/test_observability_packaging.py +78 -0
- agency_sdk/test/test_observability_pipeline.py +246 -0
- agency_sdk/test/test_ontology_client.py +152 -0
- agency_sdk/test/test_ontology_dto.py +158 -0
- agency_sdk/test/test_session_client.py +128 -0
- agency_sdk/test/test_session_dto.py +75 -0
- agency_sdk/test/test_session_templates_client.py +36 -0
- agency_sdk/test/test_session_templates_dto.py +45 -0
- agency_sdk/test/test_session_vault_client.py +151 -0
- agency_sdk/test/test_session_vault_dto.py +27 -0
- agency_sdk/test/test_stub_requests.py +39 -0
- agency_sdk/test/test_work_queue_client.py +388 -0
- agency_sdk/test/test_work_queue_dto.py +233 -0
- gts_agency_python_sdk-0.0.1.dist-info/METADATA +221 -0
- gts_agency_python_sdk-0.0.1.dist-info/RECORD +66 -0
- gts_agency_python_sdk-0.0.1.dist-info/WHEEL +5 -0
- gts_agency_python_sdk-0.0.1.dist-info/licenses/LICENSE +201 -0
- gts_agency_python_sdk-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
"""Offline protocol tests for AgencyAnnotationsClient.
|
|
2
|
+
|
|
3
|
+
Publishing is two calls — create a DRAFT batch, then upload the graph that
|
|
4
|
+
materialises the jobs — so these assert the exact wire shapes of both legs: the
|
|
5
|
+
``{command, organisation, payload}`` envelope with unset fields omitted, and the
|
|
6
|
+
multipart ``file`` field with only the query params the caller actually supplied.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
|
|
11
|
+
import pytest
|
|
12
|
+
import requests
|
|
13
|
+
|
|
14
|
+
from agency_sdk.delegates.annotations_client import AgencyAnnotationsClient
|
|
15
|
+
from agency_sdk.delegates.annotations_dto import BatchStatus, SpecStatus
|
|
16
|
+
from agency_sdk.test.test_annotations_dto import ACTIVE_BATCH_JSON, DRAFT_BATCH_JSON, SPEC_JSON
|
|
17
|
+
|
|
18
|
+
GRAPH = {
|
|
19
|
+
"run_id": "run-2026-08-03-a",
|
|
20
|
+
"vertices": [
|
|
21
|
+
{"bid": "v-rule-1", "class": "rule", "name": "Knee MRI indications"},
|
|
22
|
+
{"bid": "v-rule-2", "class": "rule", "name": "Conservative care first"},
|
|
23
|
+
{"bid": "v-doc-1", "class": "document", "name": "MTUS 2026"},
|
|
24
|
+
],
|
|
25
|
+
"edges": [{"from": "v-rule-1", "to": "v-doc-1", "label": "sourced_from"}],
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
CREATED_ENVELOPE = {
|
|
29
|
+
"success": True,
|
|
30
|
+
"message": "Batch created: 7f1d9c62-1f2a-4a51-9a1e-2d0c3f5b8e40",
|
|
31
|
+
"data": {"id": "7f1d9c62-1f2a-4a51-9a1e-2d0c3f5b8e40"},
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@pytest.fixture
|
|
36
|
+
def client(fake_credentials):
|
|
37
|
+
return AgencyAnnotationsClient(token_supplier=fake_credentials, base_url="http://cp.test/")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class TestCreateBatch:
|
|
41
|
+
def test_posts_the_command_envelope_and_lifts_the_id(self, client, stub_requests):
|
|
42
|
+
stub_requests.queue(json_data=CREATED_ENVELOPE)
|
|
43
|
+
|
|
44
|
+
result = client.create_batch(organisation_id=2, name="MTUS Knee 2026")
|
|
45
|
+
|
|
46
|
+
call = stub_requests.calls[0]
|
|
47
|
+
assert call.method == "POST"
|
|
48
|
+
assert call.url == "http://cp.test/api/annotations/_command"
|
|
49
|
+
assert call.kwargs["json"] == {
|
|
50
|
+
"command": "create",
|
|
51
|
+
"organisation": 2,
|
|
52
|
+
"payload": {"name": "MTUS Knee 2026", "batch_type": "graph"},
|
|
53
|
+
}
|
|
54
|
+
assert result.id == "7f1d9c62-1f2a-4a51-9a1e-2d0c3f5b8e40"
|
|
55
|
+
assert result.success is True
|
|
56
|
+
assert result.message == CREATED_ENVELOPE["message"]
|
|
57
|
+
|
|
58
|
+
def test_omits_unset_payload_fields(self, client, stub_requests):
|
|
59
|
+
stub_requests.queue(json_data=CREATED_ENVELOPE)
|
|
60
|
+
|
|
61
|
+
client.create_batch(organisation_id=2, name="n")
|
|
62
|
+
|
|
63
|
+
payload = stub_requests.calls[0].kwargs["json"]["payload"]
|
|
64
|
+
assert set(payload) == {"name", "batch_type"}
|
|
65
|
+
|
|
66
|
+
def test_sends_every_supplied_payload_field(self, client, stub_requests):
|
|
67
|
+
stub_requests.queue(json_data=CREATED_ENVELOPE)
|
|
68
|
+
|
|
69
|
+
client.create_batch(
|
|
70
|
+
organisation_id=2,
|
|
71
|
+
name="MTUS Knee 2026",
|
|
72
|
+
description="2026 revision",
|
|
73
|
+
instructions="Check each rule against the source PDF.",
|
|
74
|
+
batch_type="dataset",
|
|
75
|
+
confidentiality_level="RESTRICTED",
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
assert stub_requests.calls[0].kwargs["json"]["payload"] == {
|
|
79
|
+
"name": "MTUS Knee 2026",
|
|
80
|
+
"description": "2026 revision",
|
|
81
|
+
"instructions": "Check each rule against the source PDF.",
|
|
82
|
+
"batch_type": "dataset",
|
|
83
|
+
"confidentiality_level": "RESTRICTED",
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
def test_raises_when_the_envelope_carries_no_id(self, client, stub_requests):
|
|
87
|
+
stub_requests.queue(json_data={"success": True, "message": "Batch created", "data": None})
|
|
88
|
+
|
|
89
|
+
with pytest.raises(ValueError, match="no batch id"):
|
|
90
|
+
client.create_batch(organisation_id=2, name="n")
|
|
91
|
+
|
|
92
|
+
def test_raises_when_data_is_absent_entirely(self, client, stub_requests):
|
|
93
|
+
stub_requests.queue(json_data={"success": True, "message": "Batch created"})
|
|
94
|
+
|
|
95
|
+
with pytest.raises(ValueError, match="no batch id"):
|
|
96
|
+
client.create_batch(organisation_id=2, name="n")
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class TestUploadGraph:
|
|
100
|
+
def test_posts_the_graph_as_a_multipart_file_field(self, client, stub_requests):
|
|
101
|
+
stub_requests.queue(json_data=None)
|
|
102
|
+
|
|
103
|
+
assert client.upload_graph(organisation_id=2, batch_id="b-1", graph=GRAPH) is None
|
|
104
|
+
|
|
105
|
+
call = stub_requests.calls[0]
|
|
106
|
+
assert call.method == "POST"
|
|
107
|
+
assert call.url == "http://cp.test/api/annotations/b-1/upload"
|
|
108
|
+
assert call.kwargs["headers"] == {"Authorization": "Bearer test-token"}
|
|
109
|
+
assert call.kwargs["timeout"] == 300
|
|
110
|
+
filename, body, content_type = call.kwargs["files"]["file"]
|
|
111
|
+
assert filename == "graph.json"
|
|
112
|
+
assert content_type == "application/json"
|
|
113
|
+
assert json.loads(body) == GRAPH
|
|
114
|
+
|
|
115
|
+
def test_sends_only_the_query_params_supplied(self, client, stub_requests):
|
|
116
|
+
stub_requests.queue(json_data=None)
|
|
117
|
+
|
|
118
|
+
client.upload_graph(organisation_id=2, batch_id="b-1", graph=GRAPH)
|
|
119
|
+
|
|
120
|
+
assert stub_requests.calls[0].kwargs["params"] == {"o": "2"}
|
|
121
|
+
|
|
122
|
+
def test_forwards_job_generation_params_when_given(self, client, stub_requests):
|
|
123
|
+
stub_requests.queue(json_data=None)
|
|
124
|
+
|
|
125
|
+
client.upload_graph(
|
|
126
|
+
organisation_id=2,
|
|
127
|
+
batch_id="b-1",
|
|
128
|
+
graph=GRAPH,
|
|
129
|
+
job_type="rule_validation",
|
|
130
|
+
target_class="rule",
|
|
131
|
+
hops=2,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
assert stub_requests.calls[0].kwargs["params"] == {
|
|
135
|
+
"o": "2",
|
|
136
|
+
"job_type": "rule_validation",
|
|
137
|
+
"target_class": "rule",
|
|
138
|
+
"hops": "2",
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
def test_a_file_path_and_a_dict_produce_the_same_body(self, client, stub_requests, tmp_path):
|
|
142
|
+
graph_file = tmp_path / "sandbox_graph.json"
|
|
143
|
+
graph_file.write_text(json.dumps(GRAPH))
|
|
144
|
+
stub_requests.queue(json_data=None)
|
|
145
|
+
stub_requests.queue(json_data=None)
|
|
146
|
+
|
|
147
|
+
client.upload_graph(organisation_id=2, batch_id="b-1", graph=GRAPH)
|
|
148
|
+
client.upload_graph(organisation_id=2, batch_id="b-1", file_path=graph_file)
|
|
149
|
+
|
|
150
|
+
from_dict = stub_requests.calls[0].kwargs["files"]["file"]
|
|
151
|
+
from_path = stub_requests.calls[1].kwargs["files"]["file"]
|
|
152
|
+
assert from_dict[1] == from_path[1]
|
|
153
|
+
assert from_path[0] == "sandbox_graph.json"
|
|
154
|
+
|
|
155
|
+
def test_filename_can_be_overridden(self, client, stub_requests):
|
|
156
|
+
stub_requests.queue(json_data=None)
|
|
157
|
+
|
|
158
|
+
client.upload_graph(organisation_id=2, batch_id="b-1", graph=GRAPH, filename="mtus-knee.json")
|
|
159
|
+
|
|
160
|
+
assert stub_requests.calls[0].kwargs["files"]["file"][0] == "mtus-knee.json"
|
|
161
|
+
|
|
162
|
+
def test_rejects_both_graph_and_file_path_before_any_call(self, client, stub_requests, tmp_path):
|
|
163
|
+
graph_file = tmp_path / "graph.json"
|
|
164
|
+
graph_file.write_text("{}")
|
|
165
|
+
|
|
166
|
+
with pytest.raises(ValueError, match="exactly one"):
|
|
167
|
+
client.upload_graph(organisation_id=2, batch_id="b-1", graph=GRAPH, file_path=graph_file)
|
|
168
|
+
|
|
169
|
+
assert stub_requests.calls == []
|
|
170
|
+
|
|
171
|
+
def test_rejects_neither_graph_nor_file_path_before_any_call(self, client, stub_requests):
|
|
172
|
+
with pytest.raises(ValueError, match="exactly one"):
|
|
173
|
+
client.upload_graph(organisation_id=2, batch_id="b-1")
|
|
174
|
+
|
|
175
|
+
assert stub_requests.calls == []
|
|
176
|
+
|
|
177
|
+
def test_propagates_a_400_from_the_server(self, client, stub_requests):
|
|
178
|
+
stub_requests.queue(
|
|
179
|
+
json_data={"error": {"message": "No vertices of class 'rule' found in graph"}}, status_code=400
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
with pytest.raises(requests.HTTPError):
|
|
183
|
+
client.upload_graph(organisation_id=2, batch_id="b-1", graph={"vertices": []})
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
class TestBatchReads:
|
|
187
|
+
def test_get_batch_reads_the_flattened_response(self, client, stub_requests):
|
|
188
|
+
stub_requests.queue(json_data={**ACTIVE_BATCH_JSON, "viewer_role": "admin"})
|
|
189
|
+
|
|
190
|
+
batch = client.get_batch(organisation_id=2, batch_id=ACTIVE_BATCH_JSON["id"])
|
|
191
|
+
|
|
192
|
+
call = stub_requests.calls[0]
|
|
193
|
+
assert call.method == "GET"
|
|
194
|
+
assert call.url == f"http://cp.test/api/annotations/{ACTIVE_BATCH_JSON['id']}"
|
|
195
|
+
assert call.kwargs["params"] == {"o": "2"}
|
|
196
|
+
assert batch.viewer_role == "admin"
|
|
197
|
+
assert batch.status == BatchStatus.ACTIVE
|
|
198
|
+
assert batch.total_jobs == 325
|
|
199
|
+
assert batch.graph_run_id == "run-2026-08-03-a"
|
|
200
|
+
|
|
201
|
+
def test_get_batch_of_a_draft_reports_no_jobs_yet(self, client, stub_requests):
|
|
202
|
+
stub_requests.queue(json_data=DRAFT_BATCH_JSON)
|
|
203
|
+
|
|
204
|
+
batch = client.get_batch(organisation_id=2, batch_id=DRAFT_BATCH_JSON["id"])
|
|
205
|
+
|
|
206
|
+
assert batch.status == BatchStatus.DRAFT
|
|
207
|
+
assert batch.total_jobs == 0
|
|
208
|
+
assert batch.viewer_role is None
|
|
209
|
+
|
|
210
|
+
def test_list_batches_pages_with_defaults(self, client, stub_requests):
|
|
211
|
+
stub_requests.queue(json_data={"page": {"page": 0, "size": 50, "total": 1}, "items": [DRAFT_BATCH_JSON]})
|
|
212
|
+
|
|
213
|
+
result = client.list_batches(organisation_id=2)
|
|
214
|
+
|
|
215
|
+
call = stub_requests.calls[0]
|
|
216
|
+
assert call.method == "GET"
|
|
217
|
+
assert call.url == "http://cp.test/api/annotations"
|
|
218
|
+
assert call.kwargs["params"] == {"o": "2", "p": "0", "s": "50"}
|
|
219
|
+
assert [b.name for b in result.items] == ["MTUS Knee 2026"]
|
|
220
|
+
|
|
221
|
+
def test_list_batches_forwards_pagination_and_filters(self, client, stub_requests):
|
|
222
|
+
stub_requests.queue(json_data={"page": {"page": 2, "size": 5, "total": 0}, "items": []})
|
|
223
|
+
|
|
224
|
+
client.list_batches(organisation_id=9, page=2, size=5, batch_type="graph", view="mine")
|
|
225
|
+
|
|
226
|
+
assert stub_requests.calls[0].kwargs["params"] == {
|
|
227
|
+
"o": "9",
|
|
228
|
+
"p": "2",
|
|
229
|
+
"s": "5",
|
|
230
|
+
"batch_type": "graph",
|
|
231
|
+
"view": "mine",
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
class TestSpecs:
|
|
236
|
+
def test_create_spec_posts_to_the_specs_root(self, client, stub_requests):
|
|
237
|
+
stub_requests.queue(
|
|
238
|
+
json_data={"success": True, "message": "Specification created: 3b0e", "data": {"id": "3b0e"}}
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
result = client.create_spec(
|
|
242
|
+
organisation_id=2,
|
|
243
|
+
code="rule_validation",
|
|
244
|
+
name="Rule validation",
|
|
245
|
+
checklist=[{"id": "text_matches_source", "label": "Rule text matches the source"}],
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
call = stub_requests.calls[0]
|
|
249
|
+
assert call.method == "POST"
|
|
250
|
+
assert call.url == "http://cp.test/api/annotation-specs/_command"
|
|
251
|
+
assert call.kwargs["json"] == {
|
|
252
|
+
"command": "create",
|
|
253
|
+
"organisation": 2,
|
|
254
|
+
"payload": {
|
|
255
|
+
"code": "rule_validation",
|
|
256
|
+
"name": "Rule validation",
|
|
257
|
+
"checklist": [{"id": "text_matches_source", "label": "Rule text matches the source"}],
|
|
258
|
+
},
|
|
259
|
+
}
|
|
260
|
+
assert result.id == "3b0e"
|
|
261
|
+
|
|
262
|
+
def test_create_spec_sends_instructions_when_given(self, client, stub_requests):
|
|
263
|
+
stub_requests.queue(json_data={"success": True, "message": "created", "data": {"id": "3b0e"}})
|
|
264
|
+
|
|
265
|
+
client.create_spec(
|
|
266
|
+
organisation_id=2, code="c", name="n", checklist=[], instructions="Check the page reference."
|
|
267
|
+
)
|
|
268
|
+
|
|
269
|
+
assert stub_requests.calls[0].kwargs["json"]["payload"]["instructions"] == "Check the page reference."
|
|
270
|
+
|
|
271
|
+
def test_create_spec_raises_when_the_envelope_carries_no_id(self, client, stub_requests):
|
|
272
|
+
stub_requests.queue(json_data={"success": True, "message": "created"})
|
|
273
|
+
|
|
274
|
+
with pytest.raises(ValueError, match="no specification id"):
|
|
275
|
+
client.create_spec(organisation_id=2, code="c", name="n", checklist=[])
|
|
276
|
+
|
|
277
|
+
def test_get_spec_puts_the_CODE_in_the_path(self, client, stub_requests):
|
|
278
|
+
stub_requests.queue(json_data=SPEC_JSON)
|
|
279
|
+
|
|
280
|
+
spec = client.get_spec(organisation_id=2, code="rule_validation")
|
|
281
|
+
|
|
282
|
+
call = stub_requests.calls[0]
|
|
283
|
+
assert call.method == "GET"
|
|
284
|
+
assert call.url == "http://cp.test/api/annotation-specs/rule_validation"
|
|
285
|
+
assert call.kwargs["params"] == {"o": "2"}
|
|
286
|
+
assert spec.id == SPEC_JSON["id"]
|
|
287
|
+
assert spec.status == SpecStatus.ACTIVE
|
|
288
|
+
|
|
289
|
+
def test_get_spec_propagates_a_404_for_an_unseeded_code(self, client, stub_requests):
|
|
290
|
+
stub_requests.queue(json_data={"error": {"message": "Specification 'nope' not found"}}, status_code=404)
|
|
291
|
+
|
|
292
|
+
with pytest.raises(requests.HTTPError):
|
|
293
|
+
client.get_spec(organisation_id=2, code="nope")
|
|
294
|
+
|
|
295
|
+
def test_list_specs_pages_over_the_specs_root(self, client, stub_requests):
|
|
296
|
+
stub_requests.queue(json_data={"page": {"page": 0, "size": 50, "total": 1}, "items": [SPEC_JSON]})
|
|
297
|
+
|
|
298
|
+
result = client.list_specs(organisation_id=2)
|
|
299
|
+
|
|
300
|
+
call = stub_requests.calls[0]
|
|
301
|
+
assert call.url == "http://cp.test/api/annotation-specs"
|
|
302
|
+
assert call.kwargs["params"] == {"o": "2", "p": "0", "s": "50"}
|
|
303
|
+
assert [s.code for s in result.items] == ["rule_validation"]
|
|
304
|
+
|
|
305
|
+
def test_list_specs_forwards_pagination(self, client, stub_requests):
|
|
306
|
+
stub_requests.queue(json_data={"page": {"page": 1, "size": 5, "total": 0}, "items": []})
|
|
307
|
+
|
|
308
|
+
client.list_specs(organisation_id=9, page=1, size=5)
|
|
309
|
+
|
|
310
|
+
assert stub_requests.calls[0].kwargs["params"] == {"o": "9", "p": "1", "s": "5"}
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
class TestPushGraph:
|
|
314
|
+
"""The push legs other than workflow resolution, which TestPushGraphBindsAWorkflow owns.
|
|
315
|
+
|
|
316
|
+
These pass ``workflow_id`` so the workflow lookup is skipped and each test stays
|
|
317
|
+
about one thing.
|
|
318
|
+
"""
|
|
319
|
+
|
|
320
|
+
def _queue_happy_path(self, stub_requests):
|
|
321
|
+
stub_requests.queue(json_data=CREATED_ENVELOPE) # create
|
|
322
|
+
stub_requests.queue(json_data=BOUND_ENVELOPE) # bind
|
|
323
|
+
stub_requests.queue(json_data=None) # upload (null body)
|
|
324
|
+
stub_requests.queue(json_data=ACTIVE_BATCH_JSON) # read-back
|
|
325
|
+
|
|
326
|
+
def test_reports_the_read_back_batch(self, client, stub_requests):
|
|
327
|
+
self._queue_happy_path(stub_requests)
|
|
328
|
+
|
|
329
|
+
result = client.push_graph(organisation_id=2, name="MTUS Knee 2026", graph=GRAPH, workflow_id="w-1")
|
|
330
|
+
|
|
331
|
+
assert result.batch_id == CREATED_ENVELOPE["data"]["id"]
|
|
332
|
+
assert result.total_jobs == 325
|
|
333
|
+
assert result.status == BatchStatus.ACTIVE
|
|
334
|
+
assert result.batch.graph_run_id == "run-2026-08-03-a"
|
|
335
|
+
|
|
336
|
+
def test_threads_batch_and_upload_options_through(self, client, stub_requests):
|
|
337
|
+
self._queue_happy_path(stub_requests)
|
|
338
|
+
|
|
339
|
+
client.push_graph(
|
|
340
|
+
organisation_id=2,
|
|
341
|
+
name="MTUS Knee 2026",
|
|
342
|
+
graph=GRAPH,
|
|
343
|
+
description="2026 revision",
|
|
344
|
+
instructions="Check each rule.",
|
|
345
|
+
confidentiality_level="RESTRICTED",
|
|
346
|
+
job_type="rule_validation",
|
|
347
|
+
target_class="rule",
|
|
348
|
+
hops=2,
|
|
349
|
+
workflow_id="w-1",
|
|
350
|
+
)
|
|
351
|
+
|
|
352
|
+
create, _bind, upload, _read_back = stub_requests.calls
|
|
353
|
+
assert create.kwargs["json"]["payload"] == {
|
|
354
|
+
"name": "MTUS Knee 2026",
|
|
355
|
+
"batch_type": "graph",
|
|
356
|
+
"description": "2026 revision",
|
|
357
|
+
"instructions": "Check each rule.",
|
|
358
|
+
"confidentiality_level": "RESTRICTED",
|
|
359
|
+
}
|
|
360
|
+
assert upload.kwargs["params"] == {
|
|
361
|
+
"o": "2",
|
|
362
|
+
"job_type": "rule_validation",
|
|
363
|
+
"target_class": "rule",
|
|
364
|
+
"hops": "2",
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
def test_pushes_a_graph_file(self, client, stub_requests, tmp_path):
|
|
368
|
+
graph_file = tmp_path / "sandbox_graph.json"
|
|
369
|
+
graph_file.write_text(json.dumps(GRAPH))
|
|
370
|
+
self._queue_happy_path(stub_requests)
|
|
371
|
+
|
|
372
|
+
result = client.push_graph(organisation_id=2, name="MTUS Knee 2026", file_path=graph_file, workflow_id="w-1")
|
|
373
|
+
|
|
374
|
+
assert json.loads(stub_requests.calls[2].kwargs["files"]["file"][1]) == GRAPH
|
|
375
|
+
assert result.total_jobs == 325
|
|
376
|
+
|
|
377
|
+
def test_validates_the_graph_source_before_creating_a_batch(self, client, stub_requests):
|
|
378
|
+
with pytest.raises(ValueError, match="exactly one"):
|
|
379
|
+
client.push_graph(organisation_id=2, name="MTUS Knee 2026")
|
|
380
|
+
|
|
381
|
+
assert stub_requests.calls == []
|
|
382
|
+
|
|
383
|
+
def test_a_failed_upload_propagates_and_leaves_the_draft_batch(self, client, stub_requests):
|
|
384
|
+
stub_requests.queue(json_data=CREATED_ENVELOPE)
|
|
385
|
+
stub_requests.queue(json_data=BOUND_ENVELOPE)
|
|
386
|
+
stub_requests.queue(json_data={"error": {"message": "No vertices of class 'rule' found"}}, status_code=400)
|
|
387
|
+
|
|
388
|
+
with pytest.raises(requests.HTTPError):
|
|
389
|
+
client.push_graph(organisation_id=2, name="MTUS Knee 2026", graph={"vertices": []}, workflow_id="w-1")
|
|
390
|
+
|
|
391
|
+
# The batch was created and bound before the upload failed: three calls, no
|
|
392
|
+
# read-back, and the DRAFT batch survives server-side (documented residue,
|
|
393
|
+
# recoverable via list_batches).
|
|
394
|
+
assert len(stub_requests.calls) == 3
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
WORKFLOWS_PAGE = {
|
|
398
|
+
"page": {"page": 0, "size": 50, "total": 3},
|
|
399
|
+
"items": [
|
|
400
|
+
{
|
|
401
|
+
"id": "sys-wf-dataset-2",
|
|
402
|
+
"code": "dataset_single_step",
|
|
403
|
+
"name": "Dataset single step",
|
|
404
|
+
"description": None,
|
|
405
|
+
"target_batch_type": "dataset",
|
|
406
|
+
"is_system": True,
|
|
407
|
+
"status": "active",
|
|
408
|
+
"current_published_version_id": "sys-wfv-dataset-2",
|
|
409
|
+
"draft_version_id": None,
|
|
410
|
+
},
|
|
411
|
+
{
|
|
412
|
+
"id": "org-wf-graph-draft",
|
|
413
|
+
"code": "graph_draft_only",
|
|
414
|
+
"name": "Graph, never published",
|
|
415
|
+
"description": None,
|
|
416
|
+
"target_batch_type": "graph",
|
|
417
|
+
"is_system": False,
|
|
418
|
+
"status": "active",
|
|
419
|
+
"current_published_version_id": None,
|
|
420
|
+
"draft_version_id": "draft-1",
|
|
421
|
+
},
|
|
422
|
+
{
|
|
423
|
+
"id": "sys-wf-graph-2",
|
|
424
|
+
"code": "graph_two_step",
|
|
425
|
+
"name": "Graph two-step review",
|
|
426
|
+
"description": None,
|
|
427
|
+
"target_batch_type": "graph",
|
|
428
|
+
"is_system": True,
|
|
429
|
+
"status": "active",
|
|
430
|
+
"current_published_version_id": "sys-wfv-graph-2",
|
|
431
|
+
"draft_version_id": None,
|
|
432
|
+
},
|
|
433
|
+
],
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
BOUND_ENVELOPE = {
|
|
437
|
+
"success": True,
|
|
438
|
+
"message": "0 job(s) re-governed",
|
|
439
|
+
"data": {"jobs_regoverned": 0, "workflow_version_id": "sys-wfv-graph-2"},
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
class TestWorkflows:
|
|
444
|
+
def test_list_workflows_hits_the_workflows_root(self, client, stub_requests):
|
|
445
|
+
stub_requests.queue(json_data=WORKFLOWS_PAGE)
|
|
446
|
+
|
|
447
|
+
result = client.list_workflows(organisation_id=2)
|
|
448
|
+
|
|
449
|
+
call = stub_requests.calls[0]
|
|
450
|
+
assert call.method == "GET"
|
|
451
|
+
assert call.url == "http://cp.test/api/annotation-workflows"
|
|
452
|
+
assert call.kwargs["params"] == {"o": "2", "p": "0", "s": "50"}
|
|
453
|
+
assert [w.id for w in result.items] == ["sys-wf-dataset-2", "org-wf-graph-draft", "sys-wf-graph-2"]
|
|
454
|
+
|
|
455
|
+
def test_list_workflows_forwards_pagination(self, client, stub_requests):
|
|
456
|
+
stub_requests.queue(json_data={"page": {"page": 1, "size": 5, "total": 0}, "items": []})
|
|
457
|
+
|
|
458
|
+
client.list_workflows(organisation_id=9, page=1, size=5)
|
|
459
|
+
|
|
460
|
+
assert stub_requests.calls[0].kwargs["params"] == {"o": "9", "p": "1", "s": "5"}
|
|
461
|
+
|
|
462
|
+
def test_bind_workflow_posts_the_command_envelope(self, client, stub_requests):
|
|
463
|
+
stub_requests.queue(json_data=BOUND_ENVELOPE)
|
|
464
|
+
|
|
465
|
+
result = client.bind_workflow(organisation_id=2, batch_id="b-1", workflow_id="sys-wf-graph-2")
|
|
466
|
+
|
|
467
|
+
call = stub_requests.calls[0]
|
|
468
|
+
assert call.method == "POST"
|
|
469
|
+
assert call.url == "http://cp.test/api/annotations/b-1/_command"
|
|
470
|
+
assert call.kwargs["json"] == {
|
|
471
|
+
"command": "bind_workflow",
|
|
472
|
+
"organisation": 2,
|
|
473
|
+
"payload": {"job_type": "*", "workflow_id": "sys-wf-graph-2"},
|
|
474
|
+
}
|
|
475
|
+
assert result.workflow_version_id == "sys-wfv-graph-2"
|
|
476
|
+
assert result.jobs_regoverned == 0
|
|
477
|
+
|
|
478
|
+
def test_bind_workflow_forwards_job_type_and_rebind_reason(self, client, stub_requests):
|
|
479
|
+
stub_requests.queue(json_data=BOUND_ENVELOPE)
|
|
480
|
+
|
|
481
|
+
client.bind_workflow(
|
|
482
|
+
organisation_id=2,
|
|
483
|
+
batch_id="b-1",
|
|
484
|
+
workflow_id="sys-wf-graph-2",
|
|
485
|
+
job_type="dbq_rules",
|
|
486
|
+
rebind_reason="moving to the two-step review",
|
|
487
|
+
)
|
|
488
|
+
|
|
489
|
+
assert stub_requests.calls[0].kwargs["json"]["payload"] == {
|
|
490
|
+
"job_type": "dbq_rules",
|
|
491
|
+
"workflow_id": "sys-wf-graph-2",
|
|
492
|
+
"rebind_reason": "moving to the two-step review",
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
class TestPushGraphBindsAWorkflow:
|
|
497
|
+
"""Issue #14: a batch cannot hold jobs until a workflow is bound to it.
|
|
498
|
+
|
|
499
|
+
The server's insert trigger refuses every job otherwise, surfacing as an opaque
|
|
500
|
+
500 from the upload, so push_graph has to bind between create and upload.
|
|
501
|
+
"""
|
|
502
|
+
|
|
503
|
+
def _queue_full_push(self, stub_requests):
|
|
504
|
+
stub_requests.queue(json_data=WORKFLOWS_PAGE) # resolve
|
|
505
|
+
stub_requests.queue(json_data=CREATED_ENVELOPE) # create
|
|
506
|
+
stub_requests.queue(json_data=BOUND_ENVELOPE) # bind
|
|
507
|
+
stub_requests.queue(json_data=None) # upload (null body)
|
|
508
|
+
stub_requests.queue(json_data=ACTIVE_BATCH_JSON) # read back
|
|
509
|
+
|
|
510
|
+
def test_resolves_creates_binds_uploads_then_reads_back_in_order(self, client, stub_requests):
|
|
511
|
+
self._queue_full_push(stub_requests)
|
|
512
|
+
|
|
513
|
+
result = client.push_graph(organisation_id=2, name="MTUS Knee 2026", graph=GRAPH)
|
|
514
|
+
|
|
515
|
+
batch_id = CREATED_ENVELOPE["data"]["id"]
|
|
516
|
+
assert [(c.method, c.url) for c in stub_requests.calls] == [
|
|
517
|
+
("GET", "http://cp.test/api/annotation-workflows"),
|
|
518
|
+
("POST", "http://cp.test/api/annotations/_command"),
|
|
519
|
+
("POST", f"http://cp.test/api/annotations/{batch_id}/_command"),
|
|
520
|
+
("POST", f"http://cp.test/api/annotations/{batch_id}/upload"),
|
|
521
|
+
("GET", f"http://cp.test/api/annotations/{batch_id}"),
|
|
522
|
+
]
|
|
523
|
+
assert result.total_jobs == 325
|
|
524
|
+
|
|
525
|
+
def test_binds_the_published_system_workflow_matching_the_batch_type(self, client, stub_requests):
|
|
526
|
+
# The page also holds a dataset workflow and an unpublished graph one; neither
|
|
527
|
+
# is bindable here — the dataset one governs the wrong batch type, and a
|
|
528
|
+
# workflow with no published version cannot be bound at all.
|
|
529
|
+
self._queue_full_push(stub_requests)
|
|
530
|
+
|
|
531
|
+
client.push_graph(organisation_id=2, name="n", graph=GRAPH)
|
|
532
|
+
|
|
533
|
+
assert stub_requests.calls[2].kwargs["json"]["payload"] == {
|
|
534
|
+
"job_type": "*",
|
|
535
|
+
"workflow_id": "sys-wf-graph-2",
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
def test_an_explicit_workflow_id_skips_the_lookup(self, client, stub_requests):
|
|
539
|
+
stub_requests.queue(json_data=CREATED_ENVELOPE)
|
|
540
|
+
stub_requests.queue(json_data=BOUND_ENVELOPE)
|
|
541
|
+
stub_requests.queue(json_data=None)
|
|
542
|
+
stub_requests.queue(json_data=ACTIVE_BATCH_JSON)
|
|
543
|
+
|
|
544
|
+
client.push_graph(organisation_id=2, name="n", graph=GRAPH, workflow_id="org-wf-custom")
|
|
545
|
+
|
|
546
|
+
assert stub_requests.calls[0].url == "http://cp.test/api/annotations/_command" # no workflow list
|
|
547
|
+
assert stub_requests.calls[1].kwargs["json"]["payload"]["workflow_id"] == "org-wf-custom"
|
|
548
|
+
|
|
549
|
+
def test_forwards_a_rebind_reason(self, client, stub_requests):
|
|
550
|
+
stub_requests.queue(json_data=CREATED_ENVELOPE)
|
|
551
|
+
stub_requests.queue(json_data=BOUND_ENVELOPE)
|
|
552
|
+
stub_requests.queue(json_data=None)
|
|
553
|
+
stub_requests.queue(json_data=ACTIVE_BATCH_JSON)
|
|
554
|
+
|
|
555
|
+
client.push_graph(
|
|
556
|
+
organisation_id=2, name="n", graph=GRAPH, workflow_id="w-1", rebind_reason="switching review flow"
|
|
557
|
+
)
|
|
558
|
+
|
|
559
|
+
assert stub_requests.calls[1].kwargs["json"]["payload"]["rebind_reason"] == "switching review flow"
|
|
560
|
+
|
|
561
|
+
def test_no_bindable_workflow_raises_before_creating_a_batch(self, client, stub_requests):
|
|
562
|
+
# Only a dataset workflow on offer: pushing a graph batch cannot proceed, and
|
|
563
|
+
# must not leave a batch behind while failing.
|
|
564
|
+
stub_requests.queue(
|
|
565
|
+
json_data={"page": {"page": 0, "size": 50, "total": 1}, "items": [WORKFLOWS_PAGE["items"][0]]}
|
|
566
|
+
)
|
|
567
|
+
|
|
568
|
+
with pytest.raises(ValueError, match="no bindable annotation workflow"):
|
|
569
|
+
client.push_graph(organisation_id=2, name="n", graph=GRAPH)
|
|
570
|
+
|
|
571
|
+
assert len(stub_requests.calls) == 1 # the lookup only; nothing was created
|
|
572
|
+
|
|
573
|
+
def test_a_pre_workflow_server_404s_the_lookup_and_the_bind_is_skipped(self, client, stub_requests):
|
|
574
|
+
# Control planes older than the workflow model have no such endpoint and need
|
|
575
|
+
# no binding; the push must still work against them.
|
|
576
|
+
stub_requests.queue(json_data={"error": {"message": "Not Found"}}, status_code=404)
|
|
577
|
+
stub_requests.queue(json_data=CREATED_ENVELOPE)
|
|
578
|
+
stub_requests.queue(json_data=None)
|
|
579
|
+
stub_requests.queue(json_data=ACTIVE_BATCH_JSON)
|
|
580
|
+
|
|
581
|
+
result = client.push_graph(organisation_id=2, name="n", graph=GRAPH)
|
|
582
|
+
|
|
583
|
+
assert [c.method for c in stub_requests.calls] == ["GET", "POST", "POST", "GET"]
|
|
584
|
+
assert "_command" not in stub_requests.calls[2].url # straight to the upload
|
|
585
|
+
assert result.total_jobs == 325
|
|
586
|
+
|
|
587
|
+
def test_other_errors_from_the_lookup_propagate(self, client, stub_requests):
|
|
588
|
+
stub_requests.queue(json_data={"error": {"message": "boom"}}, status_code=500)
|
|
589
|
+
|
|
590
|
+
with pytest.raises(requests.HTTPError):
|
|
591
|
+
client.push_graph(organisation_id=2, name="n", graph=GRAPH)
|
|
592
|
+
|
|
593
|
+
assert len(stub_requests.calls) == 1
|
|
594
|
+
|
|
595
|
+
def test_validates_the_graph_source_before_anything_else(self, client, stub_requests):
|
|
596
|
+
with pytest.raises(ValueError, match="exactly one"):
|
|
597
|
+
client.push_graph(organisation_id=2, name="n")
|
|
598
|
+
|
|
599
|
+
assert stub_requests.calls == []
|