Skip to content

Commit

Permalink
Add new test record creation helper function
Browse files Browse the repository at this point in the history
Adds a function to create a dummy record alongside data. Updates a test to use this.
  • Loading branch information
ItIsJordan committed Nov 14, 2023
1 parent 35a33ac commit 78f44bc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
32 changes: 29 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"""HEPData Test Fixtures"""

import os
import shutil
import time
from unittest import mock

from invenio_accounts.models import Role, User
Expand All @@ -37,8 +39,10 @@
from hepdata.modules.dashboard.api import create_record_for_dashboard
from hepdata.modules.records.importer.api import import_records, _download_file
from hepdata.modules.records.utils.common import get_record_by_id
from hepdata.modules.records.utils.submission import get_or_create_hepsubmission
from hepdata.modules.records.utils.data_files import get_data_path_for_record
from hepdata.modules.records.utils.submission import get_or_create_hepsubmission, process_submission_directory
from hepdata.modules.records.utils.workflow import create_record
from hepdata.modules.submission.views import process_submission_payload

TEST_EMAIL = '[email protected]'
TEST_PWD = 'hello1'
Expand Down Expand Up @@ -163,17 +167,39 @@ def load_submission(app, load_default_data):

def create_blank_test_record():
"""
Helper function to create a single, blank record
Helper function to create a single, blank, finished submission
:returns submission: The newly created submission object
"""
record_information = create_record(
{'journal_info': 'Journal', 'title': 'Test Paper'})
recid = record_information['recid']
submission = get_or_create_hepsubmission(recid)
# Set overall status to finished so related data appears on dashboard
submission.overall_status = 'finished'
record = get_record_by_id(recid)
user = User(email=f'[email protected]', password='hello1', active=True,
id=1)
test_submissions = {}
create_record_for_dashboard(recid, test_submissions, user)
return submission


def create_test_record(file_location):
"""
Helper function to create a dummy record with data.
:param file_location: Path to the data directory.
:returns test_submission: The newly created submission object
"""
record = {'title': 'HEPData Testing',
'reviewer': {'name': 'Testy McTester', 'email': '[email protected]'},
'uploader': {'name': 'Testy McTester', 'email': '[email protected]'},
'message': 'This is ready',
'user_id': 1}
# Set up a new test submission
test_submission = process_submission_payload(**record)
# Ensure the status is set to `finished` so the related data can be accessed.
test_submission.overall_status = 'finished'
record_dir = get_data_path_for_record(test_submission.publication_recid, str(int(round(time.time()))))
shutil.copytree(file_location, record_dir)
process_submission_directory(record_dir, os.path.join(record_dir, 'submission.yaml'),
test_submission.publication_recid)
return test_submission
19 changes: 3 additions & 16 deletions tests/submission_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from hepdata.modules.submission.models import DataSubmission, HEPSubmission, RelatedRecid
from hepdata.modules.submission.views import process_submission_payload
from hepdata.config import HEPDATA_DOI_PREFIX
from tests.conftest import create_test_record


def test_submission_endpoint(app, client):
Expand Down Expand Up @@ -332,26 +333,12 @@ def test_related_records(app, admin_idx):
{"dir" : "related_submission_3", "related" : None},
{"dir": "related_submission_4", "related": None}
]
# Dummy record data
record = {'title': 'HEPData Testing',
'reviewer': {'name': 'Testy McTester', 'email': '[email protected]'},
'uploader': {'name': 'Testy McTester', 'email': '[email protected]'},
'message': 'This is ready',
'user_id': 1}
base_dir = os.path.dirname(os.path.realpath(__file__))

# Begin submission of test submissions
for data in test_data:
# Set up a new test submission
test_sub = process_submission_payload(**record)
data['sub'] = test_sub
# Ensure the status is set to `finished` so the related data can be accessed.
test_sub.overall_status = 'finished'
test_directory = os.path.join(base_dir, test_dir, data['dir'])
record_dir = get_data_path_for_record(test_sub.publication_recid, str(int(round(time.time()))))
shutil.copytree(test_directory, record_dir)
process_submission_directory(record_dir, os.path.join(record_dir, 'submission.yaml'),
test_sub.publication_recid)
location = os.path.join(base_dir, test_dir, data['dir'])
data['sub'] = create_test_record(location)

# Checking against results in test_data
for data in test_data:
Expand Down

0 comments on commit 78f44bc

Please sign in to comment.