grantforge-api / backend /tests /test_regulation_resolver.py
GrantForge Bot
Deploy sha-9a5957fcdef15b7e2623f8b147cda6026475aee0 — source build (no GHCR)
3a3734f
Raw
History Blame Contribute Delete
3.27 kB
"""Testy resolve_regulation_for_program."""
import pytest
from core.search.regulation_snapshot import regulation_snapshot_store
from core.regulation.regulation_resolver import (
RegulationResolveResult,
resolve_regulation_for_program,
apply_regulation_context,
)
@pytest.fixture(autouse=True)
def _isolate_snapshot_store(monkeypatch):
"""Izolowany store in-memory na czas testu."""
from core.search import regulation_snapshot as rs_mod
fresh = rs_mod.RegulationSnapshotStore()
monkeypatch.setattr(rs_mod, "regulation_snapshot_store", fresh)
monkeypatch.setattr(
"core.regulation.regulation_resolver.regulation_snapshot_store",
fresh,
raising=False,
)
yield fresh
@pytest.mark.asyncio
async def test_resolve_from_existing_snapshot(_isolate_snapshot_store):
store = _isolate_snapshot_store
snap = store.create_snapshot(
program="PARP",
raw_text="Rozdział 1. Opis przedsięwzięcia\nZałącznik 1. Kosztorys inwestycji",
source_url="https://example.com/reg",
)
result = await resolve_regulation_for_program("PARP", program_type="PARP")
assert result.snapshot_id == snap.id
assert result.source == "snapshot"
assert result.snapshot_backed is True
assert not result.needs_user_url
@pytest.mark.asyncio
async def test_resolve_fallback_knowledge_base():
result = await resolve_regulation_for_program("Program PARP wsparcie MŚP", program_type="PARP")
assert result.source in ("knowledge_base", "snapshot", "heuristic")
assert len(result.required_sections) > 0 or len(result.key_rules) > 0
assert result.needs_user_url is False
@pytest.mark.asyncio
async def test_resolve_needs_user_url_for_unknown_program(monkeypatch):
async def _no_fetch(url):
return ""
monkeypatch.setattr(
"core.regulation.regulation_resolver._fetch_regulation_text",
_no_fetch,
)
result = await resolve_regulation_for_program("NIEPOWIADANY_PROGRAM_XYZ_123", program_type="UNKNOWN")
assert result.needs_user_url is True
assert result.source == "none"
@pytest.mark.asyncio
async def test_resolve_fetch_url_creates_snapshot(monkeypatch):
sample = "Sekcja 1. Opis projektu\nPunkt 2. Budżet i harmonogram\n" * 20
async def _mock_fetch(url):
return sample
monkeypatch.setattr(
"core.regulation.regulation_resolver._fetch_regulation_text",
_mock_fetch,
)
result = await resolve_regulation_for_program(
"TEST_FETCH",
regulation_url="https://example.com/regulamin.pdf",
)
assert result.source == "url_fetch"
assert result.snapshot_id
assert result.regulation_url == "https://example.com/regulamin.pdf"
def test_apply_regulation_context():
ext = apply_regulation_context(
{"company_data": {"nip": "123"}},
RegulationResolveResult(
snapshot_id="snap-1",
required_sections=["Opis projektu"],
source="snapshot",
regulation_url="https://x.pl/reg",
),
)
assert ext["regulation_snapshot_id"] == "snap-1"
assert ext["required_sections"] == ["Opis projektu"]
assert ext["regulation_pending"] is False
assert ext["company_data"]["nip"] == "123"