refactor(api): unify scheme query endpoints

This commit is contained in:
2026-07-30 11:01:45 +08:00
parent 03bb2d75c2
commit 31e2728db1
14 changed files with 457 additions and 261 deletions
+1 -1
View File
@@ -197,7 +197,7 @@ def test_run_burst_location_uses_single_timerange_with_burst_source_split(monkey
monkeypatch.setattr(
module,
"query_scheme_list",
lambda name: [
lambda name, scheme_type=None: [
(
1,
"BurstSchemeA",
+133
View File
@@ -0,0 +1,133 @@
from app.services import scheme_management, tjnetwork
class _FakeCursor:
def __init__(self):
self.calls = []
def __enter__(self):
return self
def __exit__(self, *_exc_info):
return False
def execute(self, statement, params=None):
self.calls.append((str(statement), params))
def fetchall(self):
return []
class _FakeConnection:
def __init__(self, cursor):
self._cursor = cursor
def __enter__(self):
return self
def __exit__(self, *_exc_info):
return False
def cursor(self):
return self._cursor
def test_query_scheme_list_pushes_scheme_type_into_sql(monkeypatch):
cursor = _FakeCursor()
monkeypatch.setattr(
scheme_management, "get_pgconn_string", lambda db_name=None: "postgres://test"
)
monkeypatch.setattr(
scheme_management.psycopg, "connect", lambda _conn_string: _FakeConnection(cursor)
)
assert scheme_management.query_scheme_list("demo", scheme_type="burst_analysis") == []
statement, params = cursor.calls[0]
assert "WHERE scheme_type = %s" in statement
assert params == ("burst_analysis",)
def test_get_all_schemes_filters_central_scheme_list_by_type(monkeypatch):
captured = {}
def fake_query_scheme_list(name, scheme_type=None, query_date=None):
captured["name"] = name
captured["scheme_type"] = scheme_type
captured["query_date"] = query_date
return [
(
7,
"burst_case",
"burst_analysis",
"alice",
"2026-01-01T00:00:00+08:00",
"2026-01-01T01:00:00+08:00",
{"burst_ID": ["P1"]},
)
]
monkeypatch.setattr(
scheme_management, "query_scheme_list", fake_query_scheme_list
)
result = tjnetwork.get_all_schemes("demo", scheme_type="burst_analysis")
assert captured == {
"name": "demo",
"scheme_type": "burst_analysis",
"query_date": None,
}
assert result == [
{
"scheme_id": 7,
"scheme_name": "burst_case",
"scheme_type": "burst_analysis",
"username": "alice",
"create_time": "2026-01-01T00:00:00+08:00",
"scheme_start_time": "2026-01-01T01:00:00+08:00",
"scheme_detail": {"burst_ID": ["P1"]},
}
]
def test_query_scheme_detail_rejects_wrong_specialized_type(monkeypatch):
monkeypatch.setattr(
scheme_management,
"query_burst_detection_scheme_detail",
lambda name, scheme_name: {
"scheme_name": scheme_name,
"scheme_type": "burst_analysis",
"network": name,
},
)
assert (
scheme_management.query_scheme_detail(
"demo",
"same_name",
scheme_type="burst_detection",
)
== {}
)
def test_query_scheme_detail_rejects_wrong_network(monkeypatch):
monkeypatch.setattr(
scheme_management,
"query_burst_location_scheme_detail",
lambda name, scheme_name: {
"scheme_name": scheme_name,
"scheme_type": "burst_location",
"network": "other_network",
},
)
assert (
scheme_management.query_scheme_detail(
"demo",
"same_name",
scheme_type="burst_location",
)
== {}
)