-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_models.py
659 lines (610 loc) · 21.8 KB
/
test_models.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
import enum
from enum import Enum
import json
import sys
from typing import Any, Dict, List, Optional, Type, Union
import pydantic
from pydantic import Field, ValidationError
import pytest
from .test_datacite import _basic_publishmeta
from .. import models
from ..models import (
DANDI_INSTANCE_URL_PATTERN,
AccessRequirements,
AccessType,
Affiliation,
AgeReferenceType,
Asset,
BaseType,
CommonModel,
DandiBaseModel,
Dandiset,
DigestType,
IdentifierType,
LicenseType,
Organization,
ParticipantRelationType,
Person,
PropertyValue,
PublishedDandiset,
RelationType,
Resource,
RoleType,
)
if sys.version_info < (3, 8):
from typing_extensions import Literal
else:
from typing import Literal
def test_dandiset() -> None:
assert Dandiset.unvalidated()
def test_asset() -> None:
assert Asset.unvalidated()
def test_asset_digest() -> None:
with pytest.raises(pydantic.ValidationError) as exc:
models.BareAsset(
contentSize=100, encodingFormat="nwb", digest={"sha1": ""}, path="/"
)
assert any(
[
"value is not a valid enumeration member" in val
for val in set([el["msg"] for el in exc.value.errors()])
]
)
digest = 32 * "a"
digest_model = {models.DigestType.dandi_etag: digest}
with pytest.raises(pydantic.ValidationError) as exc:
models.BareAsset(
contentSize=100, encodingFormat="nwb", digest=digest_model, path="/"
)
assert any(
[
"Digest must have an appropriate dandi-etag value." in val
for val in set([el["msg"] for el in exc.value.errors()])
]
)
digest = 32 * "a" + "-1"
digest_model = {models.DigestType.dandi_etag: digest}
models.BareAsset(
contentSize=100, encodingFormat="nwb", digest=digest_model, path="/"
)
with pytest.raises(pydantic.ValidationError) as exc:
models.PublishedAsset( # type: ignore[call-arg]
contentSize=100,
encodingFormat="nwb",
digest={models.DigestType.dandi_etag: digest, "sha1": ""},
path="/",
)
assert any(
[
"value is not a valid enumeration member" in val
for val in set([el["msg"] for el in exc.value.errors()])
]
)
digest_model = {
models.DigestType.dandi_etag: digest,
models.DigestType.sha2_256: 63 * "a",
}
with pytest.raises(pydantic.ValidationError) as exc:
models.PublishedAsset( # type: ignore[call-arg]
contentSize=100, encodingFormat="nwb", digest=digest_model, path="/"
)
assert any(
"Digest must have an appropriate sha2_256 value." in el["msg"]
for el in exc.value.errors()
)
digest_model = {
models.DigestType.dandi_etag: digest,
models.DigestType.sha2_256: 64 * "a",
}
with pytest.raises(pydantic.ValidationError) as exc:
models.PublishedAsset( # type: ignore[call-arg]
contentSize=100, encodingFormat="nwb", digest=digest_model, path="/"
)
assert not any(
"Digest must have an appropriate dandi-etag value." in el["msg"]
for el in exc.value.errors()
)
digest_model = {
models.DigestType.dandi_etag: digest,
}
with pytest.raises(pydantic.ValidationError) as exc:
models.PublishedAsset( # type: ignore[call-arg]
contentSize=100, encodingFormat="nwb", digest=digest_model, path="/"
)
assert any(
"A non-zarr asset must have a sha2_256." in el["msg"]
for el in exc.value.errors()
)
digest = 32 * "a"
digest_model = {models.DigestType.dandi_zarr_checksum: digest}
with pytest.raises(pydantic.ValidationError) as exc:
models.BareAsset(
contentSize=100,
encodingFormat="application/x-zarr",
digest=digest_model,
path="/",
)
assert any(
[
"Digest must have an appropriate dandi-zarr-checksum value." in val
for val in set([el["msg"] for el in exc.value.errors()])
]
)
digest = f"{32 * 'a'}-1--42"
digest_model = {models.DigestType.dandi_zarr_checksum: digest}
with pytest.raises(pydantic.ValidationError) as exc:
models.BareAsset(
contentSize=100,
encodingFormat="application/x-zarr",
digest=digest_model,
path="/",
)
assert any(
[
"contentSize 100 is not equal to the checksum size 42." in val
for val in set([el["msg"] for el in exc.value.errors()])
]
)
digest = f"{32 * 'a'}-1--100"
digest_model = {models.DigestType.dandi_zarr_checksum: digest}
with pytest.raises(pydantic.ValidationError) as exc:
models.PublishedAsset( # type: ignore[call-arg]
contentSize=100,
encodingFormat="application/x-zarr",
digest=digest_model,
path="/",
)
assert all(
[
"field required" in val
for val in set([el["msg"] for el in exc.value.errors()])
]
)
digest_model = {
models.DigestType.dandi_zarr_checksum: digest,
models.DigestType.dandi_etag: digest + "-1",
}
with pytest.raises(pydantic.ValidationError) as exc:
models.BareAsset(
contentSize=100,
encodingFormat="application/x-zarr",
digest=digest_model,
path="/",
)
assert any(
[
"Digest cannot have both etag and zarr checksums." in val
for val in set([el["msg"] for el in exc.value.errors()])
]
)
with pytest.raises(pydantic.ValidationError) as exc:
models.PublishedAsset( # type: ignore[call-arg]
contentSize=100,
encodingFormat="application/x-zarr",
digest=digest_model,
path="/",
)
assert any(
[
"Digest cannot have both etag and zarr checksums." in val
for val in set([el["msg"] for el in exc.value.errors()])
]
)
digest_model = {}
with pytest.raises(pydantic.ValidationError) as exc:
models.BareAsset(
contentSize=100,
encodingFormat="application/x-zarr",
digest=digest_model,
path="/",
)
assert any(
[
"A zarr asset must have a zarr checksum." in val
for val in set([el["msg"] for el in exc.value.errors()])
]
)
with pytest.raises(pydantic.ValidationError) as exc:
models.PublishedAsset( # type: ignore[call-arg]
contentSize=100,
encodingFormat="application/x-zarr",
digest=digest_model,
path="/",
)
assert any(
[
"A zarr asset must have a zarr checksum." in val
for val in set([el["msg"] for el in exc.value.errors()])
]
)
@pytest.mark.parametrize(
"enumtype,values",
[
(
AccessType,
{
"OpenAccess": "dandi:OpenAccess",
"EmbargoedAccess": "dandi:EmbargoedAccess",
# "RestrictedAccess": "dandi:RestrictedAccess",
},
),
(
RoleType,
{
"Author": "dcite:Author",
"Conceptualization": "dcite:Conceptualization",
"ContactPerson": "dcite:ContactPerson",
"DataCollector": "dcite:DataCollector",
"DataCurator": "dcite:DataCurator",
"DataManager": "dcite:DataManager",
"FormalAnalysis": "dcite:FormalAnalysis",
"FundingAcquisition": "dcite:FundingAcquisition",
"Investigation": "dcite:Investigation",
"Maintainer": "dcite:Maintainer",
"Methodology": "dcite:Methodology",
"Producer": "dcite:Producer",
"ProjectLeader": "dcite:ProjectLeader",
"ProjectManager": "dcite:ProjectManager",
"ProjectMember": "dcite:ProjectMember",
"ProjectAdministration": "dcite:ProjectAdministration",
"Researcher": "dcite:Researcher",
"Resources": "dcite:Resources",
"Software": "dcite:Software",
"Supervision": "dcite:Supervision",
"Validation": "dcite:Validation",
"Visualization": "dcite:Visualization",
"Funder": "dcite:Funder",
"Sponsor": "dcite:Sponsor",
"StudyParticipant": "dcite:StudyParticipant",
"Affiliation": "dcite:Affiliation",
"EthicsApproval": "dcite:EthicsApproval",
"Other": "dcite:Other",
},
),
(
RelationType,
{
"IsCitedBy": "dcite:IsCitedBy",
"Cites": "dcite:Cites",
"IsSupplementTo": "dcite:IsSupplementTo",
"IsSupplementedBy": "dcite:IsSupplementedBy",
"IsContinuedBy": "dcite:IsContinuedBy",
"Continues": "dcite:Continues",
"Describes": "dcite:Describes",
"IsDescribedBy": "dcite:IsDescribedBy",
"HasMetadata": "dcite:HasMetadata",
"IsMetadataFor": "dcite:IsMetadataFor",
"HasVersion": "dcite:HasVersion",
"IsVersionOf": "dcite:IsVersionOf",
"IsNewVersionOf": "dcite:IsNewVersionOf",
"IsPreviousVersionOf": "dcite:IsPreviousVersionOf",
"IsPartOf": "dcite:IsPartOf",
"HasPart": "dcite:HasPart",
"IsReferencedBy": "dcite:IsReferencedBy",
"References": "dcite:References",
"IsDocumentedBy": "dcite:IsDocumentedBy",
"Documents": "dcite:Documents",
"IsCompiledBy": "dcite:IsCompiledBy",
"Compiles": "dcite:Compiles",
"IsVariantFormOf": "dcite:IsVariantFormOf",
"IsOriginalFormOf": "dcite:IsOriginalFormOf",
"IsIdenticalTo": "dcite:IsIdenticalTo",
"IsReviewedBy": "dcite:IsReviewedBy",
"Reviews": "dcite:Reviews",
"IsDerivedFrom": "dcite:IsDerivedFrom",
"IsSourceOf": "dcite:IsSourceOf",
"IsRequiredBy": "dcite:IsRequiredBy",
"Requires": "dcite:Requires",
"Obsoletes": "dcite:Obsoletes",
"IsObsoletedBy": "dcite:IsObsoletedBy",
"IsPublishedIn": "dcite:IsPublishedIn",
},
),
(
ParticipantRelationType,
{
"isChildOf": "dandi:isChildOf",
"isDizygoticTwinOf": "dandi:isDizygoticTwinOf",
"isMonozygoticTwinOf": "dandi:isMonozygoticTwinOf",
"isSiblingOf": "dandi:isSiblingOf",
"isParentOf": "dandi:isParentOf",
},
),
(
LicenseType,
{
"CC0_10": "spdx:CC0-1.0",
"CC_BY_40": "spdx:CC-BY-4.0",
},
),
(
IdentifierType,
{
"doi": "dandi:doi",
"orcid": "dandi:orcid",
"ror": "dandi:ror",
"dandi": "dandi:dandi",
"rrid": "dandi:rrid",
},
),
(
DigestType,
{
"md5": "dandi:md5",
"sha1": "dandi:sha1",
"sha2_256": "dandi:sha2-256",
"sha3_256": "dandi:sha3-256",
"blake2b_256": "dandi:blake2b-256",
"blake3": "dandi:blake3",
"dandi_etag": "dandi:dandi-etag",
"dandi_zarr_checksum": "dandi:dandi-zarr-checksum",
},
),
(
AgeReferenceType,
{
"BirthReference": "dandi:BirthReference",
"GestationalReference": "dandi:GestationalReference",
},
),
],
)
def test_types(enumtype: Type[Enum], values: Dict[str, str]) -> None:
assert {v.name: v.value for v in enumtype} == values
def test_autogenerated_titles() -> None:
schema = Asset.schema()
assert schema["title"] == "Asset"
assert schema["properties"]["schemaVersion"]["title"] == "Schema Version"
assert schema["definitions"]["PropertyValue"]["title"] == "Property Value"
def test_dantimeta_1() -> None:
"""checking basic metadata for publishing"""
# meta data without doi, datePublished and publishedBy
meta_dict: Dict[str, Any] = {
"identifier": "DANDI:999999",
"id": "DANDI:999999/draft",
"version": "1.0.0",
"name": "testing dataset",
"description": "testing",
"contributor": [
{
"name": "last name, first name",
"roleName": [RoleType("dcite:ContactPerson")],
}
],
"license": [LicenseType("spdx:CC-BY-4.0")],
"citation": "Last, first (2021). Test citation.",
"assetsSummary": {
"numberOfBytes": 0,
"numberOfFiles": 0,
"dataStandard": [{"name": "NWB"}],
"approach": [{"name": "electrophysiology"}],
"measurementTechnique": [{"name": "two-photon microscopy technique"}],
"species": [{"name": "Human"}],
},
"manifestLocation": [
"https://api.dandiarchive.org/api/dandisets/999999/versions/draft/assets/"
],
"url": "https://dandiarchive.org/dandiset/999999/draft",
}
# should work for Dandiset but PublishedDandiset should raise an error
Dandiset(**meta_dict)
with pytest.raises(ValidationError) as exc:
PublishedDandiset(**meta_dict)
error_msgs = [
"field required",
"A Dandiset containing no files or zero bytes is not publishable",
f'string does not match regex "^{DANDI_INSTANCE_URL_PATTERN}/dandiset/'
'\\d{6}/\\d+\\.\\d+\\.\\d+$"',
'string does not match regex "^DANDI:\\d{6}/\\d+\\.\\d+\\.\\d+"',
]
assert all([el["msg"] in error_msgs for el in exc.value.errors()])
assert set([el["loc"][0] for el in exc.value.errors()]) == {
"assetsSummary",
"datePublished",
"publishedBy",
"doi",
"url",
"id",
}
# after adding basic meta required to publish: doi, datePublished, publishedBy, assetsSummary,
# so PublishedDandiset should work
meta_dict["url"] = "https://dandiarchive.org/dandiset/999999/0.0.0"
meta_dict["id"] = "DANDI:999999/0.0.0"
meta_dict["version"] = "0.0.0"
meta_dict.update(_basic_publishmeta(dandi_id="999999"))
meta_dict["assetsSummary"].update(**{"numberOfBytes": 1, "numberOfFiles": 1})
PublishedDandiset(**meta_dict)
def test_schemakey() -> None:
typemap = {
"BareAsset": "Asset",
"PublishedAsset": "Asset",
"PublishedDandiset": "Dandiset",
}
for val in dir(models):
if val in ["BaseModel"]:
continue
klass = getattr(models, val)
if isinstance(klass, pydantic.main.ModelMetaclass):
assert "schemaKey" in klass.__fields__
if val in typemap:
assert typemap[val] == klass.__fields__["schemaKey"].default
else:
assert val == klass.__fields__["schemaKey"].default
def test_duplicate_classes() -> None:
qnames: Dict[str, Optional[type]] = {}
def check_qname(qname: str, klass: type) -> None:
if (
qname
in [
"dandi:id",
"dandi:schemaKey",
]
or qname.startswith("schema")
or qname.startswith("prov")
):
return
if qname in qnames:
t = qnames[qname]
if t is None:
return
elif issubclass(klass, (t,)):
return
elif issubclass(t, klass):
qnames[qname] = klass
return
if qname == "dandi:repository" and klass.__name__ in (
"Resource",
"CommonModel",
):
return
if qname == "dandi:relation" and klass.__name__ in (
"Resource",
"RelatedParticipant",
):
return
if qname in "dandi:approach" and klass.__name__ in (
"Asset",
"AssetsSummary",
):
return
if qname == "dandi:species" and klass.__name__ in (
"Participant",
"AssetsSummary",
):
return
raise ValueError(f"{qname},{klass} already exists {qnames[qname]}")
qnames[qname] = klass
modelnames = dir(models)
modelnames.remove("CommonModel")
modelnames.remove("BaseType")
modelnames.remove("BaseModel")
modelnames.remove("DandiBaseModel")
for val in ["CommonModel", "BaseType"] + modelnames:
klass = getattr(models, val)
if not isinstance(klass, pydantic.main.ModelMetaclass):
continue
if isinstance(klass, enum.EnumMeta):
enumval: Any
for enumval in klass:
qname = enumval.value
check_qname(qname, klass)
if hasattr(klass, "_ldmeta"):
if "nskey" in klass._ldmeta:
name = klass.__name__
qname = f'{klass._ldmeta["nskey"]}:{name}'
else:
qname = f"dandi:{name}"
check_qname(qname, klass)
for name, field in klass.__fields__.items():
if "nskey" in field.field_info.extra:
qname = field.field_info.extra["nskey"] + ":" + name
else:
qname = f"dandi:{name}"
check_qname(qname, klass)
def test_properties_mismatch() -> None:
prop_names = {}
errors = []
modelnames = dir(models)
modelnames.remove("BaseModel")
modelnames.remove("DandiBaseModel")
modelnames.remove("CommonModel")
modelnames.remove("Contributor")
modelnames.remove("Publishable")
for val in modelnames:
klass = getattr(models, val)
if not isinstance(klass, pydantic.main.ModelMetaclass):
continue
if not hasattr(klass, "_ldmeta") or "nskey" not in klass._ldmeta:
errors.append(f"{klass} does not have nskey")
for name, field in klass.__fields__.items():
nskey = field.field_info.extra.get("nskey", "dandi")
if name not in prop_names:
prop_names[name] = nskey
elif nskey != prop_names[name]:
errors.append(
f"{klass}:{name} has multiple nskeys: {nskey}, {prop_names[name]}"
)
assert errors == []
def test_schemakey_roundtrip() -> None:
class TempKlass(DandiBaseModel):
contributor: Optional[List[Union[Organization, Person]]]
schemaKey: Literal["TempKlass"] = Field("TempKlass", readOnly=True)
contributor = [
{
"name": "first",
"roleName": [],
"schemaKey": "Person",
"affiliation": [],
"includeInCitation": True,
},
{
"name": "last2, first2",
"roleName": ["dcite:ContactPerson"],
"schemaKey": "Person",
"affiliation": [],
"includeInCitation": True,
},
]
with pytest.raises(pydantic.ValidationError):
TempKlass(contributor=contributor)
contributor[0]["name"] = ", "
with pytest.raises(pydantic.ValidationError):
TempKlass(contributor=contributor)
contributor[0]["name"] = "last, first"
klassobj = TempKlass(contributor=contributor)
assert klassobj.contributor is not None and all(
[isinstance(val, Person) for val in klassobj.contributor]
)
@pytest.mark.parametrize("name", ["Mitášová, Helena", "O'Brien, Claire"])
def test_name_regex(name: str) -> None:
class TempKlass(DandiBaseModel):
contributor: Person
schemaKey: Literal["TempKlass"] = Field("TempKlass", readOnly=True)
contributor = {
"name": name,
"roleName": [],
"schemaKey": "Person",
"affiliation": [],
"includeInCitation": True,
}
TempKlass(contributor=contributor)
def test_resource() -> None:
with pytest.raises(pydantic.ValidationError):
Resource(relation=RelationType.IsCitedBy)
Resource(identifier="123", relation=RelationType.IsCitedBy)
Resource(url="http://example.org/resource", relation=RelationType.IsCitedBy)
def test_basetype() -> None:
props = json.loads(BaseType.schema_json())["properties"]
identifier = props["identifier"]
assert "anyOf" not in identifier
assert identifier.get("maxLength") == 1000
key = props["schemaKey"]
assert key["const"] == "BaseType"
def test_https_regex() -> None:
props = json.loads(Affiliation.schema_json())["properties"]["identifier"]
assert props["format"] == "uri"
assert props.get("maxLength") == 1000
def test_schemakey_in_required() -> None:
props = json.loads(Affiliation.schema_json())["required"]
assert "schemaKey" in props
@pytest.mark.parametrize("value", [None, [], {}, (), ""])
def test_propertyvalue(value: Any) -> None:
with pytest.raises(pydantic.ValidationError):
PropertyValue(value=value)
def test_propertyvalue_valid() -> None:
PropertyValue(value=1)
def test_propertyvalue_json() -> None:
reqprops = json.loads(PropertyValue.schema_json())["definitions"]["PropertyValue"][
"required"
]
assert "value" == reqprops[1]
def test_embargoedaccess() -> None:
with pytest.raises(pydantic.ValidationError):
CommonModel(access=[AccessRequirements(status=AccessType.EmbargoedAccess)])
CommonModel(
access=[
AccessRequirements(
status=AccessType.EmbargoedAccess, embargoedUntil="2022-12-31"
)
]
)