Skip to content

Commit a15edf0

Browse files
test: bootstrap resolver tests on temporary sqlite
1 parent 587c15b commit a15edf0

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

tests/conftest.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
License. See http://www.apache.org/licenses/LICENSE-2.0 for details.
88
"""
99

10+
import atexit
1011
import os
12+
import shutil
1113
import sys
14+
import tempfile
15+
from pathlib import Path
1216

1317
import pytest
1418

@@ -18,6 +22,36 @@
1822
if ROOT not in sys.path:
1923
sys.path.insert(0, ROOT)
2024

25+
_TEST_SQLITE_DIR = Path(tempfile.mkdtemp(prefix="observantio-resolver-tests-"))
26+
_TEST_SQLITE_URL = f"sqlite:///{_TEST_SQLITE_DIR / 'resolver.sqlite3'}"
27+
_TEST_DATABASE_INITIALIZED = False
28+
29+
atexit.register(shutil.rmtree, _TEST_SQLITE_DIR, ignore_errors=True)
30+
31+
32+
def _bootstrap_sqlite_database() -> None:
33+
global _TEST_DATABASE_INITIALIZED
34+
35+
use_temp_sqlite = os.getenv("USE_TEMP_SQLITE_TEST_DB", "").strip().lower() in {"1", "true", "yes", "on"}
36+
database_url = os.getenv("RESOLVER_DATABASE_URL", "").strip()
37+
if use_temp_sqlite:
38+
database_url = _TEST_SQLITE_URL
39+
os.environ["RESOLVER_DATABASE_URL"] = database_url
40+
os.environ["DATABASE_URL"] = database_url
41+
42+
if _TEST_DATABASE_INITIALIZED or not database_url.startswith("sqlite"):
43+
return
44+
45+
from database import dispose_database, init_database, init_db
46+
47+
dispose_database()
48+
init_database(database_url)
49+
init_db()
50+
_TEST_DATABASE_INITIALIZED = True
51+
52+
53+
_bootstrap_sqlite_database()
54+
2155
from store.client import _fallback # noqa: E402
2256

2357

0 commit comments

Comments
 (0)