diff --git a/src/caosadvancedtools/testutils/register_tests.py b/src/caosadvancedtools/testutils/register_tests.py index cad1d10c7499b6681f58dea814ebb181baf10f00..c9ee99b94d266a3861c88fc328aba16236f0ea4e 100644 --- a/src/caosadvancedtools/testutils/register_tests.py +++ b/src/caosadvancedtools/testutils/register_tests.py @@ -30,7 +30,7 @@ import os """ This module implements a registration procedure for integration tests which need -a running CaosDB instance or a docker container. +a running CaosDB instance. It ensures that tests do not accidently overwrite data in real CaosDB instances, as it checks whether the running CaosDB instance is actually the correct one, that @@ -56,28 +56,21 @@ the database in case of success. TEST_KEY = None -def rfp(*pathcomponents): - """ - Return full path. - Shorthand convenience function. - """ - return os.path.join(os.path.dirname(__file__), *pathcomponents) - - def set_test_key(KEY): global TEST_KEY TEST_KEY = KEY -def not_test_record(r: db.Record): +def belongs_to_test_identification(r: db.Record): global TEST_KEY - if r.name == "PyTestInfo" or r.name == "TestIdentification": - return False - if r.get_property("TestIdentification") is not None: - if (r.get_property("TestIdentification").value == - TEST_KEY): - return False - return True + # RecordType and Property + if r.name == "PyTestInfo" or r.name == "testIdentification": + return True + if ( + r.get_property("testIdentification") is not None and + r.get_property("testIdentification").value == TEST_KEY): + return True + return False def register_test(): @@ -101,16 +94,16 @@ def register_test(): r = db.Record() r.add_parent("PyTestInfo") - r.add_property("TestIdentification", TEST_KEY) + r.add_property("testIdentification", TEST_KEY) r.insert() @pytest.fixture def clear_database(): - """First remove Records, then RecordTypes, then Properties, finally - files. Since there may be no entities, execute all deletions - without raising errors. + """Remove Records, RecordTypes, Properties, and Files ONLY IF the CaosDB server the current + connection points to was registered with the appropriate key. + PyTestInfo Records and the corresponding RecordType and Property are preserved. """ global TEST_KEY if TEST_KEY is None: @@ -125,7 +118,7 @@ def clear_database(): raise RuntimeError("Test has been registered. Please rerun tests.") else: raise RuntimeError("The database has not been setup for this test.") - if test_record.get_property("TestIdentification").value != TEST_KEY: + if test_record.get_property("testIdentification").value != TEST_KEY: raise RuntimeError("The database has been setup for a different test.") c = db.execute_query("FIND Record") diff --git a/src/caosadvancedtools/testutils/test_model.yml b/src/caosadvancedtools/testutils/test_model.yml index 2537f85eb767d69bbc14e48c71dc4e50baf4f794..2f00c873f0f0409c2df1e59f8218a360ef762497 100644 --- a/src/caosadvancedtools/testutils/test_model.yml +++ b/src/caosadvancedtools/testutils/test_model.yml @@ -1,6 +1,5 @@ -TestIdentification: - description: "This is a unique key which should be only known to the pytest file that is used to run tests within this instance of CaosDB." - datatype: TEXT PyTestInfo: obligatory_properties: - TestIdentification: \ No newline at end of file + testIdentification: + description: "This is a unique key which should be only known to the pytest file that is used to run tests within this instance of CaosDB." + datatype: TEXT