import hashlib import json import tempfile import unittest from datetime import datetime, timezone from pathlib import Path from urllib.parse import parse_qs from app.bot_tourism import BotTourismSource, TourismSourceError, parse_bot_tourism_report FIXTURES = Path(__file__).parent / "fixtures" REPORT_HTML = (FIXTURES / "bot_tourism_report.html").read_text(encoding="utf-8") FORM_HTML = (FIXTURES / "bot_tourism_form.html").read_text(encoding="utf-8") class FakeResponse: def __init__(self, body): self.body = body.encode("utf-8") self.status = 200 def read(self): return self.body def __enter__(self): return self def __exit__(self, *_args): return False class FakeOpener: def __init__(self): self.calls = [] def open(self, request, timeout): body = FORM_HTML if request.data is None else REPORT_HTML self.calls.append({"url": request.full_url, "body": request.data, "timeout": timeout}) return FakeResponse(body) class BotTourismSourceTests(unittest.TestCase): def test_default_source_opener_is_constructible(self): source = BotTourismSource() self.assertIsNotNone(source.opener) def test_parser_builds_point_in_time_snapshot_from_bot_table(self): raw_hash = hashlib.sha256(REPORT_HTML.encode("utf-8")).hexdigest() snapshot = parse_bot_tourism_report( REPORT_HTML, retrieved_at="2026-08-23T02:00:00+00:00", raw_payload_hash=raw_hash, ) self.assertEqual(snapshot["as_of"], "2026-06-30") self.assertEqual(snapshot["data_quality"], "provisional") self.assertEqual(snapshot["source"]["source_id"], "bot.ec_ei_028_s2") self.assertEqual(snapshot["source"]["published_at"], "2026-07-31T14:30:00+07:00") self.assertEqual(snapshot["source"]["raw_payload_hash"], raw_hash) observation = snapshot["observations"][0] self.assertEqual(observation["metric_key"], "foreign_arrivals_yoy") self.assertEqual(observation["period"], "2026-06") self.assertLess(observation["value"], 0) self.assertTrue(observation["scale"] > 0) self.assertEqual(observation["history_points"], 12) def test_parser_rejects_report_without_tourism_row(self): with self.assertRaisesRegex(TourismSourceError, "tourism table"): parse_bot_tourism_report("
| wrong |
|---|