test: verify level-four failures and history index
This commit is contained in:
@@ -5,17 +5,20 @@ import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from contextlib import contextmanager
|
||||
from unittest.mock import patch
|
||||
from uuid import uuid4
|
||||
|
||||
import psycopg
|
||||
|
||||
from src.store import Store
|
||||
from src.db import database_url
|
||||
from src.store import Store, connection
|
||||
|
||||
|
||||
URL = os.environ.get("DATABASE_URL")
|
||||
URL = database_url() if os.environ.get("DATABASE_URL") or os.environ.get("DATABASE_URL_FILE") else None
|
||||
|
||||
|
||||
@unittest.skipUnless(URL, "set DATABASE_URL to a disposable PostgreSQL database")
|
||||
@unittest.skipUnless(URL, "set DATABASE_URL or DATABASE_URL_FILE to a disposable PostgreSQL database")
|
||||
class DatabaseTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
@@ -80,6 +83,37 @@ class DatabaseTests(unittest.TestCase):
|
||||
self.store.chat(eve["id"], chat["id"])
|
||||
self.assertEqual(hidden.exception.status_code, 404)
|
||||
|
||||
def test_membership_failure_rolls_back_inserted_chat(self):
|
||||
alice, bob = self.user(), self.user()
|
||||
title = "rollback-" + uuid4().hex
|
||||
|
||||
class FailingConnection:
|
||||
def __init__(self, wrapped):
|
||||
self.wrapped = wrapped
|
||||
self.memberships = 0
|
||||
|
||||
def execute(self, query, params=None):
|
||||
if query.startswith("INSERT INTO memberships"):
|
||||
self.memberships += 1
|
||||
if self.memberships == 2:
|
||||
raise RuntimeError("synthetic second membership failure")
|
||||
return self.wrapped.execute(query, params)
|
||||
|
||||
@contextmanager
|
||||
def failing_connection():
|
||||
with connection() as conn:
|
||||
yield FailingConnection(conn)
|
||||
|
||||
with patch("src.store.connection", failing_connection):
|
||||
with self.assertRaisesRegex(RuntimeError, "synthetic second membership failure"):
|
||||
self.store.create_chat(alice["id"], title, [alice["id"], bob["id"]])
|
||||
with psycopg.connect(URL) as conn:
|
||||
self.assertEqual(conn.execute("SELECT count(*) FROM chats WHERE title=%s", (title,)).fetchone()[0], 0)
|
||||
self.assertEqual(conn.execute(
|
||||
"SELECT count(*) FROM memberships WHERE user_id IN (%s,%s)",
|
||||
(alice["id"], bob["id"]),
|
||||
).fetchone()[0], 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user