Skip to content

Commit 3c25172

Browse files
committed
fix more tests by mocking doi handling
1 parent 2d0fa12 commit 3c25172

File tree

5 files changed

+14
-5
lines changed

5 files changed

+14
-5
lines changed

dandiapi/api/services/publish/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,11 @@ def _publish_dandiset(dandiset_id: int, user_id: int) -> None:
233233
new_version.metadata['doi'] = '10.80507/dandi.123456/0.123456.1234'
234234

235235
validate(new_version.metadata, schema_key='PublishedDandiset', json_validation=True)
236-
_handle_publication_dois(new_version.id)
237236

238237
# Write updated manifest files and create DOI after
239238
# published version has been committed to DB.
240239
transaction.on_commit(lambda: write_manifest_files.delay(new_version.id))
240+
transaction.on_commit(lambda: _handle_publication_dois.delay(new_version.id))
241241

242242

243243
user = User.objects.get(id=user_id)

dandiapi/api/tests/test_asset_paths.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def test_asset_path_search_asset_paths(draft_version_factory, asset_factory):
298298

299299

300300
@pytest.mark.django_db
301-
def test_asset_path_publish_version(draft_version_factory, asset_factory, user):
301+
def test_asset_path_publish_version(draft_version_factory, asset_factory, user, mocker):
302302
version: Version = draft_version_factory()
303303
asset = asset_factory(path='foo/bar.txt', status=Asset.Status.VALID)
304304
version.assets.add(asset)

dandiapi/api/tests/test_audit.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,12 @@ def user_info(u):
118118

119119

120120
@pytest.mark.django_db
121-
def test_audit_update_metadata(api_client, draft_version, user):
121+
def test_audit_update_metadata(api_client, draft_version, user, mocker):
122122
# Create a Dandiset.
123123
dandiset = draft_version.dandiset
124124
add_dandiset_owner(dandiset, user)
125125

126+
mock_update_doi = mocker.patch('dandiapi.api.views.version.update_draft_version_doi')
126127
# Edit its metadata.
127128
metadata = draft_version.metadata
128129
metadata['foo'] = 'bar'
@@ -144,6 +145,7 @@ def test_audit_update_metadata(api_client, draft_version, user):
144145
metadata = rec.details['metadata']
145146
assert metadata['name'] == 'baz'
146147
assert metadata['foo'] == 'bar'
148+
mock_update_doi.assert_called_once()
147149

148150

149151
@pytest.mark.django_db
@@ -275,7 +277,7 @@ def test_audit_remove_asset(
275277

276278
@pytest.mark.django_db(transaction=True)
277279
def test_audit_publish_dandiset(
278-
api_client, user, dandiset_factory, draft_version_factory, draft_asset_factory
280+
api_client, user, dandiset_factory, draft_version_factory, draft_asset_factory, mocker
279281
):
280282
# Create a Dandiset whose draft version has one asset.
281283
dandiset = dandiset_factory()
@@ -289,6 +291,7 @@ def test_audit_publish_dandiset(
289291
# through the API).
290292
validate_asset_metadata(asset=draft_asset)
291293
validate_version_metadata(version=draft_version)
294+
mock_handle_dois = mocker.patch('dandiapi.api.services.publish._handle_publication_dois')
292295

293296
# Publish the Dandiset.
294297
api_client.force_authenticate(user=user)
@@ -301,6 +304,7 @@ def test_audit_publish_dandiset(
301304
rec = get_latest_audit_record(dandiset=dandiset, record_type='publish_dandiset')
302305
verify_model_properties(rec, user)
303306
assert rec.details['version'] == dandiset.most_recent_published_version.version
307+
mock_handle_dois.delay.assert_called_once()
304308

305309

306310
@pytest.mark.django_db

dandiapi/api/tests/test_tasks.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,9 @@ def test_publish_task(
386386
published_asset_factory,
387387
draft_version_factory,
388388
django_capture_on_commit_callbacks,
389+
mocker,
389390
):
391+
mock_handle_dois = mocker.patch('dandiapi.api.services.publish._handle_publication_dois')
390392
# Create a draft_version in PUBLISHING state
391393
draft_version: Version = draft_version_factory(status=Version.Status.PUBLISHING)
392394

@@ -444,7 +446,7 @@ def test_publish_task(
444446
f'/{published_version.version}'
445447
),
446448
'citation': published_version.citation(published_version.metadata),
447-
'doi': f'10.80507/dandi.{draft_version.dandiset.identifier}/{published_version.version}',
449+
# We have mocked the doi generation, so we will not have a DOI on the model.
448450
# Once the assets are linked, assetsSummary should be computed properly
449451
'assetsSummary': {
450452
'schemaKey': 'AssetsSummary',
@@ -501,3 +503,4 @@ def test_publish_task(
501503
assert new_published_asset.path == old_published_asset.path
502504
assert new_published_asset.blob == old_published_asset.blob
503505
assert new_published_asset.metadata == old_published_asset.metadata
506+
mock_handle_dois.delay.assert_called_once()

dandiapi/api/tests/test_unembargo.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ def test_unembargo_dandiset_validate_version_metadata(
313313
draft_version.save()
314314
draft_version.assets.add(asset_factory())
315315

316+
mock_create_doi = mocker.patch('dandiapi.api.services.embargo._create_dandiset_draft_doi')
316317
# Spy on the imported function in the embargo service
317318
validate_version_spy = mocker.spy(embargo_service, 'validate_version_metadata')
318319

@@ -321,6 +322,7 @@ def test_unembargo_dandiset_validate_version_metadata(
321322
assert validate_version_spy.call_count == 1
322323
draft_version.refresh_from_db()
323324
assert not draft_version.validation_errors
325+
mock_create_doi.assert_called_once()
324326

325327

326328
@pytest.mark.django_db

0 commit comments

Comments
 (0)