Files
planet/backend/tests/test_datasources_batch.py
T
linkong 4b2004b7d8
ci / backend (push) Waiting to run
ci / frontend (push) Waiting to run
release: bump version to 0.80.1
2026-11-26 19:41:17 +08:00

351 lines
11 KiB
Python

import asyncio
import inspect
from datetime import datetime, timedelta, timezone
from types import SimpleNamespace
import pytest
from fastapi import HTTPException
from app.api.v1 import datasources as datasources_api
from app.models.datasource import DataSource
from app.models.task import CollectionTask
from app.services import data_jobs
from app.services import earth_layer_cache as earth_cache
def make_datasource(
datasource_id: int,
source: str,
*,
name: str | None = None,
module: str = "L4",
is_active: bool = True,
last_status: str | None = None,
last_run_at: datetime | None = None,
frequency_minutes: int = 60,
) -> DataSource:
return DataSource(
id=datasource_id,
name=name or source,
source=source,
module=module,
priority="P1",
frequency_minutes=frequency_minutes,
collector_class=source,
is_active=is_active,
last_status=last_status,
last_run_at=last_run_at,
)
def test_datasource_product_key_groups_domain_specific_sources():
assert datasources_api.datasource_product_key(make_datasource(1, "aisstream_vessels")) == "vessels"
assert datasources_api.datasource_product_key(make_datasource(2, "telegeography_cables")) == "cables"
assert datasources_api.datasource_product_key(make_datasource(3, "celestrak_tle")) == "satellites"
assert datasources_api.datasource_product_key(make_datasource(4, "ris_live_bgp")) == "bgp"
def test_filter_datasources_by_product_status_and_collected_state():
vessels = make_datasource(1, "aisstream_vessels", last_status="success")
cables = make_datasource(2, "telegeography_cables", last_status="failed")
filtered = datasources_api._filter_datasources_in_memory(
[vessels, cables],
running_tasks={},
record_counts={"aisstream_vessels": 12, "telegeography_cables": 0},
product="vessels",
run_status="success",
collected=True,
)
assert filtered == [vessels]
def test_serialize_datasource_row_can_skip_endpoint_resolution():
datasource = make_datasource(7, "arcgis_cables", last_status="success", module="L2")
class ExplodingConfig:
def get_yaml_url(self, _source):
raise AssertionError("endpoint resolution should be skipped")
row = datasources_api.serialize_datasource_row(
datasource,
running_tasks={},
latest_tasks={},
record_counts={"arcgis_cables": 343},
endpoint_overrides={},
config=ExplodingConfig(),
include_endpoint=False,
)
assert row["id"] == 7
assert row["source"] == "arcgis_cables"
assert row["module"] == "L2"
assert row["last_status"] == "success"
assert row["collected_records"] == 343
assert row["has_collected_data"] is True
assert "endpoint" not in row
def test_serialize_datasource_row_includes_endpoint_when_requested():
datasource = make_datasource(8, "arcgis_landing_points")
class Config:
def get_yaml_url(self, source):
return f"https://example.test/{source}"
row = datasources_api.serialize_datasource_row(
datasource,
running_tasks={},
latest_tasks={},
record_counts={},
endpoint_overrides={},
config=Config(),
include_endpoint=True,
)
assert row["endpoint"] == "https://example.test/arcgis_landing_points"
def test_serialize_datasource_row_separates_collector_running_from_delete_task():
datasource = make_datasource(9, "top500", last_status="success")
task = CollectionTask(
id=44,
datasource_id=9,
source="top500",
task_type="clear_data",
status="running",
phase="clearing_data",
)
row = datasources_api.serialize_datasource_row(
datasource,
running_tasks={9: task},
latest_tasks={9: task},
record_counts={"top500": 500},
endpoint_overrides={},
config=object(),
include_endpoint=False,
)
assert row["is_task_active"] is True
assert row["is_running"] is False
assert row["task_type"] == "clear_data"
assert row["task_status"] == "running"
def test_cancel_queued_delete_task_finishes_immediately(monkeypatch):
task = CollectionTask(
id=45,
datasource_id=9,
source="top500",
task_type="clear_data",
status="queued",
phase="queued",
)
class FakeDb:
async def commit(self):
return None
async def refresh(self, _task):
return None
async def fake_broadcast(_task):
return None
monkeypatch.setattr(data_jobs, "_broadcast_task_update", fake_broadcast)
async def run():
return await data_jobs.request_cancel_datasource_task(FakeDb(), task)
result = asyncio.run(run())
assert result.status == "cancelled"
assert result.phase == "cancelled"
assert result.completed_at is not None
def test_clear_data_job_relies_on_db_outbox_instead_of_extra_earth_refresh_task():
source = inspect.getsource(data_jobs._run_clear_data_job)
assert "enqueue_earth_refresh_job" not in source
def test_invalidate_earth_layer_cache_for_source_covers_datasource_aliases(monkeypatch):
patterns: list[str] = []
def fake_delete_pattern(pattern: str) -> int:
patterns.append(pattern)
return 1
monkeypatch.setattr(earth_cache.earth_layer_cache, "delete_pattern", fake_delete_pattern)
deleted = earth_cache.invalidate_earth_layer_cache_for_source("telegeography_cables")
assert deleted == 3
assert patterns == [
"earth:layer:v1:cables*",
"earth:layer:v1:landing-points*",
"earth:layer:v1:summary*",
]
def test_trigger_datasource_batch_skips_disabled_and_frequency_window(monkeypatch):
async def run():
now = datetime.now(timezone.utc)
disabled = make_datasource(1, "aisstream_vessels", is_active=False)
not_due = make_datasource(2, "telegeography_cables", last_run_at=now, frequency_minutes=120)
due = make_datasource(3, "ris_live_bgp", last_run_at=now - timedelta(hours=2))
queued_sources: list[str] = []
async def fake_running_tasks(_db, _ids):
return {}
async def fake_enqueue(_db, datasource, task_type, **_kwargs):
queued_sources.append(datasource.source)
return CollectionTask(id=100 + datasource.id, datasource_id=datasource.id, source=datasource.source, task_type=task_type, status="queued")
monkeypatch.setattr(datasources_api, "_load_latest_running_tasks", fake_running_tasks)
monkeypatch.setattr(
datasources_api,
"enqueue_datasource_job",
fake_enqueue,
)
result = await datasources_api._trigger_datasource_batch(
object(),
[disabled, not_due, due],
force=False,
)
assert [item["source"] for item in result["triggered"]] == ["ris_live_bgp"]
assert {item["reason"] for item in result["skipped"]} == {"disabled", "within_frequency_window"}
assert queued_sources == ["ris_live_bgp"]
asyncio.run(run())
class _CommitDb:
def __init__(self, datasources=None):
self.commits = 0
self.datasources = list(datasources or [])
async def commit(self):
self.commits += 1
async def execute(self, _query):
datasources = self.datasources
class _Result:
def scalars(self):
return self
def all(self):
return datasources
return _Result()
def _patch_row_helpers(monkeypatch, synced: list[int]):
async def fake_sync(datasource_id):
synced.append(datasource_id)
async def fake_rows(_db, datasources):
return [{"id": item.id, "source": item.source, "is_active": item.is_active} for item in datasources]
async def fake_counts(_db):
return {"all": 3, "active": 2, "inactive": 1}
monkeypatch.setattr(datasources_api, "sync_datasource_job", fake_sync)
monkeypatch.setattr(datasources_api, "_serialize_datasource_rows", fake_rows)
monkeypatch.setattr(datasources_api, "_load_datasource_status_counts", fake_counts)
def test_activation_only_commits_and_syncs_changed_sources(monkeypatch):
async def run():
synced: list[int] = []
_patch_row_helpers(monkeypatch, synced)
db = _CommitDb()
enabled = make_datasource(1, "arcgis_cables", is_active=True)
disabled = make_datasource(2, "ris_live_bgp", is_active=False)
changed, failed = await datasources_api._apply_datasource_activation(db, [enabled, disabled], True)
assert [item.id for item in changed] == [2]
assert disabled.is_active is True
assert failed == []
assert synced == [2]
assert db.commits == 1
unchanged, _ = await datasources_api._apply_datasource_activation(db, [enabled], True)
assert unchanged == []
assert db.commits == 1
asyncio.run(run())
def test_batch_status_reports_updated_unchanged_and_missing(monkeypatch):
async def run():
synced: list[int] = []
_patch_row_helpers(monkeypatch, synced)
already = make_datasource(1, "arcgis_cables", is_active=True)
target = make_datasource(2, "ris_live_bgp", is_active=False)
db = _CommitDb([already, target])
admin = SimpleNamespace(id=9, role="admin")
result = await datasources_api.update_datasource_batch_status(
datasources_api.DatasourceBatchStatusRequest(source_ids=[1, 2, 2, 99], is_active=True),
current_user=admin,
db=db,
)
assert [row["id"] for row in result["updated"]] == [2]
assert result["updated"][0]["is_active"] is True
assert result["unchanged"] == [1]
assert result["not_found"] == [99]
assert result["sync_failed"] == []
assert result["counts"] == {"all": 3, "active": 2, "inactive": 1}
assert synced == [2]
asyncio.run(run())
def test_scheduling_changes_require_admin(monkeypatch):
async def run():
_patch_row_helpers(monkeypatch, [])
viewer = SimpleNamespace(id=5, role="viewer")
with pytest.raises(HTTPException) as batch_error:
await datasources_api.update_datasource_batch_status(
datasources_api.DatasourceBatchStatusRequest(source_ids=[1], is_active=False),
current_user=viewer,
db=_CommitDb(),
)
with pytest.raises(HTTPException) as single_error:
await datasources_api.enable_datasource("1", current_user=viewer, db=_CommitDb())
assert batch_error.value.status_code == 403
assert single_error.value.status_code == 403
asyncio.run(run())
def test_single_toggle_returns_row_and_counts(monkeypatch):
async def run():
synced: list[int] = []
_patch_row_helpers(monkeypatch, synced)
datasource = make_datasource(4, "ris_live_bgp", is_active=False)
async def fake_record(_db, _source_id):
return datasource
monkeypatch.setattr(datasources_api, "get_datasource_record", fake_record)
result = await datasources_api.enable_datasource(
"4", current_user=SimpleNamespace(id=1, role="super_admin"), db=_CommitDb()
)
assert result["status"] == "enabled"
assert result["data"] == {"id": 4, "source": "ris_live_bgp", "is_active": True}
assert result["counts"]["active"] == 2
assert synced == [4]
asyncio.run(run())