File size: 2,502 Bytes
88d2f2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
"""Shared pytest fixtures (ingestion + backend tests)."""

from __future__ import annotations

import os
import sys
import tempfile
from pathlib import Path
from typing import Any, Generator

import pytest

# Ensure the repo root is on sys.path so ``polyglot_alpha`` is importable when
# running ``pytest`` from anywhere in the tree.
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))


# ---------------------------------------------------------------------------
# Isolated SQLite DB per test (function scope).
# ---------------------------------------------------------------------------


@pytest.fixture()
def isolated_db(monkeypatch: pytest.MonkeyPatch) -> Generator[str, None, None]:
    """Point the persistence engine at a fresh on-disk sqlite file.

    Using on-disk SQLite (instead of ``:memory:``) keeps the engine usable
    across multiple sessions opened by the orchestrator's
    ``session_scope()`` context manager.
    """

    with tempfile.TemporaryDirectory() as tmp:
        db_path = os.path.join(tmp, "test.db")
        url = f"sqlite:///{db_path}"
        monkeypatch.setenv("DATABASE_URL", url)

        # Rebuild engine + create tables.
        from polyglot_alpha.persistence import db as persistence_db
        from polyglot_alpha.persistence import init_db

        persistence_db.reset_engine(url)
        init_db()

        # Reset pub/sub so each test starts clean.
        from polyglot_alpha import pubsub as pubsub_mod

        pubsub_mod.reset_pubsub()

        # Reset the slowapi limiter so per-IP buckets do not leak across
        # tests (the TestClient always uses ``testclient`` as the remote
        # address, so the per-route limits would otherwise accumulate).
        try:
            from polyglot_alpha.api.rate_limit import limiter as _limiter

            _limiter.reset()
        except Exception:
            pass

        yield url

        # Tear-down: drop the singleton so the next test rebuilds.
        pubsub_mod.reset_pubsub()
        try:
            from polyglot_alpha.api.rate_limit import limiter as _limiter

            _limiter.reset()
        except Exception:
            pass


@pytest.fixture()
def sample_event() -> dict[str, Any]:
    return {
        "title": "Sample geopolitical event for tests",
        "sources": [
            {"name": "test-source", "url": "https://example.com/a"},
        ],
        "language": "en",
        "category": "geopolitics",
    }