SaarthiAI / tests /conftest.py
parthmax24's picture
working proto 5
8b96826
Raw
History Blame Contribute Delete
1.69 kB
"""Shared fixtures: isolated cache DB per test session, fake API keys."""
import sys
from pathlib import Path
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from app import cache, config # noqa: E402
@pytest.fixture(autouse=True)
def isolated_cache(tmp_path, monkeypatch):
"""Each test gets a throwaway SQLite cache so tests never share state."""
monkeypatch.setattr(config, "CACHE_DB", str(tmp_path / "test_cache.sqlite"))
monkeypatch.setattr(cache, "_conn", None)
yield
if cache._conn is not None:
cache._conn.close()
cache._conn = None
@pytest.fixture
def fake_keys(monkeypatch):
monkeypatch.setattr(config, "tomtom_key", lambda: "fake-tomtom")
monkeypatch.setattr(config, "calendarific_key", lambda: "fake-calendarific")
monkeypatch.setattr(config, "ticketmaster_key", lambda: "fake-ticketmaster")
monkeypatch.setattr(config, "gemini_key", lambda: "fake-gemini")
monkeypatch.setattr(config, "groq_key", lambda: "fake-groq")
@pytest.fixture
def no_llm_keys(monkeypatch):
monkeypatch.setattr(config, "gemini_key", lambda: None)
monkeypatch.setattr(config, "groq_key", lambda: None)
class FakeResponse:
def __init__(self, json_data=None, status_code=200, text=""):
self._json = json_data or {}
self.status_code = status_code
self.text = text or str(json_data)
def json(self):
return self._json
def raise_for_status(self):
if self.status_code >= 400:
import requests
raise requests.HTTPError(f"HTTP {self.status_code}", response=self)
@pytest.fixture
def fake_response():
return FakeResponse