taosdemoTestQueryWithJson.py 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
###################################################################
#           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
from util.log import *
from util.cases import *
from util.sql import *
from util.dnodes import *
import time
from datetime import datetime
22 23 24
import ast
# from assertpy import assert_that
import subprocess
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

class TDTestCase:
    def init(self, conn, logSql):
        tdLog.debug("start to execute %s" % __file__)
        tdSql.init(conn.cursor(), logSql)
        
    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):
            if ("taosd" in files):
                rootRealPath = os.path.dirname(os.path.realpath(root))
                if ("packaging" not in rootRealPath):
                    buildPath = root[:len(root)-len("/build/bin")]
                    break
        return buildPath
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    # 获取taosc接口查询的结果文件中的内容,返回每行数据,并断言数据的第一列内容。
    def assertfileDataTaosc(self,filename,expectResult):
        self.filename = filename
        self.expectResult = expectResult
        with open("%s" % filename, 'r+') as f1:  
            for line in f1.readlines():
                queryResult = line.strip().split()[0]
                self.assertCheck(filename,queryResult,expectResult)

    # 获取restful接口查询的结果文件中的关键内容,目前的关键内容找到第一个key就跳出循,所以就只有一个数据。后续再修改多个结果文件。
    def getfileDataRestful(self,filename):
        self.filename = filename
        with open("%s" % filename, 'r+') as f1:  
            for line in f1.readlines():
                contents = line.strip()
                if contents.find("data") != -1:
                    contentsDict = ast.literal_eval(contents)   # 字符串转换为字典
                    queryResult = contentsDict['data'][0][0]
                    break
        return queryResult

    # 获取taosc接口查询次数
    def queryTimesTaosc(self,filename):
        self.filename = filename
        command = 'cat %s |wc -l'% filename
        times = int(subprocess.getstatusoutput(command)[1]) 
        return times

    # 获取restful接口查询次数
    def queryTimesRestful(self,filename):
        self.filename = filename
        command = 'cat %s |grep "200 OK" |wc -l'% filename
        times = int(subprocess.getstatusoutput(command)[1]) 
        return times

   # 定义断言结果是否正确。不正确返回错误结果,正确即通过。
    def assertCheck(self,filename,queryResult,expectResult):
        self.filename = filename
        self.queryResult = queryResult
        self.expectResult = expectResult
        args0 = (filename, queryResult, expectResult)
        assert queryResult == expectResult , "Queryfile:%s ,result is %s != expect: %s" % args0 

90 91 92 93 94 95
    def run(self):
        buildPath = self.getBuildPath()
        if (buildPath == ""):
            tdLog.exit("taosd not found!")
        else:
            tdLog.info("taosd found in %s" % buildPath)
96 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
        binPath = buildPath+ "/build/bin/"  
          
        # delete useless files
        os.system("rm -rf ./query_res*")   
        os.system("rm -rf ./all_query*")
        
        # taosc query: query specified  table  and query  super table 
        os.system("%staosdemo -f tools/taosdemoAllTest/queryInsertdata.json" % binPath)
        os.system("%staosdemo -f tools/taosdemoAllTest/queryTaosc.json" % binPath)
        os.system("cat query_res0.txt* > all_query_res0_taosc.txt")
        os.system("cat query_res1.txt* > all_query_res1_taosc.txt")
        os.system("cat query_res2.txt* > all_query_res2_taosc.txt")
        
        # correct Times testcases 
        queryTimes0Taosc = self.queryTimesTaosc("all_query_res0_taosc.txt")
        self.assertCheck("all_query_res0_taosc.txt",queryTimes0Taosc,6)

        queryTimes1Taosc = self.queryTimesTaosc("all_query_res1_taosc.txt")
        self.assertCheck("all_query_res1_taosc.txt",queryTimes1Taosc,6)

        queryTimes2Taosc = self.queryTimesTaosc("all_query_res2_taosc.txt")
        self.assertCheck("all_query_res2_taosc.txt",queryTimes2Taosc,20)
        
        # correct data testcase
        self.assertfileDataTaosc("all_query_res0_taosc.txt","1604160000099")
        self.assertfileDataTaosc("all_query_res1_taosc.txt","100")
        self.assertfileDataTaosc("all_query_res2_taosc.txt","1604160000199")
123
        
124 125 126 127 128 129 130 131 132 133 134
        # delete useless files
        os.system("rm -rf ./query_res*")   
        os.system("rm -rf ./all_query*")


        # use restful api to query
        os.system("%staosdemo -f tools/taosdemoAllTest/queryInsertrestdata.json" % binPath)
        os.system("%staosdemo -f tools/taosdemoAllTest/queryRestful.json" % binPath)
        os.system("cat query_res0.txt*  > all_query_res0_rest.txt")
        os.system("cat query_res1.txt*  > all_query_res1_rest.txt")
        os.system("cat query_res2.txt*  > all_query_res2_rest.txt")
135
        
136 137 138 139 140 141
        # correct Times testcases 
        queryTimes0Restful = self.queryTimesRestful("all_query_res0_rest.txt")
        self.assertCheck("all_query_res0_rest.txt",queryTimes0Restful,6)

        queryTimes1Restful = self.queryTimesRestful("all_query_res1_rest.txt")
        self.assertCheck("all_query_res1_rest.txt",queryTimes1Restful,6)
142
        
143 144 145 146 147 148 149 150 151
        queryTimes2Restful = self.queryTimesRestful("all_query_res2_rest.txt")
        self.assertCheck("all_query_res2_rest.txt",queryTimes2Restful,4)

        # correct data testcase
        data0 = self.getfileDataRestful("all_query_res0_rest.txt")
        self.assertCheck('all_query_res0_rest.txt',data0,"2020-11-01 00:00:00.009")

        data1 = self.getfileDataRestful("all_query_res1_rest.txt")
        self.assertCheck('all_query_res1_rest.txt',data1,10) 
152

153 154 155
        data2 = self.getfileDataRestful("all_query_res2_rest.txt")
        self.assertCheck('all_query_res2_rest.txt',data2,"2020-11-01 00:00:00.004")  
        
156 157
        
        # query times less than or equal to 100
158
        os.system("%staosdemo -f tools/taosdemoAllTest/queryInsertdata.json" % binPath)
159 160 161
        os.system("%staosdemo -f tools/taosdemoAllTest/querySpeciMutisql100.json" % binPath)
        os.system("%staosdemo -f tools/taosdemoAllTest/querySuperMutisql100.json" % binPath)

162 163
        #query result print QPS
        os.system("%staosdemo -f tools/taosdemoAllTest/queryInsertdata.json" % binPath)
164
        os.system("%staosdemo -f tools/taosdemoAllTest/queryQps.json" % binPath)
165 166 167 168 169 170 171 172 173 174 175
        
        # use illegal or out of range parameters query json file
        os.system("%staosdemo -f tools/taosdemoAllTest/queryInsertdata.json" % binPath)
        exceptcode = os.system("%staosdemo -f tools/taosdemoAllTest/queryTimes0.json" % binPath)
        assert exceptcode != 0

        exceptcode0 = os.system("%staosdemo -f tools/taosdemoAllTest/queryTimesless0.json" % binPath)
        assert exceptcode0 != 0

        exceptcode1 = os.system("%staosdemo -f tools/taosdemoAllTest/queryConcurrentless0.json" % binPath)
        assert exceptcode1 != 0
176

177 178 179 180 181 182 183 184
        exceptcode2 = os.system("%staosdemo -f tools/taosdemoAllTest/queryConcurrent0.json" % binPath)
        assert exceptcode2 != 0

        exceptcode3 = os.system("%staosdemo -f tools/taosdemoAllTest/querrThreadsless0.json" % binPath)
        assert exceptcode3 != 0

        exceptcode4 = os.system("%staosdemo -f tools/taosdemoAllTest/querrThreads0.json" % binPath)
        assert exceptcode4 != 0
185

186
        # delete useless files
187
        os.system("rm -rf ./insert_res.txt")
188
        os.system("rm -rf tools/taosdemoAllTest/*.py.sql")        
189 190 191 192
        os.system("rm -rf ./querySystemInfo*")  
        os.system("rm -rf ./query_res*")   
        os.system("rm -rf ./all_query*")
        os.system("rm -rf ./test_query_res0.txt")
193 194


195 196 197 198 199 200 201
         
    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)

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