nwb-linkml/nwb_linkml/tests/conftest.py

71 lines
2.1 KiB
Python
Raw Permalink Normal View History

import os
2024-07-02 07:34:01 +00:00
from pathlib import Path
from typing import List
2024-07-02 04:44:35 +00:00
import pytest
2024-07-02 07:34:01 +00:00
import requests_cache
2024-07-02 07:01:54 +00:00
from .fixtures import * # noqa: F403
2024-07-02 04:23:31 +00:00
2024-07-02 07:34:01 +00:00
def pytest_addoption(parser):
parser.addoption(
"--clean",
action="store_true",
default=False,
help="Don't reuse cached resources like cloned git repos or generated files",
)
2024-07-02 07:34:01 +00:00
parser.addoption(
"--with-output",
action="store_true",
help="keep test outputs for richer debugging information",
2024-07-02 07:34:01 +00:00
)
parser.addoption(
"--without-cache", action="store_true", help="Don't use a sqlite cache for network requests"
)
parser.addoption(
2024-07-25 05:49:35 +00:00
"--dev",
action="store_true",
help=(
"run tests that are intended only for development use, eg. those that generate output"
" for inspection"
),
)
2024-07-25 05:49:35 +00:00
def pytest_collection_modifyitems(config, items: List[pytest.Item]):
# remove dev tests from collection if we're not in dev mode!
2024-07-25 05:49:35 +00:00
if config.getoption("--dev"):
remove_tests = [t for t in items if not t.get_closest_marker("dev")]
else:
2024-07-25 05:49:35 +00:00
remove_tests = [t for t in items if t.get_closest_marker("dev")]
for t in remove_tests:
items.remove(t)
2024-07-02 07:34:01 +00:00
2024-07-02 04:23:31 +00:00
@pytest.fixture(autouse=True, scope="session")
def set_config_vars(tmp_output_dir):
2024-07-02 04:23:31 +00:00
os.environ["NWB_LINKML_CACHE_DIR"] = str(tmp_output_dir)
2024-07-02 07:34:01 +00:00
@pytest.fixture(scope="session", autouse=True)
def patch_requests_cache(pytestconfig):
"""
Cache network requests - for each unique network request, store it in
an sqlite cache. only do unique requests once per session.
"""
if pytestconfig.getoption("--without-cache"):
yield
return
cache_file = Path(__file__).parent / "output" / "requests-cache.sqlite"
requests_cache.install_cache(
str(cache_file),
backend="sqlite",
urls_expire_after={"localhost": requests_cache.DO_NOT_CACHE},
)
requests_cache.clear()
yield
# delete cache file unless we have requested it to persist for inspection
if not pytestconfig.getoption("--with-output"):
cache_file.unlink(missing_ok=True)