update_data.py 15.6 KB
Newer Older
J
jiacy-jcy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
###################################################################
#           Copyright (c) 2016 by TAOS Technologies, Inc.
#                     All rights reserved.
#
#  This file is proprietary and confidential to TAOS Technologies.
#  No part of this file may be reproduced, stored, transmitted,
#  disclosed or used in any form or by any means other than as
#  expressly provided by the written permission from Jianhui Tao
#
###################################################################

# -*- coding: utf-8 -*-

import random
import string
J
jiacy-jcy 已提交
16
from datetime import datetime
J
jiacy-jcy 已提交
17 18 19 20 21
from util import constant
from util.log import *
from util.cases import *
from util.sql import *
from util.common import *
J
update  
jiacy-jcy 已提交
22
from util.sqlset import TDSetSql
J
jiacy-jcy 已提交
23 24 25
class TDTestCase:
    def init(self, conn, logSql):
        tdLog.debug("start to execute %s" % __file__)
J
jiacy-jcy 已提交
26
        tdSql.init(conn.cursor(),logSql)
J
update  
jiacy-jcy 已提交
27
        self.setsql = TDSetSql()
J
jiacy-jcy 已提交
28 29 30
        self.dbname = 'db_test'
        self.ntbname = 'ntb'
        self.stbname = 'stb'
J
update  
jiacy-jcy 已提交
31
        self.ctbname = 'ctb'
J
jiacy-jcy 已提交
32
        self.ts = 1537146000000
J
jiacy-jcy 已提交
33
        self.str_length = 20
J
jiacy-jcy 已提交
34 35 36 37 38 39 40 41 42 43 44 45
        self.column_dict = {
            'col1': 'tinyint',
            'col2': 'smallint',
            'col3': 'int',
            'col4': 'bigint',
            'col5': 'tinyint unsigned',
            'col6': 'smallint unsigned',
            'col7': 'int unsigned',
            'col8': 'bigint unsigned',
            'col9': 'float',
            'col10': 'double',
            'col11': 'bool',
J
jiacy-jcy 已提交
46 47
            'col12': f'binary({self.str_length})',
            'col13': f'nchar({self.str_length})',
J
jiacy-jcy 已提交
48 49
            'col_ts'  : 'timestamp'
        }
J
jiacy-jcy 已提交
50
           
J
update  
jiacy-jcy 已提交
51
    def data_check(self,tbname,col_name,col_type,value):
J
jiacy-jcy 已提交
52 53 54 55 56 57
        tdSql.query(f'select {col_name} from {tbname}')
        if col_type.lower() == 'float' or col_type.lower() == 'double':
            if abs(tdSql.queryResult[0][0] - value) / value <= 0.0001:
                tdSql.checkEqual(tdSql.queryResult[0][0],tdSql.queryResult[0][0])
            else:
                tdLog.exit(f'{col_name} data check failure')
J
update  
jiacy-jcy 已提交
58
        elif col_type.lower() == 'timestamp':
J
jiacy-jcy 已提交
59
            tdSql.checkEqual(str(tdSql.queryResult[0][0]),str(datetime.fromtimestamp(value/1000).strftime("%Y-%m-%d %H:%M:%S.%f")))
J
jiacy-jcy 已提交
60 61
        else:
            tdSql.checkEqual(tdSql.queryResult[0][0],value)
J
update  
jiacy-jcy 已提交
62 63 64 65 66 67 68
    def update_and_check_data(self,tbname,col_name,col_type,value,dbname):
        if 'binary' in col_type.lower() or 'nchar' in col_type.lower():
            tdSql.execute(f'insert into {tbname} values({self.ts},"{value}")')
        else:
            tdSql.execute(f'insert into {tbname} values({self.ts},{value})')
        self.data_check(tbname,col_name,col_type,value)
        tdSql.execute(f'flush database {dbname}')
J
update  
jiacy-jcy 已提交
69
        tdSql.execute('reset query cache')
J
update  
jiacy-jcy 已提交
70
        self.data_check(tbname,col_name,col_type,value)
J
update  
jiacy-jcy 已提交
71 72
        for func in ['first','last']:
            tdSql.execute(f'select {func}({col_name}) from {tbname}')
J
update  
jiacy-jcy 已提交
73 74 75 76 77 78 79 80
    def error_check(self,tbname,column_dict,tb_type=None,stbname=None):
        str_length = self.str_length+1
        for col_name,col_type in column_dict.items():
            if tb_type == 'ntb':
                tdSql.execute(f'create table {tbname} (ts timestamp,{col_name} {col_type})')
            elif tb_type == 'ctb':
                tdSql.execute(f'create table {stbname} (ts timestamp,{col_name} {col_type}) tags(t0 int)')
                tdSql.execute(f'create table {tbname} using {stbname} tags(1)')
J
jiacy-jcy 已提交
81
            tdSql.execute(f'insert into {tbname} values({self.ts},null)')
J
update  
jiacy-jcy 已提交
82 83 84
            if col_type.lower() == 'double':
                for error_value in [tdCom.getLongName(self.str_length),True,False,1.1*constant.DOUBLE_MIN,1.1*constant.DOUBLE_MAX]:
                    tdSql.error(f'insert into {tbname} values({self.ts},{error_value})')
J
jiacy-jcy 已提交
85 86
                    if tb_type == 'ctb':
                        tdSql.error(f'insert into {stbname} values({self.ts},{error_value})')
J
update  
jiacy-jcy 已提交
87 88 89
            elif col_type.lower() == 'float':
                for error_value in [tdCom.getLongName(self.str_length),True,False,1.1*constant.FLOAT_MIN,1.1*constant.FLOAT_MAX]:
                    tdSql.error(f'insert into {tbname} values({self.ts},{error_value})')
J
jiacy-jcy 已提交
90 91
                    if tb_type == 'ctb':
                        tdSql.error(f'insert into {stbname} values({self.ts},{error_value})')
J
update  
jiacy-jcy 已提交
92 93 94
            elif 'binary' in col_type.lower() or 'nchar' in col_type.lower():
                for error_value in [tdCom.getLongName(str_length)]:
                    tdSql.error(f'insert into {tbname} values({self.ts},"{error_value}")')
J
jiacy-jcy 已提交
95 96
                    if tb_type == 'ctb':
                        tdSql.error(f'insert into {stbname} values({self.ts},{error_value})')
J
update  
jiacy-jcy 已提交
97 98 99
            elif col_type.lower() == 'bool':
                for error_value in [tdCom.getLongName(self.str_length)]:
                    tdSql.error(f'insert into {tbname} values({self.ts},{error_value})')
J
jiacy-jcy 已提交
100 101
                    if tb_type == 'ctb':
                        tdSql.error(f'insert into {stbname} values({self.ts},{error_value})')
J
update  
jiacy-jcy 已提交
102 103 104
            elif col_type.lower() == 'tinyint':
                for error_value in [constant.TINYINT_MIN-1,constant.TINYINT_MAX+1,random.uniform(constant.FLOAT_MIN,constant.FLOAT_MAX),tdCom.getLongName(self.str_length),True,False]:
                    tdSql.error(f'insert into {tbname} values({self.ts},{error_value})')
J
jiacy-jcy 已提交
105 106
                    if tb_type == 'ctb':
                        tdSql.error(f'insert into {stbname} values({self.ts},{error_value})')
J
update  
jiacy-jcy 已提交
107 108 109
            elif col_type.lower() == 'smallint':
                for error_value in [constant.SMALLINT_MIN-1,constant.SMALLINT_MAX+1,random.uniform(constant.FLOAT_MIN,constant.FLOAT_MAX),tdCom.getLongName(self.str_length),True,False]:
                    tdSql.error(f'insert into {tbname} values({self.ts},{error_value})')
J
jiacy-jcy 已提交
110 111
                    if tb_type == 'ctb':
                        tdSql.error(f'insert into {stbname} values({self.ts},{error_value})')
J
update  
jiacy-jcy 已提交
112 113 114
            elif col_type.lower() == 'int':
                for error_value in [constant.INT_MIN-1,constant.INT_MAX+1,random.uniform(constant.FLOAT_MIN,constant.FLOAT_MAX),tdCom.getLongName(self.str_length),True,False]:
                    tdSql.error(f'insert into {tbname} values({self.ts},{error_value})')
J
jiacy-jcy 已提交
115 116
                    if tb_type == 'ctb':
                        tdSql.error(f'insert into {stbname} values({self.ts},{error_value})')
J
update  
jiacy-jcy 已提交
117 118 119
            elif col_type.lower() == 'bigint':
                for error_value in [constant.BIGINT_MIN-1,constant.BIGINT_MAX+1,random.uniform(constant.FLOAT_MIN,constant.FLOAT_MAX),tdCom.getLongName(self.str_length),True,False]:
                    tdSql.error(f'insert into {tbname} values({self.ts},{error_value})')
J
jiacy-jcy 已提交
120 121
                    if tb_type == 'ctb':
                        tdSql.error(f'insert into {stbname} values({self.ts},{error_value})')
J
update  
jiacy-jcy 已提交
122 123
            elif col_type.lower() == 'tinyint unsigned':
                for error_value in [constant.TINYINT_UN_MIN-1,constant.TINYINT_UN_MAX+1,random.uniform(constant.FLOAT_MIN,constant.FLOAT_MAX),tdCom.getLongName(self.str_length),True,False]:
J
jiacy-jcy 已提交
124 125 126
                    tdSql.error(f'insert into {tbname} values({self.ts},{error_value})')   
                    if tb_type == 'ctb':
                        tdSql.error(f'insert into {stbname} values({self.ts},{error_value})')  
J
update  
jiacy-jcy 已提交
127 128 129
            elif col_type.lower() == 'smallint unsigned':
                for error_value in [constant.SMALLINT_UN_MIN-1,constant.SMALLINT_UN_MAX+1,random.uniform(constant.FLOAT_MIN,constant.FLOAT_MAX),tdCom.getLongName(self.str_length),True,False]:
                    tdSql.error(f'insert into {tbname} values({self.ts},{error_value})')
J
jiacy-jcy 已提交
130 131
                    if tb_type == 'ctb':
                        tdSql.error(f'insert into {stbname} values({self.ts},{error_value})')
J
update  
jiacy-jcy 已提交
132 133 134
            elif col_type.lower() == 'int unsigned':
                for error_value in [constant.INT_UN_MIN-1,constant.INT_UN_MAX+1,random.uniform(constant.FLOAT_MIN,constant.FLOAT_MAX),tdCom.getLongName(self.str_length),True,False]:
                    tdSql.error(f'insert into {tbname} values({self.ts},{error_value})')
J
jiacy-jcy 已提交
135 136
                    if tb_type == 'ctb':
                        tdSql.error(f'insert into {stbname} values({self.ts},{error_value})')
J
update  
jiacy-jcy 已提交
137 138
            elif col_type.lower() == 'bigint unsigned':
                for error_value in [constant.BIGINT_UN_MIN-1,constant.BIGINT_UN_MAX+1,random.uniform(constant.FLOAT_MIN,constant.FLOAT_MAX),tdCom.getLongName(self.str_length),True,False]:
J
jiacy-jcy 已提交
139 140 141
                    tdSql.error(f'insert into {tbname} values({self.ts},{error_value})')    
                    if tb_type == 'ctb':
                        tdSql.error(f'insert into {stbname} values({self.ts},{error_value})')       
J
update  
jiacy-jcy 已提交
142 143 144 145
            tdSql.execute(f'drop table {tbname}')
            if tb_type == 'ctb':
                tdSql.execute(f'drop table {stbname}')
    def update_data_check(self,tbname,column_dict,dbname,tb_type=None,stbname=None):
J
jiacy-jcy 已提交
146 147 148 149 150 151 152 153 154 155 156
        up_tinyint = random.randint(constant.TINYINT_MIN,constant.TINYINT_MAX)
        up_smallint = random.randint(constant.SMALLINT_MIN,constant.SMALLINT_MAX)
        up_int = random.randint(constant.INT_MIN,constant.INT_MAX)
        up_bigint = random.randint(constant.BIGINT_MIN,constant.BIGINT_MAX)
        up_untinyint = random.randint(constant.TINYINT_UN_MIN,constant.TINYINT_UN_MAX)
        up_unsmallint = random.randint(constant.SMALLINT_UN_MIN,constant.SMALLINT_UN_MAX)
        up_unint = random.randint(constant.INT_UN_MIN,constant.INT_MAX)
        up_unbigint = random.randint(constant.BIGINT_UN_MIN,constant.BIGINT_UN_MAX)
        up_bool = random.randint(0,100)%2
        up_float = random.uniform(constant.FLOAT_MIN,constant.FLOAT_MAX)
        up_double = random.uniform(constant.DOUBLE_MIN*(1E-300),constant.DOUBLE_MAX*(1E-300))
J
jiacy-jcy 已提交
157 158
        binary_length = random.randint(0,self.str_length)
        nchar_length = random.randint(0,self.str_length)
J
jiacy-jcy 已提交
159 160
        up_binary = tdCom.getLongName(binary_length)
        up_nchar = tdCom.getLongName(nchar_length)
J
update  
jiacy-jcy 已提交
161 162 163 164 165 166
        for col_name,col_type in column_dict.items():
            if tb_type == 'ntb':
                tdSql.execute(f'create table {tbname} (ts timestamp,{col_name} {col_type})')
            elif tb_type == 'ctb':
                tdSql.execute(f'create table {stbname} (ts timestamp,{col_name} {col_type}) tags(t0 int)')
                tdSql.execute(f'create table {tbname} using {stbname} tags(1)')
J
jiacy-jcy 已提交
167
            tdSql.execute(f'insert into {tbname} values({self.ts},null)')
J
jiacy-jcy 已提交
168
            if col_type.lower() == 'tinyint':
J
update  
jiacy-jcy 已提交
169
                self.update_and_check_data(tbname,col_name,col_type,up_tinyint,dbname)
J
jiacy-jcy 已提交
170
            elif col_type.lower() == 'smallint':
J
update  
jiacy-jcy 已提交
171
                self.update_and_check_data(tbname,col_name,col_type,up_smallint,dbname)
J
jiacy-jcy 已提交
172
            elif col_type.lower() == 'int':
J
update  
jiacy-jcy 已提交
173
                self.update_and_check_data(tbname,col_name,col_type,up_int,dbname)
J
jiacy-jcy 已提交
174
            elif col_type.lower() == 'bigint':
J
update  
jiacy-jcy 已提交
175
                self.update_and_check_data(tbname,col_name,col_type,up_bigint,dbname)
J
jiacy-jcy 已提交
176
            elif col_type.lower() == 'tinyint unsigned':
J
update  
jiacy-jcy 已提交
177
                self.update_and_check_data(tbname,col_name,col_type,up_untinyint,dbname)
J
jiacy-jcy 已提交
178
            elif col_type.lower() == 'smallint unsigned':
J
update  
jiacy-jcy 已提交
179
                self.update_and_check_data(tbname,col_name,col_type,up_unsmallint,dbname)
J
jiacy-jcy 已提交
180
            elif col_type.lower() == 'int unsigned':
J
update  
jiacy-jcy 已提交
181
                self.update_and_check_data(tbname,col_name,col_type,up_unint,dbname)
J
jiacy-jcy 已提交
182
            elif col_type.lower() == 'bigint unsigned':
J
update  
jiacy-jcy 已提交
183
                self.update_and_check_data(tbname,col_name,col_type,up_unbigint,dbname)
J
jiacy-jcy 已提交
184
            elif col_type.lower() == 'bool':
J
update  
jiacy-jcy 已提交
185
                self.update_and_check_data(tbname,col_name,col_type,up_bool,dbname)    
J
jiacy-jcy 已提交
186
            elif col_type.lower() == 'float':
J
update  
jiacy-jcy 已提交
187
                self.update_and_check_data(tbname,col_name,col_type,up_float,dbname)      
J
jiacy-jcy 已提交
188
            elif col_type.lower() == 'double':
J
update  
jiacy-jcy 已提交
189
                self.update_and_check_data(tbname,col_name,col_type,up_double,dbname)
J
jiacy-jcy 已提交
190
            elif 'binary' in col_type.lower():
J
update  
jiacy-jcy 已提交
191
                self.update_and_check_data(tbname,col_name,col_type,up_binary,dbname)
J
jiacy-jcy 已提交
192
            elif 'nchar' in col_type.lower():
J
update  
jiacy-jcy 已提交
193
                self.update_and_check_data(tbname,col_name,col_type,up_nchar,dbname)
J
update  
jiacy-jcy 已提交
194 195
            elif col_type.lower() == 'timestamp':
                self.update_and_check_data(tbname,col_name,col_type,self.ts+1,dbname)
J
update  
jiacy-jcy 已提交
196 197
            tdSql.execute(f'insert into {tbname} values({self.ts},null)')
            tdSql.query(f'select {col_name} from {tbname}')
J
jiacy-jcy 已提交
198
            tdSql.checkEqual(tdSql.queryResult[0][0],None)
J
update  
jiacy-jcy 已提交
199
            tdSql.execute(f'flush database {self.dbname}')
J
update  
jiacy-jcy 已提交
200 201
            tdSql.execute('reset query cache')
            tdSql.query(f'select {col_name} from {tbname}')
J
update  
jiacy-jcy 已提交
202
            tdSql.checkEqual(tdSql.queryResult[0][0],None)
J
update  
jiacy-jcy 已提交
203 204 205 206
            tdSql.execute(f'drop table {tbname}')
            if tb_type == 'ctb':
                tdSql.execute(f'drop table {stbname}')
    def update_check(self):
J
jiacy-jcy 已提交
207 208 209
        tdSql.execute(f'drop database if exists {self.dbname}')
        tdSql.execute(f'create database {self.dbname}')
        tdSql.execute(f'use {self.dbname}')
J
update  
jiacy-jcy 已提交
210
        self.update_data_check(self.ntbname,self.column_dict,self.dbname,'ntb')
J
jiacy-jcy 已提交
211 212
        for col_name,col_type in self.column_dict.items():
            tdSql.execute(f'create table {self.ntbname} (ts timestamp,{col_name} {col_type})')
J
jiacy-jcy 已提交
213
            tdSql.execute(f'insert into {self.ntbname} values({self.ts},null)')
J
update  
jiacy-jcy 已提交
214 215 216 217 218 219 220 221
            if 'binary' in col_type.lower():
                up_binary = tdCom.getLongName(self.str_length+1)
                tdSql.execute(f'alter table {self.ntbname} modify column {col_name} binary({self.str_length+1})')
                self.update_and_check_data(self.ntbname,col_name,col_type,up_binary,self.dbname)
            elif 'nchar' in col_type.lower():
                up_nchar = tdCom.getLongName(self.str_length+1)
                tdSql.execute(f'alter table {self.ntbname} modify column {col_name} nchar({self.str_length+1})')
                self.update_and_check_data(self.ntbname,col_name,col_type,up_nchar,self.dbname)
J
jiacy-jcy 已提交
222
            tdSql.execute(f'drop table {self.ntbname}')
J
update  
jiacy-jcy 已提交
223 224 225 226
        self.update_data_check(self.ctbname,self.column_dict,self.dbname,'ctb',self.stbname)
        for col_name,col_type in self.column_dict.items():
            tdSql.execute(f'create table {self.stbname} (ts timestamp,{col_name} {col_type}) tags(t0 int)')
            tdSql.execute(f'create table {self.ctbname} using {self.stbname} tags(1)')
J
jiacy-jcy 已提交
227
            tdSql.execute(f'insert into {self.ctbname} values({self.ts},null)')
J
update  
jiacy-jcy 已提交
228 229 230 231 232 233 234 235 236
            if 'binary' in col_type.lower():
                up_binary = tdCom.getLongName(self.str_length+1)
                tdSql.execute(f'alter table {self.stbname} modify column {col_name} binary({self.str_length+1})')
                self.update_and_check_data(self.ctbname,col_name,col_type,up_binary,self.dbname)
            elif 'nchar' in col_type.lower():
                up_nchar = tdCom.getLongName(self.str_length+1)
                tdSql.execute(f'alter table {self.stbname} modify column {col_name} nchar({self.str_length+1})')
                self.update_and_check_data(self.ctbname,col_name,col_type,up_nchar,self.dbname)
            tdSql.execute(f'drop table {self.stbname}')
J
jiacy-jcy 已提交
237

J
update  
jiacy-jcy 已提交
238 239 240 241 242 243
    def update_check_error(self):
        tdSql.execute(f'drop database if exists {self.dbname}')
        tdSql.execute(f'create database {self.dbname}')
        tdSql.execute(f'use {self.dbname}')
        self.error_check(self.ntbname,self.column_dict,'ntb')
        self.error_check(self.ctbname,self.column_dict,'ctb',self.stbname)
J
jiacy-jcy 已提交
244

J
update  
jiacy-jcy 已提交
245
    def run(self):
J
update  
jiacy-jcy 已提交
246 247
        #!bug TD-17708 and TD-17709
        # for i in range(10):
J
jiacy-jcy 已提交
248 249
            self.update_check()
            self.update_check_error()
J
update  
jiacy-jcy 已提交
250
            # i+=1
J
update  
jiacy-jcy 已提交
251
        
J
jiacy-jcy 已提交
252 253 254 255 256 257
    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)

tdCases.addWindows(__file__, TDTestCase())
tdCases.addLinux(__file__, TDTestCase())