feat: add project-scoped operations
This commit is contained in:
@@ -49,6 +49,32 @@ class _FakeConnection:
|
||||
return self.cursor_instance
|
||||
|
||||
|
||||
class _ReadingCursor:
|
||||
def __init__(self, rows):
|
||||
self.rows = rows
|
||||
self.calls: list[tuple[str, tuple]] = []
|
||||
|
||||
async def __aenter__(self):
|
||||
return self
|
||||
|
||||
async def __aexit__(self, exc_type, exc, tb):
|
||||
return False
|
||||
|
||||
async def execute(self, query, params):
|
||||
self.calls.append((str(query), params))
|
||||
|
||||
async def fetchall(self):
|
||||
return self.rows
|
||||
|
||||
|
||||
class _ReadingConnection:
|
||||
def __init__(self, rows):
|
||||
self.cursor_instance = _ReadingCursor(rows)
|
||||
|
||||
def cursor(self):
|
||||
return self.cursor_instance
|
||||
|
||||
|
||||
def test_update_scada_field_inserts_when_update_hits_no_rows():
|
||||
ScadaRepository = _load_scada_repository()
|
||||
conn = _FakeConnection(initial_rowcount=0)
|
||||
@@ -114,3 +140,35 @@ def test_update_scada_field_batch_uses_one_set_based_statement():
|
||||
["170490", "170491"],
|
||||
[26.5, 27.0],
|
||||
)
|
||||
|
||||
|
||||
def test_get_latest_scada_readings_uses_canonical_ids_and_one_bounded_query():
|
||||
ScadaRepository = _load_scada_repository()
|
||||
observed_at = datetime(2026, 10, 22, 1, 2, tzinfo=timezone.utc)
|
||||
conn = _ReadingConnection([
|
||||
{
|
||||
"time": observed_at,
|
||||
"device_id": "D-1",
|
||||
"monitored_value": 0.31,
|
||||
"cleaned_value": 0.3,
|
||||
}
|
||||
])
|
||||
|
||||
result = asyncio.run(
|
||||
ScadaRepository.get_latest_scada_readings(conn, ["D-1", "D-2"])
|
||||
)
|
||||
|
||||
assert result["D-1"]["time"] == observed_at
|
||||
assert len(conn.cursor_instance.calls) == 1
|
||||
query, params = conn.cursor_instance.calls[0]
|
||||
assert "DISTINCT ON (device_id)" in query
|
||||
assert "ORDER BY device_id, time DESC" in query
|
||||
assert params == (["D-1", "D-2"],)
|
||||
|
||||
|
||||
def test_get_latest_scada_readings_skips_database_for_empty_page():
|
||||
ScadaRepository = _load_scada_repository()
|
||||
conn = _ReadingConnection([])
|
||||
|
||||
assert asyncio.run(ScadaRepository.get_latest_scada_readings(conn, [])) == {}
|
||||
assert conn.cursor_instance.calls == []
|
||||
|
||||
Reference in New Issue
Block a user