test_stmt_insert_query_ex.py 9.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
###################################################################
#           Copyright (c) 2016 by TAOS Technologies, Inc.
#                     All rights reserved.
#
#  This file is proprietary and confidential to TAOS Technologies.
#  No part of this file may be reproduced, stored, transmitted,
#  disclosed or used in any form or by any means other than as
#  expressly provided by the written permission from Jianhui Tao
#
###################################################################

# -*- coding: utf-8 -*-

import sys
import os
import threading as thd
import multiprocessing as mp
from numpy.lib.function_base import insert
import taos
from taos import *
from util.log import *
from util.cases import *
from util.sql import *
import numpy as np
import datetime as dt
from datetime import datetime
from ctypes import *
import time
# constant define
WAITS = 5 # wait seconds

class TDTestCase:
    #
    # --------------- main frame -------------------
    def caseDescription(self):
        '''
        limit and offset keyword function test cases;
        case1: limit offset base function test
        case2: offset return valid
        '''
        return 

    def getBuildPath(self):
        selfPath = os.path.dirname(os.path.realpath(__file__))

        if ("community" in selfPath):
            projPath = selfPath[:selfPath.find("community")]
        else:
            projPath = selfPath[:selfPath.find("tests")]

        for root, dirs, files in os.walk(projPath):
wafwerar's avatar
wafwerar 已提交
52
            if ("taosd" in files or "taosd.exe" in files):
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 79 80 81 82 83 84
                rootRealPath = os.path.dirname(os.path.realpath(root))
                if ("packaging" not in rootRealPath):
                    buildPath = root[:len(root)-len("/build/bin")]
                    break
        return buildPath

    # init
    def init(self, conn, logSql):
        tdLog.debug("start to execute %s" % __file__)
        tdSql.init(conn.cursor())
        # tdSql.prepare()
        # self.create_tables();
        self.ts = 1500000000000

    # stop 
    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)


    # --------------- case  -------------------


    def newcon(self,host,cfg):
        user = "root"
        password = "taosdata"
        port =6030 
        con=taos.connect(host=host, user=user, password=password, config=cfg ,port=port)
        print(con)
        return con
        
    def test_stmt_set_tbname_tag(self,conn):
85
        dbname = "stmt_set_tbname_tag"
86 87 88 89 90 91 92
        
        try:
            conn.execute("drop database if exists %s" % dbname)
            conn.execute("create database if not exists %s PRECISION 'us' "  % dbname)
            conn.select_db(dbname)
            conn.execute("create table if not exists log(ts timestamp, bo bool, nil tinyint, ti tinyint, si smallint, ii int,\
                bi bigint, tu tinyint unsigned, su smallint unsigned, iu int unsigned, bu bigint unsigned, \
93
                ff float, dd double, bb binary(100), nn nchar(100), tt timestamp , vc varchar(100)) tags (t1 timestamp, t2 bool,\
94 95 96 97
                t3 tinyint, t4 tinyint, t5 smallint, t6 int, t7 bigint, t8 tinyint unsigned, t9 smallint unsigned, \
                t10 int unsigned, t11 bigint unsigned, t12 float, t13 double, t14 binary(100), t15 nchar(100), t16 timestamp)")
            
            stmt = conn.statement("insert into ? using log tags (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) \
98
                values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
99 100 101
            tags = new_bind_params(16)
            tags[0].timestamp(1626861392589123, PrecisionEnum.Microseconds)
            tags[1].bool(True)
102
            tags[2].bool(False)
103 104 105 106 107 108 109 110 111 112 113 114 115 116
            tags[3].tinyint(2)
            tags[4].smallint(3)
            tags[5].int(4)
            tags[6].bigint(5)
            tags[7].tinyint_unsigned(6)
            tags[8].smallint_unsigned(7)
            tags[9].int_unsigned(8)
            tags[10].bigint_unsigned(9)
            tags[11].float(10.1)
            tags[12].double(10.11)
            tags[13].binary("hello")
            tags[14].nchar("stmt")
            tags[15].timestamp(1626861392589, PrecisionEnum.Milliseconds)
            stmt.set_tbname_tags("tb1", tags)
117
            params = new_multi_binds(17)
118 119 120 121 122 123 124 125 126 127 128 129 130 131
            params[0].timestamp((1626861392589111,  1626861392590111, 1626861392591111))
            params[1].bool((True, None, False))
            params[2].tinyint([-128, -128, None]) # -128 is tinyint null
            params[3].tinyint([0, 127, None])
            params[4].smallint([3, None, 2])
            params[5].int([3, 4, None])
            params[6].bigint([3, 4, None])
            params[7].tinyint_unsigned([3, 4, None])
            params[8].smallint_unsigned([3, 4, None])
            params[9].int_unsigned([3, 4, None])
            params[10].bigint_unsigned([3, 4, 5])
            params[11].float([3, None, 1])
            params[12].double([3, None, 1.2])
            params[13].binary(["abc", "dddafadfadfadfadfa", None])
132
            params[14].nchar(["涛思数据", None, "a long string with 中文字符"])
133
            params[15].timestamp([None, None, 1626861392591])
134 135
            params[16].binary(["涛思数据16", None, "a long string with 中文-字符"])
                     
136 137 138 139 140
            stmt.bind_param_batch(params)
            stmt.execute()

            assert stmt.affected_rows == 3

141
            #query all 
142 143 144
            querystmt1=conn.statement("select * from log where bu < ?")
            queryparam1=new_bind_params(1)
            print(type(queryparam1))
145
            queryparam1[0].int(10)
146 147 148 149
            querystmt1.bind_param(queryparam1)
            querystmt1.execute() 
            result1=querystmt1.use_result()
            rows1=result1.fetch_all()
150 151 152 153 154 155
            print(rows1[0])
            print(rows1[1])
            print(rows1[2])
            assert str(rows1[0][0]) == "2021-07-21 17:56:32.589111"
            assert rows1[0][10] == 3
            assert rows1[1][10] == 4
156

157
            #query: Numeric Functions
158 159
            querystmt2=conn.statement("select abs(?) from log where bu < ?")
            queryparam2=new_bind_params(2)
160
            print(type(queryparam2))
161 162 163 164 165 166
            queryparam2[0].int(5)
            queryparam2[1].int(5)
            querystmt2.bind_param(queryparam2)
            querystmt2.execute() 
            result2=querystmt2.use_result()
            rows2=result2.fetch_all()
167
            print("2",rows2)
168 169 170 171 172
            assert rows2[0][0] == 5
            assert rows2[1][0] == 5


            #query: Numeric Functions and escapes
173 174 175 176 177 178 179 180 181 182

            querystmt3=conn.statement("select abs(?) from log where  nn= 'a? long string with 中文字符' ")
            queryparam3=new_bind_params(1)
            print(type(queryparam3))
            queryparam3[0].int(5)
            querystmt3.bind_param(queryparam3)
            querystmt3.execute() 
            result3=querystmt3.use_result()
            rows3=result3.fetch_all()
            print("3",rows3)
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
            assert rows3 == []

            #query: string Functions

            querystmt3=conn.statement("select CHAR_LENGTH(?) from log  ")
            queryparam3=new_bind_params(1)
            print(type(queryparam3))
            queryparam3[0].binary('中文字符')
            querystmt3.bind_param(queryparam3)
            querystmt3.execute() 
            result3=querystmt3.use_result()
            rows3=result3.fetch_all()
            print("4",rows3)
            assert rows3[0][0] == 12, 'fourth case is failed'
            assert rows3[1][0] == 12, 'fourth case is failed'

            # #query: conversion Functions

            # querystmt4=conn.statement("select cast( ? as bigint) from log  ")
            # queryparam4=new_bind_params(1)
            # print(type(queryparam4))
            # queryparam4[0].binary('1232a')
            # querystmt4.bind_param(queryparam4)
            # querystmt4.execute() 
            # result4=querystmt4.use_result()
            # rows4=result4.fetch_all()
            # print("5",rows4)
            # assert rows4[0][0] == 1232
            # assert rows4[1][0] == 1232

            # querystmt4=conn.statement("select cast( ? as binary(10)) from log  ")
            # queryparam4=new_bind_params(1)
            # print(type(queryparam4))
            # queryparam4[0].int(123)
            # querystmt4.bind_param(queryparam4)
            # querystmt4.execute() 
            # result4=querystmt4.use_result()
            # rows4=result4.fetch_all()
            # print("6",rows4)
            # assert rows4[0][0] == '123'
            # assert rows4[1][0] == '123'

            # #query: datatime Functions

            # querystmt4=conn.statement(" select timediff('2021-07-21 17:56:32.590111',?,1s)  from log  ")
            # queryparam4=new_bind_params(1)
            # print(type(queryparam4))
            # queryparam4[0].timestamp(1626861392591111)
            # querystmt4.bind_param(queryparam4)
            # querystmt4.execute() 
            # result4=querystmt4.use_result()
            # rows4=result4.fetch_all()
            # print("7",rows4)
            # assert rows4[0][0] == 1, 'seventh case is failed'
            # assert rows4[1][0] == 1, 'seventh case is failed'
            
            
240 241 242 243 244 245 246 247 248 249 250 251 252 253

            # conn.execute("drop database if exists %s" % dbname)
            conn.close()
        
        except Exception as err:
            # conn.execute("drop database if exists %s" % dbname)
            conn.close()
            raise err

    def run(self):
        buildPath = self.getBuildPath()
        config = buildPath+ "../sim/dnode1/cfg/"
        host="localhost"
        connectstmt=self.newcon(host,config)
254
        self.test_stmt_set_tbname_tag(connectstmt)
255 256 257 258 259 260 261 262

        return 


# add case with filename
#
tdCases.addWindows(__file__, TDTestCase())
tdCases.addLinux(__file__, TDTestCase())