delete_data.py 16.7 KB
Newer Older
J
Jason-Jia 已提交
1

J
jiacy-jcy 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
###################################################################
#           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
update  
jiacy-jcy 已提交
17 18

from numpy import logspace
J
jiacy-jcy 已提交
19 20 21 22 23 24 25 26
from util import constant
from util.log import *
from util.cases import *
from util.sql import *
from util.common import *
from util.sqlset import TDSetSql

class TDTestCase:
27
    def init(self, conn, logSql, replicaVar=1):
28
        self.replicaVar = int(replicaVar)
J
jiacy-jcy 已提交
29
        tdLog.debug("start to execute %s" % __file__)
J
update  
jiacy-jcy 已提交
30
        tdSql.init(conn.cursor())
J
jiacy-jcy 已提交
31 32
        self.dbname = 'db_test'
        self.setsql = TDSetSql()
J
update  
jiacy-jcy 已提交
33
        self.stbname = 'stb'
J
jiacy-jcy 已提交
34
        self.ntbname = 'ntb'
J
jiacy-jcy 已提交
35 36
        self.rowNum = 10
        self.tbnum = 3
J
jiacy-jcy 已提交
37 38 39
        self.ts = 1537146000000
        self.binary_str = 'taosdata'
        self.nchar_str = '涛思数据'
J
update  
jiacy-jcy 已提交
40
        self.str_length = 20
J
jiacy-jcy 已提交
41 42
        self.column_dict = {
            'col1': 'tinyint',
J
update  
jiacy-jcy 已提交
43 44 45 46 47 48 49 50 51 52 53 54
            'col2': 'smallint',
            'col3': 'int',
            'col4': 'bigint',
            'col5': 'tinyint unsigned',
            'col6': 'smallint unsigned',
            'col7': 'int unsigned',
            'col8': 'bigint unsigned',
            'col9': 'float',
            'col10': 'double',
            'col11': 'bool',
            'col12': f'binary({self.str_length})',
            'col13': f'nchar({self.str_length})',
G
Ganlin Zhao 已提交
55

J
jiacy-jcy 已提交
56
        }
G
Ganlin Zhao 已提交
57

J
update  
jiacy-jcy 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        self.tinyint_val = random.randint(constant.TINYINT_MIN,constant.TINYINT_MAX)
        self.smallint_val = random.randint(constant.SMALLINT_MIN,constant.SMALLINT_MAX)
        self.int_val = random.randint(constant.INT_MIN,constant.INT_MAX)
        self.bigint_val = random.randint(constant.BIGINT_MIN,constant.BIGINT_MAX)
        self.untingint_val = random.randint(constant.TINYINT_UN_MIN,constant.TINYINT_UN_MAX)
        self.unsmallint_val = random.randint(constant.SMALLINT_UN_MIN,constant.SMALLINT_UN_MAX)
        self.unint_val = random.randint(constant.INT_UN_MIN,constant.INT_MAX)
        self.unbigint_val = random.randint(constant.BIGINT_UN_MIN,constant.BIGINT_UN_MAX)
        self.float_val = random.uniform(constant.FLOAT_MIN,constant.FLOAT_MAX)
        self.double_val = random.uniform(constant.DOUBLE_MIN*(1E-300),constant.DOUBLE_MAX*(1E-300))
        self.bool_val = random.randint(0,100)%2
        self.binary_val = tdCom.getLongName(random.randint(0,self.str_length))
        self.nchar_val = tdCom.getLongName(random.randint(0,self.str_length))
        self.base_data = {
            'tinyint':self.tinyint_val,
            'smallint':self.smallint_val,
            'int':self.int_val,
            'bigint':self.bigint_val,
            'tinyint unsigned':self.untingint_val,
            'smallint unsigned':self.unsmallint_val,
            'int unsigned':self.unint_val,
            'bigint unsigned':self.unbigint_val,
            'bool':self.bool_val,
            'float':self.float_val,
            'double':self.double_val,
            'binary':self.binary_val,
            'nchar':self.nchar_val
                    }
J
Jason-Jia 已提交
86
        
J
update  
jiacy-jcy 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    def insert_base_data(self,col_type,tbname,rows,base_data):
        for i in range(rows):
            if col_type.lower() == 'tinyint':
                tdSql.execute(f'insert into {tbname} values({self.ts+i},{base_data["tinyint"]})')
            elif col_type.lower() == 'smallint':
                tdSql.execute(f'insert into {tbname} values({self.ts+i},{base_data["smallint"]})')
            elif col_type.lower() == 'int':
                tdSql.execute(f'insert into {tbname} values({self.ts+i},{base_data["int"]})')
            elif col_type.lower() == 'bigint':
                tdSql.execute(f'insert into {tbname} values({self.ts+i},{base_data["bigint"]})')
            elif col_type.lower() == 'tinyint unsigned':
                tdSql.execute(f'insert into {tbname} values({self.ts+i},{base_data["tinyint unsigned"]})')
            elif col_type.lower() == 'smallint unsigned':
                tdSql.execute(f'insert into {tbname} values({self.ts+i},{base_data["smallint unsigned"]})')
            elif col_type.lower() == 'int unsigned':
                tdSql.execute(f'insert into {tbname} values({self.ts+i},{base_data["int unsigned"]})')
            elif col_type.lower() == 'bigint unsigned':
                tdSql.execute(f'insert into {tbname} values({self.ts+i},{base_data["bigint unsigned"]})')
            elif col_type.lower() == 'bool':
G
Ganlin Zhao 已提交
106
                tdSql.execute(f'insert into {tbname} values({self.ts+i},{base_data["bool"]})')
J
update  
jiacy-jcy 已提交
107
            elif col_type.lower() == 'float':
G
Ganlin Zhao 已提交
108
                tdSql.execute(f'insert into {tbname} values({self.ts+i},{base_data["float"]})')
J
update  
jiacy-jcy 已提交
109 110 111 112 113
            elif col_type.lower() == 'double':
                tdSql.execute(f'insert into {tbname} values({self.ts+i},{base_data["double"]})')
            elif 'binary' in col_type.lower():
                tdSql.execute(f'''insert into {tbname} values({self.ts+i},"{base_data['binary']}")''')
            elif 'nchar' in col_type.lower():
G
Ganlin Zhao 已提交
114
                tdSql.execute(f'''insert into {tbname} values({self.ts+i},"{base_data['nchar']}")''')
J
jiacy-jcy 已提交
115 116
    def delete_all_data(self,tbname,col_type,row_num,base_data,dbname,tb_type,tb_num=1,stbname=''):
        tdSql.query(f'select count(*) from {tbname}')
J
jiacy-jcy 已提交
117
        tdSql.execute(f'delete from {tbname}')
J
update  
jiacy-jcy 已提交
118 119
        tdSql.execute(f'flush database {dbname}')
        tdSql.execute('reset query cache')
J
jiacy-jcy 已提交
120 121
        tdSql.query(f'select * from {tbname}')
        tdSql.checkRows(0)
J
jiacy-jcy 已提交
122
        if tb_type == 'ntb' or tb_type == 'ctb':
J
jiacy-jcy 已提交
123 124 125 126 127 128 129 130
            if tb_type == 'ctb':
                tdSql.query(f'select count(*) from {stbname}')
                if tb_num <= 1:
                    if len(tdSql.queryResult) != 0:
                        tdLog.exit('delete case failure!')
                else:
                    tdSql.checkEqual(tdSql.queryResult[0][0],(tb_num-1)*row_num)

J
jiacy-jcy 已提交
131 132 133 134
            self.insert_base_data(col_type,tbname,row_num,base_data)
        elif tb_type == 'stb':
            for i in range(tb_num):
                self.insert_base_data(col_type,f'{tbname}_{i}',row_num,base_data)
J
update  
jiacy-jcy 已提交
135 136 137
        tdSql.execute(f'flush database {dbname}')
        tdSql.execute('reset query cache')
        tdSql.query(f'select * from {tbname}')
J
jiacy-jcy 已提交
138 139 140 141 142
        if tb_type == 'ntb' or tb_type == 'ctb':
            tdSql.checkRows(row_num)
        elif tb_type =='stb':
            tdSql.checkRows(row_num*tb_num)
    def delete_one_row(self,tbname,column_type,column_name,base_data,row_num,dbname,tb_type,tb_num=1):
J
update  
jiacy-jcy 已提交
143 144 145 146
        tdSql.execute(f'delete from {tbname} where ts={self.ts}')
        tdSql.execute(f'flush database {dbname}')
        tdSql.execute('reset query cache')
        tdSql.query(f'select {column_name} from {tbname}')
J
jiacy-jcy 已提交
147 148 149 150
        if tb_type == 'ntb' or tb_type == 'ctb':
            tdSql.checkRows(row_num-1)
        elif tb_type == 'stb':
            tdSql.checkRows((row_num-1)*tb_num)
J
update  
jiacy-jcy 已提交
151 152
        tdSql.query(f'select {column_name} from {tbname} where ts={self.ts}')
        tdSql.checkRows(0)
J
jiacy-jcy 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        if tb_type == 'ntb' or tb_type == 'ctb':
            if 'binary' in column_type.lower():
                tdSql.execute(f'''insert into {tbname} values({self.ts},"{base_data['binary']}")''')
            elif 'nchar' in column_type.lower():
                tdSql.execute(f'''insert into {tbname} values({self.ts},"{base_data['nchar']}")''')
            else:
                tdSql.execute(f'insert into {tbname} values({self.ts},{base_data[column_type]})')
        elif tb_type == 'stb':
            for i in range(tb_num):
                if 'binary' in column_type.lower():
                    tdSql.execute(f'''insert into {tbname}_{i} values({self.ts},"{base_data['binary']}")''')
                elif 'nchar' in column_type.lower():
                    tdSql.execute(f'''insert into {tbname}_{i} values({self.ts},"{base_data['nchar']}")''')
                else:
                    tdSql.execute(f'insert into {tbname}_{i} values({self.ts},{base_data[column_type]})')
J
update  
jiacy-jcy 已提交
168 169 170 171 172 173 174 175 176 177 178
        tdSql.query(f'select {column_name} from {tbname} where ts={self.ts}')
        if column_type.lower() == 'float' or column_type.lower() == 'double':
            if abs(tdSql.queryResult[0][0] - base_data[column_type]) / base_data[column_type] <= 0.0001:
                tdSql.checkEqual(tdSql.queryResult[0][0],tdSql.queryResult[0][0])
            else:
                tdLog.exit(f'{column_type} data check failure')
        elif 'binary' in column_type.lower():
            tdSql.checkEqual(tdSql.queryResult[0][0],base_data['binary'])
        elif 'nchar' in column_type.lower():
            tdSql.checkEqual(tdSql.queryResult[0][0],base_data['nchar'])
        else:
G
Ganlin Zhao 已提交
179
            tdSql.checkEqual(tdSql.queryResult[0][0],base_data[column_type])
J
jiacy-jcy 已提交
180 181
    def delete_rows(self,dbname,tbname,col_name,col_type,base_data,row_num,tb_type,tb_num=1):
        for i in range(row_num):
J
update  
jiacy-jcy 已提交
182 183 184 185
            tdSql.execute(f'delete from {tbname} where ts>{self.ts+i}')
            tdSql.execute(f'flush database {dbname}')
            tdSql.execute('reset query cache')
            tdSql.query(f'select {col_name} from {tbname}')
J
jiacy-jcy 已提交
186 187 188 189 190 191 192 193
            if tb_type == 'ntb' or tb_type == 'ctb':
                tdSql.checkRows(i+1)
                self.insert_base_data(col_type,tbname,row_num,base_data)
            elif tb_type == 'stb':
                tdSql.checkRows((i+1)*tb_num)
                for j in range(tb_num):
                    self.insert_base_data(col_type,f'{tbname}_{j}',row_num,base_data)
        for i in range(row_num):
J
update  
jiacy-jcy 已提交
194 195 196 197
            tdSql.execute(f'delete from {tbname} where ts>={self.ts+i}')
            tdSql.execute(f'flush database {dbname}')
            tdSql.execute('reset query cache')
            tdSql.query(f'select {col_name} from {tbname}')
J
jiacy-jcy 已提交
198 199 200 201 202 203
            if tb_type == 'ntb' or tb_type == 'ctb':
                tdSql.checkRows(i)
                self.insert_base_data(col_type,tbname,row_num,base_data)
            elif tb_type == 'stb':
                tdSql.checkRows(i*tb_num)
                for j in range(tb_num):
G
Ganlin Zhao 已提交
204
                    self.insert_base_data(col_type,f'{tbname}_{j}',row_num,base_data)
J
jiacy-jcy 已提交
205
        for i in range(row_num):
J
update  
jiacy-jcy 已提交
206 207 208 209
            tdSql.execute(f'delete from {tbname} where ts<={self.ts+i}')
            tdSql.execute(f'flush database {dbname}')
            tdSql.execute('reset query cache')
            tdSql.query(f'select {col_name} from {tbname}')
J
jiacy-jcy 已提交
210 211 212 213 214 215 216 217
            if tb_type == 'ntb' or tb_type == 'ctb':
                tdSql.checkRows(row_num-i-1)
                self.insert_base_data(col_type,tbname,row_num,base_data)
            elif tb_type == 'stb':
                tdSql.checkRows((row_num-i-1)*tb_num)
                for j in range(tb_num):
                    self.insert_base_data(col_type,f'{tbname}_{j}',row_num,base_data)
        for i in range(row_num):
J
update  
jiacy-jcy 已提交
218 219 220 221
            tdSql.execute(f'delete from {tbname} where ts<{self.ts+i}')
            tdSql.execute(f'flush database {dbname}')
            tdSql.execute('reset query cache')
            tdSql.query(f'select {col_name} from {tbname}')
J
jiacy-jcy 已提交
222 223 224 225 226 227 228
            if tb_type == 'ntb' or tb_type == 'ctb':
                tdSql.checkRows(row_num-i)
                self.insert_base_data(col_type,tbname,row_num,base_data)
            elif tb_type == 'stb':
                tdSql.checkRows((row_num-i)*tb_num)
                for j in range(tb_num):
                    self.insert_base_data(col_type,f'{tbname}_{j}',row_num,base_data)
J
update  
jiacy-jcy 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
        for i in range(row_num):
            tdSql.execute(f'delete from {tbname} where ts between {self.ts} and {self.ts+i}')
            tdSql.execute(f'flush database {dbname}')
            tdSql.execute('reset query cache')
            tdSql.query(f'select {col_name} from {tbname}')
            if tb_type == 'ntb' or tb_type == 'ctb':
                tdSql.checkRows(row_num - i-1)
                self.insert_base_data(col_type,tbname,row_num,base_data)
            elif tb_type == 'stb':
                tdSql.checkRows(tb_num*(row_num - i-1))
                for j in range(tb_num):
                    self.insert_base_data(col_type,f'{tbname}_{j}',row_num,base_data)
            tdSql.execute(f'delete from {tbname} where ts between {self.ts+i+1} and {self.ts}')
            tdSql.query(f'select {col_name} from {tbname}')
            if tb_type == 'ntb' or tb_type == 'ctb':
                tdSql.checkRows(row_num)
            elif tb_type == 'stb':
                tdSql.checkRows(tb_num*row_num)
J
update  
jiacy-jcy 已提交
247 248 249 250 251 252 253
    def delete_error(self,tbname,column_name,column_type,base_data):
        for error_list in ['',f'ts = {self.ts} and',f'ts = {self.ts} or']:
            if 'binary' in column_type.lower():
                tdSql.error(f'''delete from {tbname} where {error_list} {column_name} ="{base_data['binary']}"''')
            elif 'nchar' in column_type.lower():
                tdSql.error(f'''delete from {tbname} where {error_list} {column_name} ="{base_data['nchar']}"''')
            else:
J
update  
jiacy-jcy 已提交
254
                tdSql.error(f'delete from {tbname} where {error_list} {column_name} = {base_data[column_type]}')
G
Ganlin Zhao 已提交
255

J
jiacy-jcy 已提交
256 257 258
    def delete_data_ntb(self):
        tdSql.execute(f'create database if not exists {self.dbname}')
        tdSql.execute(f'use {self.dbname}')
J
update  
jiacy-jcy 已提交
259 260 261
        for col_name,col_type in self.column_dict.items():
            tdSql.execute(f'create table {self.ntbname} (ts timestamp,{col_name} {col_type})')
            self.insert_base_data(col_type,self.ntbname,self.rowNum,self.base_data)
J
jiacy-jcy 已提交
262 263
            self.delete_one_row(self.ntbname,col_type,col_name,self.base_data,self.rowNum,self.dbname,'ntb')
            self.delete_all_data(self.ntbname,col_type,self.rowNum,self.base_data,self.dbname,'ntb')
J
update  
jiacy-jcy 已提交
264
            self.delete_error(self.ntbname,col_name,col_type,self.base_data)
J
jiacy-jcy 已提交
265 266 267
            self.delete_rows(self.dbname,self.ntbname,col_name,col_type,self.base_data,self.rowNum,'ntb')
            for func in ['first','last']:
                tdSql.query(f'select {func}(*) from {self.ntbname}')
J
update  
jiacy-jcy 已提交
268
            tdSql.execute(f'drop table {self.ntbname}')
J
update  
jiacy-jcy 已提交
269 270 271 272 273 274 275 276 277
        tdSql.execute(f'drop database {self.dbname}')
    def delete_data_ctb(self):
        tdSql.execute(f'create database if not exists {self.dbname}')
        tdSql.execute(f'use {self.dbname}')
        for col_name,col_type in self.column_dict.items():
            tdSql.execute(f'create table {self.stbname} (ts timestamp,{col_name} {col_type}) tags(t1 int)')
            for i in range(self.tbnum):
                tdSql.execute(f'create table {self.stbname}_{i} using {self.stbname} tags(1)')
                self.insert_base_data(col_type,f'{self.stbname}_{i}',self.rowNum,self.base_data)
J
jiacy-jcy 已提交
278
                self.delete_one_row(f'{self.stbname}_{i}',col_type,col_name,self.base_data,self.rowNum,self.dbname,'ctb')
J
jiacy-jcy 已提交
279
                self.delete_all_data(f'{self.stbname}_{i}',col_type,self.rowNum,self.base_data,self.dbname,'ctb',i+1,self.stbname)
J
update  
jiacy-jcy 已提交
280
                self.delete_error(f'{self.stbname}_{i}',col_name,col_type,self.base_data)
J
jiacy-jcy 已提交
281 282 283
                self.delete_rows(self.dbname,f'{self.stbname}_{i}',col_name,col_type,self.base_data,self.rowNum,'ctb')
                for func in ['first','last']:
                    tdSql.query(f'select {func}(*) from {self.stbname}_{i}')
J
update  
jiacy-jcy 已提交
284
            tdSql.execute(f'drop table {self.stbname}')
J
jiacy-jcy 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    def delete_data_stb(self):
        tdSql.execute(f'create database if not exists {self.dbname}')
        tdSql.execute(f'use {self.dbname}')
        for col_name,col_type in self.column_dict.items():
            tdSql.execute(f'create table {self.stbname} (ts timestamp,{col_name} {col_type}) tags(t1 int)')
            for i in range(self.tbnum):
                tdSql.execute(f'create table {self.stbname}_{i} using {self.stbname} tags(1)')
                self.insert_base_data(col_type,f'{self.stbname}_{i}',self.rowNum,self.base_data)
            self.delete_error(self.stbname,col_name,col_type,self.base_data)
            self.delete_one_row(self.stbname,col_type,col_name,self.base_data,self.rowNum,self.dbname,'stb',self.tbnum)
            self.delete_all_data(self.stbname,col_type,self.rowNum,self.base_data,self.dbname,'stb',self.tbnum)
            self.delete_rows(self.dbname,self.stbname,col_name,col_type,self.base_data,self.rowNum,'stb',self.tbnum)
            for func in ['first','last']:
                tdSql.query(f'select {func}(*) from {self.stbname}')
            tdSql.execute(f'drop table {self.stbname}')
        tdSql.execute(f'drop database {self.dbname}')
J
jiacy-jcy 已提交
301
    def run(self):
J
update  
jiacy-jcy 已提交
302
        self.delete_data_ntb()
J
jiacy-jcy 已提交
303 304
        self.delete_data_ctb()
        self.delete_data_stb()
J
jiacy-jcy 已提交
305 306 307
        tdDnodes.stoptaosd(1)
        tdDnodes.starttaosd(1)
        self.delete_data_ntb()
J
jiacy-jcy 已提交
308 309 310 311 312
    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)

tdCases.addWindows(__file__, TDTestCase())
J
Jason-Jia 已提交
313
tdCases.addLinux(__file__, TDTestCase())