timetruncate.py 11.4 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 9 10 11 12 13 14 15

class TDTestCase:
    def init(self, conn, logSql):
        tdLog.debug("start to execute %s" % __file__)
        tdSql.init(conn.cursor())

        self.rowNum = 10
        self.ts = 1537146000000  # 2018-9-17 09:00:00.000
G
Ganlin Zhao 已提交
16

J
update  
jiacy-jcy 已提交
17 18 19 20 21 22 23 24 25 26
        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']
        self.time_unit = ['1w','1d','1h','1m','1s','1a','1u']
G
Ganlin Zhao 已提交
27 28
        #self.error_unit = ['1b','2w','2d','2h','2m','2s','2a','2u','1c','#1']
        self.error_unit = ['2w','2d','2h','2m','2s','2a','2u','1c','#1']
J
jiacy-jcy 已提交
29 30 31
        self.ntbname = 'ntb'
        self.stbname = 'stb'
        self.ctbname = 'ctb'
J
update  
jiacy-jcy 已提交
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
    def get_ms_timestamp(self,ts_str):
        _ts_str = ts_str
        if " " in ts_str:
            p = ts_str.split(" ")[1]
            if len(p) > 15 :
                _ts_str = ts_str[:-3]
        if ':' in _ts_str and '.' in _ts_str:
            timestamp = datetime.strptime(_ts_str, "%Y-%m-%d %H:%M:%S.%f")
            date_time = int(int(time.mktime(timestamp.timetuple()))*1000 + timestamp.microsecond/1000)
        elif ':' in _ts_str and '.' not in _ts_str:
            timestamp = datetime.strptime(_ts_str, "%Y-%m-%d %H:%M:%S")
            date_time = int(int(time.mktime(timestamp.timetuple()))*1000 + timestamp.microsecond/1000)
        else:
            timestamp = datetime.strptime(_ts_str, "%Y-%m-%d")
            date_time = int(int(time.mktime(timestamp.timetuple()))*1000 + timestamp.microsecond/1000)
        return date_time
    def get_us_timestamp(self,ts_str):
        _ts = self.get_ms_timestamp(ts_str) * 1000
        if " " in ts_str:
            p = ts_str.split(" ")[1]
            if len(p) > 12:
                us_ts = p[12:15]
                _ts += int(us_ts)
        return _ts
    def get_ns_timestamp(self,ts_str):
        _ts = self.get_us_timestamp(ts_str) *1000
        if " " in ts_str:
            p = ts_str.split(" ")[1]
            if len(p) > 15:
                us_ts = p[15:]
                _ts += int(us_ts)
        return _ts
    def time_transform(self,ts_str,precision):
        date_time = []
        if precision == 'ms':
            for i in ts_str:
                date_time.append(self.get_ms_timestamp(i))
        elif precision == 'us':
            for i in ts_str:
                date_time.append(self.get_us_timestamp(i))
        elif precision == 'ns':
            for i in ts_str:
                date_time.append(self.get_us_timestamp(i))
        return date_time
J
jiacy-jcy 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    def check_ms_timestamp(self,unit,date_time):
        if unit.lower() == '1a':
            for i in range(len(self.ts_str)):
                ts_result = self.get_ms_timestamp(str(tdSql.queryResult[i][0]))
                tdSql.checkEqual(ts_result,int(date_time[i]))
        elif unit.lower() == '1s':
            for i in range(len(self.ts_str)):
                ts_result = self.get_ms_timestamp(str(tdSql.queryResult[i][0]))
                tdSql.checkEqual(ts_result,int(date_time[i]/1000)*1000)
        elif unit.lower() == '1m':
            for i in range(len(self.ts_str)):
                ts_result = self.get_ms_timestamp(str(tdSql.queryResult[i][0]))
                tdSql.checkEqual(ts_result,int(date_time[i]/1000/60)*60*1000)
        elif unit.lower() == '1h':
            for i in range(len(self.ts_str)):
                ts_result = self.get_ms_timestamp(str(tdSql.queryResult[i][0]))
                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)):
                ts_result = self.get_ms_timestamp(str(tdSql.queryResult[i][0]))
G
Ganlin Zhao 已提交
96
                tdSql.checkEqual(ts_result,int(date_time[i]/1000/60/60/24)*24*60*60*1000)
J
jiacy-jcy 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        elif unit.lower() == '1w':
            for i in range(len(self.ts_str)):
                ts_result = self.get_ms_timestamp(str(tdSql.queryResult[i][0]))
                tdSql.checkEqual(ts_result,int(date_time[i]/1000/60/60/24/7)*7*24*60*60*1000)
    def check_us_timestamp(self,unit,date_time):
        if unit.lower() == '1u':
            for i in range(len(self.ts_str)):
                ts_result = self.get_us_timestamp(str(tdSql.queryResult[i][0]))
                tdSql.checkEqual(ts_result,int(date_time[i]))
        elif unit.lower() == '1a':
            for i in range(len(self.ts_str)):
                ts_result = self.get_us_timestamp(str(tdSql.queryResult[i][0]))
                tdSql.checkEqual(ts_result,int(date_time[i]/1000)*1000)
        elif unit.lower() == '1s':
            for i in range(len(self.ts_str)):
                ts_result = self.get_us_timestamp(str(tdSql.queryResult[i][0]))
                tdSql.checkEqual(ts_result,int(date_time[i]/1000/1000)*1000*1000)
        elif unit.lower() == '1m':
            for i in range(len(self.ts_str)):
                ts_result = self.get_us_timestamp(str(tdSql.queryResult[i][0]))
                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)):
                ts_result = self.get_us_timestamp(str(tdSql.queryResult[i][0]))
                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)):
                ts_result = self.get_us_timestamp(str(tdSql.queryResult[i][0]))
G
Ganlin Zhao 已提交
125
                tdSql.checkEqual(ts_result,int(date_time[i]/1000/1000/60/60/24)*24*60*60*1000*1000 )
J
jiacy-jcy 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        elif unit.lower() == '1w':
            for i in range(len(self.ts_str)):
                ts_result = self.get_us_timestamp(str(tdSql.queryResult[i][0]))
                tdSql.checkEqual(ts_result,int(date_time[i]/1000/1000/60/60/24/7)*7*24*60*60*1000*1000)
    def check_ns_timestamp(self,unit,date_time):
        if unit.lower() == '1u':
            for i in range(len(self.ts_str)):
                tdSql.checkEqual(tdSql.queryResult[i][0],int(date_time[i]*1000/1000)*1000)
        elif unit.lower() == '1a':
            for i in range(len(self.ts_str)):
                tdSql.checkEqual(tdSql.queryResult[i][0],int(date_time[i]*1000/1000/1000)*1000*1000)
        elif unit.lower() == '1s':
            for i in range(len(self.ts_str)):
                tdSql.checkEqual(tdSql.queryResult[i][0],int(date_time[i]*1000/1000/1000/1000)*1000*1000*1000)
        elif unit.lower() == '1m':
            for i in range(len(self.ts_str)):
                tdSql.checkEqual(tdSql.queryResult[i][0],int(date_time[i]*1000/1000/1000/1000/60)*60*1000*1000*1000)
        elif unit.lower() == '1h':
            for i in range(len(self.ts_str)):
                tdSql.checkEqual(tdSql.queryResult[i][0],int(date_time[i]*1000/1000/1000/1000/60/60)*60*60*1000*1000*1000  )
        elif unit.lower() == '1d':
            for i in range(len(self.ts_str)):
G
Ganlin Zhao 已提交
148
                tdSql.checkEqual(tdSql.queryResult[i][0],int(date_time[i]*1000/1000/1000/1000/60/60/24)*24*60*60*1000*1000*1000 )
J
jiacy-jcy 已提交
149 150 151 152 153 154
        elif unit.lower() == '1w':
            for i in range(len(self.ts_str)):
                tdSql.checkEqual(tdSql.queryResult[i][0],int(date_time[i]*1000/1000/1000/1000/60/60/24/7)*7*24*60*60*1000*1000*1000)
    def data_check(self,date_time,precision,tb_type):
        for unit in self.time_unit:
            if (unit.lower() == '1u' and precision.lower() == 'ms') or () :
G
Ganlin Zhao 已提交
155
                if tb_type.lower() == 'ntb':
J
jiacy-jcy 已提交
156 157 158 159 160 161
                    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':
G
Ganlin Zhao 已提交
162
                if tb_type.lower() == 'ntb':
J
jiacy-jcy 已提交
163 164 165 166 167 168 169 170
                    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}')
                tdSql.checkRows(len(self.ts_str))
                self.check_ms_timestamp(unit,date_time)
            elif precision.lower() == 'us':
G
Ganlin Zhao 已提交
171
                if tb_type.lower() == 'ntb':
J
jiacy-jcy 已提交
172 173 174 175 176 177 178 179
                    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}')
                tdSql.checkRows(len(self.ts_str))
                self.check_us_timestamp(unit,date_time)
            elif precision.lower() == 'ns':
G
Ganlin Zhao 已提交
180
                if tb_type.lower() == 'ntb':
J
jiacy-jcy 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194
                    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}')
                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}')
J
update  
jiacy-jcy 已提交
195 196 197 198 199
    def function_check_ntb(self):
        for precision in self.db_param_precision:
            tdSql.execute('drop database if exists db')
            tdSql.execute(f'create database db precision "{precision}"')
            tdSql.execute('use db')
J
jiacy-jcy 已提交
200
            tdSql.execute(f'create table {self.ntbname} (ts timestamp,c0 int)')
J
update  
jiacy-jcy 已提交
201
            for ts in self.ts_str:
J
jiacy-jcy 已提交
202
                tdSql.execute(f'insert into {self.ntbname} values("{ts}",1)')
J
update  
jiacy-jcy 已提交
203
            date_time = self.time_transform(self.ts_str,precision)
J
jiacy-jcy 已提交
204
            self.data_check(date_time,precision,'ntb')
J
update  
jiacy-jcy 已提交
205

J
jiacy-jcy 已提交
206 207 208 209 210 211 212 213 214 215 216 217
    def function_check_stb(self):
        for precision in self.db_param_precision:
            tdSql.execute('drop database if exists db')
            tdSql.execute(f'create database db precision "{precision}"')
            tdSql.execute('use db')
            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)')
            date_time = self.time_transform(self.ts_str,precision)
            self.data_check(date_time,precision,'ctb')
            self.data_check(date_time,precision,'stb')
J
jiacy-jcy 已提交
218
    def run(self):
J
update  
jiacy-jcy 已提交
219
        self.function_check_ntb()
J
jiacy-jcy 已提交
220
        self.function_check_stb()
J
jiacy-jcy 已提交
221 222 223 224 225
    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)

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