feat: add nowcast point alarm register client
Registers one point alarm per CSV row (recipientId, lat, lng, radius) with the shared filter JSON and the existing webhook bootstrap, records status, httpStatus, registrationId and errorBody back into the CSV and recovers ambiguous failures only on an identical recipient, point and radius match.
This commit is contained in:
@@ -1,14 +1,20 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import csv
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from common.security import TokenStore
|
||||
|
||||
from common import ApiError, ConfigurationError
|
||||
from common.client import ApiResponse
|
||||
from point_alarm import (
|
||||
PointRegistrationProcessor,
|
||||
PointStore,
|
||||
fetch_pages,
|
||||
flatten_registration,
|
||||
@@ -145,7 +151,7 @@ class PointAlarmTest(unittest.TestCase):
|
||||
return ApiResponse(200, registration(1))
|
||||
|
||||
def get_by_recipient_id(self, value):
|
||||
return ApiResponse(200, [registration(1), registration(2)])
|
||||
return ApiResponse(200, {"registrations": [registration(1), registration(2)]})
|
||||
|
||||
rows, _ = lookup_rows(Svc(), self.store, ["ok", "missing", "boom"], "id", None)
|
||||
self.assertEqual([r["status"] for r in rows], ["SUCCESS", "NOT_FOUND", "FAILED"])
|
||||
@@ -155,5 +161,87 @@ class PointAlarmTest(unittest.TestCase):
|
||||
self.assertEqual(self.store.count(), 2)
|
||||
|
||||
|
||||
REG_ID = "f7587d9e-2481-4b4c-818d-c8d1946851b7"
|
||||
|
||||
|
||||
class FakeRegisterService:
|
||||
def __init__(self, env_file: Path, results: list) -> None:
|
||||
self.client = type("C", (), {"token_store": TokenStore(env_file)})()
|
||||
self.results = list(results)
|
||||
self.payloads: list[dict] = []
|
||||
self.recipient_hits: list = []
|
||||
|
||||
def get_account_id(self):
|
||||
return "acc"
|
||||
|
||||
def register(self, payload, key):
|
||||
self.payloads.append(payload)
|
||||
result = self.results.pop(0)
|
||||
if isinstance(result, Exception):
|
||||
raise result
|
||||
return result
|
||||
|
||||
def get_by_recipient_id(self, recipient_id):
|
||||
return ApiResponse(200, {"registrations": self.recipient_hits})
|
||||
|
||||
|
||||
class RegisterTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.tmp.cleanup)
|
||||
self.dir = Path(self.tmp.name)
|
||||
(self.dir / ".env").write_text("", encoding="utf-8")
|
||||
self.filter = self.dir / "filter.json"
|
||||
self.filter.write_text(json.dumps({"lightning": {"type": "PULSE_IN_CLOUD"}}), encoding="utf-8")
|
||||
self.webhook = self.dir / "webhook.json"
|
||||
self.webhook.write_text("{}", encoding="utf-8")
|
||||
self.csv = self.dir / "r.csv"
|
||||
|
||||
def run_processor(self, rows: str | None, service) -> list[dict]:
|
||||
if rows is not None:
|
||||
self.csv.write_text("recipientId;lat;lng;radius\n" + rows, encoding="utf-8")
|
||||
processor = PointRegistrationProcessor(
|
||||
service, csv_path=self.csv, webhook_config_path=self.webhook, filter_config_path=self.filter
|
||||
)
|
||||
with patch.dict(os.environ, {"IKLIM_WEBHOOK_INITIALIZED": "true"}):
|
||||
processor.run()
|
||||
with self.csv.open(encoding="utf-8-sig", newline="") as handle:
|
||||
return list(csv.DictReader(handle, delimiter=";"))
|
||||
|
||||
def test_success_failure_and_skip(self) -> None:
|
||||
service = FakeRegisterService(
|
||||
self.dir / ".env",
|
||||
[
|
||||
ApiResponse(201, {"registrationId": REG_ID}),
|
||||
ApiError("bad", status_code=400, response_data={"message": "duplicate"}),
|
||||
],
|
||||
)
|
||||
rows = self.run_processor("a;39.9;32.8;1000\nb;39.9;32.8;1000\nc;99;32.8;1000\n", service)
|
||||
self.assertEqual([r["status"] for r in rows], ["SUCCESS", "FAILED", "FAILED"])
|
||||
self.assertEqual(rows[0]["registrationId"], REG_ID)
|
||||
self.assertIn("duplicate", rows[1]["errorBody"])
|
||||
self.assertEqual(rows[2]["httpStatus"], "NOT_SENT")
|
||||
self.assertEqual(service.payloads[0]["webhook"], {"accountId": "acc"})
|
||||
self.assertEqual(service.payloads[0]["filter"]["lightning"]["type"], "PULSE_IN_CLOUD")
|
||||
# Re-run: SUCCESS row is skipped, the failed one is retried.
|
||||
service2 = FakeRegisterService(
|
||||
self.dir / ".env", [ApiResponse(201, {"registrationId": REG_ID})] * 2
|
||||
)
|
||||
rows = self.run_processor(None, service2)
|
||||
self.assertEqual(len(service2.payloads), 1)
|
||||
self.assertEqual(rows[0]["registrationId"], REG_ID)
|
||||
|
||||
def test_recovery_requires_matching_point(self) -> None:
|
||||
service = FakeRegisterService(self.dir / ".env", [ApiError("t", status_code=503)])
|
||||
service.recipient_hits = [dict(registration(1, "a"), registrationId=REG_ID)]
|
||||
rows = self.run_processor("a;39.9;32.8;1000\n", service)
|
||||
self.assertEqual(rows[0]["status"], "SUCCESS")
|
||||
self.assertEqual(rows[0]["registrationId"], REG_ID)
|
||||
other = FakeRegisterService(self.dir / ".env", [ApiError("t", status_code=503)])
|
||||
other.recipient_hits = [dict(registration(1, "a"), registrationId=REG_ID)]
|
||||
rows = self.run_processor("a;40.0;32.8;1000\n", other)
|
||||
self.assertEqual(rows[0]["status"], "FAILED")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user