From 78f44bc107a42885225fa0eb0a5e8b2c6c805b5f Mon Sep 17 00:00:00 2001 From: Jordan <21129425+ItIsJordan@users.noreply.github.com> Date: Tue, 14 Nov 2023 15:09:37 +0000 Subject: [PATCH] Add new test record creation helper function Adds a function to create a dummy record alongside data. Updates a test to use this. --- tests/conftest.py | 32 +++++++++++++++++++++++++++++--- tests/submission_test.py | 19 +++---------------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index b57da060..c94ac0b1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -25,6 +25,8 @@ """HEPData Test Fixtures""" import os +import shutil +import time from unittest import mock from invenio_accounts.models import Role, User @@ -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 = 'test@hepdata.net' TEST_PWD = 'hello1' @@ -163,7 +167,8 @@ 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'}) @@ -171,9 +176,30 @@ def create_blank_test_record(): 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'test@test.com', 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': 'test@test.com'}, + 'uploader': {'name': 'Testy McTester', 'email': 'test@test.com'}, + '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 diff --git a/tests/submission_test.py b/tests/submission_test.py index c752094d..0d1b86a6 100644 --- a/tests/submission_test.py +++ b/tests/submission_test.py @@ -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): @@ -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': 'test@test.com'}, - 'uploader': {'name': 'Testy McTester', 'email': 'test@test.com'}, - '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: