File size: 1,685 Bytes
8b96826
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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