- make realtime replacement and analysis result writes transactional\n- consolidate SCADA repositories and remove process-global project state\n- validate SCADA batches and use indexed GIS-backed business queries\n\nBREAKING CHANGE: remove the public analysis result writer and the pipeline-health network_name query parameter.
91 lines
2.3 KiB
Python
91 lines
2.3 KiB
Python
import asyncio
|
|
from types import MappingProxyType
|
|
|
|
import pytest
|
|
|
|
from app.infra.db.postgresql import scada
|
|
from app.infra.db.postgresql.scada import ScadaInfoRepository
|
|
|
|
|
|
class _FakeCursor:
|
|
def __init__(self):
|
|
self.query = None
|
|
|
|
async def __aenter__(self):
|
|
return self
|
|
|
|
async def __aexit__(self, exc_type, exc, tb):
|
|
return False
|
|
|
|
async def execute(self, query):
|
|
self.query = query
|
|
|
|
async def fetchall(self):
|
|
return [
|
|
{
|
|
"device_id": " 25470001 ",
|
|
"device_type": " PRESSURE ",
|
|
"node_id": " J1 ",
|
|
"link_id": None,
|
|
"api_query_id": "query-1",
|
|
"transmission_mode": "realtime",
|
|
"transmission_frequency": None,
|
|
"reliability": "95",
|
|
"x": "117.1",
|
|
"y": "32.9",
|
|
}
|
|
]
|
|
|
|
|
|
class _FakeConnection:
|
|
def __init__(self):
|
|
self.cursor_instance = _FakeCursor()
|
|
|
|
def cursor(self):
|
|
return self.cursor_instance
|
|
|
|
|
|
def test_get_scadas_normalizes_id_and_type():
|
|
conn = _FakeConnection()
|
|
|
|
result = asyncio.run(ScadaInfoRepository.get_scadas(conn))
|
|
|
|
assert result == [
|
|
{
|
|
"device_id": "25470001",
|
|
"device_type": "pressure",
|
|
"node_id": "J1",
|
|
"link_id": None,
|
|
"api_query_id": "query-1",
|
|
"transmission_mode": "realtime",
|
|
"transmission_frequency": None,
|
|
"reliability": 95,
|
|
"x": 117.1,
|
|
"y": 32.9,
|
|
}
|
|
]
|
|
assert "node_id" in conn.cursor_instance.query
|
|
assert "link_id" in conn.cursor_instance.query
|
|
assert "FROM gis.scada_devices" in conn.cursor_instance.query
|
|
|
|
|
|
def test_realtime_element_mappings_are_project_local_and_immutable(monkeypatch):
|
|
monkeypatch.setattr(
|
|
scada,
|
|
"read_all",
|
|
lambda *_args: [
|
|
{
|
|
"device_type": " PRESSURE ",
|
|
"element_id": " J1 ",
|
|
"api_query_id": " sensor-1 ",
|
|
}
|
|
],
|
|
)
|
|
|
|
mappings = scada.load_realtime_element_mappings("project-a")
|
|
|
|
assert mappings.pressure == {"J1": "sensor-1"}
|
|
assert isinstance(mappings.pressure, MappingProxyType)
|
|
with pytest.raises(TypeError):
|
|
mappings.pressure["J2"] = "sensor-2"
|