count.py 5.8 KB
Newer Older
J
jiacy-jcy 已提交
1 2 3
from util.log import *
from util.sql import *
from util.cases import *
J
jiacy-jcy 已提交
4
from util.sqlset import *
J
jiacy-jcy 已提交
5 6 7
class TDTestCase:
    def init(self, conn, logSql):
        tdLog.debug("start to execute %s" % __file__)
J
jiacy-jcy 已提交
8 9
        tdSql.init(conn.cursor(),logSql)
        self.setsql = TDSetSql()
J
jiacy-jcy 已提交
10 11 12
        self.rowNum = 10
        self.ts = 1537146000000

J
jiacy-jcy 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
        self.ntbname = 'ntb'
        self.stbname = 'stb'
        self.column_dict = {
            'ts':'timestamp',
            'c1':'int',
            'c2':'float',
            'c3':'double',
            'c4':'timestamp'
        }
        self.tag_dict = {
            't0':'int'
        }
        # The number of tag_values should be same as tbnum
        self.tbnum = 2
        self.tag_values = [
            f'10',
            f'100'
        ]
J
update  
jiacy-jcy 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    def query_stb(self,k,stbname,tbnum,rownum):
        tdSql.query(f'select count({k}) from {stbname}')
        tdSql.checkEqual(tdSql.queryResult[0][0],tbnum*rownum)
        tdSql.query(f'select count({k}) from {stbname} where ts <={self.ts+self.rowNum-1}')
        tdSql.checkEqual(tdSql.queryResult[0][0],tbnum*rownum)
        tdSql.query(f'select count({k}) from {stbname} where ts <={self.ts+self.rowNum-2}')
        tdSql.checkEqual(tdSql.queryResult[0][0],tbnum*(rownum-1))
    def query_ctb(self,k,i,stbname,rownum):
        tdSql.query(f'select count({k}) from {stbname}_{i}')
        tdSql.checkEqual(tdSql.queryResult[0][0],rownum)
        tdSql.query(f'select count({k}) from {stbname}_{i} where ts <={self.ts+self.rowNum-1}')
        tdSql.checkEqual(tdSql.queryResult[0][0],rownum)
        tdSql.query(f'select count({k}) from {stbname}_{i} where ts <={self.ts+self.rowNum-2}')
        tdSql.checkEqual(tdSql.queryResult[0][0],rownum-1)
    def query_ntb(self,k,ntbname,rownum):
        tdSql.query(f'select count({k}) from {ntbname}')
        tdSql.checkEqual(tdSql.queryResult[0][0],rownum)
        tdSql.query(f'select count({k}) from {ntbname} where ts <={self.ts+self.rowNum-1}')
        tdSql.checkEqual(tdSql.queryResult[0][0],rownum)
        tdSql.query(f'select count({k}) from {ntbname} where ts <={self.ts+self.rowNum-2}')
        tdSql.checkEqual(tdSql.queryResult[0][0],rownum-1)
    def count_query_stb(self,column_dict,tag_dict,stbname,tbnum,rownum):
        tdSql.query(f'select count(tbname) from {stbname}')
        tdSql.checkEqual(tdSql.queryResult[0][0],tbnum*rownum)
        tdSql.query(f'SELECT count(*) from (select distinct tbname from {stbname})')
        tdSql.checkEqual(tdSql.queryResult[0][0],tbnum)
        for k in column_dict.keys():
            self.query_stb(k,stbname,tbnum,rownum)
        for k in tag_dict.keys():
            self.query_stb(k,stbname,tbnum,rownum)
    def count_query_ctb(self,column_dict,tag_dict,stbname,tbnum,rownum):
        for i in range(tbnum):
            tdSql.query(f'select count(tbname) from {stbname}_{i}')
            tdSql.checkEqual(tdSql.queryResult[0][0],rownum)
            for k in column_dict.keys():
                self.query_ctb(k,i,stbname,rownum)
            for k in tag_dict.keys():
                self.query_ctb(k,i,stbname,rownum)
    def count_query_ntb(self,column_dict,ntbname,rownum):
        tdSql.query(f'select count(tbname) from {ntbname}')
        tdSql.checkEqual(tdSql.queryResult[0][0],rownum)
        for k in column_dict.keys():
            self.query_ntb(k,ntbname,rownum)
    def insert_data(self,column_dict,tbname,row_num):
        insert_sql = self.setsql.set_insertsql(column_dict,tbname)
        for i in range(row_num):
            insert_list = []
            self.setsql.insert_values(column_dict,i,insert_sql,insert_list,self.ts)
J
jiacy-jcy 已提交
79
    def check_ntb(self):
J
jiacy-jcy 已提交
80
        tdSql.prepare()
J
jiacy-jcy 已提交
81
        tdSql.execute(self.setsql.set_create_normaltable_sql(self.ntbname,self.column_dict))
J
update  
jiacy-jcy 已提交
82 83 84 85 86 87 88 89 90 91
        tdSql.query(f'select count(tbname) from {self.ntbname}')
        tdSql.checkRows(0)
        tdSql.execute('flush database db')
        tdSql.query(f'select count(tbname) from {self.ntbname}')
        tdSql.checkRows(0)
        self.insert_data(self.column_dict,self.ntbname,self.rowNum)
        self.count_query_ntb(self.column_dict,self.ntbname,self.rowNum)
        tdSql.execute('flush database db')
        self.count_query_ntb(self.column_dict,self.ntbname,self.rowNum)
        tdSql.execute('drop database db')
J
jiacy-jcy 已提交
92 93 94 95 96
    def check_stb(self):
        tdSql.prepare()
        tdSql.execute(self.setsql.set_create_stable_sql(self.stbname,self.column_dict,self.tag_dict))
        for i in range(self.tbnum):
            tdSql.execute(f'create table {self.stbname}_{i} using {self.stbname} tags({self.tag_values[i]})')
J
update  
jiacy-jcy 已提交
97 98
        tdSql.query(f'SELECT count(*) from (select distinct tbname from {self.stbname})')
        tdSql.checkEqual(tdSql.queryResult[0][0],self.tbnum)
J
jiacy-jcy 已提交
99 100 101 102 103
        tdSql.query(f'select count(tbname) from {self.stbname}')
        tdSql.checkRows(0)
        tdSql.execute('flush database db')
        tdSql.query(f'select count(tbname) from {self.stbname}')
        tdSql.checkRows(0)
J
update  
jiacy-jcy 已提交
104 105
        tdSql.query(f'SELECT count(*) from (select distinct tbname from {self.stbname})')
        tdSql.checkEqual(tdSql.queryResult[0][0],self.tbnum)
J
jiacy-jcy 已提交
106
        for i in range(self.tbnum):
J
update  
jiacy-jcy 已提交
107 108
            self.insert_data(self.column_dict,f'{self.stbname}_{i}',self.rowNum)
        self.count_query_stb(self.column_dict,self.tag_dict,self.stbname,self.tbnum,self.rowNum)
J
update  
jiacy-jcy 已提交
109
        self.count_query_ctb(self.column_dict,self.tag_dict,self.stbname,self.tbnum,self.rowNum)
J
jiacy-jcy 已提交
110
        tdSql.execute('flush database db')
J
update  
jiacy-jcy 已提交
111 112 113
        self.count_query_stb(self.column_dict,self.tag_dict,self.stbname,self.tbnum,self.rowNum)
        self.count_query_ctb(self.column_dict,self.tag_dict,self.stbname,self.tbnum,self.rowNum)
        tdSql.execute('drop database db')
J
jiacy-jcy 已提交
114 115
    def run(self):
        self.check_stb()
J
update  
jiacy-jcy 已提交
116
        self.check_ntb()
J
jiacy-jcy 已提交
117 118 119 120 121 122
    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)


tdCases.addWindows(__file__, TDTestCase())
G
Ganlin Zhao 已提交
123
tdCases.addLinux(__file__, TDTestCase())