test(tradein): mock has_running_run в sweep-endpoint тестах после single-run guard
test_start_endpoint_ok / test_sweep_endpoint_passes_enrich_imv POST'ят на /scrape/avito-city-sweep с mock-db (has_running_run → truthy) → новый guard давал 409 вместо 200. Мокаем app.api.v1.admin.has_running_run→False (патчим где используется). + ruff autofix import-sort, убран unused mock_create.
This commit is contained in:
parent
997eda489c
commit
b12d7140d0
2 changed files with 10 additions and 7 deletions
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue