Skip to content

Commit

Permalink
Auto-update databases/KP cache before each pytest session #2263
Browse files Browse the repository at this point in the history
  • Loading branch information
amykglen committed Apr 7, 2024
1 parent 65c5b30 commit ad57d5f
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion code/ARAX/test/conftest.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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")
Expand Down

0 comments on commit ad57d5f

Please sign in to comment.