From ad57d5f817874fe4c25f001771767f5b39d72905 Mon Sep 17 00:00:00 2001 From: amykglen Date: Sun, 7 Apr 2024 10:43:52 -0700 Subject: [PATCH] Auto-update databases/KP cache before each pytest session #2263 --- code/ARAX/test/conftest.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/code/ARAX/test/conftest.py b/code/ARAX/test/conftest.py index 7c75a6517..11299e78b 100644 --- a/code/ARAX/test/conftest.py +++ b/code/ARAX/test/conftest.py @@ -1,6 +1,14 @@ #!/usr/bin/env python3 +import os +import sys +import time + import pytest -# Thanks https://docs.pytest.org/en/latest/example/simple.html#control-skipping-of-tests-according-to-command-line-option +pathlist = os.path.realpath(__file__).split(os.path.sep) +sys.path.append(os.path.sep.join([*pathlist[:(pathlist.index("RTX") + 1)], "code", "ARAX", "ARAXQuery"])) +from ARAX_database_manager import ARAXDatabaseManager +sys.path.append(os.path.sep.join([*pathlist[:(pathlist.index("RTX") + 1)], "code", "ARAX", "ARAXQuery", "Expand"])) +from kp_info_cacher import KPInfoCacher def pytest_addoption(parser): @@ -23,7 +31,28 @@ def pytest_configure(config): config.addinivalue_line("markers", "external: mark test as relying on an external KP") +def pytest_sessionstart(session): + """ + Pytest runs these steps at the beginning of the testing session (prior to running any tests) + """ + # Ensure local databases are up to date + print(f"Running database manager to check for missing databases..") + db_manager = ARAXDatabaseManager(allow_downloads=True) + db_manager.update_databases() + + # Refresh KP info cache if it hasn't been updated in more than an hour + kp_info_cacher = KPInfoCacher() + cache_exists = os.path.exists(kp_info_cacher.smart_api_and_meta_map_cache) + cache_is_stale = time.time() - os.path.getmtime(kp_info_cacher.smart_api_and_meta_map_cache) > 3600 + if cache_exists and not cache_is_stale: + print(f"KP info cache is up to date.") + else: + print(f"Running KP info cacher to update stale/missing cache..") + kp_info_cacher.refresh_kp_info_caches() + + def pytest_collection_modifyitems(config, items): + # Thanks docs.pytest.org/en/latest/example/simple.html#control-skipping-of-tests-according-to-command-line-option skip_slow = pytest.mark.skip(reason="need --runslow option to run") skip_fast = pytest.mark.skip(reason="--runonlyslow option was used; this test is fast") skip_external = pytest.mark.skip(reason="need --runexternal option to run")