taosdemoTestQueryWithJson.py 8.9 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
import ast
23
import re
24 25
# from assertpy import assert_that
import subprocess
26

27

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

33 34 35 36 37 38 39 40 41 42 43 44
    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):
45
                    buildPath = root[:len(root) - len("/build/bin")]
46 47
                    break
        return buildPath
48 49

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

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

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

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

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

94 95 96 97 98 99
    def run(self):
        buildPath = self.getBuildPath()
        if (buildPath == ""):
            tdLog.exit("taosd not found!")
        else:
            tdLog.info("taosd found in %s" % buildPath)
100 101
        binPath = buildPath + "/build/bin/"

102
        # delete useless files
103
        os.system("rm -rf ./query_res*")
104
        os.system("rm -rf ./all_query*")
105 106 107 108 109 110 111 112

        # 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)
113 114 115
        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")
116 117

        # correct Times testcases
118
        queryTimes0Taosc = self.queryTimesTaosc("all_query_res0_taosc.txt")
119
        self.assertCheck("all_query_res0_taosc.txt", queryTimes0Taosc, 6)
120 121

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

        queryTimes2Taosc = self.queryTimesTaosc("all_query_res2_taosc.txt")
125 126
        self.assertCheck("all_query_res2_taosc.txt", queryTimes2Taosc, 20)

127
        # correct data testcase
128 129 130 131
        self.assertfileDataTaosc("all_query_res0_taosc.txt", "1604160000099")
        self.assertfileDataTaosc("all_query_res1_taosc.txt", "100")
        self.assertfileDataTaosc("all_query_res2_taosc.txt", "1604160000199")

132
        # delete useless files
133
        os.system("rm -rf ./query_res*")
134 135 136
        os.system("rm -rf ./all_query*")

        # use restful api to query
137 138 139 140 141 142
        os.system(
            "%staosdemo -f tools/taosdemoAllTest/queryInsertrestdata.json" %
            binPath)
        os.system(
            "%staosdemo -f tools/taosdemoAllTest/queryRestful.json" %
            binPath)
143 144 145
        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")
146 147

        # correct Times testcases
148
        queryTimes0Restful = self.queryTimesRestful("all_query_res0_rest.txt")
149
        self.assertCheck("all_query_res0_rest.txt", queryTimes0Restful, 6)
150 151

        queryTimes1Restful = self.queryTimesRestful("all_query_res1_rest.txt")
152 153
        self.assertCheck("all_query_res1_rest.txt", queryTimes1Restful, 6)

154
        queryTimes2Restful = self.queryTimesRestful("all_query_res2_rest.txt")
155
        self.assertCheck("all_query_res2_rest.txt", queryTimes2Restful, 4)
156 157 158

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

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

167
        data2 = self.getfileDataRestful("all_query_res2_rest.txt")
168 169 170 171 172
        self.assertCheck(
            'all_query_res2_rest.txt',
            data2,
            "2020-11-01 00:00:00.004")

173
        # query times less than or equal to 100
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
        os.system(
            "%staosdemo -f tools/taosdemoAllTest/queryInsertdata.json" %
            binPath)
        os.system(
            "%staosdemo -f tools/taosdemoAllTest/querySpeciMutisql100.json" %
            binPath)
        os.system(
            "%staosdemo -f tools/taosdemoAllTest/querySuperMutisql100.json" %
            binPath)

        # query result print QPS
        os.system(
            "%staosdemo -f tools/taosdemoAllTest/queryInsertdata.json" %
            binPath)
        os.system(
            "%staosdemo -f tools/taosdemoAllTest/queryQps.json" %
            binPath)

192
        # use illegal or out of range parameters query json file
193 194 195 196 197 198
        os.system(
            "%staosdemo -f tools/taosdemoAllTest/queryInsertdata.json" %
            binPath)
        exceptcode = os.system(
            "%staosdemo -f tools/taosdemoAllTest/queryTimes0.json" %
            binPath)
199 200
        assert exceptcode != 0

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

206 207 208
        exceptcode1 = os.system(
            "%staosdemo -f tools/taosdemoAllTest/queryConcurrentless0.json" %
            binPath)
209
        assert exceptcode1 != 0
210

211 212 213
        exceptcode2 = os.system(
            "%staosdemo -f tools/taosdemoAllTest/queryConcurrent0.json" %
            binPath)
214 215
        assert exceptcode2 != 0

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

221 222 223
        exceptcode4 = os.system(
            "%staosdemo -f tools/taosdemoAllTest/querrThreads0.json" %
            binPath)
224
        assert exceptcode4 != 0
225

226
        # delete useless files
227
        os.system("rm -rf ./insert_res.txt")
228 229 230 231
        os.system("rm -rf tools/taosdemoAllTest/*.py.sql")
        os.system("rm -rf ./querySystemInfo*")
        os.system("rm -rf ./query_res*")
#        os.system("rm -rf ./all_query*")
232
        os.system("rm -rf ./test_query_res0.txt")
233

234 235 236 237
    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)

238

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