2021-04-26 20:07:00 +02:00
|
|
|
#coding:utf-8
|
2022-02-04 19:05:19 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
ID: fkey.primary.insert-03
|
|
|
|
FBTEST: functional.fkey.primary.insert_pk_03
|
|
|
|
TITLE: Check correct work fix with foreign key
|
|
|
|
DESCRIPTION:
|
|
|
|
Check foreign key work.
|
|
|
|
Master transaction:
|
|
|
|
1) modifies non key field
|
|
|
|
2) create savepoint
|
|
|
|
3) modifies primary key
|
|
|
|
4) rollback to savepoint
|
|
|
|
Detail transaction inserts record in detail_table.
|
|
|
|
Expected: error - primary key has been changed
|
|
|
|
"""
|
2021-04-26 20:07:00 +02:00
|
|
|
|
|
|
|
import pytest
|
2022-02-04 19:05:19 +01:00
|
|
|
from firebird.qa import *
|
2021-12-22 20:25:10 +01:00
|
|
|
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,
|
|
|
|
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;"""
|
|
|
|
|
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:
|
2021-12-22 20:25:10 +01:00
|
|
|
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=2')
|
|
|
|
con.savepoint('A')
|
|
|
|
c.execute('UPDATE MASTER_TABLE SET ID=2 WHERE ID=1')
|
|
|
|
con.rollback(savepoint='A')
|
|
|
|
#Create second connection for change detail table
|
2022-02-04 19:05:19 +01:00
|
|
|
with act.db.connect() as con_detail:
|
2021-12-22 20:25:10 +01:00
|
|
|
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.
|