File size: 7,357 Bytes
02c1dbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""Test de integración: Admin Client ←→ Scraper Service.

Levanta el servicio FastAPI con TestClient y usa ScrapperClient
para enviar configuraciones de scraping y verificar resultados.
"""

from __future__ import annotations

import json
from typing import Any
from unittest.mock import patch

import pytest
from fastapi.testclient import TestClient

from modulo_1_servicio.main import app
from modulo_2_admin.core.client import ScrapperClient
from modulo_2_admin.core.models import FieldConfig, ScrapeJobConfig


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


@pytest.fixture
def admin_client(test_client: TestClient) -> ScrapperClient:
    """Admin client apuntando al TestClient de FastAPI.

    Parcheamos httpx.Client para que use el TestClient internamente.
    """
    client = ScrapperClient(base_url="http://testserver")

    original_post = client._client.post
    original_get = client._client.get

    def _patched_post(url, *args, **kwargs):
        relative = url.replace("http://testserver", "")
        response = test_client.post(relative, *args, **kwargs)
        return _FakeResponse(response.status_code, response.json())

    def _patched_get(url, *args, **kwargs):
        relative = url.replace("http://testserver", "")
        response = test_client.get(relative, *args, **kwargs)
        content_type = response.headers.get("content-type", "")
        if "json" in content_type:
            return _FakeResponse(response.status_code, response.json())
        return _FakeResponse(response.status_code, response.text)

    client._client.post = _patched_post
    client._client.get = _patched_get
    return client


class _FakeResponse:
    def __init__(self, status_code: int, data: Any) -> None:
        self.status_code = status_code
        self._data = data

    def json(self) -> Any:
        if isinstance(self._data, str):
            return json.loads(self._data)
        return self._data

    @property
    def text(self) -> str:
        if isinstance(self._data, str):
            return self._data
        return json.dumps(self._data)

    def raise_for_status(self) -> None:
        if self.status_code >= 400:
            raise Exception(f"HTTP {self.status_code}")


class TestIntegrationAdminWithScraper:
    """Test de integración: Admin Client → Scraper API."""

    def test_health_check(self, admin_client: ScrapperClient) -> None:
        """El admin client puede verificar que el servicio está vivo."""
        assert admin_client.health() is True

    def test_run_scrape_basic(self, admin_client: ScrapperClient) -> None:
        """Admin envía config básica de web_page y recibe operation_id."""
        config = ScrapeJobConfig(
            source="https://example.com",
            source_type="web_page",
            fields=[
                FieldConfig(name="title", selector="h1"),
            ],
        ).to_api_dict()

        response = admin_client.run_scrape(config)
        assert response.operation_id
        assert len(response.operation_id) == 8
        assert response.status in ("completed", "completed_with_errors")
        assert response.total_found >= 0

    def test_run_scrape_and_get_results(
        self, admin_client: ScrapperClient
    ) -> None:
        """Admin ejecuta scraping y luego consulta resultados."""
        config = ScrapeJobConfig(
            source="https://example.com",
            source_type="web_page",
            fields=[
                FieldConfig(name="title", selector="h1"),
                FieldConfig(name="description", selector="p"),
            ],
        ).to_api_dict()

        scrape_resp = admin_client.run_scrape(config)
        assert scrape_resp.operation_id

        results = admin_client.get_results(scrape_resp.operation_id)
        assert results.operation_id == scrape_resp.operation_id
        assert results.total_found >= 0
        assert isinstance(results.items, list)

    def test_run_scrape_and_get_web_results(
        self, admin_client: ScrapperClient
    ) -> None:
        """Admin ejecuta scraping y obtiene página HTML de resultados."""
        config = ScrapeJobConfig(
            source="https://example.com",
            source_type="web_page",
            fields=[FieldConfig(name="title", selector="h1")],
        ).to_api_dict()

        scrape_resp = admin_client.run_scrape(config)
        html = admin_client.get_results_web(scrape_resp.operation_id)
        assert isinstance(html, str)
        assert "doctype" in html.lower() or "<html" in html.lower()

    def test_run_scrape_with_pagination(
        self, admin_client: ScrapperClient
    ) -> None:
        """Admin puede configurar paginación."""
        config = ScrapeJobConfig(
            source="https://example.com",
            source_type="web_page",
            fields=[FieldConfig(name="title", selector="h1")],
            pagination=type(
                "PaginationConfig",
                (),
                {"strategy": "url", "url_template": None, "max_pages": 1},
            )(),
        ).to_api_dict()

        response = admin_client.run_scrape(config)
        assert response.operation_id

    def test_run_scrape_invalid_source(self, admin_client: ScrapperClient) -> None:
        """Admin recibe error con fuente inválida (URL sin protocolo)."""
        config = ScrapeJobConfig(
            source="not-a-valid-url",
            source_type="web_page",
            fields=[FieldConfig(name="title", selector="h1")],
        ).to_api_dict()

        response = admin_client.run_scrape(config)
        # La API captura el error y devuelve un operation_id igual
        assert response.operation_id
        assert response.status == "completed_with_errors"
        assert response.total_found == 0

    def test_get_results_not_found(self, admin_client: ScrapperClient) -> None:
        """Admin recibe 404 al consultar operation_id inexistente."""
        with pytest.raises(Exception):
            admin_client.get_results("nonexistent")

    def test_full_admin_workflow(self, admin_client: ScrapperClient) -> None:
        """Workflow completo: config → scrape → results → web."""
        config = ScrapeJobConfig(
            source="https://httpbin.org/html",
            source_type="web_page",
            fields=[
                FieldConfig(name="title", selector="h1"),
                FieldConfig(name="body", selector="p"),
            ],
            filters=type(
                "FilterConfig",
                (),
                {
                    "include_patterns": [],
                    "exclude_patterns": [],
                    "min_length": None,
                    "max_length": None,
                    "deduplicate": True,
                    "max_results": 10,
                },
            )(),
        ).to_api_dict()

        # 1. Ejecutar scraping
        scrape_resp = admin_client.run_scrape(config)
        assert scrape_resp.operation_id

        # 2. Obtener resultados JSON
        results = admin_client.get_results(scrape_resp.operation_id)
        assert results.source == "https://httpbin.org/html"
        assert isinstance(results.items, list)

        # 3. Obtener página web de resultados
        html = admin_client.get_results_web(scrape_resp.operation_id)
        assert isinstance(html, str) and len(html) > 50