release: bump version to 0.80.1
This commit is contained in:
1 parent
41059ca42a
commit
4b2004b7d8
29 files changed
+1843
-220
No files matched your search
@@ -1,6 +1,10 @@
|
||||
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
|
||||
@@ -219,3 +223,128 @@ def test_trigger_datasource_batch_skips_disabled_and_frequency_window(monkeypatc
|
||||
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())
|
||||
Reference in new issue
Block a user