Skip to content

Integration test registration procedure

It could be dangerous to run integration tests that possibly modify existing databases.

I therefore suggest a registration mechanism to validate suitable test databases for a specific test as follows:

TEST_KEY = "ABCDEF"

def not_test_record(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

@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.

    """
    global TEST_KEY
    test_record = db.execute_query("FIND Record PyTestInfo", unique=True)
    if test_record.get_property("TestIdentification").value != TEST_KEY:
        raise RuntimeError("Test database has not been setup for this test.")
    c = db.execute_query("FIND Record")
    c.extend(db.execute_query("FIND RecordType"))
    c.extend(db.execute_query("FIND Property"))
    c.extend(db.execute_query("FIND File"))

    c = db.Container().extend([r for r in c if not_test_record(r)])
    c.delete(raise_exception_on_error=False)

Explanations:

  • The database is only cleared when a predefined key that is stored in the global variable TEST_KEY has been set up as a proprty in record type "PyTestInfo".
  • The database is cleared except for the TEST_KEY Record and its data model.
  • TEST_KEY should of course be a unique key which is generated especially for this test file and its corresponding CaosDB container.

Data Model:

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