Skip to content

Commit ea56469

Browse files
Move file metadata population to unit test fixtures (#9994)
* Move datacite schema population out of migration stream * Move pytest fixuture data populator out of test class Co-authored-by: John Tordoff <> Co-authored-by: Longze Chen <[email protected]>
1 parent 8910fdb commit ea56469

File tree

7 files changed

+51
-4
lines changed

7 files changed

+51
-4
lines changed

api_tests/files/views/test_file_metadata_record_detail.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,24 @@
1111
PreprintFactory,
1212
)
1313

14+
from osf.migrations import ensure_datacite_file_schema
15+
16+
17+
@pytest.fixture(autouse=True)
18+
def datacite_file_schema():
19+
return ensure_datacite_file_schema()
20+
21+
1422
@pytest.fixture()
1523
def user():
1624
return AuthUserFactory()
1725

26+
1827
@pytest.fixture()
1928
def preprint(user):
2029
return PreprintFactory(creator=user)
2130

31+
2232
@pytest.fixture()
2333
def preprint_record(user, preprint):
2434
primary_file = preprint.primary_file
@@ -99,6 +109,7 @@ def test_preprint_file_metadata_record(self, app, user, preprint_record, unpubli
99109
assert res.status_code == 200
100110
assert res.json['data']['id'] == unpublished_preprint_record._id
101111

112+
102113
@pytest.mark.django_db
103114
class TestFileMetadataRecordUpdate:
104115

api_tests/files/views/test_file_metadata_record_download.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
ProjectFactory
88
)
99

10+
from osf.migrations import ensure_datacite_file_schema
11+
12+
13+
@pytest.fixture(autouse=True)
14+
def datacite_file_schema():
15+
return ensure_datacite_file_schema()
16+
17+
1018
@pytest.mark.django_db
1119
class TestFileMetadataRecordDownload:
1220

api_tests/schemas/views/test_file_metadata_schema_detail.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
AuthUserFactory,
77
)
88

9+
from osf.migrations import ensure_datacite_file_schema
10+
11+
12+
@pytest.fixture(autouse=True)
13+
def datacite_file_schema():
14+
return ensure_datacite_file_schema()
15+
916

1017
@pytest.mark.django_db
1118
class TestFileMetadataSchemaDetail:

osf/migrations/0136_add_datacite_file_metadata_schema.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,4 @@ class Migration(migrations.Migration):
4040
]
4141

4242
operations = [
43-
migrations.RunPython(add_datacite_schema, remove_datacite_schema)
4443
]

osf/migrations/0137_add_fm_record_to_osfstorage_files.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,4 @@ class Migration(migrations.Migration):
5454
('osf', '0136_add_datacite_file_metadata_schema'),
5555
]
5656

57-
operations = [
58-
migrations.RunPython(add_records_to_files_sql, remove_records_from_files),
59-
]
57+
operations = []

osf/migrations/__init__.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import sys
3+
import json
34
import logging
45

56
from django.apps import apps
@@ -230,3 +231,18 @@ def ensure_default_storage_region():
230231
'waterbutler_url': osf_settings.WATERBUTLER_URL
231232
}
232233
)
234+
235+
236+
def ensure_datacite_file_schema():
237+
''' Test use only '''
238+
from osf.models import FileMetadataSchema
239+
with open('osf/metadata/schemas/datacite.json') as f:
240+
jsonschema = json.load(f)
241+
_, created = FileMetadataSchema.objects.get_or_create(
242+
_id='datacite',
243+
schema_version=1,
244+
defaults={
245+
'name': 'datacite',
246+
'schema': jsonschema
247+
}
248+
)

osf_tests/test_file_metadata.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,27 @@
77
from website.settings import DOI_FORMAT, DATACITE_PREFIX
88
from website.project.licenses import set_license
99
from osf.models import FileMetadataSchema, NodeLicense, NodeLog
10+
from osf.migrations import ensure_datacite_file_schema
1011
from osf_tests.factories import ProjectFactory, SubjectFactory, AuthUserFactory
1112
from osf.utils.permissions import READ
1213
from api_tests.utils import create_test_file
1314

1415

16+
@pytest.fixture(autouse=True)
17+
def datacite_file_schema():
18+
return ensure_datacite_file_schema()
19+
20+
1521
@pytest.fixture()
1622
def node():
1723
return ProjectFactory()
1824

25+
1926
@pytest.fixture()
2027
def osf_file(node):
2128
return create_test_file(target=node, user=node.creator)
2229

30+
2331
def inject_placeholder_doi(json_data):
2432
# the OSF cannot currently issue DOIs for a file, which is required for datacite schema validation.
2533
# Manually add a placeholder in tests for validation until we handle this better.

0 commit comments

Comments
 (0)