"""In-process HTTP test katalogu (TestClient, realny import JSON).""" from __future__ import annotations import os import pytest from fastapi import FastAPI from fastapi.testclient import TestClient os.environ.setdefault("DATABASE_URL", "sqlite:///:memory:") from core.subscription.db import Base, SessionLocal, engine, init_models # noqa: E402 from core.grants.wyszukiwarka_import import ( # noqa: E402 import_wyszukiwarka_file, resolve_wyszukiwarka_path, ) from endpoints.grants_nabory_enhanced import apply_nabory_enhancements # noqa: E402 import endpoints.grants_catalog # noqa: E402, F401 — rejestruje trasy na routerze from endpoints.grants import router # noqa: E402 @pytest.fixture(scope="module") def client(): init_models() Base.metadata.create_all(bind=engine) db = SessionLocal() path = resolve_wyszukiwarka_path() assert path is not None import_wyszukiwarka_file(path, db=db) db.close() apply_nabory_enhancements() app = FastAPI() app.include_router(router) return TestClient(app) def test_post_catalog_search_closed_region(client): resp = client.post( "/api/grants/catalog/search", json={ "query": "", "filters": {"status": "closed", "region": "mazowieckie"}, "limit": 30, }, ) assert resp.status_code == 200 body = resp.json() assert body["count"] >= 1, body for g in body["grants"]: assert g["status"] == "closed" regions = " ".join(g.get("eligible_regions") or []).lower() assert "mazowieckie" in regions or "cała polska" in regions def test_get_nabory_active_region_has_catalog_stats(client): resp = client.get( "/api/grants/nabory", params={"status": "active", "region": "mazowieckie", "q": "PARP", "limit": 10}, ) assert resp.status_code == 200 body = resp.json() assert "catalog_stats" in body, body.keys() assert body["catalog_stats"]["visible_in_catalog"] > 100 assert body["count"] >= 1 for n in body["nabory"]: assert n["status"] == "active" if (n.get("program_year") or 2024) < 2023: pytest.fail(f"stale active: {n.get('name')}") def test_get_nabory_closed_region(client): resp = client.get( "/api/grants/nabory", params={"status": "closed", "region": "mazowieckie", "limit": 15}, ) assert resp.status_code == 200 body = resp.json() assert body["count"] >= 1, body for n in body["nabory"]: assert n["status"] == "closed"