File size: 3,578 Bytes
02c1dbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a16a580
02c1dbb
 
a16a580
02c1dbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e597ad1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""Tests de integración para la API REST."""

from __future__ import annotations

import pytest
from fastapi.testclient import TestClient

from modulo_1_servicio.main import app


@pytest.fixture
def client() -> TestClient:
    return TestClient(app)


class TestHealth:
    def test_health_endpoint(self, client: TestClient) -> None:
        response = client.get("/api/health")
        assert response.status_code == 200
        data = response.json()
        assert data["status"] == "ok"

    def test_root_endpoint(self, client: TestClient) -> None:
        """La raíz sirve la UI de NovaSearch (Gradio montado sobre FastAPI)."""
        response = client.get("/")
        assert response.status_code == 200
        assert "NovaSearch" in response.text

    def test_swagger_docs(self, client: TestClient) -> None:
        response = client.get("/docs")
        assert response.status_code == 200


class TestScrapeAPI:
    def test_scrape_invalid_config(self, client: TestClient) -> None:
        response = client.post("/api/scrape", json={"invalid": True})
        assert response.status_code == 400

    def test_scrape_missing_source(self, client: TestClient) -> None:
        response = client.post(
            "/api/scrape",
            json={"source_type": "web_page", "fields": []},
        )
        assert response.status_code == 400

    def test_results_not_found(self, client: TestClient) -> None:
        response = client.get("/api/results/nonexistent")
        assert response.status_code == 404

    def test_results_web_not_found(self, client: TestClient) -> None:
        response = client.get("/api/results/nonexistent/web")
        assert response.status_code == 404

    def test_deliver_not_found(self, client: TestClient) -> None:
        response = client.post(
            "/api/deliver/nonexistent",
            json={"emails": []},
        )
        assert response.status_code == 404


class TestAdaptersAPI:
    def test_list_adapters(self, client: TestClient) -> None:
        """GET /api/adapters retorna lista de adaptadores."""
        response = client.get("/api/adapters")
        assert response.status_code == 200
        data = response.json()
        assert "total" in data
        assert "adapters" in data
        assert isinstance(data["adapters"], list)

    def test_list_adapters_with_vertical(self, client: TestClient) -> None:
        """Filtrar por vertical retorna solo los de esa vertical."""
        response = client.get("/api/adapters?vertical=test")
        assert response.status_code == 200
        data = response.json()
        assert data["vertical"] == "test"
        assert isinstance(data["adapters"], list)

    def test_list_adapters_unknown_vertical(self, client: TestClient) -> None:
        """Vertical sin adaptadores retorna lista vacía."""
        response = client.get("/api/adapters?vertical=nonexistent")
        assert response.status_code == 200
        data = response.json()
        assert data["total"] == 0
        assert data["adapters"] == []

    def test_adapter_has_expected_structure(self, client: TestClient) -> None:
        """Cada adaptador en la respuesta tiene los campos esperados."""
        response = client.get("/api/adapters")
        assert response.status_code == 200
        data = response.json()
        if data["total"] > 0:
            adapter = data["adapters"][0]
            assert "name" in adapter
            assert "site" in adapter
            assert "vertical" in adapter
            assert "search_url" in adapter
            assert "fields" in adapter