forked from mtusi2026/mtusi2026lab2
init commit: create skeleton from lab1
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
"""Ограничения на уровне БД: приложение — не единственный защитник данных."""
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from tests.helpers import create_chat, create_user
|
||||
|
||||
|
||||
def test_duplicate_username_is_rejected_by_database(client, db_engine):
|
||||
create_user(client, username="unique_name")
|
||||
|
||||
with pytest.raises(IntegrityError):
|
||||
with db_engine.begin() as connection:
|
||||
connection.execute(
|
||||
text(
|
||||
"INSERT INTO users (id, username, display_name, password_hash) "
|
||||
"VALUES (gen_random_uuid(), 'unique_name', 'Other', 'hash')"
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def test_message_requires_existing_chat(client, db_engine):
|
||||
alice = create_user(client)
|
||||
|
||||
with pytest.raises(IntegrityError):
|
||||
with db_engine.begin() as connection:
|
||||
connection.execute(
|
||||
text(
|
||||
"INSERT INTO messages (id, chat_id, sender_id, text) "
|
||||
"VALUES (gen_random_uuid(), gen_random_uuid(), :sender, 'x')"
|
||||
),
|
||||
{"sender": alice["id"]},
|
||||
)
|
||||
|
||||
|
||||
def test_duplicate_membership_is_rejected_by_database(client, db_engine):
|
||||
alice = create_user(client)
|
||||
chat = create_chat(client, alice)
|
||||
|
||||
with pytest.raises(IntegrityError):
|
||||
with db_engine.begin() as connection:
|
||||
connection.execute(
|
||||
text("INSERT INTO chat_members (chat_id, user_id) VALUES (:c, :u)"),
|
||||
{"c": chat["id"], "u": alice["id"]},
|
||||
)
|
||||
Reference in New Issue
Block a user