Move CORM settings and histories to dedicated PostgreSQL database
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
"""Validate operator deployment decisions without production access."""
|
||||
|
||||
import io
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from contextlib import redirect_stderr, redirect_stdout
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import deploy_corm as deployment
|
||||
from history_store import normalize_record
|
||||
from settings_store import HEADER, SettingsNotInitialized
|
||||
|
||||
|
||||
ROWS = [HEADER, ["APP_SETTINGS", "shipping_fee", "3500"]]
|
||||
HISTORY = {"kind": "corm-history", "sms": [], "manual": []}
|
||||
|
||||
|
||||
class DeploymentTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.output = io.StringIO()
|
||||
|
||||
def test_environment_switch_preserves_existing_secret_and_database_keys(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
path = Path(directory) / ".env"
|
||||
path.write_text("POSTGRES_PASSWORD='keep-$-secret'\nITEMCODE_DB=itemcode_db\n# preserve comment\n", encoding="utf-8")
|
||||
with patch.object(os, "chown", create=True), redirect_stdout(self.output):
|
||||
deployment.switch_environment(path)
|
||||
text = path.read_text(encoding="utf-8")
|
||||
self.assertIn("POSTGRES_PASSWORD='keep-$-secret'", text)
|
||||
self.assertIn("ITEMCODE_DB=itemcode_db", text)
|
||||
self.assertIn("# preserve comment", text)
|
||||
self.assertIn("SETTINGS_DB=corm_db", text)
|
||||
self.assertIn("HISTORY_BACKEND=postgres", text)
|
||||
|
||||
def test_existing_settings_difference_is_never_overwritten(self):
|
||||
store = MagicMock()
|
||||
store.read.return_value = ([HEADER, ["APP_SETTINGS", "shipping_fee", "9999"]], 2)
|
||||
with patch.object(deployment, "load_snapshot", return_value=(ROWS, "test")), \
|
||||
patch.object(deployment, "load_backup", return_value=(HISTORY, "test")), \
|
||||
patch.object(deployment, "PostgresSettingsStore", return_value=store), \
|
||||
self.assertRaises(RuntimeError):
|
||||
deployment.migrate("unused")
|
||||
store.import_rows.assert_not_called()
|
||||
|
||||
def test_incomplete_import_can_resume_without_reimporting_settings(self):
|
||||
settings = MagicMock()
|
||||
settings.read.return_value = (ROWS, 1)
|
||||
histories = MagicMock()
|
||||
histories.check_ready.side_effect = SettingsNotInitialized()
|
||||
histories.export_all.return_value = HISTORY
|
||||
with patch.object(deployment, "load_snapshot", return_value=(ROWS, "test")), \
|
||||
patch.object(deployment, "load_backup", return_value=(HISTORY, "test")), \
|
||||
patch.object(deployment, "PostgresSettingsStore", return_value=settings), \
|
||||
patch.object(deployment, "PostgresHistoryStore", return_value=histories), redirect_stdout(self.output):
|
||||
deployment.migrate("unused")
|
||||
settings.import_rows.assert_not_called()
|
||||
histories.import_all.assert_called_once_with(HISTORY, "test")
|
||||
|
||||
def test_snapshot_uses_existing_database_when_already_converted(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
settings = MagicMock()
|
||||
settings.read.return_value = (ROWS, 1)
|
||||
histories = MagicMock()
|
||||
histories.export_all.return_value = HISTORY
|
||||
with patch.object(deployment, "PostgresSettingsStore", return_value=settings), \
|
||||
patch.object(deployment, "PostgresHistoryStore", return_value=histories), \
|
||||
patch.object(deployment, "read_google_rows") as google, \
|
||||
patch.object(os, "chown", create=True), redirect_stdout(self.output):
|
||||
deployment.snapshot(directory, "postgres", "postgres")
|
||||
google.assert_not_called()
|
||||
self.assertTrue((Path(directory) / "settings-source.json").exists())
|
||||
self.assertTrue((Path(directory) / "history-source.json").exists())
|
||||
|
||||
def test_database_creation_does_not_modify_an_existing_database(self):
|
||||
connection = MagicMock()
|
||||
cursor = connection.cursor.return_value.__enter__.return_value
|
||||
cursor.fetchone.return_value = (1,)
|
||||
with patch.object(deployment, "settings_db_config", return_value={"dbname": "corm_db", "user": "app"}), \
|
||||
patch.object(deployment.psycopg2, "connect", return_value=connection), redirect_stdout(self.output):
|
||||
deployment.ensure_database()
|
||||
cursor.execute.assert_called_once()
|
||||
|
||||
def test_deployment_error_output_does_not_reveal_credentials(self):
|
||||
with patch.object(deployment, "ensure_database", side_effect=RuntimeError("password=do-not-print")), \
|
||||
redirect_stderr(self.output):
|
||||
self.assertEqual(deployment.main(["database"]), 1)
|
||||
self.assertNotIn("do-not-print", self.output.getvalue())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -165,6 +165,13 @@ class TimeoutTests(unittest.TestCase):
|
||||
|
||||
class ApiTests(unittest.IsolatedAsyncioTestCase):
|
||||
def setUp(self):
|
||||
# These tests exercise the legacy Google backend regardless of local .env.
|
||||
backend = patch.object(main, "SETTINGS_BACKEND", "google")
|
||||
backend.start()
|
||||
self.addCleanup(backend.stop)
|
||||
histories = patch.object(main, "HISTORY_BACKEND", "google")
|
||||
histories.start()
|
||||
self.addCleanup(histories.stop)
|
||||
self.directory = tempfile.TemporaryDirectory()
|
||||
self.cache = runtime.SettingsCache(Path(self.directory.name) / "settings.json", "test")
|
||||
self.cache_patch = patch.object(main, "_settings_cache", self.cache)
|
||||
|
||||
@@ -0,0 +1,279 @@
|
||||
"""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()
|
||||
@@ -0,0 +1,254 @@
|
||||
"""Settings compatibility, isolation from Google, migration and write conflicts."""
|
||||
|
||||
import copy
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
from contextlib import redirect_stderr, redirect_stdout
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import psycopg2
|
||||
|
||||
import main
|
||||
import settings_migrate as migration
|
||||
from settings_store import (
|
||||
HEADER, PostgresSettingsStore, SettingsAlreadyInitialized, SettingsConflict,
|
||||
SettingsNotInitialized, config_to_rows, rows_to_config, settings_db_config, validate_rows,
|
||||
)
|
||||
from test_google_resilience import request
|
||||
|
||||
|
||||
ROWS = [
|
||||
HEADER.copy(),
|
||||
["APP_SETTINGS", "shipping_fee", "3500"],
|
||||
["GROUP_NAMES", "ORDER_SINGLE", "단품"],
|
||||
["ORDER_SINGLE", "상품 B", "B-2", "상품 B", "2000", "1500"],
|
||||
["ORDER_SINGLE", "---separator_1---"],
|
||||
["ORDER_SINGLE", "상품 A", "A-1", "상품 A", "1000"],
|
||||
["SMS_GIFT_RULES", "header_text", "안녕하세요\n주문 안내 😊"],
|
||||
["EMPTY", "", ""],
|
||||
]
|
||||
|
||||
|
||||
class CodecTests(unittest.TestCase):
|
||||
def test_order_separators_empty_sections_and_multiline_survive_roundtrip(self):
|
||||
restored = rows_to_config(config_to_rows(rows_to_config(ROWS)))
|
||||
self.assertEqual({s: dict(restored.items(s)) for s in restored.sections()},
|
||||
{s: dict(rows_to_config(ROWS).items(s)) for s in restored.sections()})
|
||||
self.assertEqual(list(rows_to_config(ROWS)["ORDER_SINGLE"]),
|
||||
["상품 B", "---separator_1---", "상품 A"])
|
||||
|
||||
def test_invalid_or_empty_imports_are_rejected(self):
|
||||
for rows in ([], [HEADER], [[123]], [["APP_SETTINGS", "key", "a\x00b"]], {"a": 1}):
|
||||
with self.subTest(rows=rows), self.assertRaises(ValueError):
|
||||
validate_rows(rows)
|
||||
|
||||
|
||||
class DatabaseRoutingTests(unittest.TestCase):
|
||||
def test_settings_default_is_independent_of_itemcode_db(self):
|
||||
with patch.dict(os.environ, {"ITEMCODE_DB": "existing_codes"}, clear=True):
|
||||
self.assertEqual(settings_db_config()["dbname"], "corm_db")
|
||||
self.assertEqual(migration.build_store().db_config["dbname"], "corm_db")
|
||||
|
||||
def test_explicit_settings_db_uses_existing_server_credentials(self):
|
||||
env = {"ITEMCODE_DB": "existing_codes", "SETTINGS_DB": "custom_corm",
|
||||
"POSTGRES_HOST": "shared-pg", "POSTGRES_PORT": "5433",
|
||||
"POSTGRES_USER": "app-user", "POSTGRES_PASSWORD": "test-only"}
|
||||
with patch.dict(os.environ, env, clear=True):
|
||||
config = settings_db_config()
|
||||
self.assertEqual(config, {"host": "shared-pg", "port": 5433, "dbname": "custom_corm",
|
||||
"user": "app-user", "password": "test-only"})
|
||||
self.assertEqual(migration.build_store().db_config, config)
|
||||
|
||||
def test_application_settings_store_has_separate_config(self):
|
||||
self.assertIsNot(main.SETTINGS_DB_CONFIG, main.DB_CONFIG)
|
||||
self.assertEqual(main._postgres_settings.db_config, main.SETTINGS_DB_CONFIG)
|
||||
|
||||
|
||||
class StoreTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.store = PostgresSettingsStore({"host": "test-db", "password": "test-only"})
|
||||
self.connection = MagicMock()
|
||||
self.cursor = self.connection.cursor.return_value.__enter__.return_value
|
||||
self.connect = patch.object(self.store, "_connect", return_value=self.connection)
|
||||
self.connect.start()
|
||||
self.addCleanup(self.connect.stop)
|
||||
|
||||
def test_read_closes_connection_and_preserves_version(self):
|
||||
self.cursor.fetchone.return_value = (copy.deepcopy(ROWS), 7)
|
||||
self.assertEqual(self.store.read(), (ROWS, 7))
|
||||
self.connection.close.assert_called_once()
|
||||
|
||||
def test_missing_data_or_table_does_not_return_empty_settings(self):
|
||||
self.cursor.fetchone.return_value = None
|
||||
with self.assertRaises(SettingsNotInitialized):
|
||||
self.store.read()
|
||||
self.cursor.execute.side_effect = psycopg2.errors.UndefinedTable()
|
||||
with self.assertRaises(SettingsNotInitialized):
|
||||
self.store.read()
|
||||
|
||||
def test_write_uses_expected_version_and_parameterized_values(self):
|
||||
self.cursor.fetchone.return_value = (8,)
|
||||
self.assertEqual(self.store.write(ROWS, 7), 8)
|
||||
query, args = self.cursor.execute.call_args.args
|
||||
self.assertIn("AND version = %s", query)
|
||||
self.assertEqual(args[1], 7)
|
||||
self.assertEqual(args[0].adapted, ROWS)
|
||||
self.connection.__exit__.assert_called_once_with(None, None, None)
|
||||
|
||||
def test_conflict_aborts_transaction(self):
|
||||
self.cursor.fetchone.return_value = None
|
||||
with self.assertRaises(SettingsConflict):
|
||||
self.store.write(ROWS, 7)
|
||||
self.assertIs(self.connection.__exit__.call_args.args[0], SettingsConflict)
|
||||
|
||||
def test_import_never_overwrites_an_existing_document(self):
|
||||
self.cursor.fetchone.return_value = None
|
||||
with self.assertRaises(SettingsAlreadyInitialized):
|
||||
self.store.import_rows(ROWS, "test")
|
||||
self.assertIn("ON CONFLICT (id) DO NOTHING", self.cursor.execute.call_args.args[0])
|
||||
self.assertIs(self.connection.__exit__.call_args.args[0], SettingsAlreadyInitialized)
|
||||
|
||||
def test_import_readback_mismatch_aborts_before_commit(self):
|
||||
self.cursor.fetchone.side_effect = [(1,), ([HEADER],)]
|
||||
with self.assertRaises(RuntimeError):
|
||||
self.store.import_rows(ROWS, "test")
|
||||
self.assertIs(self.connection.__exit__.call_args.args[0], RuntimeError)
|
||||
|
||||
|
||||
class PostgresApiTests(unittest.IsolatedAsyncioTestCase):
|
||||
def setUp(self):
|
||||
histories = patch.object(main, "HISTORY_BACKEND", "google")
|
||||
histories.start()
|
||||
self.addCleanup(histories.stop)
|
||||
self.backend = patch.object(main, "SETTINGS_BACKEND", "postgres")
|
||||
self.backend.start()
|
||||
self.addCleanup(self.backend.stop)
|
||||
self.store = MagicMock()
|
||||
self.store.read.return_value = (copy.deepcopy(ROWS), 7)
|
||||
self.store.write.return_value = 8
|
||||
self.store_patch = patch.object(main, "_postgres_settings", self.store)
|
||||
self.store_patch.start()
|
||||
self.addCleanup(self.store_patch.stop)
|
||||
self.google = patch.object(main, "get_settings_worksheet", side_effect=AssertionError("Unexpected Google I/O"))
|
||||
self.google_mock = self.google.start()
|
||||
self.addCleanup(self.google.stop)
|
||||
|
||||
async def test_config_reads_db_and_preserves_existing_api_order(self):
|
||||
status, headers, body = await request("GET", "/api/config")
|
||||
self.assertEqual(status, 200)
|
||||
data = json.loads(body)
|
||||
self.assertEqual(data["ORDER_SINGLE"]["__order"], ["상품 B", "---separator_1---", "상품 A"])
|
||||
self.assertEqual(data["SMS_GIFT_RULES"]["header_text"], "안녕하세요\n주문 안내 😊")
|
||||
self.assertEqual(headers[b"x-settings-backend"], b"postgres")
|
||||
|
||||
async def test_all_setting_endpoints_write_db_without_google(self):
|
||||
payloads = [
|
||||
("/api/app/settings", {"shipping_fee": "4000"}),
|
||||
("/api/gift/settings", {"rules": {"header_text": "새 문구"}, "app_settings": {"shipping_fee": "4500"}}),
|
||||
("/api/products/subgroup-name", {"group_key": "ORDER_SINGLE", "subgroup_index": 0, "name": "분류"}),
|
||||
("/api/products/settings", {
|
||||
"groups": [{"old_code": "ORDER_SINGLE", "new_code": "ORDER_SINGLE", "name": "단품"}],
|
||||
"products": {"ORDER_SINGLE": [{"label": "상품", "code": "NEW", "name": "새 상품", "price": 5000}]},
|
||||
}),
|
||||
]
|
||||
for url, payload in payloads:
|
||||
with self.subTest(url=url):
|
||||
self.assertEqual((await request("POST", url, payload))[0], 200)
|
||||
self.assertEqual(self.store.write.call_args.args[1], 7)
|
||||
self.google_mock.assert_not_called()
|
||||
|
||||
async def test_conflict_returns_409_and_db_failure_never_falls_back_to_google(self):
|
||||
self.store.write.side_effect = SettingsConflict("다른 요청이 변경했습니다.")
|
||||
self.assertEqual((await request("POST", "/api/app/settings", {"shipping_fee": "4000"}))[0], 409)
|
||||
self.store.read.side_effect = RuntimeError("database unavailable")
|
||||
self.assertEqual((await request("GET", "/api/config"))[0], 503)
|
||||
self.google_mock.assert_not_called()
|
||||
|
||||
async def test_google_saturation_does_not_block_db_settings_reads_or_saves(self):
|
||||
with patch.object(main, "run_google_io", side_effect=AssertionError("Google pool unavailable")):
|
||||
self.assertEqual((await request("GET", "/api/config"))[0], 200)
|
||||
self.assertEqual((await request("POST", "/api/app/settings", {"font": "Arial"}))[0], 200)
|
||||
|
||||
async def test_postgres_startup_does_not_refresh_google(self):
|
||||
with patch.dict(os.environ, {"SKIP_DB_INIT": "1"}), \
|
||||
patch.object(main._settings_cache, "refresh_in_background") as refresh:
|
||||
await main.startup_event()
|
||||
refresh.assert_not_called()
|
||||
self.store.read.assert_called_once()
|
||||
|
||||
async def test_startup_rejects_incomplete_migration(self):
|
||||
self.store.read.side_effect = SettingsNotInitialized("missing")
|
||||
with patch.dict(os.environ, {"SKIP_DB_INIT": "1"}), self.assertRaises(SettingsNotInitialized):
|
||||
await main.startup_event()
|
||||
|
||||
|
||||
class MigrationTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.directory = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.directory.cleanup)
|
||||
self.backup = Path(self.directory.name) / "settings.json"
|
||||
self.store = MagicMock()
|
||||
self.store.read.return_value = (copy.deepcopy(ROWS), 1)
|
||||
self.store.import_rows.return_value = 1
|
||||
for name, value in (("read_google_rows", (copy.deepcopy(ROWS), "google:test:설정")),
|
||||
("build_store", self.store)):
|
||||
patched = patch.object(migration, name, return_value=value)
|
||||
patched.start()
|
||||
self.addCleanup(patched.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_never_connects_to_or_writes_db(self):
|
||||
self.assertEqual(self.run_cli(["import"]), 0)
|
||||
migration.build_store.assert_not_called()
|
||||
|
||||
def test_apply_backs_up_before_import_and_does_not_overwrite_backups(self):
|
||||
def check_backup(rows, source):
|
||||
self.assertEqual(migration.load_snapshot(self.backup)[0], rows)
|
||||
return 1
|
||||
self.store.import_rows.side_effect = check_backup
|
||||
args = ["import", "--apply", "--backup", str(self.backup)]
|
||||
self.assertEqual(self.run_cli(args), 0)
|
||||
self.assertEqual(self.run_cli(args), 1)
|
||||
self.store.import_rows.assert_called_once()
|
||||
|
||||
def test_apply_requires_backup_before_source_or_db_access(self):
|
||||
with self.assertRaises(SystemExit), redirect_stderr(self.output):
|
||||
migration.main(["import", "--apply"])
|
||||
migration.read_google_rows.assert_not_called()
|
||||
migration.build_store.assert_not_called()
|
||||
|
||||
def test_backup_checksum_rejects_modified_content(self):
|
||||
migration.save_snapshot(self.backup, ROWS, "test")
|
||||
payload = json.loads(self.backup.read_text(encoding="utf-8"))
|
||||
payload["rows"][1][2] = "9999"
|
||||
self.backup.write_text(json.dumps(payload), encoding="utf-8")
|
||||
with self.assertRaises(ValueError):
|
||||
migration.load_snapshot(self.backup)
|
||||
|
||||
def test_verify_detects_order_or_cell_differences(self):
|
||||
self.assertEqual(self.run_cli(["verify"]), 0)
|
||||
self.store.read.return_value = (list(reversed(ROWS)), 1)
|
||||
self.assertEqual(self.run_cli(["verify"]), 1)
|
||||
self.store.import_rows.assert_not_called()
|
||||
|
||||
def test_export_and_offline_import_need_no_google(self):
|
||||
self.assertEqual(self.run_cli(["export", "--output", str(self.backup)]), 0)
|
||||
self.assertEqual(self.run_cli(["import", "--input-file", str(self.backup)]), 0)
|
||||
migration.read_google_rows.assert_not_called()
|
||||
|
||||
def test_errors_do_not_print_database_credentials(self):
|
||||
self.store.import_rows.side_effect = RuntimeError("password=secret-value")
|
||||
self.assertEqual(self.run_cli(["import", "--apply", "--backup", str(self.backup)]), 1)
|
||||
self.assertNotIn("secret-value", self.output.getvalue())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,115 @@
|
||||
"""Opt-in real PostgreSQL tests, confined to a newly created temporary schema.
|
||||
|
||||
SETTINGS_TEST_DSN must point to a disposable test database, never production.
|
||||
"""
|
||||
|
||||
import os
|
||||
import threading
|
||||
import unittest
|
||||
import uuid
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
import psycopg2
|
||||
from psycopg2 import sql
|
||||
|
||||
from settings_store import (
|
||||
HEADER, PostgresSettingsStore, SettingsAlreadyInitialized, SettingsConflict,
|
||||
SettingsNotInitialized,
|
||||
)
|
||||
from history_store import PostgresHistoryStore
|
||||
|
||||
|
||||
@unittest.skipUnless(os.getenv("SETTINGS_TEST_DSN"), "SETTINGS_TEST_DSN test database is not configured")
|
||||
class RealPostgresTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.dsn = os.environ["SETTINGS_TEST_DSN"]
|
||||
self.schema = "corm_settings_test_" + uuid.uuid4().hex
|
||||
connection = psycopg2.connect(self.dsn, connect_timeout=3)
|
||||
try:
|
||||
with connection, connection.cursor() as cursor:
|
||||
cursor.execute(sql.SQL("CREATE SCHEMA {}").format(sql.Identifier(self.schema)))
|
||||
finally:
|
||||
connection.close()
|
||||
self.addCleanup(self.drop_schema)
|
||||
self.store = PostgresSettingsStore({})
|
||||
self.store._connect = self.connect
|
||||
self.histories = PostgresHistoryStore({})
|
||||
self.histories._connect = self.connect
|
||||
self.rows = [HEADER.copy(), ["APP_SETTINGS", "font", "한글 😊"],
|
||||
["ORDER_SINGLE", "B", "B-1"], ["ORDER_SINGLE", "A", "A-1"]]
|
||||
|
||||
def connect(self):
|
||||
connection = psycopg2.connect(self.dsn, connect_timeout=3)
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute(sql.SQL("SET search_path TO {}").format(sql.Identifier(self.schema)))
|
||||
cursor.execute("SET statement_timeout = 5000")
|
||||
return connection
|
||||
|
||||
def drop_schema(self):
|
||||
assert self.schema.startswith("corm_settings_test_") and len(self.schema) == 51
|
||||
connection = psycopg2.connect(self.dsn, connect_timeout=3)
|
||||
try:
|
||||
with connection, connection.cursor() as cursor:
|
||||
cursor.execute(sql.SQL("DROP SCHEMA {} CASCADE").format(sql.Identifier(self.schema)))
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
def test_jsonb_roundtrip_update_and_duplicate_import(self):
|
||||
self.assertEqual(self.store.import_rows(self.rows, "test"), 1)
|
||||
self.assertEqual(self.store.read(), (self.rows, 1))
|
||||
with self.assertRaises(SettingsAlreadyInitialized):
|
||||
self.store.import_rows(list(reversed(self.rows)), "test")
|
||||
self.assertEqual(self.store.read(), (self.rows, 1))
|
||||
changed = self.rows + [["APP_SETTINGS", "shipping_fee", "4000"]]
|
||||
self.assertEqual(self.store.write(changed, 1), 2)
|
||||
self.assertEqual(self.store.read(), (changed, 2))
|
||||
|
||||
def test_two_connections_cannot_silently_overwrite_each_other(self):
|
||||
self.store.import_rows(self.rows, "test")
|
||||
barrier = threading.Barrier(2)
|
||||
|
||||
def save(value):
|
||||
rows, version = self.store.read()
|
||||
rows.append(["APP_SETTINGS", "shipping_fee", value])
|
||||
barrier.wait(timeout=3)
|
||||
try:
|
||||
self.store.write(rows, version)
|
||||
return "saved"
|
||||
except SettingsConflict:
|
||||
return "conflict"
|
||||
|
||||
with ThreadPoolExecutor(max_workers=2) as executor:
|
||||
self.assertCountEqual(list(executor.map(save, ["4000", "5000"])), ["saved", "conflict"])
|
||||
self.assertEqual(self.store.read()[1], 2)
|
||||
|
||||
def test_failed_import_rolls_back_table_and_data(self):
|
||||
with self.assertRaises(psycopg2.IntegrityError):
|
||||
self.store.import_rows(self.rows, None)
|
||||
with self.assertRaises(SettingsNotInitialized):
|
||||
self.store.read()
|
||||
|
||||
def test_history_import_full_payload_ids_filters_and_deletion(self):
|
||||
sms = {"timestamp": "2026-10-22 12:00:00", "items": [{"code": "TEST", "qty": 2}],
|
||||
"previewText": "문자\n두 줄", "smsOpts": {"discount": True}}
|
||||
manual = [{"timestamp": "2020-01-01 10:00:00", "total_amount": "누락"},
|
||||
{"timestamp": "2026-10-22 12:00:00", "total_amount": 3500}]
|
||||
bundle = {"kind": "corm-history", "sms": [sms], "manual": manual}
|
||||
self.histories.import_all(bundle, "test")
|
||||
self.histories.check_ready()
|
||||
with self.assertRaises(SettingsAlreadyInitialized):
|
||||
self.histories.import_all(bundle, "test")
|
||||
saved = self.histories.list_records("sms")
|
||||
self.assertEqual(saved[0]["smsOpts"], sms["smsOpts"])
|
||||
self.assertEqual(len(self.histories.list_records("manual", "2026-10-22")), 1)
|
||||
self.assertEqual(len(self.histories.export_all()["manual"]), 2)
|
||||
new_id = self.histories.add("sms", {**sms, "timestamp": "2026-10-22 12:00:01"})
|
||||
self.histories.delete("sms", saved[0]["id"])
|
||||
self.assertEqual([r["id"] for r in self.histories.list_records("sms")], [new_id])
|
||||
self.histories.clear_sms()
|
||||
self.assertEqual(self.histories.list_records("sms"), [])
|
||||
with self.assertRaises(SettingsAlreadyInitialized):
|
||||
self.histories.import_all(bundle, "test")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user