From c42f8288c5c2a3f860e85e13a2a9dce36ad2a5fc Mon Sep 17 00:00:00 2001 From: Jordan <21129425+ItIsJordan@users.noreply.github.com> Date: Tue, 24 Sep 2024 13:02:01 +0100 Subject: [PATCH] Add tests for add_analyses Adds a basic test for the add_analyses functionality, and a test data folder. --- tests/search_test.py | 66 ++++++++++++++++++- .../test_analysis_submission/data1.yaml | 10 +++ .../test_analysis_submission/submission.yaml | 12 ++++ .../test_analysis_submission/test.cxx | 0 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 tests/test_data/test_analysis_submission/data1.yaml create mode 100644 tests/test_data/test_analysis_submission/submission.yaml create mode 100644 tests/test_data/test_analysis_submission/test.cxx diff --git a/tests/search_test.py b/tests/search_test.py index 8b9975629..5284e47a4 100644 --- a/tests/search_test.py +++ b/tests/search_test.py @@ -19,6 +19,7 @@ from opensearch_dsl import Search, Index import datetime import pytest +import os as op_s from invenio_db import db from unittest.mock import call @@ -26,7 +27,8 @@ add_default_aggregations, sort_fields_mapping from hepdata.ext.opensearch import api as os_api from hepdata.ext.opensearch.config.os_config import get_filter_field -from hepdata.ext.opensearch.document_enhancers import add_data_keywords, process_cmenergies +from hepdata.ext.opensearch.document_enhancers import add_data_keywords, process_cmenergies, add_analyses +from hepdata.modules.records.utils.submission import process_submission_directory from hepdata.utils.miscellaneous import get_resource_data from hepdata.ext.opensearch.process_results import merge_results, match_tables_to_papers, \ get_basic_record_information, is_datatable @@ -419,6 +421,68 @@ def test_add_data_keywords(): assert 'NOTAREALKEYWORD' not in doc['data_keywords'] +def test_add_analyses(app): + """ + Tests the add_analyses function to ensure that DataSubmission data + is properly added to the doc object during document enhancement. + + Currently testing against: NUISANCE, HistFactory + """ + # Here, test_data should match the contents of the test_folder + test_folder = "test_data/test_analysis_submission" + test_data = [ + { # ProSelecta/NUISANCE + "type": "NUISANCE", + "filename": "test.cxx" + }, + { # HistFactory entry + "type": "HistFactory", + "filename": "test.cxx" + } + ] + analysis_url = "http://localhost:5000/record/resource/%s?landing_page=true" + + with app.app_context(): + # Creating and submitting the test submission containing resources + # op_s is os module + base_dir = op_s.path.dirname(op_s.path.realpath(__file__)) + + hepsubmission = HEPSubmission(publication_recid=123456, + overall_status="finished", + version=1, + doi="10.17182/hepdata.123456") + db.session.add(hepsubmission) + db.session.commit() + + # Setting directory and executing processing + directory = op_s.path.join(base_dir, test_folder) + errors = process_submission_directory( + directory, + op_s.path.join(directory, "submission.yaml"), + hepsubmission.publication_recid + ) + + # No errors should happen + assert not errors + + # Set up a generic doc object to match what add_analyses expects + test_doc = {"analyses": [], "recid": hepsubmission.publication_recid} + # Run the test add_analyses function + add_analyses(test_doc) + + # A sorted list of all DataResource object IDs from subm,ission + data_ids = sorted([r.id for r in hepsubmission.resources]) + + assert len(data_ids) == len(test_doc["analyses"]) == len(test_data) + # There should be one entry into test_data per resource ID + # Looping through the test, resource IDs and the analysis outputs + for test, d_id, analysis in zip(test_data, data_ids, test_doc["analyses"]): + # Set the expected ID in the url to the sorted data_id entry + test["analysis"] = (analysis_url % d_id) + # Confirm data has been added to + assert analysis == test + + def test_process_cmenergies(): test_keywords = { "cmenergies": [ diff --git a/tests/test_data/test_analysis_submission/data1.yaml b/tests/test_data/test_analysis_submission/data1.yaml new file mode 100644 index 000000000..ec9813a1c --- /dev/null +++ b/tests/test_data/test_analysis_submission/data1.yaml @@ -0,0 +1,10 @@ +dependent_variables: +- header: {name: TestData1-dependent-V1, units: NA} + qualifiers: + - {name: TestData1-qualifier-V1, value: 0} + values: + - {value: 0} +independent_variables: +- header: {name: TestData1-independent-V1, units: NA} + values: + - value: 0 \ No newline at end of file diff --git a/tests/test_data/test_analysis_submission/submission.yaml b/tests/test_data/test_analysis_submission/submission.yaml new file mode 100644 index 000000000..b491df558 --- /dev/null +++ b/tests/test_data/test_analysis_submission/submission.yaml @@ -0,0 +1,12 @@ +--- +description: "TestSubmission1-V1" +comment: TestComment1-V1 +additional_resources: +- {description: Test, location: test.cxx, type: ProSelecta } +- {description: Test, location: test.cxx, type: HistFactory } +--- +name: "TestTable1-V1" +description: TestTable1-description-V1 +keywords: +- {name: cmenergies, values: [0]} +data_file: data1.yaml diff --git a/tests/test_data/test_analysis_submission/test.cxx b/tests/test_data/test_analysis_submission/test.cxx new file mode 100644 index 000000000..e69de29bb