forked from datajoint/datajoint-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_gc.py
More file actions
467 lines (367 loc) · 15.7 KB
/
test_gc.py
File metadata and controls
467 lines (367 loc) · 15.7 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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
"""
Tests for garbage collection (gc.py).
"""
from unittest.mock import MagicMock, patch
import numpy as np
import pytest
import datajoint as dj
from datajoint import gc
from datajoint.errors import DataJointError
# Tables used by TestScanWithLiveData. Defined at module scope so dj.Schema's
# context resolution can find them by class name; bound to a schema inside
# each fixture (see schema(...) calls below).
class GcBlobTest(dj.Manual):
definition = """
rid : int
---
payload : <blob@local>
"""
class GcNpyTest(dj.Manual):
definition = """
rid : int
---
waveform : <npy@local>
"""
class GcObjectTest(dj.Manual):
definition = """
rid : int
---
results : <object@local>
"""
class TestUsesHashStorage:
"""Tests for _uses_hash_storage helper function."""
def test_returns_false_for_no_adapter(self):
"""Test that False is returned when attribute has no codec."""
attr = MagicMock()
attr.codec = None
assert gc._uses_hash_storage(attr) is False
def test_returns_true_for_hash_type(self):
"""Test that True is returned for <hash@> type."""
attr = MagicMock()
attr.codec = MagicMock()
attr.codec.name = "hash"
attr.store = "mystore"
assert gc._uses_hash_storage(attr) is True
def test_returns_true_for_blob_external(self):
"""Test that True is returned for <blob@> type (external)."""
attr = MagicMock()
attr.codec = MagicMock()
attr.codec.name = "blob"
attr.store = "mystore"
assert gc._uses_hash_storage(attr) is True
def test_returns_true_for_attach_external(self):
"""Test that True is returned for <attach@> type (external)."""
attr = MagicMock()
attr.codec = MagicMock()
attr.codec.name = "attach"
attr.store = "mystore"
assert gc._uses_hash_storage(attr) is True
def test_returns_false_for_blob_internal(self):
"""Test that False is returned for <blob> internal storage."""
attr = MagicMock()
attr.codec = MagicMock()
attr.codec.name = "blob"
attr.store = None
assert gc._uses_hash_storage(attr) is False
class TestExtractHashRefs:
"""Tests for _extract_hash_refs helper function."""
def test_returns_empty_for_none(self):
"""Test that empty list is returned for None value."""
assert gc._extract_hash_refs(None) == []
def test_parses_json_string(self):
"""Test parsing JSON string with path."""
value = '{"path": "_hash/schema/abc123", "hash": "abc123", "store": "mystore"}'
refs = gc._extract_hash_refs(value)
assert len(refs) == 1
assert refs[0] == ("_hash/schema/abc123", "mystore")
def test_parses_dict_directly(self):
"""Test parsing dict with path."""
value = {"path": "_hash/schema/def456", "hash": "def456", "store": None}
refs = gc._extract_hash_refs(value)
assert len(refs) == 1
assert refs[0] == ("_hash/schema/def456", None)
def test_returns_empty_for_invalid_json(self):
"""Test that empty list is returned for invalid JSON."""
assert gc._extract_hash_refs("not json") == []
def test_returns_empty_for_dict_without_path(self):
"""Test that empty list is returned for dict without path key."""
assert gc._extract_hash_refs({"hash": "abc123"}) == []
class TestUsesSchemaStorage:
"""Tests for _uses_schema_storage helper function."""
def test_returns_false_for_no_adapter(self):
"""Test that False is returned when attribute has no codec."""
attr = MagicMock()
attr.codec = None
assert gc._uses_schema_storage(attr) is False
def test_returns_true_for_object_type(self):
"""Test that True is returned for <object@> type."""
attr = MagicMock()
attr.codec = MagicMock()
attr.codec.name = "object"
assert gc._uses_schema_storage(attr) is True
def test_returns_true_for_npy_type(self):
"""Test that True is returned for <npy@> type."""
attr = MagicMock()
attr.codec = MagicMock()
attr.codec.name = "npy"
assert gc._uses_schema_storage(attr) is True
def test_returns_false_for_other_types(self):
"""Test that False is returned for non-schema-addressed types."""
attr = MagicMock()
attr.codec = MagicMock()
attr.codec.name = "blob"
assert gc._uses_schema_storage(attr) is False
class TestExtractSchemaRefs:
"""Tests for _extract_schema_refs helper function."""
def test_returns_empty_for_none(self):
"""Test that empty list is returned for None value."""
assert gc._extract_schema_refs(None) == []
def test_parses_json_string(self):
"""Test parsing JSON string with path."""
value = '{"path": "schema/table/pk/field", "store": "mystore"}'
refs = gc._extract_schema_refs(value)
assert len(refs) == 1
assert refs[0] == ("schema/table/pk/field", "mystore")
def test_parses_dict_directly(self):
"""Test parsing dict with path."""
value = {"path": "test/path", "store": None}
refs = gc._extract_schema_refs(value)
assert len(refs) == 1
assert refs[0] == ("test/path", None)
def test_returns_empty_for_dict_without_path(self):
"""Test that empty list is returned for dict without path key."""
assert gc._extract_schema_refs({"other": "data"}) == []
class TestScan:
"""Tests for scan function."""
def test_requires_at_least_one_schema(self):
"""Test that at least one schema is required."""
with pytest.raises(DataJointError, match="At least one schema must be provided"):
gc.scan()
@patch("datajoint.gc.scan_schema_references")
@patch("datajoint.gc.list_schema_paths")
@patch("datajoint.gc.scan_hash_references")
@patch("datajoint.gc.list_stored_hashes")
def test_returns_stats(self, mock_list_hashes, mock_scan_hash, mock_list_schemas, mock_scan_schema):
"""Test that scan returns proper statistics."""
# Mock hash-addressed storage (now uses paths)
mock_scan_hash.return_value = {"_hash/schema/path1", "_hash/schema/path2"}
mock_list_hashes.return_value = {
"_hash/schema/path1": 100,
"_hash/schema/path3": 200, # orphaned
}
# Mock schema-addressed storage
mock_scan_schema.return_value = {"schema/table/pk1/field"}
mock_list_schemas.return_value = {
"schema/table/pk1/field": 500,
"schema/table/pk2/field": 300, # orphaned
}
mock_schema = MagicMock()
stats = gc.scan(mock_schema, store_name="test_store")
# Hash stats
assert stats["hash_referenced"] == 2
assert stats["hash_stored"] == 2
assert stats["hash_orphaned"] == 1
assert "_hash/schema/path3" in stats["orphaned_hashes"]
# Schema stats
assert stats["schema_paths_referenced"] == 1
assert stats["schema_paths_stored"] == 2
assert stats["schema_paths_orphaned"] == 1
assert "schema/table/pk2/field" in stats["orphaned_paths"]
# Combined totals
assert stats["referenced"] == 3
assert stats["stored"] == 4
assert stats["orphaned"] == 2
assert stats["orphaned_bytes"] == 500 # 200 hash + 300 schema
class TestCollect:
"""Tests for collect function."""
@patch("datajoint.gc.scan")
def test_dry_run_does_not_delete(self, mock_scan):
"""Test that dry_run=True doesn't delete anything."""
mock_scan.return_value = {
"referenced": 1,
"stored": 2,
"orphaned": 1,
"orphaned_bytes": 100,
"orphaned_hashes": ["_hash/schema/orphan_path"],
"orphaned_paths": [],
"hash_orphaned": 1,
"schema_paths_orphaned": 0,
}
mock_schema = MagicMock()
stats = gc.collect(mock_schema, store_name="test_store", dry_run=True)
assert stats["deleted"] == 0
assert stats["bytes_freed"] == 0
assert stats["dry_run"] is True
@patch("datajoint.gc.delete_path")
@patch("datajoint.gc.list_stored_hashes")
@patch("datajoint.gc.scan")
def test_deletes_orphaned_hashes(self, mock_scan, mock_list_stored, mock_delete):
"""Test that orphaned hashes are deleted when dry_run=False."""
mock_scan.return_value = {
"referenced": 1,
"stored": 2,
"orphaned": 1,
"orphaned_bytes": 100,
"orphaned_hashes": ["_hash/schema/orphan_path"],
"orphaned_paths": [],
"hash_orphaned": 1,
"schema_paths_orphaned": 0,
}
mock_list_stored.return_value = {"_hash/schema/orphan_path": 100}
mock_delete.return_value = True
mock_schema = MagicMock()
stats = gc.collect(mock_schema, store_name="test_store", dry_run=False)
assert stats["deleted"] == 1
assert stats["hash_deleted"] == 1
assert stats["bytes_freed"] == 100
assert stats["dry_run"] is False
mock_delete.assert_called_once_with("_hash/schema/orphan_path", "test_store", config=mock_schema.connection._config)
@patch("datajoint.gc.delete_schema_path")
@patch("datajoint.gc.list_schema_paths")
@patch("datajoint.gc.scan")
def test_deletes_orphaned_schemas(self, mock_scan, mock_list_schemas, mock_delete):
"""Test that orphaned schema paths are deleted when dry_run=False."""
mock_scan.return_value = {
"referenced": 1,
"stored": 2,
"orphaned": 1,
"orphaned_bytes": 500,
"orphaned_hashes": [],
"orphaned_paths": ["schema/table/pk/field"],
"hash_orphaned": 0,
"schema_paths_orphaned": 1,
}
mock_list_schemas.return_value = {"schema/table/pk/field": 500}
mock_delete.return_value = True
mock_schema = MagicMock()
stats = gc.collect(mock_schema, store_name="test_store", dry_run=False)
assert stats["deleted"] == 1
assert stats["schema_paths_deleted"] == 1
assert stats["bytes_freed"] == 500
assert stats["dry_run"] is False
mock_delete.assert_called_once_with("schema/table/pk/field", "test_store", config=mock_schema.connection._config)
class TestFormatStats:
"""Tests for format_stats function."""
def test_formats_scan_stats(self):
"""Test formatting scan statistics."""
stats = {
"referenced": 10,
"stored": 15,
"orphaned": 5,
"orphaned_bytes": 1024 * 1024, # 1 MB
"hash_referenced": 6,
"hash_stored": 8,
"hash_orphaned": 2,
"hash_orphaned_bytes": 512 * 1024,
"schema_paths_referenced": 4,
"schema_paths_stored": 7,
"schema_paths_orphaned": 3,
"schema_paths_orphaned_bytes": 512 * 1024,
}
result = gc.format_stats(stats)
assert "Referenced in database: 10" in result
assert "Stored in backend: 15" in result
assert "Orphaned (unreferenced): 5" in result
assert "1.00 MB" in result
# Check for detailed sections
assert "Hash-Addressed Storage" in result
assert "Schema-Addressed Storage" in result
def test_formats_collect_stats_dry_run(self):
"""Test formatting collect statistics with dry_run."""
stats = {
"referenced": 10,
"stored": 15,
"orphaned": 5,
"deleted": 0,
"bytes_freed": 0,
"dry_run": True,
}
result = gc.format_stats(stats)
assert "DRY RUN" in result
def test_formats_collect_stats_actual(self):
"""Test formatting collect statistics after actual deletion."""
stats = {
"referenced": 10,
"stored": 15,
"orphaned": 5,
"deleted": 3,
"hash_deleted": 2,
"schema_paths_deleted": 1,
"bytes_freed": 2 * 1024 * 1024, # 2 MB
"errors": 2,
"dry_run": False,
}
result = gc.format_stats(stats)
assert "Deleted: 3" in result
assert "Hash items: 2" in result
assert "Schema paths: 1" in result
assert "2.00 MB" in result
assert "Errors: 2" in result
class TestScanWithLiveData:
"""End-to-end tests for gc.scan() against real schemas with external storage.
Exercises the full production path:
scan_*_references → table.proj(attr).cursor() → raw JSON metadata.
These are the regression tests that would have caught issue #1442
(silent type mismatch when scan helpers iterated decoded codec outputs
instead of raw stored metadata).
"""
@pytest.fixture
def schema_blob(self, connection_test, prefix, mock_stores):
schema_name = f"{prefix}_test_gc_e2e_blob"
schema = dj.Schema(
schema_name,
context={"GcBlobTest": GcBlobTest},
connection=connection_test,
)
schema(GcBlobTest)
yield schema
schema.drop()
@pytest.fixture
def schema_npy(self, connection_test, prefix, mock_stores):
schema_name = f"{prefix}_test_gc_e2e_npy"
schema = dj.Schema(
schema_name,
context={"GcNpyTest": GcNpyTest},
connection=connection_test,
)
schema(GcNpyTest)
yield schema
schema.drop()
@pytest.fixture
def schema_object(self, connection_test, prefix, mock_stores):
schema_name = f"{prefix}_test_gc_e2e_object"
schema = dj.Schema(
schema_name,
context={"GcObjectTest": GcObjectTest},
connection=connection_test,
)
schema(GcObjectTest)
yield schema
schema.drop()
def test_scan_finds_active_blob_reference(self, schema_blob):
"""scan() must report hash_referenced >= 1 for a populated <blob@> column.
Decoded value type returned by BlobCodec.decode is numpy.ndarray, which
does not satisfy `_extract_hash_refs`'s dict/JSON-string check — this
test fails before the cursor-based fix in scan_hash_references.
"""
GcBlobTest.insert1({"rid": 1, "payload": np.arange(64, dtype="uint8")})
stats = gc.scan(schema_blob, store_name="local")
assert stats["hash_referenced"] >= 1, f"scan should find the active <blob@> reference; got {stats}"
def test_scan_finds_active_npy_reference(self, schema_npy):
"""scan() must report schema_paths_referenced >= 1 for a populated <npy@> column.
Decoded value type returned by NpyCodec.decode is NpyRef (lazy handle),
which does not satisfy `_extract_schema_refs`'s dict check — this test
fails before the cursor-based fix in scan_schema_references.
"""
GcNpyTest.insert1({"rid": 1, "waveform": np.arange(64, dtype="float32")})
stats = gc.scan(schema_npy, store_name="local")
assert stats["schema_paths_referenced"] >= 1, f"scan should find the active <npy@> reference; got {stats}"
def test_scan_finds_active_object_reference(self, schema_object):
"""scan() must report schema_paths_referenced >= 1 for a populated <object@> column.
Decoded value type returned by ObjectCodec.decode is ObjectRef (lazy
handle), which does not satisfy `_extract_schema_refs`'s dict check —
this test fails before the cursor-based fix in scan_schema_references.
"""
GcObjectTest.insert1({"rid": 1, "results": b"hello-gc-test"})
stats = gc.scan(schema_object, store_name="local")
assert stats["schema_paths_referenced"] >= 1, f"scan should find the active <object@> reference; got {stats}"