Files
scrap/tests/cli/test_fetch.py
Gilles Soulier 152c2724fc feat: improve SPA scraping and increase test coverage
- Add SPA support for Playwright with wait_for_network_idle and extra_wait_ms
- Add BaseStore.get_spa_config() and requires_playwright() methods
- Implement AliExpress SPA config with JSON price extraction patterns
- Fix Amazon price parsing to prioritize whole+fraction combination
- Fix AliExpress regex patterns (remove double backslashes)
- Add CLI tests: detect, doctor, fetch, parse, run commands
- Add API tests: auth, logs, products, scraping_logs, webhooks

Tests: 417 passed, 85% coverage

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-17 14:46:55 +01:00

100 lines
3.8 KiB
Python

"""Tests pour la commande CLI fetch."""
import pytest
from unittest.mock import patch, MagicMock
from typer.testing import CliRunner
from pricewatch.app.cli.main import app
runner = CliRunner()
class TestFetchCommand:
"""Tests pour la commande fetch."""
def test_fetch_conflicting_options(self):
"""Fetch doit echouer si --http et --playwright sont specifies."""
result = runner.invoke(
app, ["fetch", "https://example.com", "--http", "--playwright"]
)
assert result.exit_code == 1
assert "impossible" in result.stdout.lower()
@patch("pricewatch.app.cli.main.fetch_http")
def test_fetch_http_success(self, mock_fetch: MagicMock):
"""Fetch HTTP doit afficher le resultat."""
mock_result = MagicMock()
mock_result.success = True
mock_result.html = "<html>test</html>"
mock_result.status_code = 200
mock_result.duration_ms = 150
mock_fetch.return_value = mock_result
result = runner.invoke(app, ["fetch", "https://example.com", "--http"])
assert result.exit_code == 0
assert "Succes" in result.stdout or "" in result.stdout
assert "150" in result.stdout
@patch("pricewatch.app.cli.main.fetch_http")
def test_fetch_http_failure(self, mock_fetch: MagicMock):
"""Fetch HTTP doit signaler l'echec."""
mock_result = MagicMock()
mock_result.success = False
mock_result.error = "Connection refused"
mock_fetch.return_value = mock_result
result = runner.invoke(app, ["fetch", "https://example.com", "--http"])
assert result.exit_code == 1
assert "Connection refused" in result.stdout
@patch("pricewatch.app.cli.main.fetch_playwright")
def test_fetch_playwright_success(self, mock_fetch: MagicMock):
"""Fetch Playwright doit afficher le resultat."""
mock_result = MagicMock()
mock_result.success = True
mock_result.html = "<html>test playwright</html>"
mock_result.duration_ms = 2500
mock_fetch.return_value = mock_result
result = runner.invoke(app, ["fetch", "https://example.com", "--playwright"])
assert result.exit_code == 0
assert "Succes" in result.stdout or "" in result.stdout
assert "2500" in result.stdout
@patch("pricewatch.app.cli.main.fetch_playwright")
def test_fetch_playwright_failure(self, mock_fetch: MagicMock):
"""Fetch Playwright doit signaler l'echec."""
mock_result = MagicMock()
mock_result.success = False
mock_result.error = "Timeout waiting for page"
mock_fetch.return_value = mock_result
result = runner.invoke(app, ["fetch", "https://example.com", "--playwright"])
assert result.exit_code == 1
assert "Timeout" in result.stdout
@patch("pricewatch.app.cli.main.fetch_playwright")
def test_fetch_default_is_playwright(self, mock_fetch: MagicMock):
"""Fetch sans option utilise Playwright par defaut."""
mock_result = MagicMock()
mock_result.success = True
mock_result.html = "<html>test</html>"
mock_result.duration_ms = 1000
mock_fetch.return_value = mock_result
result = runner.invoke(app, ["fetch", "https://example.com"])
assert result.exit_code == 0
mock_fetch.assert_called_once()
@patch("pricewatch.app.cli.main.fetch_playwright")
def test_fetch_with_debug(self, mock_fetch: MagicMock):
"""Fetch doit fonctionner avec --debug."""
mock_result = MagicMock()
mock_result.success = True
mock_result.html = "<html>test</html>"
mock_result.duration_ms = 1000
mock_fetch.return_value = mock_result
result = runner.invoke(app, ["fetch", "https://example.com", "--debug"])
assert result.exit_code == 0