test(tradein): fix sweep-endpoint тесты после single-run guard (#874 follow-up) #876

Merged
bot-reviewer merged 1 commit from fix/sweep-guard-test-mocks into main 2026-05-31 09:07:41 +00:00
2 changed files with 10 additions and 7 deletions

View file

@ -9,11 +9,11 @@ import os
os.environ.setdefault("DATABASE_URL", "postgresql+psycopg://test:test@localhost:5432/test")
from datetime import UTC
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
# ── EKB_ANCHORS ────────────────────────────────────────────────────────────
@ -53,9 +53,10 @@ def test_city_sweep_counters_to_dict() -> None:
def test_city_sweep_counters_to_dict_all_keys() -> None:
from app.services.scrape_pipeline import CitySweepCounters
from dataclasses import fields
from app.services.scrape_pipeline import CitySweepCounters
c = CitySweepCounters()
d = c.to_dict()
expected_keys = {f.name for f in fields(c)}
@ -174,7 +175,8 @@ def test_start_endpoint_validates_request_delay_too_low(app_with_admin) -> None:
def test_start_endpoint_ok(app_with_admin) -> None:
"""Valid request → 200, run_id в ответе."""
with (
patch("app.services.scrape_runs.create_run", return_value=7) as mock_create,
patch("app.api.v1.admin.has_running_run", return_value=False),
patch("app.services.scrape_runs.create_run", return_value=7),
patch("app.services.scrape_pipeline.run_avito_city_sweep", new_callable=AsyncMock),
):
r = app_with_admin.post(
@ -209,7 +211,7 @@ def test_cancel_endpoint_not_running(app_with_admin) -> None:
def test_list_runs_endpoint(app_with_admin) -> None:
"""List runs endpoint возвращает список."""
from datetime import datetime, timezone
from datetime import datetime
fake_rows = [
{
@ -219,9 +221,9 @@ def test_list_runs_endpoint(app_with_admin) -> None:
"params": {"pages_per_anchor": 3},
"counters": {"lots_fetched": 300},
"error": None,
"started_at": datetime(2025, 1, 1, tzinfo=timezone.utc),
"finished_at": datetime(2025, 1, 1, 1, tzinfo=timezone.utc),
"heartbeat_at": datetime(2025, 1, 1, 1, tzinfo=timezone.utc),
"started_at": datetime(2025, 1, 1, tzinfo=UTC),
"finished_at": datetime(2025, 1, 1, 1, tzinfo=UTC),
"heartbeat_at": datetime(2025, 1, 1, 1, tzinfo=UTC),
}
]
with patch("app.services.scrape_runs.list_recent", return_value=fake_rows):

View file

@ -443,6 +443,7 @@ def test_city_sweep_start_request_enrich_imv_false() -> None:
def test_sweep_endpoint_passes_enrich_imv(app_with_admin) -> None:
"""POST /scrape/avito-city-sweep с enrich_imv=False → sweep вызывается без IMV."""
with (
patch("app.api.v1.admin.has_running_run", return_value=False),
patch("app.services.scrape_runs.create_run", return_value=42),
patch(
"app.services.scrape_pipeline.run_avito_city_sweep",