-
Notifications
You must be signed in to change notification settings - Fork 13
/
test_asset_paths.py
359 lines (281 loc) · 11.5 KB
/
test_asset_paths.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
from __future__ import annotations
from django.db.models import Q, QuerySet
import pytest
from dandiapi.api.asset_paths import (
add_asset_paths,
add_version_asset_paths,
delete_asset_paths,
extract_paths,
get_root_paths,
get_root_paths_many,
search_asset_paths,
update_asset_paths,
)
from dandiapi.api.models import Asset, AssetPath, Version
from dandiapi.api.models.asset_paths import AssetPathRelation
from dandiapi.api.services.asset.exceptions import AssetAlreadyExistsError
from dandiapi.api.tasks import publish_dandiset_task
@pytest.fixture
def ingested_asset(draft_version_factory, asset_factory) -> Asset:
asset: Asset = asset_factory()
version: Version = draft_version_factory()
version.assets.add(asset)
# Add asset to paths
add_asset_paths(asset, version)
return asset
@pytest.mark.parametrize(
('path', 'expected'),
[
('foo.txt', ['foo.txt']),
('foo/bar.txt', ['foo', 'foo/bar.txt']),
('foo/bar/baz.txt', ['foo', 'foo/bar', 'foo/bar/baz.txt']),
],
)
def test_extract_paths(path, expected):
assert extract_paths(path) == expected
@pytest.mark.django_db
def test_asset_path_add_asset(draft_version_factory, asset_factory):
# Create asset with version
asset: Asset = asset_factory()
version: Version = draft_version_factory()
version.assets.add(asset)
# Add asset to paths
add_asset_paths(asset, version)
# Get asset path
path: AssetPath = AssetPath.objects.get(asset=asset, version=version)
# Get parent paths
parent_paths: QuerySet[AssetPath] = AssetPath.objects.filter(
version=version, child_links__child=path
)
# Assert not empty
assert parent_paths.exists()
# Check parent paths and relations
for path in parent_paths:
assert path.aggregate_size == asset.size
assert path.aggregate_files == 1
# Assert self referencing link
assert AssetPathRelation.objects.filter(parent=path, child=path).exists()
# Get child paths
child_paths = AssetPath.objects.filter(version=version, path__startswith=path.path).exclude(
id=path.id
)
# Assert relation to all of these child paths exists
assert (
AssetPathRelation.objects.filter(parent=path, child__in=child_paths).count()
== child_paths.count()
)
@pytest.mark.django_db
def test_asset_path_add_asset_idempotent(draft_version_factory, asset_factory):
# Create asset with version
asset: Asset = asset_factory()
version: Version = draft_version_factory()
version.assets.add(asset)
# Add asset to paths
add_asset_paths(asset, version)
add_asset_paths(asset, version)
# Get asset path
path: AssetPath = AssetPath.objects.get(asset=asset, version=version)
assert path.aggregate_files == 1
assert path.aggregate_size == asset.size
@pytest.mark.django_db
def test_asset_path_add_asset_conflicting_path(draft_version_factory, asset_factory):
# Create asset with version
asset1: Asset = asset_factory()
asset2: Asset = asset_factory(path=asset1.path)
version: Version = draft_version_factory()
version.assets.add(asset1)
version.assets.add(asset2)
# Add asset1 paths
add_asset_paths(asset1, version)
assert version.asset_paths.filter(asset__isnull=False).count() == 1
# Ensure that adding asset2 raises the correct exception
with pytest.raises(AssetAlreadyExistsError):
add_asset_paths(asset2, version)
# Ensure that there no new asset paths created
assert version.asset_paths.filter(asset__isnull=False).count() == 1
@pytest.mark.django_db
def test_asset_path_add_version_asset_paths(draft_version_factory, asset_factory):
# Create asset with version
version: Version = draft_version_factory()
version.assets.add(asset_factory(path='foo/bar/baz.txt'))
version.assets.add(asset_factory(path='foo/bar/baz2.txt'))
version.assets.add(asset_factory(path='foo/baz/file.txt'))
version.assets.add(asset_factory(path='top.txt'))
# Add version asset paths
add_version_asset_paths(version)
# Check paths have expected file count and size
paths = [
('foo', 3, 300),
('foo/bar', 2, 200),
('foo/baz', 1, 100),
('top.txt', 1, 100),
]
# Check parent paths and relations
for path, count, size in paths:
asset_path = AssetPath.objects.get(version=version, path=path)
assert asset_path.aggregate_files == count
assert asset_path.aggregate_size == size
# Assert no paths have size or file count of zero
assert (
not AssetPath.objects.filter(version=version)
.filter(Q(aggregate_size=0) | Q(aggregate_files=0))
.exists()
)
@pytest.mark.django_db
def test_asset_path_add_version_asset_paths_idempotent(draft_version_factory, asset_factory):
# Create asset with version
version: Version = draft_version_factory()
version.assets.add(asset_factory(path='foo/bar/baz.txt'))
version.assets.add(asset_factory(path='foo/bar/baz2.txt'))
version.assets.add(asset_factory(path='foo/baz/file.txt'))
version.assets.add(asset_factory(path='top.txt'))
# Add version asset paths
add_version_asset_paths(version)
add_version_asset_paths(version)
# Ensure no duplicate counts
leafpaths = AssetPath.objects.select_related('asset').filter(
version=version, asset__isnull=False
)
for path in leafpaths:
assert path.aggregate_files == 1
assert path.aggregate_size == path.asset.size
@pytest.mark.django_db
def test_asset_path_add_asset_shared_paths(draft_version_factory, asset_factory):
# Create asset with version
version: Version = draft_version_factory()
asset1: Asset = asset_factory(path='foo/bar.txt')
asset2: Asset = asset_factory(path='foo/baz.txt')
version.assets.add(asset1)
version.assets.add(asset2)
# Add asset to paths
add_asset_paths(asset1, version)
add_asset_paths(asset2, version)
# Get path
path = AssetPath.objects.get(path='foo', version=version)
assert path.aggregate_size == asset1.blob.size + asset2.blob.size
assert path.aggregate_files == 2
@pytest.mark.django_db
def test_asset_path_delete_asset(ingested_asset):
asset = ingested_asset
version = ingested_asset.versions.first()
# Delete asset
delete_asset_paths(asset, version)
# Ensure it no longer exists
assert not AssetPath.objects.filter(asset=asset, version=version).exists()
assert not AssetPath.objects.filter(path=asset.path, version=version).exists()
@pytest.mark.django_db
def test_asset_path_delete_asset_idempotent(ingested_asset):
asset = ingested_asset
version = ingested_asset.versions.first()
# Try deleting twice, ensuring no error raised
delete_asset_paths(asset, version)
delete_asset_paths(asset, version)
@pytest.mark.django_db
def test_asset_path_update_asset(draft_version_factory, asset_factory):
# Create asset with version
version: Version = draft_version_factory()
old_asset: Asset = asset_factory(path='a/b.txt')
version.assets.add(old_asset)
add_asset_paths(old_asset, version)
# Create new asset
new_asset: Asset = asset_factory(path='c/d.txt')
version.assets.add(new_asset)
add_asset_paths(new_asset, version)
# Update asset (call twice to ensure idempotence)
update_asset_paths(old_asset=old_asset, new_asset=new_asset, version=version)
update_asset_paths(old_asset=old_asset, new_asset=new_asset, version=version)
# Assert none of old paths exist
old_paths = extract_paths(old_asset.path)
for path in old_paths:
assert not AssetPath.objects.filter(path=path, version=version).exists()
# Assert new paths exist
new_paths = extract_paths(new_asset.path)
for path in new_paths:
assert AssetPath.objects.filter(path=path, version=version).exists()
@pytest.mark.django_db
def test_asset_path_delete_asset_shared_paths(
draft_version_factory, asset_factory, asset_blob_factory
):
# Create asset with version
version: Version = draft_version_factory()
asset1: Asset = asset_factory(path='foo/bar.txt', blob=asset_blob_factory(size=128))
asset2: Asset = asset_factory(path='foo/baz.txt', blob=asset_blob_factory(size=256))
version.assets.add(asset1)
version.assets.add(asset2)
# Add asset to paths
add_asset_paths(asset1, version)
add_asset_paths(asset2, version)
# Delete asset
delete_asset_paths(asset1, version)
# Get path
path = AssetPath.objects.get(path='foo', version=version)
assert path.aggregate_size == asset2.blob.size
assert path.aggregate_files == 1
@pytest.mark.django_db
def test_asset_path_search_asset_paths(draft_version_factory, asset_factory):
version: Version = draft_version_factory()
assets = [asset_factory(path=path) for path in ['foo/bar.txt', 'foo/baz.txt', 'bar/foo.txt']]
for asset in assets:
version.assets.add(asset)
add_asset_paths(asset, version)
# Search root path
qs = search_asset_paths('', version)
# Assert that there are two folders
assert qs.count() == 2
assert sorted([x.path for x in qs]) == ['bar', 'foo']
assert all(x.asset is None for x in qs)
# Search foo, assert there are two files
qs = search_asset_paths('foo', version)
assert qs.count() == 2
assert all(x.asset is not None for x in qs)
@pytest.mark.django_db
def test_asset_path_publish_version(draft_version_factory, asset_factory, user):
version: Version = draft_version_factory()
asset = asset_factory(path='foo/bar.txt', status=Asset.Status.VALID)
version.assets.add(asset)
add_asset_paths(asset, version)
# Get existing paths
paths = AssetPath.objects.filter(version=version).values_list('path', flat=True)
# Pretend this dandiset is locked for publishing
version.status = Version.Status.PUBLISHING
version.save()
# Publish
publish_dandiset_task(version.dandiset.id, user.id)
# Get published version
published_version = (
Version.objects.filter(dandiset=version.dandiset)
.exclude(version='draft')
.order_by('-version')
.first()
)
assert published_version is not None
# Assert all paths copied
for path in paths:
AssetPath.objects.get(path=path, version=published_version)
@pytest.mark.django_db
def test_asset_path_get_root_paths(draft_version_factory, asset_factory):
version = draft_version_factory()
version.assets.add(asset_factory(path='a'))
version.assets.add(asset_factory(path='b/c'))
version.assets.add(asset_factory(path='d'))
version.assets.add(asset_factory(path='e/f/g'))
add_version_asset_paths(version)
assert get_root_paths(version).count() == 4
@pytest.mark.django_db
def test_asset_path_get_root_paths_many(draft_version_factory, asset_factory):
version = draft_version_factory()
version.assets.add(asset_factory(path='a'))
version.assets.add(asset_factory(path='b/c'))
version.assets.add(asset_factory(path='d'))
version.assets.add(asset_factory(path='e/f/g'))
add_version_asset_paths(version)
version2 = draft_version_factory()
version2.assets.add(asset_factory(path='a'))
version2.assets.add(asset_factory(path='b/c'))
version2.assets.add(asset_factory(path='d'))
version2.assets.add(asset_factory(path='e/f/g'))
add_version_asset_paths(version2)
# assert that the 4 root paths from each version are found
assert (
get_root_paths_many(Version.objects.filter(id__in=[version.id, version2.id])).count() == 8
)