Move CORM settings and histories to dedicated PostgreSQL database
This commit is contained in:
@@ -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