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

175 lines
5.1 KiB
Python
Raw Normal View History

2021-04-26 20:07:00 +02:00
#coding:utf-8
#
# id: bugs.core_2197
# title: Add support for -nodbtriggers switch in gbak into services API
2021-11-12 18:29:54 +01:00
# decription:
# We add two database triggers (on connect and on disconnect) and make them do real work only when
2021-04-26 20:07:00 +02:00
# new attachment will be established (see trick with rdb$get_context('USER_SESSION', 'INIT_STATE') ).
# After finish backup we restore database and check that there is no records in 'log' table.
# (if option 'bkp_no_triggers' will be omitted then two records will be in that table).
# Checked on:
# 2.5.9.27103: OK, 0.938s.
# 3.0.4.32920: OK, 2.875s.
# 4.0.0.912: OK, 3.328s.
2021-11-12 18:29:54 +01:00
#
2021-04-26 20:07:00 +02:00
# tracker_id: CORE-2197
# min_versions: ['2.5.0']
# versions: 2.5
2021-11-12 18:29:54 +01:00
# qmid:
2021-04-26 20:07:00 +02:00
import pytest
from firebird.qa import db_factory, isql_act, Action
2021-11-12 18:29:54 +01:00
from io import BytesIO
from firebird.driver import SrvRestoreFlag, SrvBackupFlag
2021-04-26 20:07:00 +02:00
# version: 2.5
# resources: None
substitutions_1 = []
init_script_1 = """
recreate table log( att bigint default current_connection, event_name varchar(20) );
create sequence g;
set term ^;
create or alter trigger trg_attach inactive on connect as
begin
if ( rdb$get_context('USER_SESSION', 'INIT_STATE') is null ) then
insert into log(event_name) values ('attach');
end
^
create or alter trigger trg_detach inactive on disconnect as
begin
if ( rdb$get_context('USER_SESSION', 'INIT_STATE') is null ) then
insert into log(event_name) values ('detach');
end
^
set term ^;
commit;
2021-12-22 20:23:11 +01:00
"""
2021-04-26 20:07:00 +02:00
db_1 = db_factory(sql_dialect=3, init=init_script_1)
# test_script_1
#---
# import os
# import time
# import subprocess
# from subprocess import Popen
2021-11-12 18:29:54 +01:00
#
2021-04-26 20:07:00 +02:00
# os.environ["ISC_USER"] = user_name
# os.environ["ISC_PASSWORD"] = user_password
2021-11-12 18:29:54 +01:00
#
2021-04-26 20:07:00 +02:00
# db_conn.close()
2021-11-12 18:29:54 +01:00
#
2021-04-26 20:07:00 +02:00
# thisdb='$(DATABASE_LOCATION)bugs.core_2197.fdb'
# tmpbkp='$(DATABASE_LOCATION)bugs.core_2197_fbk.tmp'
# tmpres='$(DATABASE_LOCATION)bugs.core_2197_new.tmp'
2021-11-12 18:29:54 +01:00
#
#
2021-04-26 20:07:00 +02:00
# #---------------------------------------------------------------
2021-11-12 18:29:54 +01:00
#
2021-04-26 20:07:00 +02:00
# isql_txt=''' delete from log;
# commit;
# set term ^;
# execute block as begin
# rdb$set_context('USER_SESSION', 'INIT_STATE','1');
# end
# ^
# set term ;^
# alter trigger trg_attach active;
# alter trigger trg_detach active;
# commit;
# --set count on;
# --select * from log;
# '''
2021-11-12 18:29:54 +01:00
#
2021-04-26 20:07:00 +02:00
# runProgram('isql',[dsn], isql_txt)
2021-11-12 18:29:54 +01:00
#
2021-04-26 20:07:00 +02:00
# f_svc_log=open( os.path.join(context['temp_directory'],'tmp_svc_2197.log'), 'w')
# f_svc_err=open( os.path.join(context['temp_directory'],'tmp_svc_2197.err'), 'w')
2021-11-12 18:29:54 +01:00
#
# subprocess.call( [ context['fbsvcmgr_path'], 'localhost:service_mgr','action_backup',
2021-04-26 20:07:00 +02:00
# 'dbname', thisdb, 'bkp_file', tmpbkp,
# 'bkp_no_triggers'
2021-11-12 18:29:54 +01:00
# ],
2021-04-26 20:07:00 +02:00
# stdout=f_svc_log,stderr=f_svc_err
# )
2021-11-12 18:29:54 +01:00
#
2021-04-26 20:07:00 +02:00
# runProgram('isql',[dsn, '-nod'], 'set list on; set count on; select 1, g.* from log g;')
2021-11-12 18:29:54 +01:00
#
# subprocess.call( [ context['fbsvcmgr_path'], 'localhost:service_mgr','action_restore',
# 'bkp_file', tmpbkp, 'dbname', tmpres,
2021-04-26 20:07:00 +02:00
# 'res_replace'
2021-11-12 18:29:54 +01:00
# ],
2021-04-26 20:07:00 +02:00
# stdout=f_svc_log,stderr=f_svc_err
# )
2021-11-12 18:29:54 +01:00
#
2021-04-26 20:07:00 +02:00
# f_svc_log.close()
# f_svc_err.close()
2021-11-12 18:29:54 +01:00
#
2021-04-26 20:07:00 +02:00
# runProgram('isql',[dsn, '-nod'], 'set list on; set count on; select 1, g.* from log g;')
2021-11-12 18:29:54 +01:00
#
#
2021-04-26 20:07:00 +02:00
# #############################################
# # Cleanup.
2021-11-12 18:29:54 +01:00
# f_list=(
2021-04-26 20:07:00 +02:00
# f_svc_log
# ,f_svc_err
# )
2021-11-12 18:29:54 +01:00
#
2021-04-26 20:07:00 +02:00
# for i in range(len(f_list)):
# if os.path.isfile(f_list[i].name):
# os.remove(f_list[i].name)
2021-11-12 18:29:54 +01:00
#
2021-04-26 20:07:00 +02:00
# os.remove(tmpbkp)
# os.remove(tmpres)
2021-11-12 18:29:54 +01:00
#
#
2021-04-26 20:07:00 +02:00
#---
2021-11-12 18:29:54 +01:00
test_script_1 = '''
delete from log;
commit;
set term ^;
execute block as begin
rdb$set_context('USER_SESSION', 'INIT_STATE','1');
end
^
set term ;^
alter trigger trg_attach active;
alter trigger trg_detach active;
commit;
--set count on;
--select * from log;
'''
act_1 = isql_act('db_1', test_script_1, substitutions=substitutions_1)
2021-04-26 20:07:00 +02:00
expected_stdout_1 = """
Records affected: 0
2021-11-12 18:29:54 +01:00
"""
2021-04-26 20:07:00 +02:00
@pytest.mark.version('>=2.5')
2021-11-12 18:29:54 +01:00
def test_1(act_1: Action):
check_sql = 'set list on; set count on; select 1, g.* from log g;'
act_1.execute()
backup = BytesIO()
with act_1.connect_server() as srv:
srv.database.local_backup(database=act_1.db.db_path, backup_stream=backup,
2021-11-12 18:29:54 +01:00
flags=SrvBackupFlag.NO_TRIGGERS)
backup.seek(0)
act_1.reset()
act_1.expected_stdout = expected_stdout_1
act_1.isql(switches=['-nod'], input=check_sql)
assert act_1.clean_stdout == act_1.clean_expected_stdout
srv.database.local_restore(backup_stream=backup, database=act_1.db.db_path,
2021-11-12 18:29:54 +01:00
flags=SrvRestoreFlag.REPLACE)
backup.close()
act_1.reset()
act_1.expected_stdout = expected_stdout_1
act_1.isql(switches=['-nod'], input=check_sql)
assert act_1.clean_stdout == act_1.clean_expected_stdout
2021-04-26 20:07:00 +02:00