diff --git a/.docker/docker-compose.yml b/.docker/docker-compose.yml index 61d455f43ec92974207ba0ae8aadf528e15dfcaa..798717baaf96a801e946211d1e6bfe6f463cad88 100644 --- a/.docker/docker-compose.yml +++ b/.docker/docker-compose.yml @@ -27,6 +27,8 @@ services: - "10080:10080" environment: DEBUG: 1 + CAOSDB_CONFIG__CAOSDB_INTEGRATION_TEST_SUITE_KEY: "_CAOSDB_ADV_TEST_SUITE" + networks: caosnet: diff --git a/integrationtests/test_ex_import_xlsx.py b/integrationtests/test_ex_import_xlsx.py index ae6d922a6bd41f3ac9eb3bab5809f7d50a9c974b..9a9a9a0669dcf987cb9e8fa3dbc0c3559470d355 100755 --- a/integrationtests/test_ex_import_xlsx.py +++ b/integrationtests/test_ex_import_xlsx.py @@ -26,9 +26,12 @@ Data is partly reused from the unit tests. import json import os import sys +import pytest from datetime import datetime from pathlib import Path +from linkahead.utils.register_tests import clear_database, set_test_key + from openpyxl import load_workbook import linkahead as db @@ -42,6 +45,7 @@ from utils import ( # noqa: E402, pylint: disable=wrong-import-position purge_from_json, ) # noqa: E402 +set_test_key("_CAOSDB_ADV_TEST_SUITE") def rfp(*pathcomponents): """ @@ -122,76 +126,62 @@ def _insert_multiple_refs_data(): cont.insert() -# def _fill_data(): - - -def setup_function(function): +@pytest.fixture(autouse=True) +def setup(clear_database): "Create needed test data" - try: - # Setup data structure - test_rt_0 = db.RecordType(name="Person", description="An observant human.") - test_prop_0 = db.Property(name="full_name", datatype=db.TEXT) - test_rt_0.add_property(test_prop_0) - test_rt_1 = db.RecordType(name="ObservationRecord") - test_prop_1 = db.Property(name="date", datatype=db.DATETIME) - test_prop_2 = db.Property(name="amount", datatype=db.INTEGER) - test_prop_3 = db.Property(name="observer", datatype=test_rt_0) - test_rt_1.add_property(test_prop_1).add_property(test_prop_2).add_property(test_prop_3) - test_rt_2 = db.RecordType(name="Conference") - test_prop_4 = db.Property(name="attendees", datatype=db.LIST(test_rt_0)) - test_rt_2.add_property(test_prop_4) - - # Setup data - test_person_0 = db.Record(name="Person 0").add_parent(test_rt_0).add_property( - test_prop_0, value="Their Name") - test_person_1 = db.Record(name="Person 1").add_parent(test_rt_0).add_property( - test_prop_0, value="Also Name") - test_person_2 = db.Record(name="Person 2").add_parent(test_rt_0).add_property( - test_prop_0, value="Third Name") - test_observation_0 = (db.Record().add_parent(test_rt_1) - .add_property(test_prop_1, value="2025-01-01") - .add_property(test_prop_2, value=5) - .add_property(test_prop_3, value=test_person_1)) - test_observation_1 = (db.Record().add_parent(test_rt_1) - .add_property(test_prop_1, value="2025-02-02") - .add_property(test_prop_2, value=3) - .add_property(test_prop_3, value=test_person_0)) - test_observation_2 = (db.Record().add_parent(test_rt_1) - .add_property(test_prop_1, value="2025-03-03") - .add_property(test_prop_2, value=12) - .add_property(test_prop_3, value=test_person_0)) - test_observation_3 = (db.Record().add_parent(test_rt_1) - .add_property(test_prop_1, value="2025-04-04") - .add_property(test_prop_2, value=0) - .add_property(test_prop_3, value=test_person_2)) - test_conference_0 = (db.Record(description="Only for Also").add_parent(test_rt_2) - .add_property(test_prop_4, value=[test_person_1])) - test_conference_1 = (db.Record(name="Official Conf", description="For everyone") - .add_parent(test_rt_2) - .add_property(test_prop_4, - value=[test_person_0, test_person_1, test_person_2])) - testdata = [test_rt_0, test_rt_1, test_rt_2, - test_prop_0, test_prop_1, test_prop_2, - test_prop_3, test_prop_4, - test_person_0, test_person_1, test_person_2, - test_observation_0, test_observation_1, test_observation_2, test_observation_3, - test_conference_0, test_conference_1] - - # Insert - c = db.Container() - c.extend(testdata) - c.insert() - except Exception as setup_exc: - print(setup_exc) - - -def teardown_function(function): - """Delete created test data""" - try: - db.execute_query("FIND ENTITY WITH ID > 99").delete() - except Exception as delete_exc: - print(delete_exc) - + # Setup data structure + test_rt_0 = db.RecordType(name="Person", description="An observant human.") + test_prop_0 = db.Property(name="full_name", datatype=db.TEXT) + test_rt_0.add_property(test_prop_0) + test_rt_1 = db.RecordType(name="ObservationRecord") + test_prop_1 = db.Property(name="date", datatype=db.DATETIME) + test_prop_2 = db.Property(name="amount", datatype=db.INTEGER) + test_prop_3 = db.Property(name="observer", datatype=test_rt_0) + test_rt_1.add_property(test_prop_1).add_property(test_prop_2).add_property(test_prop_3) + test_rt_2 = db.RecordType(name="Conference") + test_prop_4 = db.Property(name="attendees", datatype=db.LIST(test_rt_0)) + test_rt_2.add_property(test_prop_4) + + # Setup data + test_person_0 = db.Record(name="Person 0").add_parent(test_rt_0).add_property( + test_prop_0, value="Their Name") + test_person_1 = db.Record(name="Person 1").add_parent(test_rt_0).add_property( + test_prop_0, value="Also Name") + test_person_2 = db.Record(name="Person 2").add_parent(test_rt_0).add_property( + test_prop_0, value="Third Name") + test_observation_0 = (db.Record().add_parent(test_rt_1) + .add_property(test_prop_1, value="2025-01-01") + .add_property(test_prop_2, value=5) + .add_property(test_prop_3, value=test_person_1)) + test_observation_1 = (db.Record().add_parent(test_rt_1) + .add_property(test_prop_1, value="2025-02-02") + .add_property(test_prop_2, value=3) + .add_property(test_prop_3, value=test_person_0)) + test_observation_2 = (db.Record().add_parent(test_rt_1) + .add_property(test_prop_1, value="2025-03-03") + .add_property(test_prop_2, value=12) + .add_property(test_prop_3, value=test_person_0)) + test_observation_3 = (db.Record().add_parent(test_rt_1) + .add_property(test_prop_1, value="2025-04-04") + .add_property(test_prop_2, value=0) + .add_property(test_prop_3, value=test_person_2)) + test_conference_0 = (db.Record(description="Only for Also").add_parent(test_rt_2) + .add_property(test_prop_4, value=[test_person_1])) + test_conference_1 = (db.Record(name="Official Conf", description="For everyone") + .add_parent(test_rt_2) + .add_property(test_prop_4, + value=[test_person_0, test_person_1, test_person_2])) + testdata = [test_rt_0, test_rt_1, test_rt_2, + test_prop_0, test_prop_1, test_prop_2, + test_prop_3, test_prop_4, + test_person_0, test_person_1, test_person_2, + test_observation_0, test_observation_1, test_observation_2, test_observation_3, + test_conference_0, test_conference_1] + + # Insert + c = db.Container() + c.extend(testdata) + c.insert() def test_successful_export(): records = next(db.execute_query("FIND Record", page_length=50)) @@ -208,6 +198,10 @@ def test_successful_export(): def test_export_list_refs(tmpdir): """Test the export to XLSX of list-valued references. + + We retrieve all "Training" Records from LinkAhead and run `export_container_to_xlsx` on the + result. This shall create an XLSX template, a JSON schema, a filled JSON, and a filled XLSX. + All are checked against our expectation. """ tmpdir = Path(tmpdir) # Setup database @@ -230,15 +224,18 @@ def test_export_list_refs(tmpdir): with open(tmpdir/"schema.json", encoding="utf-8") as schema_f: schema_generated = json.load(schema_f) - training = schema_generated.get("properties", {}).get("Training") - assert training - assert len(schema_generated.get("properties", {})) == 1 # All top-level sheets? - for props in (training["properties"], - training["properties"]["trainer"]["items"]["properties"], - training["properties"]["participant"]["items"]["properties"], - training["properties"]["Organisation"]["items"]["properties"], - ): - assert "id" in props.keys() + try: + assert len(schema_generated["properties"]) == 1 # Only 'Training' should be top level + training = schema_generated["properties"]["Training"] + for props in (training["properties"], + training["properties"]["trainer"]["items"]["properties"], + training["properties"]["participant"]["items"]["properties"], + training["properties"]["Organisation"]["items"]["properties"], + ): + assert "id" in props.keys() + except KeyError: + print("It seems the generated JSON schema does not have the expected structure!") + raise # Check: XLSX template template_known_good = load_workbook(rfp("data", "multiple_refs_template.xlsx"))