-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_tasks.py
503 lines (393 loc) · 17.1 KB
/
test_tasks.py
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
from __future__ import annotations
import datetime
import hashlib
from pathlib import Path
from typing import TYPE_CHECKING
from django.conf import settings
from django.forms.models import model_to_dict
from django.utils import timezone
from guardian.shortcuts import assign_perm
import pytest
from rest_framework.test import APIClient
from zarr_checksum import compute_zarr_checksum
from zarr_checksum.checksum import EMPTY_CHECKSUM
from zarr_checksum.generators import ZarrArchiveFile
from dandiapi.api import tasks
from dandiapi.api.models import Asset, AssetBlob, Version
from dandiapi.zarr.models import ZarrArchiveStatus
from .fuzzy import URN_RE, UTC_ISO_TIMESTAMP_RE
if TYPE_CHECKING:
from django.contrib.auth.models import User
from django.core.files.storage import Storage
from rest_framework.test import APIClient
@pytest.mark.django_db
def test_calculate_checksum_task(storage: Storage, asset_blob_factory):
# Pretend like AssetBlob was defined with the given storage
AssetBlob.blob.field.storage = storage
asset_blob = asset_blob_factory(sha256=None)
h = hashlib.sha256()
h.update(asset_blob.blob.read())
sha256 = h.hexdigest()
tasks.calculate_sha256(asset_blob.blob_id)
asset_blob.refresh_from_db()
assert asset_blob.sha256 == sha256
@pytest.mark.django_db
def test_calculate_checksum_task_embargo(
storage: Storage, embargoed_asset_blob_factory, monkeypatch
):
# Pretend like AssetBlob was defined with the given storage
monkeypatch.setattr(AssetBlob.blob.field, 'storage', storage)
asset_blob = embargoed_asset_blob_factory(sha256=None)
h = hashlib.sha256()
h.update(asset_blob.blob.read())
sha256 = h.hexdigest()
tasks.calculate_sha256(asset_blob.blob_id)
asset_blob.refresh_from_db()
assert asset_blob.sha256 == sha256
@pytest.mark.django_db
def test_write_manifest_files(storage: Storage, version: Version, asset_factory):
# Pretend like AssetBlob was defined with the given storage
# The task piggybacks off of the AssetBlob storage to write the yamls
AssetBlob.blob.field.storage = storage
# Create a new asset in the version so there is information to write
version.assets.add(asset_factory())
# All of these files should be generated by the task
assets_yaml_path = (
f'{settings.DANDI_DANDISETS_BUCKET_PREFIX}'
f'dandisets/{version.dandiset.identifier}/{version.version}/assets.yaml'
)
dandiset_yaml_path = (
f'{settings.DANDI_DANDISETS_BUCKET_PREFIX}'
f'dandisets/{version.dandiset.identifier}/{version.version}/dandiset.yaml'
)
assets_jsonld_path = (
f'{settings.DANDI_DANDISETS_BUCKET_PREFIX}'
f'dandisets/{version.dandiset.identifier}/{version.version}/assets.jsonld'
)
dandiset_jsonld_path = (
f'{settings.DANDI_DANDISETS_BUCKET_PREFIX}'
f'dandisets/{version.dandiset.identifier}/{version.version}/dandiset.jsonld'
)
collection_jsonld_path = (
f'{settings.DANDI_DANDISETS_BUCKET_PREFIX}'
f'dandisets/{version.dandiset.identifier}/{version.version}/collection.jsonld'
)
tasks.write_manifest_files(version.id)
assert storage.exists(assets_yaml_path)
assert storage.exists(dandiset_yaml_path)
assert storage.exists(assets_jsonld_path)
assert storage.exists(dandiset_jsonld_path)
assert storage.exists(collection_jsonld_path)
@pytest.mark.django_db
def test_validate_asset_metadata(draft_asset: Asset):
tasks.validate_asset_metadata_task(draft_asset.id)
draft_asset.refresh_from_db()
assert draft_asset.status == Asset.Status.VALID
assert draft_asset.validation_errors == []
@pytest.mark.django_db
def test_validate_asset_metadata_malformed_schema_version(draft_asset: Asset):
draft_asset.metadata['schemaVersion'] = 'xxx'
draft_asset.save()
tasks.validate_asset_metadata_task(draft_asset.id)
draft_asset.refresh_from_db()
assert draft_asset.status == Asset.Status.INVALID
assert len(draft_asset.validation_errors) == 1
assert draft_asset.validation_errors[0]['field'] == ''
assert draft_asset.validation_errors[0]['message'].startswith(
'Metadata version xxx is not allowed.'
)
@pytest.mark.django_db
def test_validate_asset_metadata_no_encoding_format(draft_asset: Asset):
del draft_asset.metadata['encodingFormat']
draft_asset.save()
tasks.validate_asset_metadata_task(draft_asset.id)
draft_asset.refresh_from_db()
assert draft_asset.status == Asset.Status.INVALID
assert draft_asset.validation_errors == [
{'field': '', 'message': "'encodingFormat' is a required property"}
]
@pytest.mark.django_db
def test_validate_asset_metadata_no_digest(draft_asset: Asset):
draft_asset.blob.sha256 = None
draft_asset.blob.save()
del draft_asset.full_metadata['digest']
draft_asset.save()
tasks.validate_asset_metadata_task(draft_asset.id)
draft_asset.refresh_from_db()
assert draft_asset.status == Asset.Status.INVALID
assert draft_asset.validation_errors == [
{'field': 'digest', 'message': 'Value error, A non-zarr asset must have a sha2_256.'}
]
@pytest.mark.django_db
def test_validate_asset_metadata_malformed_keywords(draft_asset: Asset):
draft_asset.metadata['keywords'] = 'foo'
draft_asset.save()
tasks.validate_asset_metadata_task(draft_asset.id)
draft_asset.refresh_from_db()
assert draft_asset.status == Asset.Status.INVALID
assert draft_asset.validation_errors == [
{'field': 'keywords', 'message': "'foo' is not of type 'array'"}
]
@pytest.mark.django_db
def test_validate_asset_metadata_saves_version(draft_asset: Asset, draft_version: Version):
draft_version.assets.add(draft_asset)
# Update version with queryset so modified isn't auto incremented
old_datetime = datetime.datetime.fromtimestamp(1573782390).astimezone(datetime.UTC)
Version.objects.filter(id=draft_version.id).update(modified=old_datetime)
# Run task
tasks.validate_asset_metadata_task(draft_asset.id)
# Test that version has new modified timestamp
draft_version.refresh_from_db()
assert draft_version.modified != old_datetime
@pytest.mark.django_db
def test_validate_version_metadata(draft_version: Version, asset: Asset):
draft_version.assets.add(asset)
# Bypass .save to manually set an older timestamp
old_modified = timezone.now() - datetime.timedelta(minutes=10)
updated = Version.objects.filter(id=draft_version.id).update(modified=old_modified)
assert updated == 1
tasks.validate_version_metadata_task(draft_version.id)
draft_version.refresh_from_db()
assert draft_version.status == Version.Status.VALID
assert draft_version.validation_errors == []
# Ensure modified field was updated
assert draft_version.modified > old_modified
@pytest.mark.django_db
def test_validate_version_metadata_non_pending_version(draft_version: Version, asset: Asset):
# Bypass .save to manually set an older timestamp, set status to INVALID
old_modified = timezone.now() - datetime.timedelta(minutes=10)
updated = Version.objects.filter(id=draft_version.id).update(
modified=old_modified, status=Version.Status.INVALID, validation_errors=['foobar']
)
assert updated == 1
draft_version.refresh_from_db()
old_validation_errors = draft_version.validation_errors
tasks.validate_version_metadata_task(draft_version.id)
draft_version.refresh_from_db()
# Assert fields not updated
assert draft_version.status == Version.Status.INVALID
assert draft_version.validation_errors == old_validation_errors
assert draft_version.modified == old_modified
@pytest.mark.django_db
def test_validate_version_metadata_no_side_effects(draft_version: Version, asset: Asset):
draft_version.assets.add(asset)
# Set the version `status` and `validation_errors` fields to something
# which can't be a result of validate_version_metadata
draft_version.status = Version.Status.PENDING
draft_version.validation_errors = [{'foo': 'bar'}]
draft_version.save()
# Validate version metadata, storing the model data before and after
old_data = model_to_dict(draft_version)
tasks.validate_version_metadata_task(draft_version.id)
draft_version.refresh_from_db()
new_data = model_to_dict(draft_version)
# Check that change is isolated to these two fields
assert old_data.pop('status') != new_data.pop('status')
assert old_data.pop('validation_errors') != new_data.pop('validation_errors')
assert old_data == new_data
@pytest.mark.django_db
def test_validate_version_metadata_malformed_schema_version(draft_version: Version, asset: Asset):
draft_version.assets.add(asset)
draft_version.metadata['schemaVersion'] = 'xxx'
draft_version.save()
tasks.validate_version_metadata_task(draft_version.id)
draft_version.refresh_from_db()
assert draft_version.status == Version.Status.INVALID
assert len(draft_version.validation_errors) == 1
assert draft_version.validation_errors[0]['message'].startswith(
'Metadata version xxx is not allowed.'
)
@pytest.mark.django_db
def test_validate_version_metadata_no_description(draft_version: Version, asset: Asset):
draft_version.assets.add(asset)
del draft_version.metadata['description']
draft_version.save()
tasks.validate_version_metadata_task(draft_version.id)
draft_version.refresh_from_db()
assert draft_version.status == Version.Status.INVALID
assert draft_version.validation_errors == [
{'field': '', 'message': "'description' is a required property"}
]
@pytest.mark.django_db
def test_validate_version_metadata_malformed_license(draft_version: Version, asset: Asset):
draft_version.assets.add(asset)
draft_version.metadata['license'] = 'foo'
draft_version.save()
tasks.validate_version_metadata_task(draft_version.id)
draft_version.refresh_from_db()
assert draft_version.status == Version.Status.INVALID
assert draft_version.validation_errors == [
{'field': 'license', 'message': "'foo' is not of type 'array'"}
]
@pytest.mark.django_db
def test_validate_version_metadata_no_assets(
draft_version: Version,
):
# Validate the metadata to mark version as `VALID`
tasks.validate_version_metadata_task(draft_version.id)
draft_version.refresh_from_db()
assert draft_version.status == Version.Status.INVALID
assert draft_version.validation_errors == [
{
'field': 'assetsSummary',
'message': 'Value error, '
'A Dandiset containing no files or zero bytes is not publishable',
}
]
@pytest.mark.django_db
def test_validate_version_metadata_empty_zarr_asset(
draft_version: Version, zarr_archive_factory, draft_asset_factory
):
asset = draft_asset_factory(
blob=None,
zarr=zarr_archive_factory(
status=ZarrArchiveStatus.COMPLETE, checksum=EMPTY_CHECKSUM, size=0, file_count=0
),
status=Asset.Status.VALID,
)
assert asset.size == 0
draft_version.assets.add(asset)
# Since the zarr asset has zero size, a validation error should be produced
tasks.validate_version_metadata_task(draft_version.id)
draft_version.refresh_from_db()
assert draft_version.status == Version.Status.INVALID
assert draft_version.validation_errors == [
{
'field': 'assetsSummary',
'message': 'Value error, '
'A Dandiset containing no files or zero bytes is not publishable',
}
]
@pytest.mark.django_db
def test_validate_version_metadata_only_zarr_assets(
draft_version: Version, zarr_archive_factory, draft_asset_factory
):
asset = draft_asset_factory(
blob=None,
zarr=zarr_archive_factory(
status=ZarrArchiveStatus.COMPLETE,
checksum=compute_zarr_checksum(
[ZarrArchiveFile(path=Path('foo/bar'), size=100, digest=hashlib.md5().hexdigest())]
),
size=100,
file_count=1,
),
status=Asset.Status.VALID,
)
assert asset.size > 0
draft_version.assets.add(asset)
tasks.validate_version_metadata_task(draft_version.id)
draft_version.refresh_from_db()
assert draft_version.status == Version.Status.VALID
assert not draft_version.validation_errors
@pytest.mark.django_db
def test_publish_task(
api_client: APIClient,
user: User,
draft_asset_factory,
published_asset_factory,
draft_version_factory,
django_capture_on_commit_callbacks,
):
# Create a draft_version in PUBLISHING state
draft_version: Version = draft_version_factory(status=Version.Status.PUBLISHING)
assign_perm('owner', user, draft_version.dandiset)
api_client.force_authenticate(user=user)
old_draft_asset: Asset = draft_asset_factory(status=Asset.Status.VALID)
old_published_asset: Asset = published_asset_factory()
assert not old_draft_asset.published
assert old_published_asset.published
draft_version.assets.set([old_draft_asset, old_published_asset])
# Ensure that the number of versions increases by 1 after publishing
starting_version_count = draft_version.dandiset.versions.count()
with django_capture_on_commit_callbacks(execute=True):
tasks.publish_dandiset_task(draft_version.dandiset.id, user.id)
assert draft_version.dandiset.versions.count() == starting_version_count + 1
draft_version.refresh_from_db()
assert draft_version.status == Version.Status.PUBLISHED
published_version = draft_version.dandiset.versions.latest('created')
assert published_version.metadata == {
**draft_version.metadata,
'publishedBy': {
'id': URN_RE,
'name': 'DANDI publish',
'startDate': UTC_ISO_TIMESTAMP_RE,
'endDate': UTC_ISO_TIMESTAMP_RE,
'wasAssociatedWith': [
{
'id': URN_RE,
'identifier': 'RRID:SCR_017571',
'name': 'DANDI API',
# TODO: version the API
'version': '0.1.0',
'schemaKey': 'Software',
}
],
'schemaKey': 'PublishActivity',
},
'datePublished': UTC_ISO_TIMESTAMP_RE,
'manifestLocation': [
f'http://{settings.MINIO_STORAGE_ENDPOINT}/test-dandiapi-dandisets/test-prefix/dandisets/{draft_version.dandiset.identifier}/{published_version.version}/assets.yaml',
],
'identifier': f'DANDI:{draft_version.dandiset.identifier}',
'version': published_version.version,
'id': f'DANDI:{draft_version.dandiset.identifier}/{published_version.version}',
'url': (
f'{settings.DANDI_WEB_APP_URL}/dandiset/{draft_version.dandiset.identifier}'
f'/{published_version.version}'
),
'citation': published_version.citation(published_version.metadata),
'doi': f'10.80507/dandi.{draft_version.dandiset.identifier}/{published_version.version}',
# Once the assets are linked, assetsSummary should be computed properly
'assetsSummary': {
'schemaKey': 'AssetsSummary',
'numberOfBytes': 200,
'numberOfFiles': 2,
'dataStandard': [
{
'schemaKey': 'StandardsType',
'identifier': 'RRID:SCR_015242',
'name': 'Neurodata Without Borders (NWB)',
}
],
'approach': [],
'measurementTechnique': [],
'variableMeasured': [],
'species': [],
},
}
assert published_version.assets.count() == 2
new_draft_asset: Asset = published_version.assets.get(asset_id=old_draft_asset.asset_id)
new_published_asset: Asset = published_version.assets.get(asset_id=old_published_asset.asset_id)
# The former draft asset should have been modified into a published asset
assert new_draft_asset.published
assert new_draft_asset.asset_id == old_draft_asset.asset_id
assert new_draft_asset.path == old_draft_asset.path
assert new_draft_asset.blob == old_draft_asset.blob
assert new_draft_asset.metadata == {
**old_draft_asset.full_metadata,
'datePublished': UTC_ISO_TIMESTAMP_RE,
'publishedBy': {
'id': URN_RE,
'name': 'DANDI publish',
'startDate': UTC_ISO_TIMESTAMP_RE,
# TODO: endDate needs to be defined before publish is complete
'endDate': UTC_ISO_TIMESTAMP_RE,
'wasAssociatedWith': [
{
'id': URN_RE,
'identifier': 'RRID:SCR_017571',
'name': 'DANDI API',
'version': '0.1.0',
'schemaKey': 'Software',
}
],
'schemaKey': 'PublishActivity',
},
}
# The published_asset should be completely unchanged
assert new_published_asset.published
assert new_published_asset.asset_id == old_published_asset.asset_id
assert new_published_asset.path == old_published_asset.path
assert new_published_asset.blob == old_published_asset.blob
assert new_published_asset.metadata == old_published_asset.metadata