6
0
mirror of https://github.com/FirebirdSQL/firebird-qa.git synced 2025-01-22 21:43:06 +01:00
firebird-qa/tests/bugs/core_5389_test.py
2021-12-06 19:23:35 +01:00

222 lines
8.0 KiB
Python

#coding:utf-8
#
# id: bugs.core_5389
# title: Query cannot be executed if win1251 characters used in field aliases
# decription:
# Test prepares file that will serve as input SQL script and will have CYRYLLIC names for field aliases.
# File has name = 'tmp_non_ascii_chk_5389.sql' and is encoded to windows-1251 codepage.
# Checked on 4.0.0.639.
# 30.10.2019 checked on:
# 4.0.0.1635 SS: 2.782s.
# 4.0.0.1633 CS: 3.515s.
# NB: old checked on: 4.0.0.639 - did pass without using codecs! Strange!
#
# 13.04.2021. Adapted for run both on Windows and Linux. Checked on:
# Windows: 4.0.0.2416
# Linux: 4.0.0.2416
#
# tracker_id: CORE-5389
# min_versions: ['4.0']
# versions: 4.0
# qmid: None
import pytest
from pathlib import Path
from firebird.qa import db_factory, python_act, Action, temp_file
# version: 4.0
# resources: None
substitutions_1 = [('.*After line [0-9]+ in file .*', '')]
init_script_1 = """"""
db_1 = db_factory(sql_dialect=3, init=init_script_1)
# test_script_1
#---
# import os
# import time
# import subprocess
#
# # 30.10.2019. This is needed in Python 2.7 for converting string in UTF8 to cp1251
# import codecs
# import io
#
# os.environ["ISC_USER"] = user_name
# os.environ["ISC_PASSWORD"] = user_password
#
# db_conn.close()
#
# #--------------------------------------------
#
# def flush_and_close( file_handle ):
# # https://docs.python.org/2/library/os.html#os.fsync
# # If you're starting with a Python file object f,
# # first do f.flush(), and
# # then do os.fsync(f.fileno()), to ensure that all internal buffers associated with f are written to disk.
# global os
#
# file_handle.flush()
# if file_handle.mode not in ('r', 'rb') and file_handle.name != os.devnull:
# # otherwise: "OSError: [Errno 9] Bad file descriptor"!
# os.fsync(file_handle.fileno())
# file_handle.close()
#
# #--------------------------------------------
#
# def cleanup( f_names_list ):
# global os
# for i in range(len( f_names_list )):
# if type(f_names_list[i]) == file:
# del_name = f_names_list[i].name
# elif type(f_names_list[i]) == str:
# del_name = f_names_list[i]
# else:
# print('Unrecognized type of element:', f_names_list[i], ' - can not be treated as file.')
# print('type(f_names_list[i])=',type(f_names_list[i]))
# del_name = None
#
# if del_name and os.path.isfile( del_name ):
# os.remove( del_name )
#
# #--------------------------------------------
#
# non_ascii_ddl='''
# set bail on;
# set list on;
# -- set names win1251;
# connect '%(dsn)s' user '%(user_name)s' password '%(user_password)s';
#
# select
# '' as "ФИО"
# ,'' as "Д.рождения"
# ,'' as "Город"
# ,'' as "Расшифровка выступлений"
# from rdb$database;
# ''' % dict(globals(), **locals())
#
# f_checksql = open( os.path.join(context['temp_directory'], 'tmp_5389_w1251.sql'), 'w' )
# f_checksql.write( non_ascii_ddl.decode('utf8').encode('cp1251') )
# flush_and_close( f_checksql )
#
# # result: file 'tmp_non_ascii_chk_5389.sql' is encoded in win1251
#
# #f_checksql=os.path.join(context['temp_directory'],'tmp_non_ascii_ddl_5389.sql')
# #with io.open( f_checksql, 'w', encoding='cp1251') as f:
# # f.write( non_ascii_ddl.decode('utf-8') )
#
# ###########################################################################################################
# ### check-1: attempt to apply DDL with non-ascii characters __WITHOUT__ charset specifying (for ISQL) ###
# ###########################################################################################################
#
# f_apply_cset_none_log = open( os.path.join(context['temp_directory'],'tmp_5389_apply_cset_none.log'), 'w')
# f_apply_cset_none_err = open( os.path.join(context['temp_directory'],'tmp_5389_apply_cset_none.err'), 'w')
#
# subprocess.call( [context['isql_path'], "-q", "-i", f_checksql.name ],
# stdout = f_apply_cset_none_log,
# stderr = f_apply_cset_none_err
# )
#
# flush_and_close( f_apply_cset_none_log )
# flush_and_close( f_apply_cset_none_err )
#
# #############################################################################################################
# ### check-2: attempt to apply DDL with non-ascii characters ___WITH___ specifying: ISQL ... -ch WIN1251 ###
# #############################################################################################################
#
# f_apply_cset_1251_log = open( os.path.join(context['temp_directory'],'tmp_5389_apply_cset_1251.log'), 'w')
# f_apply_cset_1251_err = open( os.path.join(context['temp_directory'],'tmp_5389_apply_cset_1251.err'), 'w')
#
# subprocess.call( [context['isql_path'], "-q", "-i", f_checksql.name, "-ch", "win1251" ],
# stdout = f_apply_cset_1251_log,
# stderr = f_apply_cset_1251_err
# )
#
# flush_and_close( f_apply_cset_1251_log )
#
# # This file should NOT contain any errors:
# flush_and_close( f_apply_cset_1251_err )
#
# # CHECK RESULTS:
# ################
#
# # This stdout log should contain only ONE statement (create collation <non_ascii_name> ...),
# # this DDL failed and caused ISQL to immediately terminate:
# with open( f_apply_cset_none_log.name, 'r') as f:
# for line in f:
# out_txt='STDLOG WHEN CSET=NONE: ';
# if line.strip():
# print( out_txt+line.strip().decode("cp1251").encode('utf8') )
#
#
# with open( f_apply_cset_none_err.name, 'r') as f:
# for line in f:
# out_txt='STDERR WHEN CSET=NONE: ';
# if line.strip():
# print( out_txt+line.strip().decode("cp1251").encode('utf8') )
#
# with open( f_apply_cset_1251_log.name, 'r') as f:
# for line in f:
# out_txt='STDLOG WHEN CSET=WIN1251: ';
# if line.strip():
# print( out_txt+line.strip().decode("cp1251").encode('utf8') )
#
# with open( f_apply_cset_1251_err.name, 'r') as f:
# for line in f:
# out_txt='STDERR WHEN CSET=WIN1251: ';
# if line.strip():
# print( out_txt+line.strip().decode("cp1251").encode('utf8') )
#
#
# #####################################################################
# # Cleanup:
# time.sleep(1)
# cleanup( (f_apply_cset_none_log, f_apply_cset_none_err, f_apply_cset_1251_log, f_apply_cset_1251_err, f_checksql) )
#
#
#---
act_1 = python_act('db_1', substitutions=substitutions_1)
expected_stdout_1 = """
ФИО
Д.рождения
Город
Расшифровка выступлений
"""
expected_stderr_1 = """
Statement failed, SQLSTATE = 22018
arithmetic exception, numeric overflow, or string truncation
-Cannot transliterate character between character sets
"""
test_script_1 = """
set bail on;
set list on;
-- set names win1251;
select
'' as "ФИО"
,'' as "Д.рождения"
,'' as "Город"
,'' as "Расшифровка выступлений"
from rdb$database;
"""
script_file = temp_file('test-script.sql')
@pytest.mark.version('>=4.0')
def test_1(act_1: Action, script_file: Path):
script_file.write_text(test_script_1, encoding='cp1251')
# check-1: attempt to apply DDL with non-ascii characters __WITHOUT__ charset specifying (for ISQL)
act_1.expected_stderr = expected_stderr_1
act_1.isql(switches=['-q'], input_file=script_file, charset=None)
assert act_1.clean_stderr == act_1.clean_expected_stderr
# check-2: attempt to apply DDL with non-ascii characters ___WITH___ specifying: ISQL ... -ch WIN1251
act_1.reset()
act_1.expected_stdout = expected_stdout_1
act_1.isql(switches=['-q'], input_file=script_file, charset='WIN1251')
assert act_1.clean_stdout == act_1.clean_expected_stdout