280 lines
14 KiB
Python
280 lines
14 KiB
Python
"""History fidelity, stable deletion, database failures and migration safeguards."""
|
|
|
|
import asyncio
|
|
import copy
|
|
import datetime
|
|
import io
|
|
import json
|
|
import os
|
|
import tempfile
|
|
import threading
|
|
import unittest
|
|
from contextlib import redirect_stderr, redirect_stdout
|
|
from pathlib import Path
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import main
|
|
import history_migrate as migration
|
|
import history_store
|
|
from history_store import PostgresHistoryStore, merge_sheet_and_local, normalize_record, validate_bundle
|
|
from settings_store import SettingsAlreadyInitialized
|
|
from test_google_resilience import request
|
|
|
|
|
|
READ_SOURCE = migration.read_source
|
|
STAMP = "2026-10-22 11:22:33"
|
|
SMS = {"timestamp": STAMP, "orderType": "normal", "deposit_amount_str": "3,500원",
|
|
"custName": "테스트 고객", "custPhone": "010-0000-0000", "custAddress": "테스트 주소",
|
|
"items": [{"label": "상품", "code": "TEST-1", "qty": 2}],
|
|
"smsOpts": {"discount": True, "pointDeduction": 500}, "previewText": "첫줄\n둘째줄"}
|
|
SMS_ROWS = [["일시", "주문유형", "입금액", "주문아이템", "메시지", "이름", "연락처", "주소"],
|
|
[STAMP, "normal", "3,500원", "상품[TEST-1] 2개", "첫줄 둘째줄", "테스트 고객", "010-0000-0000", "테스트 주소"]]
|
|
MANUAL = {"timestamp": STAMP, "customer_name": "테스트 고객", "total_amount": 3500}
|
|
BUNDLE = {"kind": "corm-history", "sms": [SMS], "manual": [MANUAL]}
|
|
|
|
|
|
class MergeTests(unittest.TestCase):
|
|
def test_sheet_and_local_details_are_merged_without_duplicates(self):
|
|
records = merge_sheet_and_local("sms", SMS_ROWS, [SMS])
|
|
self.assertEqual(len(records), 1)
|
|
self.assertEqual(records[0]["items"], SMS["items"])
|
|
self.assertEqual(records[0]["smsOpts"], SMS["smsOpts"])
|
|
self.assertEqual(records[0]["custAddress"], SMS["custAddress"])
|
|
self.assertEqual(records[0]["previewText"], SMS["previewText"])
|
|
self.assertEqual(records[0]["_legacy_sheet_row"], SMS_ROWS[1])
|
|
|
|
def test_duplicate_timestamps_and_local_only_records_are_preserved(self):
|
|
local_only = {**SMS, "timestamp": "2026-10-22 11:22:34"}
|
|
rows = SMS_ROWS + [SMS_ROWS[1].copy()]
|
|
records = merge_sheet_and_local("sms", rows, [SMS, copy.deepcopy(SMS), local_only])
|
|
self.assertEqual(len(records), 3)
|
|
self.assertEqual([r["timestamp"] for r in records], [STAMP, STAMP, local_only["timestamp"]])
|
|
|
|
def test_distinct_orders_at_same_time_are_not_collapsed(self):
|
|
other = {**SMS, "custName": "다른 고객"}
|
|
records = merge_sheet_and_local("sms", SMS_ROWS, [other, SMS])
|
|
self.assertEqual(len(records), 2)
|
|
self.assertEqual([r["custName"] for r in records], ["테스트 고객", "다른 고객"])
|
|
|
|
def test_manual_historical_rows_and_text_amounts_are_kept(self):
|
|
rows = [["일시", "이름", "금액"], ["2020-01-01 12:00:00", "과거", "누락"],
|
|
[STAMP, "테스트 고객", "3,500", "추가 열"]]
|
|
records = merge_sheet_and_local("manual", rows, [MANUAL])
|
|
self.assertEqual(len(records), 2)
|
|
self.assertEqual(records[0]["total_amount"], "누락")
|
|
self.assertEqual(records[1]["total_amount"], 3500)
|
|
self.assertEqual(records[1]["_legacy_sheet_row"][-1], "추가 열")
|
|
|
|
def test_invalid_records_fail_instead_of_disappearing(self):
|
|
for value in ({}, {"timestamp": ""}, [], {"timestamp": STAMP, "amount": float("nan")}):
|
|
with self.subTest(value=value), self.assertRaises(ValueError):
|
|
normalize_record("manual", value)
|
|
with self.assertRaises(ValueError):
|
|
validate_bundle({"kind": "settings", "sms": [], "manual": []})
|
|
|
|
|
|
class HistoryApiTests(unittest.IsolatedAsyncioTestCase):
|
|
def setUp(self):
|
|
self.store = MagicMock()
|
|
self.store.list_records.return_value = [{**SMS, "id": 42}]
|
|
self.store.add.return_value = 42
|
|
self.store.delete.return_value = True
|
|
self.store.get_manual.return_value = MANUAL.copy()
|
|
self.store.manual_timestamp_count.return_value = 1
|
|
for name, value in (("HISTORY_BACKEND", "postgres"), ("_postgres_history", self.store)):
|
|
mocked = patch.object(main, name, value)
|
|
mocked.start()
|
|
self.addCleanup(mocked.stop)
|
|
self.google_mock = patch.object(main, "get_sms_history_worksheet", side_effect=AssertionError("Unexpected sheet history I/O"))
|
|
self.google_mock.start()
|
|
self.addCleanup(self.google_mock.stop)
|
|
self.local_file = patch("builtins.open", side_effect=AssertionError("Unexpected local history I/O"))
|
|
self.local_file.start()
|
|
self.addCleanup(self.local_file.stop)
|
|
|
|
async def test_db_list_and_save_keep_complete_sms_payload_without_google_or_files(self):
|
|
response = await request("POST", "/api/sms/history", SMS)
|
|
self.assertEqual(response[0], 200)
|
|
self.assertEqual(json.loads(response[2])["id"], 42)
|
|
self.store.add.assert_called_once_with("sms", SMS)
|
|
status, _, body = await request("GET", "/api/sms/history")
|
|
self.assertEqual(status, 200)
|
|
self.assertEqual(json.loads(body)[0]["smsOpts"], SMS["smsOpts"])
|
|
|
|
async def test_sms_deletion_uses_id_and_rejects_old_index_route(self):
|
|
self.assertEqual((await request("DELETE", "/api/sms/history/records/42"))[0], 200)
|
|
self.store.delete.assert_called_once_with("sms", 42)
|
|
self.assertEqual((await request("DELETE", "/api/sms/history/0"))[0], 409)
|
|
self.store.delete.assert_called_once()
|
|
self.assertEqual((await request("DELETE", "/api/sms/history"))[0], 200)
|
|
self.store.clear_sms.assert_called_once()
|
|
|
|
async def test_db_errors_are_failures_and_do_not_report_saved_or_fall_back(self):
|
|
self.store.add.side_effect = RuntimeError("password=do-not-print")
|
|
response = await request("POST", "/api/sms/history", SMS)
|
|
self.assertEqual(response[0], 503)
|
|
self.assertNotIn("do-not-print", response[2].decode())
|
|
self.store.list_records.side_effect = RuntimeError()
|
|
self.assertEqual((await request("GET", "/api/sms/history"))[0], 503)
|
|
|
|
async def test_manual_today_display_uses_korean_date_but_save_keeps_payload(self):
|
|
self.assertEqual((await request("POST", "/api/manual/history", MANUAL))[0], 200)
|
|
self.assertEqual((await request("GET", "/api/manual/history"))[0], 200)
|
|
day = self.store.list_records.call_args.kwargs["day"]
|
|
self.assertEqual(day, datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=9))).strftime("%Y-%m-%d"))
|
|
|
|
async def test_google_saturation_does_not_block_history_reads_or_adds(self):
|
|
with patch.object(main, "run_google_io", side_effect=AssertionError("Google saturated")):
|
|
for method, url, payload in (("GET", "/api/sms/history", None),
|
|
("POST", "/api/manual/history", MANUAL),
|
|
("GET", "/api/manual/history", None)):
|
|
self.assertEqual((await request(method, url, payload))[0], 200)
|
|
|
|
async def test_manual_delete_keeps_history_if_active_order_deletion_fails(self):
|
|
with patch.object(main, "delete_manual_order_rows", side_effect=RuntimeError("google unavailable")):
|
|
self.assertEqual((await request("DELETE", "/api/manual/history/records/42"))[0], 503)
|
|
self.store.delete.assert_not_called()
|
|
|
|
async def test_manual_delete_removes_active_order_before_database_history(self):
|
|
with patch.object(main, "delete_manual_order_rows") as remove:
|
|
self.assertEqual((await request("DELETE", "/api/manual/history/records/42"))[0], 200)
|
|
remove.assert_called_once_with(STAMP)
|
|
self.store.delete.assert_called_once_with("manual", 42)
|
|
|
|
async def test_slow_order_cancellation_does_not_block_db_history(self):
|
|
started, release = threading.Event(), threading.Event()
|
|
|
|
def stalled(timestamp):
|
|
started.set()
|
|
release.wait(3)
|
|
|
|
with patch.object(main, "delete_manual_order_rows", side_effect=stalled):
|
|
deletion = asyncio.create_task(request("DELETE", "/api/manual/history/records/42"))
|
|
try:
|
|
async with asyncio.timeout(2):
|
|
while not started.is_set():
|
|
await asyncio.sleep(0.005)
|
|
async with asyncio.timeout(1):
|
|
self.assertEqual((await request("GET", "/api/sms/history"))[0], 200)
|
|
self.assertEqual((await request("POST", "/api/sms/history", SMS))[0], 200)
|
|
finally:
|
|
release.set()
|
|
self.assertEqual((await deletion)[0], 200)
|
|
|
|
async def test_ambiguous_manual_timestamp_does_not_delete_any_order(self):
|
|
self.store.manual_timestamp_count.return_value = 2
|
|
with patch.object(main, "delete_manual_order_rows") as remove:
|
|
self.assertEqual((await request("DELETE", "/api/manual/history/records/42"))[0], 409)
|
|
remove.assert_not_called()
|
|
self.store.delete.assert_not_called()
|
|
|
|
async def test_startup_requires_history_migration_and_checks_corm_database(self):
|
|
with patch.dict(os.environ, {"SKIP_DB_INIT": "1"}), \
|
|
patch.object(main, "SETTINGS_BACKEND", "postgres"), \
|
|
patch.object(main._postgres_settings, "read"):
|
|
await main.startup_event()
|
|
self.store.check_ready.assert_called_once()
|
|
self.store.check_ready.side_effect = RuntimeError("missing history tables")
|
|
with self.assertRaises(RuntimeError):
|
|
await main.startup_event()
|
|
|
|
|
|
class StoreTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.store = PostgresHistoryStore({})
|
|
self.connection = MagicMock()
|
|
self.cursor = self.connection.cursor.return_value.__enter__.return_value
|
|
connect = patch.object(self.store, "_connect", return_value=self.connection)
|
|
connect.start()
|
|
self.addCleanup(connect.stop)
|
|
|
|
def test_record_id_cannot_be_spoofed_in_saved_payload(self):
|
|
self.cursor.fetchone.return_value = (42,)
|
|
self.store.add("sms", {**SMS, "id": 999})
|
|
parameters = self.cursor.execute.call_args.args[1]
|
|
self.assertNotIn("id", parameters[1].adapted)
|
|
self.assertEqual(parameters[1].adapted["smsOpts"], SMS["smsOpts"])
|
|
|
|
def test_import_is_atomic_and_rejects_existing_data(self):
|
|
self.cursor.fetchone.return_value = (True,)
|
|
with self.assertRaises(SettingsAlreadyInitialized):
|
|
self.store.import_all(BUNDLE, "test")
|
|
self.assertIs(self.connection.__exit__.call_args.args[0], SettingsAlreadyInitialized)
|
|
|
|
def test_readback_failure_aborts_both_history_imports(self):
|
|
self.cursor.fetchone.side_effect = [(False,), (False,), (False,)]
|
|
self.cursor.fetchall.return_value = []
|
|
with patch.object(history_store, "execute_values"), self.assertRaises(RuntimeError):
|
|
self.store.import_all(BUNDLE, "test")
|
|
self.assertIs(self.connection.__exit__.call_args.args[0], RuntimeError)
|
|
|
|
def test_active_order_row_deletion_never_deletes_intervening_orders(self):
|
|
worksheet = MagicMock(id=5)
|
|
worksheet.get_all_values.return_value = [["일시"], [STAMP], ["other"], [STAMP]]
|
|
with patch.object(main, "get_manual_worksheet", return_value=worksheet):
|
|
main.delete_manual_order_rows(STAMP)
|
|
requests = worksheet.spreadsheet.batch_update.call_args.args[0]["requests"]
|
|
self.assertEqual([r["deleteDimension"]["range"]["startIndex"] for r in requests], [3, 1])
|
|
|
|
|
|
class MigrationTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.directory = tempfile.TemporaryDirectory()
|
|
self.addCleanup(self.directory.cleanup)
|
|
self.backup = Path(self.directory.name) / "histories.json"
|
|
self.store = MagicMock()
|
|
self.store.export_all.return_value = copy.deepcopy(BUNDLE)
|
|
for name, value in (("read_source", (copy.deepcopy(BUNDLE), "test")), ("build_store", self.store)):
|
|
mocked = patch.object(migration, name, return_value=value)
|
|
mocked.start()
|
|
self.addCleanup(mocked.stop)
|
|
self.output = io.StringIO()
|
|
|
|
def run_cli(self, args):
|
|
with redirect_stdout(self.output), redirect_stderr(self.output):
|
|
return migration.main(args)
|
|
|
|
def test_dry_run_does_not_connect_to_database(self):
|
|
self.assertEqual(self.run_cli(["import"]), 0)
|
|
migration.build_store.assert_not_called()
|
|
|
|
def test_source_snapshot_preserves_original_and_never_accesses_db(self):
|
|
self.assertEqual(self.run_cli(["snapshot", "--output", str(self.backup)]), 0)
|
|
self.assertEqual(migration.load_backup(self.backup)[0], BUNDLE)
|
|
migration.build_store.assert_not_called()
|
|
|
|
def test_missing_explicit_local_file_fails_before_google_read(self):
|
|
with patch.object(main, "get_gspread_client") as google:
|
|
with self.assertRaises(FileNotFoundError):
|
|
# Call the real source reader, bypassing this class's read_source mock.
|
|
READ_SOURCE(str(self.backup), None)
|
|
google.assert_not_called()
|
|
|
|
def test_apply_saves_backup_before_any_database_write(self):
|
|
def imported(bundle, source):
|
|
self.assertEqual(migration.load_backup(self.backup)[0], bundle)
|
|
self.store.import_all.side_effect = imported
|
|
self.assertEqual(self.run_cli(["import", "--apply", "--backup", str(self.backup)]), 0)
|
|
self.assertEqual(self.run_cli(["verify", "--input-file", str(self.backup)]), 0)
|
|
|
|
def test_verify_detects_data_loss_and_export_never_overwrites(self):
|
|
self.store.export_all.return_value = {**BUNDLE, "sms": []}
|
|
self.assertEqual(self.run_cli(["verify"]), 1)
|
|
self.assertEqual(self.run_cli(["export", "--output", str(self.backup)]), 0)
|
|
self.assertEqual(self.run_cli(["export", "--output", str(self.backup)]), 1)
|
|
|
|
def test_corrupt_local_record_or_backup_is_not_silently_skipped(self):
|
|
self.backup.write_text("not-json\n", encoding="utf-8")
|
|
with self.assertRaises(ValueError):
|
|
migration.read_local_records(self.backup)
|
|
migration.save_snapshot(self.backup.with_suffix(".other"), BUNDLE, "test")
|
|
payload = json.loads(self.backup.with_suffix(".other").read_text(encoding="utf-8"))
|
|
payload["rows"]["sms"] = []
|
|
self.backup.write_text(json.dumps(payload), encoding="utf-8")
|
|
with self.assertRaises(ValueError):
|
|
migration.load_backup(self.backup)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|