-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_manifests.py
165 lines (127 loc) · 5.78 KB
/
test_manifests.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
from __future__ import annotations
from typing import TYPE_CHECKING
from django.conf import settings
from django.core.files.base import ContentFile
import pytest
from rest_framework.renderers import JSONRenderer
from rest_framework_yaml.renderers import YAMLRenderer
from dandiapi.api.manifests import (
write_assets_jsonld,
write_assets_yaml,
write_collection_jsonld,
write_dandiset_jsonld,
write_dandiset_yaml,
)
from dandiapi.api.models import AssetBlob, Version
if TYPE_CHECKING:
from django.core.files.storage import Storage
@pytest.mark.django_db
def test_write_dandiset_jsonld(storage: Storage, version: Version):
# 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
write_dandiset_jsonld(version)
expected = JSONRenderer().render(version.metadata)
dandiset_jsonld_path = (
f'{settings.DANDI_DANDISETS_BUCKET_PREFIX}'
f'dandisets/{version.dandiset.identifier}/{version.version}/dandiset.jsonld'
)
with storage.open(dandiset_jsonld_path) as f:
assert f.read() == expected
@pytest.mark.django_db
def test_write_assets_jsonld(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())
write_assets_jsonld(version)
expected = JSONRenderer().render([asset.full_metadata for asset in version.assets.all()])
assets_jsonld_path = (
f'{settings.DANDI_DANDISETS_BUCKET_PREFIX}'
f'dandisets/{version.dandiset.identifier}/{version.version}/assets.jsonld'
)
with storage.open(assets_jsonld_path) as f:
assert f.read() == expected
@pytest.mark.django_db
def test_write_collection_jsonld(storage: Storage, version: Version, asset):
# 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
version.assets.add(asset)
asset_metadata = asset.full_metadata
write_collection_jsonld(version)
expected = JSONRenderer().render(
{
'@context': version.metadata['@context'],
'id': version.metadata['id'],
'@type': 'prov:Collection',
'hasMember': [asset_metadata['id']],
}
)
collection_jsonld_path = (
f'{settings.DANDI_DANDISETS_BUCKET_PREFIX}'
f'dandisets/{version.dandiset.identifier}/{version.version}/collection.jsonld'
)
with storage.open(collection_jsonld_path) as f:
assert f.read() == expected
@pytest.mark.django_db
def test_write_dandiset_yaml(storage: Storage, version: Version):
# 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
write_dandiset_yaml(version)
expected = YAMLRenderer().render(version.metadata)
dandiset_yaml_path = (
f'{settings.DANDI_DANDISETS_BUCKET_PREFIX}'
f'dandisets/{version.dandiset.identifier}/{version.version}/dandiset.yaml'
)
with storage.open(dandiset_yaml_path) as f:
assert f.read() == expected
@pytest.mark.django_db
def test_write_assets_yaml(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())
write_assets_yaml(version)
expected = YAMLRenderer().render([asset.full_metadata for asset in version.assets.all()])
assets_yaml_path = (
f'{settings.DANDI_DANDISETS_BUCKET_PREFIX}'
f'dandisets/{version.dandiset.identifier}/{version.version}/assets.yaml'
)
with storage.open(assets_yaml_path) as f:
assert f.read() == expected
@pytest.mark.django_db
def test_write_dandiset_yaml_already_exists(storage: Storage, version: Version):
# 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
# Save an invalid file for the task to overwrite
dandiset_yaml_path = (
f'{settings.DANDI_DANDISETS_BUCKET_PREFIX}'
f'dandisets/{version.dandiset.identifier}/{version.version}/dandiset.yaml'
)
storage.save(dandiset_yaml_path, ContentFile(b'wrong contents'))
write_dandiset_yaml(version)
expected = YAMLRenderer().render(version.metadata)
with storage.open(dandiset_yaml_path) as f:
assert f.read() == expected
@pytest.mark.django_db
def test_write_assets_yaml_already_exists(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())
# Save an invalid file for the task to overwrite
assets_yaml_path = (
f'{settings.DANDI_DANDISETS_BUCKET_PREFIX}'
f'dandisets/{version.dandiset.identifier}/{version.version}/assets.yaml'
)
storage.save(assets_yaml_path, ContentFile(b'wrong contents'))
write_assets_yaml(version)
expected = YAMLRenderer().render([asset.full_metadata for asset in version.assets.all()])
with storage.open(assets_yaml_path) as f:
assert f.read() == expected