Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
- Add new test for publish task
- Modify publish rest test to only test for PUBLISHING status, not entire publish workflow
- Consolidate two publish REST endpoint tests into one
  • Loading branch information
mvandenburgh committed Sep 22, 2022
1 parent de67687 commit ee4cad2
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 138 deletions.
128 changes: 128 additions & 0 deletions dandiapi/api/tests/test_tasks.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import hashlib

from django.conf import settings
from django.contrib.auth.models import User
from django.core.files.storage import Storage
from guardian.shortcuts import assign_perm
import pytest
from rest_framework.test import APIClient

from dandiapi.api import tasks
from dandiapi.api.models import Asset, AssetBlob, EmbargoedAssetBlob, Version

from .fuzzy import URN_RE, UTC_ISO_TIMESTAMP_RE


@pytest.mark.django_db
def test_calculate_checksum_task(storage: Storage, asset_blob_factory):
Expand Down Expand Up @@ -238,3 +243,126 @@ def test_validate_version_metadata_malformed_license(version: Version, asset: As
assert version.validation_errors == [
{'field': 'license', 'message': "'foo' is not of type 'array'"}
]


@pytest.mark.django_db
def test_publish_task(
api_client: APIClient,
user: User,
draft_asset_factory,
published_asset_factory,
draft_version_factory,
):
# 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()
old_published_asset: Asset = published_asset_factory()
old_published_asset.publish()
old_published_asset.save()
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()
tasks.publish_task(draft_version.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', # noqa: E501
],
'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.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
144 changes: 6 additions & 138 deletions dandiapi/api/tests/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.contrib.auth.models import User
from guardian.shortcuts import assign_perm
import pytest
from rest_framework.test import APIClient

from dandiapi.api import tasks
from dandiapi.api.models import Asset, Version
Expand Down Expand Up @@ -580,105 +581,8 @@ def test_version_rest_update_not_an_owner(api_client, user, version):


@pytest.mark.django_db
def test_version_rest_publish(api_client, user: User, draft_version: Version, asset: Asset):
assign_perm('owner', user, draft_version.dandiset)
api_client.force_authenticate(user=user)
draft_version.assets.add(asset)

# Validate the metadata to mark the version and asset as `VALID`
tasks.validate_version_metadata(draft_version.id)
tasks.validate_asset_metadata(asset.id)
draft_version.refresh_from_db()
assert draft_version.valid

resp = api_client.post(
f'/api/dandisets/{draft_version.dandiset.identifier}'
f'/versions/{draft_version.version}/publish/'
)
assert resp.data == {
'dandiset': {
'identifier': draft_version.dandiset.identifier,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'contact_person': draft_version.metadata['contributor'][0]['name'],
'embargo_status': 'OPEN',
},
'version': VERSION_ID_RE,
'name': draft_version.name,
'created': TIMESTAMP_RE,
'modified': TIMESTAMP_RE,
'asset_count': 1,
'size': draft_version.size,
'status': 'Valid',
}
published_version = Version.objects.get(version=resp.data['version'])
assert published_version
assert draft_version.dandiset.versions.count() == 2

published_asset: Asset = published_version.assets.get()
assert published_asset.published
# The asset should be the same after publishing
assert asset.asset_id == published_asset.asset_id

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', # noqa: E501
],
'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': 100,
'numberOfFiles': 1,
'dataStandard': [
{
'schemaKey': 'StandardsType',
'identifier': 'RRID:SCR_015242',
'name': 'Neurodata Without Borders (NWB)',
}
],
'approach': [],
'measurementTechnique': [],
'variableMeasured': [],
'species': [],
},
}

draft_version.refresh_from_db()
assert draft_version.status == Version.Status.PUBLISHED
assert not draft_version.valid


@pytest.mark.django_db
def test_version_rest_publish_assets(
api_client,
def test_version_rest_publish(
api_client: APIClient,
user: User,
draft_version: Version,
draft_asset_factory,
Expand Down Expand Up @@ -707,46 +611,10 @@ def test_version_rest_publish_assets(
f'/api/dandisets/{draft_version.dandiset.identifier}'
f'/versions/{draft_version.version}/publish/'
)
assert resp.status_code == 200
published_version = Version.objects.get(version=resp.data['version'])

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.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',
},
}
assert resp.status_code == 202

# 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
draft_version.refresh_from_db()
assert draft_version.status == Version.Status.PUBLISHING


@pytest.mark.django_db
Expand Down

0 comments on commit ee4cad2

Please sign in to comment.