-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathtest_assets.py
More file actions
425 lines (344 loc) · 16.2 KB
/
test_assets.py
File metadata and controls
425 lines (344 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
"""Unit tests for the AssetsNamespace domain API."""
from unittest.mock import MagicMock
import pytest
from kili.adapters.kili_api_gateway.kili_api_gateway import KiliAPIGateway
from kili.client import Kili
from kili.domain_api.assets import AssetsNamespace
class TestAssetsNamespace:
"""Test cases for AssetsNamespace domain API."""
@pytest.fixture()
def mock_client(self):
"""Create a mock Kili client."""
client = MagicMock(spec=Kili)
# Mock all the legacy methods that AssetsNamespace delegates to
client.assets = MagicMock()
client.count_assets = MagicMock()
client.append_many_to_dataset = MagicMock()
client.delete_many_from_dataset = MagicMock()
client.update_properties_in_assets = MagicMock()
client.assign_assets_to_labelers = MagicMock()
client.send_back_to_queue = MagicMock()
client.add_to_review = MagicMock()
client.change_asset_external_ids = MagicMock()
client.add_metadata = MagicMock()
client.set_metadata = MagicMock()
client.skip_or_unskip = MagicMock()
client.update_asset_consensus = MagicMock()
return client
@pytest.fixture()
def mock_gateway(self):
"""Create a mock KiliAPIGateway."""
return MagicMock(spec=KiliAPIGateway)
@pytest.fixture()
def assets_namespace(self, mock_client, mock_gateway):
"""Create an AssetsNamespace instance."""
return AssetsNamespace(mock_client, mock_gateway)
class TestAssetsNamespaceCoreOperations:
"""Test core operations of AssetsNamespace."""
@pytest.fixture()
def mock_client(self):
"""Create a mock Kili client."""
client = MagicMock(spec=Kili)
client.assets = MagicMock()
client.count_assets = MagicMock()
client.append_many_to_dataset = MagicMock()
client.delete_many_from_dataset = MagicMock()
client.update_properties_in_assets = MagicMock()
return client
@pytest.fixture()
def mock_gateway(self):
"""Create a mock KiliAPIGateway."""
return MagicMock(spec=KiliAPIGateway)
@pytest.fixture()
def assets_namespace(self, mock_client, mock_gateway):
"""Create an AssetsNamespace instance."""
return AssetsNamespace(mock_client, mock_gateway)
def test_list_assets_generator(self, assets_namespace):
"""Test list method returns generator by default."""
# Mock the legacy client method to return a generator
def mock_generator():
yield {"id": "asset1", "externalId": "ext1"}
yield {"id": "asset2", "externalId": "ext2"}
assets_namespace._client.assets.return_value = mock_generator()
result = assets_namespace.list_as_generator(project_id="project_123")
# Should return a generator
assert hasattr(result, "__iter__")
assets_list = list(result)
assert len(assets_list) == 2
assert assets_list[0]["id"] == "asset1"
# Verify the legacy method was called with correct parameters
assets_namespace._client.assets.assert_called_once()
call_kwargs = assets_namespace._client.assets.call_args[1]
assert call_kwargs["project_id"] == "project_123"
assert call_kwargs["as_generator"] is True
def test_list_assets_as_list(self, assets_namespace):
"""Test list method returns list when as_generator=False."""
# Mock the legacy client method
assets_namespace._client.assets.return_value = [
{"id": "asset1", "externalId": "ext1"},
{"id": "asset2", "externalId": "ext2"},
]
result = assets_namespace.list(project_id="project_123")
assert isinstance(result, list)
assert len(result) == 2
assert result[0]["id"] == "asset1"
# Verify the legacy method was called with correct parameters
assets_namespace._client.assets.assert_called_once()
call_kwargs = assets_namespace._client.assets.call_args[1]
assert call_kwargs["project_id"] == "project_123"
assert call_kwargs["as_generator"] is False
def test_list_assets_with_step_status_not_in_filter(self, assets_namespace):
"""Test list method with step_status_not_in filter."""
# Mock the legacy client method
assets_namespace._client.assets.return_value = [
{"id": "asset1", "externalId": "ext1"},
]
result = assets_namespace.list(
project_id="project_123", filter={"step_status_not_in": ["DONE", "SKIPPED"]}
)
# Verify the legacy method was called with the filter
assets_namespace._client.assets.assert_called_once()
call_kwargs = assets_namespace._client.assets.call_args[1]
assert call_kwargs["step_status_not_in"] == ["DONE", "SKIPPED"]
def test_list_assets_with_step_name_not_in_filter(self, assets_namespace):
"""Test list method with step_name_not_in filter."""
# Mock the legacy client method
assets_namespace._client.assets.return_value = [
{"id": "asset1", "externalId": "ext1"},
]
result = assets_namespace.list(
project_id="project_123", filter={"step_name_not_in": ["Review", "QA"]}
)
# Verify the legacy method was called with the filter
assets_namespace._client.assets.assert_called_once()
call_kwargs = assets_namespace._client.assets.call_args[1]
assert call_kwargs["step_name_not_in"] == ["Review", "QA"]
def test_count_assets_with_step_filters_not_in(self, assets_namespace):
"""Test count method with step_status_not_in and step_name_not_in filters."""
# Mock the legacy client method
assets_namespace._client.count_assets.return_value = 15
result = assets_namespace.count(
project_id="project_123",
filter={"step_status_not_in": ["DONE"], "step_name_not_in": ["Final Review"]},
)
# Verify the result
assert result == 15
# Verify the legacy method was called with both filters
assets_namespace._client.count_assets.assert_called_once()
call_kwargs = assets_namespace._client.count_assets.call_args[1]
assert call_kwargs["step_status_not_in"] == ["DONE"]
assert call_kwargs["step_name_not_in"] == ["Final Review"]
def test_count_assets(self, assets_namespace):
"""Test count method."""
# Mock the legacy client method
assets_namespace._client.count_assets.return_value = 42
result = assets_namespace.count(project_id="project_123")
assert result == 42
# Verify the legacy method was called with correct parameters
assets_namespace._client.count_assets.assert_called_once()
call_kwargs = assets_namespace._client.count_assets.call_args[1]
assert call_kwargs["project_id"] == "project_123"
def test_list_assets_uses_project_workflow_defaults(self, assets_namespace):
"""Ensure default fields follow project workflow version."""
# Mock the legacy client method
assets_namespace._client.assets.return_value = []
assets_namespace.list(project_id="project_321")
# Verify the legacy method was called
assets_namespace._client.assets.assert_called_once()
call_kwargs = assets_namespace._client.assets.call_args[1]
# Check that fields were passed (could be None for defaults)
assert "project_id" in call_kwargs
assert call_kwargs["project_id"] == "project_321"
def test_list_assets_rejects_deprecated_filters(self, assets_namespace):
"""Ensure deprecated filter names now raise."""
# Mock the legacy client method
assets_namespace._client.assets.return_value = []
# The namespace API doesn't accept these deprecated parameters
# They should raise TypeError if passed as **kwargs
with pytest.raises(TypeError):
assets_namespace.list(
project_id="project_ext",
external_id_contains=["assetA", "assetB"],
)
with pytest.raises(TypeError):
assets_namespace.list(
project_id="project_ext",
consensus_mark_gt=0.5,
)
def test_list_assets_resolves_step_name_filters(self, assets_namespace):
"""Ensure step_name_in filter is supported."""
# Mock the legacy client method
assets_namespace._client.assets.return_value = []
# The namespace API accepts filters as a dict
assets_namespace.list(
project_id="project_steps",
filter={"step_name_in": ["Review"]},
)
# Verify the legacy method was called
assets_namespace._client.assets.assert_called_once()
call_kwargs = assets_namespace._client.assets.call_args[1]
# step_name_in should be passed as a kwarg
assert call_kwargs.get("step_name_in") == ["Review"]
def test_count_assets_rejects_deprecated_filters(self, assets_namespace):
"""Ensure deprecated count filters raise."""
# Mock the legacy client method
assets_namespace._client.count_assets.return_value = 0
# The namespace API doesn't accept these deprecated parameters
with pytest.raises(TypeError):
assets_namespace.count(
project_id="project_ext_count",
external_id_contains=["legacy"],
)
with pytest.raises(TypeError):
assets_namespace.count(
project_id="project_ext_count",
honeypot_mark_gt=0.2,
)
def test_list_assets_unknown_filter_raises(self, assets_namespace):
"""Ensure unexpected filter names raise a helpful error."""
# Mock the legacy client method
assets_namespace._client.assets.return_value = []
# Unknown kwargs should raise TypeError
with pytest.raises(TypeError):
assets_namespace.list(project_id="project_unknown", unexpected="value")
def test_create_image_assets(self, assets_namespace, mock_client):
"""Test create_image method delegates to client."""
expected_result = {"id": "project_123", "asset_ids": ["asset1", "asset2"]}
mock_client.append_many_to_dataset.return_value = expected_result
result = assets_namespace.create_image(
project_id="project_123",
content_array=["https://example.com/image.png"],
external_id_array=["ext1"],
)
assert result == expected_result
mock_client.append_many_to_dataset.assert_called_once_with(
project_id="project_123",
content_array=["https://example.com/image.png"],
external_id_array=["ext1"],
json_metadata_array=None,
disable_tqdm=None,
wait_until_availability=True,
)
def test_create_audio_assets(self, assets_namespace, mock_client):
"""Test create_audio method delegates to client."""
expected_result = {"id": "project_123", "asset_ids": ["asset1", "asset2"]}
mock_client.append_many_to_dataset.return_value = expected_result
result = assets_namespace.create_audio(
project_id="project_123",
content_array=["https://example.com/audio.mp3"],
external_id_array=["ext1"],
)
assert result == expected_result
mock_client.append_many_to_dataset.assert_called_once_with(
project_id="project_123",
content_array=["https://example.com/audio.mp3"],
external_id_array=["ext1"],
json_metadata_array=None,
disable_tqdm=None,
wait_until_availability=True,
)
def test_delete_assets(self, assets_namespace, mock_client):
"""Test delete method delegates to client."""
expected_result = {"id": "project_123"}
mock_client.delete_many_from_dataset.return_value = expected_result
result = assets_namespace.delete(asset_ids=["asset1", "asset2"])
assert result == expected_result
mock_client.delete_many_from_dataset.assert_called_once_with(
asset_ids=["asset1", "asset2"], external_ids=None, project_id=""
)
def test_update_consensus_with_asset_id(self, assets_namespace, mock_client):
"""Test update_consensus method with asset_id."""
mock_client.update_asset_consensus.return_value = True
result = assets_namespace.update_consensus(
project_id="project_123",
asset_id="asset1",
is_consensus=True,
)
assert result is True
mock_client.update_asset_consensus.assert_called_once_with(
project_id="project_123",
is_consensus=True,
asset_id="asset1",
external_id=None,
)
def test_update_consensus_with_external_id(self, assets_namespace, mock_client):
"""Test update_consensus method with external_id."""
mock_client.update_asset_consensus.return_value = True
result = assets_namespace.update_consensus(
project_id="project_123",
external_id="ext_asset1",
is_consensus=True,
)
assert result is True
mock_client.update_asset_consensus.assert_called_once_with(
project_id="project_123",
is_consensus=True,
asset_id=None,
external_id="ext_asset1",
)
def test_update_consensus_deactivate(self, assets_namespace, mock_client):
"""Test update_consensus method to deactivate consensus."""
mock_client.update_asset_consensus.return_value = False
result = assets_namespace.update_consensus(
project_id="project_123",
asset_id="asset1",
is_consensus=False,
)
assert result is False
mock_client.update_asset_consensus.assert_called_once_with(
project_id="project_123",
is_consensus=False,
asset_id="asset1",
external_id=None,
)
class TestAssetsNamespaceContractCompatibility:
"""Contract tests to ensure domain API matches legacy API behavior."""
@pytest.fixture()
def mock_client(self):
"""Create a mock Kili client."""
client = MagicMock(spec=Kili)
return client
@pytest.fixture()
def mock_gateway(self):
"""Create a mock KiliAPIGateway."""
return MagicMock(spec=KiliAPIGateway)
@pytest.fixture()
def assets_namespace(self, mock_client, mock_gateway):
"""Create an AssetsNamespace instance."""
return AssetsNamespace(mock_client, mock_gateway)
def test_api_parity_create_image_vs_append_many(self, assets_namespace, mock_client):
"""Test that create_image() correctly delegates to append_many_to_dataset()."""
# This test ensures that the domain API correctly passes parameters
# to the underlying legacy API
mock_client.append_many_to_dataset.return_value = {"id": "project", "asset_ids": []}
# Test that image-relevant parameters are correctly passed through
assets_namespace.create_image(
project_id="test_project",
content_array=["content"],
external_id_array=["ext1"],
json_metadata_array=[{"meta": "data"}],
disable_tqdm=True,
wait_until_availability=False,
is_honeypot_array=[False],
)
# Verify that the legacy method was called with correct parameters
mock_client.append_many_to_dataset.assert_called_once_with(
project_id="test_project",
content_array=["content"],
external_id_array=["ext1"],
json_metadata_array=[{"meta": "data"}],
disable_tqdm=True,
wait_until_availability=False,
is_honeypot_array=[False],
)
def test_api_parity_delete_vs_delete_many(self, assets_namespace, mock_client):
"""Test that delete() calls have same signature as delete_many_from_dataset()."""
mock_client.delete_many_from_dataset.return_value = {"id": "project"}
assets_namespace.delete(
asset_ids=["asset1", "asset2"], external_ids=None, project_id="test_project"
)
mock_client.delete_many_from_dataset.assert_called_once_with(
asset_ids=["asset1", "asset2"], external_ids=None, project_id="test_project"
)
if __name__ == "__main__":
pytest.main([__file__])