Skip to content
Snippets Groups Projects
Commit ceeb48de authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

MAINT: minor refactoring and documentation

parent 98b4c69f
No related branches found
No related tags found
1 merge request!29ENH: new module which allows to register integration tests in caosdb instances
Pipeline #22581 passed with warnings
...@@ -30,7 +30,7 @@ import os ...@@ -30,7 +30,7 @@ import os
""" """
This module implements a registration procedure for integration tests which need 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, 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 as it checks whether the running CaosDB instance is actually the correct one, that
...@@ -56,28 +56,21 @@ the database in case of success. ...@@ -56,28 +56,21 @@ the database in case of success.
TEST_KEY = None 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): def set_test_key(KEY):
global TEST_KEY global TEST_KEY
TEST_KEY = KEY TEST_KEY = KEY
def not_test_record(r: db.Record): def belongs_to_test_identification(r: db.Record):
global TEST_KEY global TEST_KEY
if r.name == "PyTestInfo" or r.name == "TestIdentification": # RecordType and Property
return False if r.name == "PyTestInfo" or r.name == "testIdentification":
if r.get_property("TestIdentification") is not None: return True
if (r.get_property("TestIdentification").value == if (
TEST_KEY): r.get_property("testIdentification") is not None and
return False r.get_property("testIdentification").value == TEST_KEY):
return True return True
return False
def register_test(): def register_test():
...@@ -101,16 +94,16 @@ def register_test(): ...@@ -101,16 +94,16 @@ def register_test():
r = db.Record() r = db.Record()
r.add_parent("PyTestInfo") r.add_parent("PyTestInfo")
r.add_property("TestIdentification", TEST_KEY) r.add_property("testIdentification", TEST_KEY)
r.insert() r.insert()
@pytest.fixture @pytest.fixture
def clear_database(): def clear_database():
"""First remove Records, then RecordTypes, then Properties, finally """Remove Records, RecordTypes, Properties, and Files ONLY IF the CaosDB server the current
files. Since there may be no entities, execute all deletions connection points to was registered with the appropriate key.
without raising errors.
PyTestInfo Records and the corresponding RecordType and Property are preserved.
""" """
global TEST_KEY global TEST_KEY
if TEST_KEY is None: if TEST_KEY is None:
...@@ -125,7 +118,7 @@ def clear_database(): ...@@ -125,7 +118,7 @@ def clear_database():
raise RuntimeError("Test has been registered. Please rerun tests.") raise RuntimeError("Test has been registered. Please rerun tests.")
else: else:
raise RuntimeError("The database has not been setup for this test.") 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.") raise RuntimeError("The database has been setup for a different test.")
c = db.execute_query("FIND Record") c = db.execute_query("FIND Record")
......
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: PyTestInfo:
obligatory_properties: obligatory_properties:
TestIdentification: testIdentification:
\ No newline at end of file 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment