timetruncate.py 10.1 KB
Newer Older
J
jiacy-jcy 已提交
1 2 3 4 5
import taos
from util.log import *
from util.cases import *
from util.sql import *
import numpy as np
J
update  
jiacy-jcy 已提交
6 7
import time
from datetime import datetime
J
jiacy-jcy 已提交
8
from util.gettime import *
J
jiacy-jcy 已提交
9 10 11 12
class TDTestCase:
    def init(self, conn, logSql):
        tdLog.debug("start to execute %s" % __file__)
        tdSql.init(conn.cursor())
J
jiacy-jcy 已提交
13
        self.get_time = GetTime()
J
update  
jiacy-jcy 已提交
14 15 16 17 18 19 20 21
        self.ts_str = [
            '2020-1-1',
            '2020-2-1 00:00:01',
            '2020-3-1 00:00:00.001',
            '2020-4-1 00:00:00.001002',
            '2020-5-1 00:00:00.001002001'
        ]
        self.db_param_precision = ['ms','us','ns']
J
jiacy-jcy 已提交
22
        self.time_unit = ['1w','1d','1h','1m','1s','1a','1u','1b']
J
jiacy-jcy 已提交
23
        self.error_unit = ['2w','2d','2h','2m','2s','2a','2u','1c','#1']
J
jiacy-jcy 已提交
24 25 26 27
        self.dbname = 'db'
        self.ntbname = f'{self.dbname}.ntb'
        self.stbname = f'{self.dbname}.stb'
        self.ctbname = f'{self.dbname}.ctb'
C
cpwu 已提交
28

J
jiacy-jcy 已提交
29 30 31
    def check_ms_timestamp(self,unit,date_time):
        if unit.lower() == '1a':
            for i in range(len(self.ts_str)):
J
jiacy-jcy 已提交
32
                ts_result = self.get_time.get_ms_timestamp(str(tdSql.queryResult[i][0]))
J
jiacy-jcy 已提交
33 34 35
                tdSql.checkEqual(ts_result,int(date_time[i]))
        elif unit.lower() == '1s':
            for i in range(len(self.ts_str)):
J
jiacy-jcy 已提交
36
                ts_result = self.get_time.get_ms_timestamp(str(tdSql.queryResult[i][0]))
J
jiacy-jcy 已提交
37 38 39
                tdSql.checkEqual(ts_result,int(date_time[i]/1000)*1000)
        elif unit.lower() == '1m':
            for i in range(len(self.ts_str)):
J
jiacy-jcy 已提交
40
                ts_result = self.get_time.get_ms_timestamp(str(tdSql.queryResult[i][0]))
J
jiacy-jcy 已提交
41 42 43
                tdSql.checkEqual(ts_result,int(date_time[i]/1000/60)*60*1000)
        elif unit.lower() == '1h':
            for i in range(len(self.ts_str)):
J
jiacy-jcy 已提交
44
                ts_result = self.get_time.get_ms_timestamp(str(tdSql.queryResult[i][0]))
J
jiacy-jcy 已提交
45 46 47
                tdSql.checkEqual(ts_result,int(date_time[i]/1000/60/60)*60*60*1000  )
        elif unit.lower() == '1d':
            for i in range(len(self.ts_str)):
J
jiacy-jcy 已提交
48
                ts_result = self.get_time.get_ms_timestamp(str(tdSql.queryResult[i][0]))
C
cpwu 已提交
49
                tdSql.checkEqual(ts_result,int(date_time[i]/1000/60/60/24)*24*60*60*1000)
J
jiacy-jcy 已提交
50 51
        elif unit.lower() == '1w':
            for i in range(len(self.ts_str)):
J
jiacy-jcy 已提交
52
                ts_result = self.get_time.get_ms_timestamp(str(tdSql.queryResult[i][0]))
J
jiacy-jcy 已提交
53
                tdSql.checkEqual(ts_result,int(date_time[i]/1000/60/60/24/7)*7*24*60*60*1000)
C
cpwu 已提交
54

J
jiacy-jcy 已提交
55 56 57
    def check_us_timestamp(self,unit,date_time):
        if unit.lower() == '1u':
            for i in range(len(self.ts_str)):
J
jiacy-jcy 已提交
58
                ts_result = self.get_time.get_us_timestamp(str(tdSql.queryResult[i][0]))
J
jiacy-jcy 已提交
59 60 61
                tdSql.checkEqual(ts_result,int(date_time[i]))
        elif unit.lower() == '1a':
            for i in range(len(self.ts_str)):
J
jiacy-jcy 已提交
62
                ts_result = self.get_time.get_us_timestamp(str(tdSql.queryResult[i][0]))
J
jiacy-jcy 已提交
63 64 65
                tdSql.checkEqual(ts_result,int(date_time[i]/1000)*1000)
        elif unit.lower() == '1s':
            for i in range(len(self.ts_str)):
J
jiacy-jcy 已提交
66
                ts_result = self.get_time.get_us_timestamp(str(tdSql.queryResult[i][0]))
J
jiacy-jcy 已提交
67 68 69
                tdSql.checkEqual(ts_result,int(date_time[i]/1000/1000)*1000*1000)
        elif unit.lower() == '1m':
            for i in range(len(self.ts_str)):
J
jiacy-jcy 已提交
70
                ts_result = self.get_time.get_us_timestamp(str(tdSql.queryResult[i][0]))
J
jiacy-jcy 已提交
71 72 73
                tdSql.checkEqual(ts_result,int(date_time[i]/1000/1000/60)*60*1000*1000)
        elif unit.lower() == '1h':
            for i in range(len(self.ts_str)):
J
jiacy-jcy 已提交
74
                ts_result = self.get_time.get_us_timestamp(str(tdSql.queryResult[i][0]))
J
jiacy-jcy 已提交
75 76 77
                tdSql.checkEqual(ts_result,int(date_time[i]/1000/1000/60/60)*60*60*1000*1000  )
        elif unit.lower() == '1d':
            for i in range(len(self.ts_str)):
J
jiacy-jcy 已提交
78
                ts_result = self.get_time.get_us_timestamp(str(tdSql.queryResult[i][0]))
C
cpwu 已提交
79
                tdSql.checkEqual(ts_result,int(date_time[i]/1000/1000/60/60/24)*24*60*60*1000*1000 )
J
jiacy-jcy 已提交
80 81
        elif unit.lower() == '1w':
            for i in range(len(self.ts_str)):
J
jiacy-jcy 已提交
82
                ts_result = self.get_time.get_us_timestamp(str(tdSql.queryResult[i][0]))
J
jiacy-jcy 已提交
83
                tdSql.checkEqual(ts_result,int(date_time[i]/1000/1000/60/60/24/7)*7*24*60*60*1000*1000)
C
cpwu 已提交
84 85

    def check_ns_timestamp(self, unit, date_time:list):
J
jiacy-jcy 已提交
86 87
        if unit.lower() == '1b':
            for i in range(len(self.ts_str)):
C
cpwu 已提交
88 89
                ts_result = self.get_time.get_ns_timestamp(str(tdSql.queryResult[i][0]))
                tdSql.checkEqual(ts_result,int(date_time[i]))
J
jiacy-jcy 已提交
90
        elif unit.lower() == '1u':
J
jiacy-jcy 已提交
91
            for i in range(len(self.ts_str)):
C
cpwu 已提交
92 93
                ts_result = self.get_time.get_ns_timestamp(str(tdSql.queryResult[i][0]))
                tdSql.checkEqual(ts_result,int(date_time[i]*1000/1000/1000)*1000)
J
jiacy-jcy 已提交
94 95
        elif unit.lower() == '1a':
            for i in range(len(self.ts_str)):
C
cpwu 已提交
96 97
                ts_result = self.get_time.get_ns_timestamp(str(tdSql.queryResult[i][0]))
                tdSql.checkEqual(ts_result,int(date_time[i]*1000/1000/1000/1000)*1000*1000)
J
jiacy-jcy 已提交
98 99
        elif unit.lower() == '1s':
            for i in range(len(self.ts_str)):
C
cpwu 已提交
100 101
                ts_result = self.get_time.get_ns_timestamp(str(tdSql.queryResult[i][0]))
                tdSql.checkEqual(ts_result,int(date_time[i]*1000/1000/1000/1000/1000)*1000*1000*1000)
J
jiacy-jcy 已提交
102 103
        elif unit.lower() == '1m':
            for i in range(len(self.ts_str)):
C
cpwu 已提交
104 105
                ts_result = self.get_time.get_ns_timestamp(str(tdSql.queryResult[i][0]))
                tdSql.checkEqual(ts_result,int(date_time[i]*1000/1000/1000/1000/1000/60)*60*1000*1000*1000)
J
jiacy-jcy 已提交
106 107
        elif unit.lower() == '1h':
            for i in range(len(self.ts_str)):
C
cpwu 已提交
108 109
                ts_result = self.get_time.get_ns_timestamp(str(tdSql.queryResult[i][0]))
                tdSql.checkEqual(ts_result,int(date_time[i]*1000/1000/1000/1000/1000/60/60)*60*60*1000*1000*1000  )
J
jiacy-jcy 已提交
110 111
        elif unit.lower() == '1d':
            for i in range(len(self.ts_str)):
C
cpwu 已提交
112 113
                ts_result = self.get_time.get_ns_timestamp(str(tdSql.queryResult[i][0]))
                tdSql.checkEqual(ts_result,int(date_time[i]*1000/1000/1000/1000/1000/60/60/24)*24*60*60*1000*1000*1000 )
J
jiacy-jcy 已提交
114 115
        elif unit.lower() == '1w':
            for i in range(len(self.ts_str)):
C
cpwu 已提交
116 117 118
                ts_result = self.get_time.get_ns_timestamp(str(tdSql.queryResult[i][0]))
                tdSql.checkEqual(ts_result,int(date_time[i]*1000/1000/1000/1000/1000/60/60/24/7)*7*24*60*60*1000*1000*1000)

J
jiacy-jcy 已提交
119
    def check_tb_type(self,unit,tb_type):
C
cpwu 已提交
120
        if tb_type.lower() == 'ntb':
J
jiacy-jcy 已提交
121 122 123 124 125
            tdSql.query(f'select timetruncate(ts,{unit}) from {self.ntbname}')
        elif tb_type.lower() == 'ctb':
            tdSql.query(f'select timetruncate(ts,{unit}) from {self.ctbname}')
        elif tb_type.lower() == 'stb':
            tdSql.query(f'select timetruncate(ts,{unit}) from {self.stbname}')
C
cpwu 已提交
126

J
jiacy-jcy 已提交
127 128
    def data_check(self,date_time,precision,tb_type):
        for unit in self.time_unit:
J
jiacy-jcy 已提交
129
            if (unit.lower() == '1u' and precision.lower() == 'ms') or (unit.lower() == '1b' and precision.lower() == 'us') or (unit.lower() == '1b' and precision.lower() == 'ms'):
C
cpwu 已提交
130
                if tb_type.lower() == 'ntb':
J
jiacy-jcy 已提交
131 132 133 134 135 136
                    tdSql.error(f'select timetruncate(ts,{unit}) from {self.ntbname}')
                elif tb_type.lower() == 'ctb':
                    tdSql.error(f'select timetruncate(ts,{unit}) from {self.ctbname}')
                elif tb_type.lower() == 'stb':
                    tdSql.error(f'select timetruncate(ts,{unit}) from {self.stbname}')
            elif precision.lower() == 'ms':
J
jiacy-jcy 已提交
137
                self.check_tb_type(unit,tb_type)
J
jiacy-jcy 已提交
138 139 140
                tdSql.checkRows(len(self.ts_str))
                self.check_ms_timestamp(unit,date_time)
            elif precision.lower() == 'us':
J
jiacy-jcy 已提交
141
                self.check_tb_type(unit,tb_type)
J
jiacy-jcy 已提交
142 143 144
                tdSql.checkRows(len(self.ts_str))
                self.check_us_timestamp(unit,date_time)
            elif precision.lower() == 'ns':
J
jiacy-jcy 已提交
145
                self.check_tb_type(unit,tb_type)
J
jiacy-jcy 已提交
146 147 148 149 150 151 152 153 154
                tdSql.checkRows(len(self.ts_str))
                self.check_ns_timestamp(unit,date_time)
        for unit in self.error_unit:
            if tb_type.lower() == 'ntb':
                tdSql.error(f'select timetruncate(ts,{unit}) from {self.ntbname}')
            elif tb_type.lower() == 'ctb':
                tdSql.error(f'select timetruncate(ts,{unit}) from {self.ctbname}')
            elif tb_type.lower() == 'stb':
                tdSql.error(f'select timetruncate(ts,{unit}) from {self.stbname}')
C
cpwu 已提交
155

J
update  
jiacy-jcy 已提交
156 157
    def function_check_ntb(self):
        for precision in self.db_param_precision:
J
jiacy-jcy 已提交
158 159
            tdSql.execute(f'drop database if exists {self.dbname}')
            tdSql.execute(f'create database {self.dbname} precision "{precision}"')
C
cpwu 已提交
160
            tdLog.info(f"=====now is in a {precision} database=====")
J
jiacy-jcy 已提交
161
            tdSql.execute(f'use {self.dbname}')
J
jiacy-jcy 已提交
162
            tdSql.execute(f'create table {self.ntbname} (ts timestamp,c0 int)')
J
update  
jiacy-jcy 已提交
163
            for ts in self.ts_str:
J
jiacy-jcy 已提交
164
                tdSql.execute(f'insert into {self.ntbname} values("{ts}",1)')
J
jiacy-jcy 已提交
165
            date_time = self.get_time.time_transform(self.ts_str,precision)
J
jiacy-jcy 已提交
166
            self.data_check(date_time,precision,'ntb')
C
cpwu 已提交
167

J
jiacy-jcy 已提交
168 169
    def function_check_stb(self):
        for precision in self.db_param_precision:
J
jiacy-jcy 已提交
170 171 172
            tdSql.execute(f'drop database if exists {self.dbname}')
            tdSql.execute(f'create database {self.dbname} precision "{precision}"')
            tdSql.execute(f'use {self.dbname}')
J
jiacy-jcy 已提交
173 174 175 176
            tdSql.execute(f'create table {self.stbname} (ts timestamp,c0 int) tags(t0 int)')
            tdSql.execute(f'create table {self.ctbname} using {self.stbname} tags(1)')
            for ts in self.ts_str:
                tdSql.execute(f'insert into {self.ctbname} values("{ts}",1)')
J
jiacy-jcy 已提交
177
            date_time = self.get_time.time_transform(self.ts_str,precision)
J
jiacy-jcy 已提交
178 179
            self.data_check(date_time,precision,'ctb')
            self.data_check(date_time,precision,'stb')
C
cpwu 已提交
180

J
jiacy-jcy 已提交
181
    def run(self):
J
update  
jiacy-jcy 已提交
182
        self.function_check_ntb()
J
jiacy-jcy 已提交
183
        self.function_check_stb()
C
cpwu 已提交
184

J
jiacy-jcy 已提交
185 186 187 188 189
    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)

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