-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
78 lines (67 loc) · 2.52 KB
/
Copy pathconftest.py
File metadata and controls
78 lines (67 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Test fixtures.
We use a per-session SQLite file for the internal control-plane DB and a
separate one for the simulated customer DB. Anthropic + Stripe are not
exercised in unit tests — those live behind environment guards in their
respective modules.
"""
from __future__ import annotations
import os
import sqlite3
import sys
import tempfile
from pathlib import Path
import pytest
# Make sure the package under test is importable
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
# Configure env BEFORE any queryshield module is imported. This MUST happen
# at conftest import time (not in a fixture) because models.py creates the
# SQLAlchemy ENGINE at module import using the env-time DATABASE_URL.
_TEST_TMP = tempfile.mkdtemp(prefix="queryshield-tests-")
os.environ["DATABASE_URL"] = f"sqlite:///{_TEST_TMP}/control.db"
os.environ["VAULT_KEY"] = "RIaPxXZl9MNm7ESjqpsnbsiBCDkfErP6Mum0lmGD-7w=" # fixed test key
os.environ["ENVIRONMENT"] = "test"
os.environ["ANTHROPIC_API_KEY"] = "sk-ant-test"
@pytest.fixture()
def control_db():
"""Init the control plane schema fresh for every test."""
from queryshield.config import get_settings
from queryshield.rate_limit import _BUCKETS
get_settings.cache_clear() # type: ignore[attr-defined]
from queryshield.models import Base, ENGINE
Base.metadata.drop_all(bind=ENGINE)
Base.metadata.create_all(bind=ENGINE)
# Clear in-process rate-limit buckets so tests don't get throttled.
_BUCKETS.clear()
yield
Base.metadata.drop_all(bind=ENGINE)
@pytest.fixture()
def sample_customer_db():
"""A small SQLite file populated with a users + orders schema."""
f = tempfile.NamedTemporaryFile(suffix=".db", delete=False)
f.close()
path = f.name
c = sqlite3.connect(path)
try:
c.executescript(
"""
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, tenant_id TEXT, active INTEGER);
CREATE TABLE orders (id INTEGER PRIMARY KEY, user_id INTEGER, amount REAL, tenant_id TEXT);
INSERT INTO users (id, name, tenant_id, active) VALUES
(1, 'alice', 't1', 1),
(2, 'bob', 't2', 1),
(3, 'carol', 't1', 0);
INSERT INTO orders (id, user_id, amount, tenant_id) VALUES
(10, 1, 50.0, 't1'),
(11, 2, 80.0, 't2'),
(12, 1, 25.0, 't1');
"""
)
c.commit()
finally:
c.close()
yield path
try:
os.unlink(path)
except OSError:
pass