6
0
mirror of https://github.com/FirebirdSQL/firebird-qa.git synced 2025-01-23 05:53:06 +01:00
firebird-qa/tests/functional/fkey/primary/test_insert_pk_02.py

53 lines
1.7 KiB
Python

#coding:utf-8
"""
ID: fkey.primary.insert-02
FBTEST: functional.fkey.primary.insert_pk_02
TITLE: Check correct work fix with foreign key
DESCRIPTION:
Check foreign key work.
Master transaction modifies primary key.
Detail transaction inserts record in detail_table.
Expected: error - primary key in master table has been changed
"""
import pytest
from firebird.qa import *
from firebird.driver import DatabaseError, tpb, Isolation
init_script = """CREATE TABLE MASTER_TABLE (
ID INTEGER PRIMARY KEY,
INT_F INTEGER
);
CREATE TABLE DETAIL_TABLE (
ID INTEGER PRIMARY KEY,
FKEY INTEGER
);
ALTER TABLE DETAIL_TABLE ADD CONSTRAINT FK_DETAIL_TABLE FOREIGN KEY (FKEY) REFERENCES MASTER_TABLE (ID);
COMMIT;
INSERT INTO MASTER_TABLE (ID, INT_F) VALUES (1, 10);
commit;"""
db = db_factory(init=init_script)
act = python_act('db')
@pytest.mark.version('>3')
def test_1(act: Action):
cust_tpb = tpb(isolation=Isolation.READ_COMMITTED_RECORD_VERSION, lock_timeout=0)
with act.db.connect() as con:
con.begin(cust_tpb)
with con.cursor() as c:
c.execute("UPDATE MASTER_TABLE SET ID = 2 WHERE ID=1")
#Create second connection for change detail table
with act.db.connect() as con_detail:
con_detail.begin(cust_tpb)
with con_detail.cursor() as cd:
with pytest.raises(DatabaseError,
match='.*violation of FOREIGN KEY constraint "FK_DETAIL_TABLE" on table "DETAIL_TABLE".*'):
cd.execute("INSERT INTO DETAIL_TABLE (ID, FKEY) VALUES (1,1)")
con_detail.commit()
# Passed.