To_iso8601.py 6.0 KB
Newer Older
wafwerar's avatar
wafwerar 已提交
1
import time
J
update  
jiacy-jcy 已提交
2
from time import sleep
J
update  
jiacy-jcy 已提交
3

J
update  
jiacy-jcy 已提交
4 5 6
from util.log import *
from util.sql import *
from util.cases import *
J
jiacy-jcy 已提交
7
import os
J
update  
jiacy-jcy 已提交
8

J
update  
jiacy-jcy 已提交
9 10 11 12 13 14 15


class TDTestCase:

    def init(self, conn, logSql):
        tdLog.debug(f"start to excute {__file__}")
        tdSql.init(conn.cursor())
J
jiacy-jcy 已提交
16 17
        self.rowNum = 10
        self.ts = 1640966400000  # 2022-1-1 00:00:00.000
J
jiacy-jcy 已提交
18 19 20
        self.dbname = 'db'
        self.stbname = f'{self.dbname}.stb'
        self.ntbname = f'{self.dbname}.ntb'
J
jiacy-jcy 已提交
21
    def check_customize_param_ms(self):
wafwerar's avatar
wafwerar 已提交
22
        time_zone = time.strftime('%z')
J
update  
jiacy-jcy 已提交
23
        tdSql.execute(f'drop database if exists {self.dbname}')
J
jiacy-jcy 已提交
24 25 26
        tdSql.execute(f'create database {self.dbname} precision "ms"')
        tdSql.execute(f'use {self.dbname}')
        tdSql.execute(f'create table if not exists {self.ntbname}(ts timestamp, c1 int, c2 timestamp)')
J
jiacy-jcy 已提交
27
        for i in range(self.rowNum):
J
jiacy-jcy 已提交
28 29
            tdSql.execute(f"insert into {self.ntbname} values({self.ts + i}, {i + 1}, {self.ts + i})")
        tdSql.query(f'select to_iso8601(ts) from {self.ntbname}')
J
jiacy-jcy 已提交
30 31 32 33 34 35 36
        for i in range(self.rowNum):
            tdSql.checkEqual(tdSql.queryResult[i][0],f'2022-01-01T00:00:00.00{i}{time_zone}')
        timezone_list = ['+0000','+0100','+0200','+0300','+0330','+0400','+0500','+0530','+0600','+0700','+0800','+0900','+1000','+1100','+1200',\
                        '+00','+01','+02','+03','+04','+05','+06','+07','+08','+09','+10','+11','+12',\
                            '+00:00','+01:00','+02:00','+03:00','+03:30','+04:00','+05:00','+05:30','+06:00','+07:00','+08:00','+09:00','+10:00','+11:00','+12:00',\
                            '-0000','-0100','-0200','-0300','-0400','-0500','-0600','-0700','-0800','-0900','-1000','-1100','-1200',\
                        '-00','-01','-02','-03','-04','-05','-06','-07','-08','-09','-10','-11','-12',\
J
update  
jiacy-jcy 已提交
37
                            '-00:00','-01:00','-02:00','-03:00','-04:00','-05:00','-06:00','-07:00','-08:00','-09:00','-10:00','-11:00','-12:00',\
J
jiacy-jcy 已提交
38
                                'z','Z']
J
jiacy-jcy 已提交
39
        for j in timezone_list:
J
jiacy-jcy 已提交
40
            tdSql.query(f'select to_iso8601(ts,"{j}") from {self.ntbname}')
J
jiacy-jcy 已提交
41
            for i in range(self.rowNum):
J
jiacy-jcy 已提交
42 43
                tdSql.checkEqual(tdSql.queryResult[i][0],f'2022-01-01T00:00:00.00{i}{j}')
        error_param_list = [0,100.5,'a','!']
J
jiacy-jcy 已提交
44
        for i in error_param_list:
J
jiacy-jcy 已提交
45
            tdSql.error(f'select to_iso8601(ts,"{i}") from {self.ntbname}')
J
update  
jiacy-jcy 已提交
46
        error_timezone_param = ['+13','-13','+1300','-1300','+0001','-0001','-0330','-0530']
J
jiacy-jcy 已提交
47
        for i in error_timezone_param:
J
jiacy-jcy 已提交
48
            tdSql.error(f'select to_iso8601(ts,"{i}") from {self.ntbname}')
G
Ganlin Zhao 已提交
49

J
jiacy-jcy 已提交
50 51
    def check_base_function(self):
        tdSql.prepare()
J
update  
jiacy-jcy 已提交
52 53 54 55 56 57
        tdSql.execute('create table if not exists db.ntb(ts timestamp, c1 int, c2 float,c3 double,c4 timestamp)')
        tdSql.execute('create table if not exists db.stb(ts timestamp, c1 int, c2 float,c3 double,c4 timestamp) tags(t0 int)')
        tdSql.execute('create table if not exists db.stb_1 using db.stb tags(100)')
        tdSql.execute('insert into db.ntb values(now,1,1.55,100.555555,today())("2020-1-1 00:00:00",10,11.11,99.999999,now())(today(),3,3.333,333.333333,now())')
        tdSql.execute('insert into db.stb_1 values(now,1,1.55,100.555555,today())("2020-1-1 00:00:00",10,11.11,99.999999,now())(today(),3,3.333,333.333333,now())')
        tdSql.query("select to_iso8601(ts) from db.ntb")
J
update  
jiacy-jcy 已提交
58
        tdSql.checkRows(3)
J
update  
jiacy-jcy 已提交
59
        tdSql.query("select c1 from db.ntb where ts = to_iso8601(1577808000000)")
J
update  
jiacy-jcy 已提交
60 61
        tdSql.checkRows(1)
        tdSql.checkData(0,0,10)
J
update  
jiacy-jcy 已提交
62
        tdSql.query("select * from db.ntb where ts = to_iso8601(1577808000000)")
J
update  
jiacy-jcy 已提交
63
        tdSql.checkRows(1)
J
update  
jiacy-jcy 已提交
64
        tdSql.query("select to_iso8601(ts) from db.ntb where ts=today()")
J
update  
jiacy-jcy 已提交
65
        tdSql.checkRows(1)
J
jiacy-jcy 已提交
66
        for i in range(0,3):
J
update  
jiacy-jcy 已提交
67
            tdSql.query("select to_iso8601(1) from db.ntb")
J
jiacy-jcy 已提交
68
            tdSql.checkData(i,0,"1970-01-01T08:00:01+0800")
J
update  
jiacy-jcy 已提交
69 70
            tdSql.checkRows(3)
        tdSql.query("select to_iso8601(ts) from db.ntb")
J
update  
jiacy-jcy 已提交
71
        tdSql.checkRows(3)
J
update  
jiacy-jcy 已提交
72
        tdSql.query("select to_iso8601(today()) from db.ntb")
J
update  
jiacy-jcy 已提交
73
        tdSql.checkRows(3)
J
update  
jiacy-jcy 已提交
74 75 76 77
        tdSql.query("select to_iso8601(now()) from db.ntb")
        tdSql.checkRows(3)
        tdSql.error("select to_iso8601(timezone()) from db.ntb")
        tdSql.error("select to_iso8601('abc') from db.ntb")
J
jiacy-jcy 已提交
78 79 80 81
        for i in ['+','-','*','/']:
            tdSql.query(f"select to_iso8601(today()) {i}null from db.ntb")
            tdSql.checkRows(3)
            tdSql.checkData(0,0,None)
J
update  
jiacy-jcy 已提交
82
        tdSql.query("select to_iso8601(9223372036854775807) from db.ntb")
J
jiacy-jcy 已提交
83
        tdSql.checkRows(3)
J
jiacy-jcy 已提交
84
        # bug TD-15207
J
jiacy-jcy 已提交
85 86 87 88 89 90
        # tdSql.query("select to_iso8601(10000000000) from ntb")
        # tdSql.checkData(0,0,None)
        # tdSql.query("select to_iso8601(-1) from ntb")
        # tdSql.checkRows(3)
        # tdSql.query("select to_iso8601(-10000000000) from ntb")
        # tdSql.checkData(0,0,None)
J
jiacy-jcy 已提交
91 92 93
        err_param = [1.5,'a','c2']
        for i in err_param:
            tdSql.error(f"select to_iso8601({i}) from db.ntb")
J
update  
jiacy-jcy 已提交
94
        tdSql.query("select to_iso8601(now) from db.stb")
J
jiacy-jcy 已提交
95
        tdSql.checkRows(3)
J
update  
jiacy-jcy 已提交
96
        tdSql.query("select to_iso8601(now()) from db.stb")
J
update  
jiacy-jcy 已提交
97
        tdSql.checkRows(3)
J
update  
jiacy-jcy 已提交
98
        tdSql.query("select to_iso8601(1) from db.stb")
J
jiacy-jcy 已提交
99
        for i in range(0,3):
J
jiacy-jcy 已提交
100
            tdSql.checkData(i,0,"1970-01-01T08:00:01+0800")
J
update  
jiacy-jcy 已提交
101
            tdSql.checkRows(3)
J
update  
jiacy-jcy 已提交
102
        tdSql.query("select to_iso8601(ts) from db.stb")
J
update  
jiacy-jcy 已提交
103
        tdSql.checkRows(3)
J
update  
jiacy-jcy 已提交
104
        tdSql.query("select to_iso8601(ts)+1 from db.stb")
J
update  
jiacy-jcy 已提交
105
        tdSql.checkRows(3)
J
update  
jiacy-jcy 已提交
106
        tdSql.query("select to_iso8601(ts)+'a' from db.stb ")
J
update  
jiacy-jcy 已提交
107
        tdSql.checkRows(3)
J
jiacy-jcy 已提交
108
        for i in ['+','-','*','/']:
J
jiacy-jcy 已提交
109
            tdSql.query(f"select to_iso8601(today()) {i}null from db.stb")
J
jiacy-jcy 已提交
110 111
            tdSql.checkRows(3)
            tdSql.checkData(0,0,None)
G
Ganlin Zhao 已提交
112

J
jiacy-jcy 已提交
113 114 115
    def run(self):  # sourcery skip: extract-duplicate-method
        self.check_base_function()
        self.check_customize_param_ms()
J
jiacy-jcy 已提交
116

J
update  
jiacy-jcy 已提交
117 118 119 120 121 122
    def stop(self):
        tdSql.close()
        tdLog.success(f"{__file__} successfully executed")

tdCases.addLinux(__file__, TDTestCase())
tdCases.addWindows(__file__, TDTestCase())