6
0
mirror of https://github.com/FirebirdSQL/firebird-qa.git synced 2025-01-23 14:03:06 +01:00
firebird-qa/tests/functional/fkey/unique/test_insert_13.py

59 lines
1.9 KiB
Python
Raw Normal View History

2021-04-26 20:07:00 +02:00
#coding:utf-8
2022-02-04 19:05:19 +01:00
"""
ID: fkey.unique.insert-12
FBTEST: functional.fkey.unique.insert_13
TITLE: Check correct work fix with foreign key
DESCRIPTION:
Check foreign key work.
Master table has one primary key field and one unique field.
1 Master transaction modifies non key or unique field.
2 Detail transaction inserts record.
3 Detail transaction committed.
4 Master transaction modifies unique field.
Detail transaction inserts record in detail_table record.
Expected: referential integrity error
"""
2021-04-26 20:07:00 +02:00
import pytest
2022-02-04 19:05:19 +01:00
from firebird.qa import *
from firebird.driver import DatabaseError, tpb, Isolation
2021-04-26 20:07:00 +02:00
2022-02-04 19:05:19 +01:00
init_script = """CREATE TABLE MASTER_TABLE (
2021-04-26 20:07:00 +02:00
ID INTEGER PRIMARY KEY,
UF INTEGER UNIQUE,
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 (UF);
COMMIT;
INSERT INTO MASTER_TABLE (ID, UF, INT_F) VALUES (1, 1, 10);
commit;"""
2022-02-04 19:05:19 +01:00
db = db_factory(init=init_script)
2021-04-26 20:07:00 +02:00
2022-02-04 19:05:19 +01:00
act = python_act('db')
2021-04-26 20:07:00 +02:00
2022-02-04 19:05:19 +01:00
@pytest.mark.version('>=3')
def test_1(act: Action):
with act.db.connect() as con:
cust_tpb = tpb(isolation=Isolation.READ_COMMITTED_RECORD_VERSION, lock_timeout=0)
con.begin(cust_tpb)
with con.cursor() as c:
c.execute("update master_table set int_f = 10 WHERE ID=1")
#Create second connection for change detail table
2022-02-04 19:05:19 +01:00
with act.db.connect() as con_detail:
con_detail.begin(cust_tpb)
with con_detail.cursor() as cd:
cd.execute("INSERT INTO DETAIL_TABLE (ID, FKEY) VALUES (1,1)")
con_detail.commit()
with pytest.raises(DatabaseError,
match='.*Foreign key references are present for the record.*'):
c.execute("update master_table set UF=10 WHERE ID=1")
# Passed.