test:fix that timestamps can be validated correctly in testcase
This commit is contained in:
parent
48bbd0eeeb
commit
6b3fa3f5af
|
@ -23,13 +23,36 @@ import pandas as pd
|
|||
from util.log import *
|
||||
from util.constant import *
|
||||
|
||||
from datetime import datetime, timedelta,timezone
|
||||
from tzlocal import get_localzone
|
||||
import pytz
|
||||
import time
|
||||
|
||||
def _locaTzTimeStamp(utctimestamp):
|
||||
tz = get_localzone()
|
||||
temptz = str(tz)
|
||||
localtz = pytz.timezone(temptz)
|
||||
defUtctimestamp=1035640800
|
||||
local_dt = datetime(2002, 10, 27, 6, 0, 0, tzinfo=localtz)
|
||||
defLocaltimestamp = time.mktime(local_dt.timetuple())
|
||||
deltaTzTime = int(defLocaltimestamp)-int(defUtctimestamp)
|
||||
temp = int(str(utctimestamp)[0:10])
|
||||
tempOther = str(utctimestamp)[10-len(str(utctimestamp)):]
|
||||
localtemp = deltaTzTime + temp
|
||||
localAll = str(localtemp) + str(tempOther)
|
||||
localtimestamp = int(localAll)
|
||||
|
||||
print(f"local timezone is {localtimestamp}, Deltel time is {deltaTzTime}s")
|
||||
return localtimestamp
|
||||
|
||||
|
||||
def _parse_datetime(timestr):
|
||||
try:
|
||||
return datetime.datetime.strptime(timestr, '%Y-%m-%d %H:%M:%S.%f')
|
||||
return datetime.strptime(timestr, '%Y-%m-%d %H:%M:%S.%f')
|
||||
except ValueError:
|
||||
pass
|
||||
try:
|
||||
return datetime.datetime.strptime(timestr, '%Y-%m-%d %H:%M:%S')
|
||||
return datetime.strptime(timestr, '%Y-%m-%d %H:%M:%S')
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
|
@ -227,18 +250,51 @@ class TDSql:
|
|||
self.checkRowCol(row, col)
|
||||
return self.cursor.istype(col, dataType)
|
||||
|
||||
|
||||
def checkData(self, row, col, data):
|
||||
self.checkRowCol(row, col)
|
||||
if self.queryResult[row][col] != data:
|
||||
if self.cursor.istype(col, "TIMESTAMP"):
|
||||
# suppose user want to check nanosecond timestamp if a longer data passed
|
||||
# suppose user want to check nanosecond timestamp if a longer data passed``
|
||||
if isinstance(data,str) :
|
||||
if (len(data) >= 28):
|
||||
if pd.to_datetime(self.queryResult[row][col]) == pd.to_datetime(data):
|
||||
tdLog.info(f"sql:{self.sql}, row:{row} col:{col} data:{self.queryResult[row][col]} == expect:{data}")
|
||||
resultData = _locaTzTimeStamp(self.queryResult[row][col])
|
||||
if pd.to_datetime(resultData) == pd.to_datetime(data):
|
||||
tdLog.info(f"sql:{self.sql}, row:{row} col:{col} data:{pd.to_datetime(resultData)} == expect:{data}")
|
||||
else:
|
||||
caller = inspect.getframeinfo(inspect.stack()[1][0])
|
||||
args = (caller.filename, caller.lineno, self.sql, row, col, self.queryResult[row][col], data)
|
||||
tdLog.exit("%s(%d) failed: sql:%s row:%d col:%d data:%s != expect:%s" % args)
|
||||
else:
|
||||
if self.queryResult[row][col] == _parse_datetime(data):
|
||||
tdLog.info(f"sql:{self.sql}, row:{row} col:{col} data:{self.queryResult[row][col]} == expect:{data}")
|
||||
else:
|
||||
caller = inspect.getframeinfo(inspect.stack()[1][0])
|
||||
args = (caller.filename, caller.lineno, self.sql, row, col, self.queryResult[row][col], data)
|
||||
tdLog.exit("%s(%d) failed: sql:%s row:%d col:%d data:%s != expect:%s" % args)
|
||||
return
|
||||
elif isinstance(data,int) :
|
||||
if len(str(data)) == 16 :
|
||||
unitTime = 'us'
|
||||
elif len(str(data)) == 13 :
|
||||
unitTime = 'ms'
|
||||
elif len(str(data)) == 19 :
|
||||
unitTime = 'ns'
|
||||
else:
|
||||
tdLog.exit("expect timeStamp type is wrong")
|
||||
resultData = pd.to_datetime(_locaTzTimeStamp(data),unit=unitTime)
|
||||
if resultData == self.queryResult[row][col] :
|
||||
tdLog.info(f"sql:{self.sql}, row:{row} col:{col} data:{self.queryResult[row][col]} == expect:{resultData}")
|
||||
else:
|
||||
caller = inspect.getframeinfo(inspect.stack()[1][0])
|
||||
args = (caller.filename, caller.lineno, self.sql, row, col, self.queryResult[row][col], data)
|
||||
tdLog.exit("%s(%d) failed: sql:%s row:%d col:%d data:%s != expect:%s" % args)
|
||||
return
|
||||
else:
|
||||
caller = inspect.getframeinfo(inspect.stack()[1][0])
|
||||
args = (caller.filename, caller.lineno, self.sql, row, col, self.queryResult[row][col], data)
|
||||
tdLog.exit("%s(%d) failed: sql:%s row:%d col:%d data:%s != expect:%s" % args)
|
||||
|
||||
|
||||
if str(self.queryResult[row][col]) == str(data):
|
||||
tdLog.info(f"sql:{self.sql}, row:{row} col:{col} data:{self.queryResult[row][col]} == expect:{data}")
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
import taos
|
||||
import sys
|
||||
from time import sleep
|
||||
from util.log import *
|
||||
from util.sql import *
|
||||
from util.cases import *
|
||||
|
||||
|
||||
|
||||
class TDTestCase:
|
||||
|
||||
def init(self, conn, logSql, replicaVar=1):
|
||||
self.replicaVar = int(replicaVar)
|
||||
tdLog.debug(f"start to excute {__file__}")
|
||||
tdSql.init(conn.cursor())
|
||||
|
||||
def run(self):
|
||||
rows = 10
|
||||
|
||||
tdSql.execute("create database dbus PRECISION 'us' ")
|
||||
tdSql.execute("create database dbns PRECISION 'ns' ")
|
||||
tdSql.execute("create database dbms PRECISION 'ms' ")
|
||||
dball = ["dbns","dbus","dbms"]
|
||||
for i in range(len(dball)):
|
||||
dbname = dball[i]
|
||||
stb = f"{dbname}.stb1"
|
||||
if i == 0 :
|
||||
qts = 1678883666951061471
|
||||
qts1 = 1678883666951061471
|
||||
|
||||
qtime = "2023-03-15 20:34:26.951061471"
|
||||
elif i == 1 :
|
||||
qts = 1678883666951061
|
||||
qts1 = 1678883666951061
|
||||
|
||||
qtime = "2023-03-15 20:34:26.951061"
|
||||
else:
|
||||
qts = 1678883666951
|
||||
qts1 = 1678883666953
|
||||
|
||||
qtime = "2023-03-15 20:34:26.951"
|
||||
|
||||
tdSql.execute(f"use {dbname}")
|
||||
tdLog.printNoPrefix("==========step1:create table")
|
||||
tdSql.execute(
|
||||
f'''create table if not exists {stb}
|
||||
(ts timestamp, c1 int, c2 float, c3 bigint, c4 double, c5 smallint, c6 tinyint)
|
||||
tags(location binary(64), type int, isused bool , family nchar(64))'''
|
||||
)
|
||||
tdSql.execute(f"create table {dbname}.t1 using {stb} tags('beijing', 1, 1, 'nchar1')")
|
||||
tdSql.execute(f"create table {dbname}.t2 using {stb} tags('shanghai', 2, 0, 'nchar2')")
|
||||
tdSql.execute(f"create table {dbname}.t3 using {stb} tags('shanghai', 3, 0, 'nchar3')")
|
||||
|
||||
tdLog.printNoPrefix("==========step2:insert data")
|
||||
for i in range(rows):
|
||||
tdSql.execute(
|
||||
f"insert into {dbname}.t1 values (now()+{i}m, {32767+i}, {20.0+i/10}, {2**31+i}, {3.4*10**38+i/10}, {127+i}, {i})"
|
||||
)
|
||||
tdSql.execute(
|
||||
f"insert into {dbname}.t2 values (now()-{i}m, {-32767-i}, {20.0-i/10}, {-i-2**31}, {-i/10-3.4*10**38}, {-127-i}, {-i})"
|
||||
)
|
||||
tdSql.execute(
|
||||
f"insert into {dbname}.t1 values (now()+11m, {2**31-1}, {pow(10,37)*34}, {pow(2,63)-1}, {1.7*10**308}, 32767, 127)"
|
||||
)
|
||||
tdSql.execute(
|
||||
f"insert into {dbname}.t2 values (now()-11m, {1-2**31}, {-3.4*10**38}, {1-2**63}, {-1.7*10**308}, -32767, -127)"
|
||||
)
|
||||
tdSql.execute(
|
||||
f"insert into {dbname}.t2 values (now()-12m, null , {-3.4*10**38}, null , {-1.7*10**308}, null , null)"
|
||||
)
|
||||
|
||||
tdSql.execute(
|
||||
f"insert into {dbname}.t3 values ({qts}, null , {-3.4*10**38}, null , {-1.7*10**308}, null , null)"
|
||||
)
|
||||
|
||||
tdLog.printNoPrefix("==========step3:query timestamp type")
|
||||
tdSql.query(f"select ts from {dbname}.t3 limit 1")
|
||||
tdSql.checkData(0,0,qtime)
|
||||
tdSql.checkData(0,0,qts)
|
||||
|
||||
def stop(self):
|
||||
tdSql.close()
|
||||
tdLog.success(f"{__file__} successfully executed")
|
||||
|
||||
tdCases.addLinux(__file__, TDTestCase())
|
||||
tdCases.addWindows(__file__, TDTestCase())
|
Loading…
Reference in New Issue