boundary.py 6.0 KB
Newer Older
B
Bomin Zhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12
# -*- coding: utf-8 -*-

import random
import string
import subprocess
import sys
from util.log import *
from util.cases import *
from util.sql import *


class TDTestCase:
13
    def init(self, conn, logSql):
B
Bomin Zhang 已提交
14
        tdLog.debug("start to execute %s" % __file__)
S
Shuduo Sang 已提交
15
        tdSql.init(conn.cursor(), logSql)
B
Bomin Zhang 已提交
16

S
Shuduo Sang 已提交
17
    def getLimitFromSourceCode(self, name):
B
Bomin Zhang 已提交
18 19 20
        cmd = "grep -w '#define %s' ../../src/inc/taosdef.h|awk '{print $3}'" % name
        return int(subprocess.check_output(cmd, shell=True))

S
Shuduo Sang 已提交
21
    def generateString(self, length):
B
Bomin Zhang 已提交
22 23
        chars = string.ascii_uppercase + string.ascii_lowercase
        v = ""
S
Shuduo Sang 已提交
24 25
        for i in range(length):
            v += random.choice(chars)
B
Bomin Zhang 已提交
26 27
        return v

S
Shuduo Sang 已提交
28 29
    def checkTagBoundaries(self):
        tdLog.debug("checking tag boundaries")
B
Bomin Zhang 已提交
30 31
        tdSql.prepare()

S
Shuduo Sang 已提交
32 33 34 35
        maxTags = self.getLimitFromSourceCode('TSDB_MAX_TAGS')
        totalTagsLen = self.getLimitFromSourceCode('TSDB_MAX_TAGS_LEN')
        tdLog.notice("max tags is %d" % maxTags)
        tdLog.notice("max total tag length is %d" % totalTagsLen)
B
Bomin Zhang 已提交
36 37 38 39 40 41

        # for binary tags, 2 bytes are used for length
        tagLen = (totalTagsLen - maxTags * 2) // maxTags
        firstTagLen = totalTagsLen - 2 * maxTags - tagLen * (maxTags - 1)

        sql = "create table cars(ts timestamp, f int) tags(t0 binary(%d)" % firstTagLen
S
Shuduo Sang 已提交
42
        for i in range(1, maxTags):
B
Bomin Zhang 已提交
43 44 45
            sql += ", t%d binary(%d)" % (i, tagLen)
        sql += ");"

S
Shuduo Sang 已提交
46 47 48 49
        tdLog.debug("creating super table: " + sql)
        tdSql.execute(sql)
        tdSql.query('show stables')
        tdSql.checkRows(1)
B
Bomin Zhang 已提交
50

S
Shuduo Sang 已提交
51
        for i in range(10):
B
Bomin Zhang 已提交
52 53
            sql = "create table car%d using cars tags('%d'" % (i, i)
            sql += ", '0'" * (maxTags - 1) + ");"
S
Shuduo Sang 已提交
54 55
            tdLog.debug("creating table: " + sql)
            tdSql.execute(sql)
B
Bomin Zhang 已提交
56 57

            sql = "insert into car%d values(now, 0);" % i
S
Shuduo Sang 已提交
58 59
            tdLog.debug("inserting data: " + sql)
            tdSql.execute(sql)
B
Bomin Zhang 已提交
60

S
Shuduo Sang 已提交
61 62 63
        tdSql.query('show tables')
        tdLog.info('tdSql.checkRow(10)')
        tdSql.checkRows(10)
B
Bomin Zhang 已提交
64

S
Shuduo Sang 已提交
65 66
        tdSql.query('select * from cars;')
        tdSql.checkRows(10)
B
Bomin Zhang 已提交
67

S
Shuduo Sang 已提交
68 69
    def checkColumnBoundaries(self):
        tdLog.debug("checking column boundaries")
B
Bomin Zhang 已提交
70 71 72
        tdSql.prepare()

        # one column is for timestamp
S
Shuduo Sang 已提交
73
        maxCols = self.getLimitFromSourceCode('TSDB_MAX_COLUMNS') - 1
B
Bomin Zhang 已提交
74 75

        sql = "create table cars (ts timestamp"
S
Shuduo Sang 已提交
76
        for i in range(maxCols):
B
Bomin Zhang 已提交
77 78
            sql += ", c%d int" % i
        sql += ");"
S
Shuduo Sang 已提交
79 80 81
        tdSql.execute(sql)
        tdSql.query('show tables')
        tdSql.checkRows(1)
B
Bomin Zhang 已提交
82 83

        sql = "insert into cars values (now"
S
Shuduo Sang 已提交
84
        for i in range(maxCols):
B
Bomin Zhang 已提交
85 86
            sql += ", %d" % i
        sql += ");"
S
Shuduo Sang 已提交
87 88 89
        tdSql.execute(sql)
        tdSql.query('select * from cars')
        tdSql.checkRows(1)
B
Bomin Zhang 已提交
90

S
Shuduo Sang 已提交
91 92
    def checkTableNameBoundaries(self):
        tdLog.debug("checking table name boundaries")
B
Bomin Zhang 已提交
93 94
        tdSql.prepare()

S
Shuduo Sang 已提交
95 96
        maxTableNameLen = self.getLimitFromSourceCode('TSDB_TABLE_NAME_LEN')
        tdLog.notice("table name max length is %d" % maxTableNameLen)
B
Bomin Zhang 已提交
97

98
        # create a super table with name exceed max length
B
Bomin Zhang 已提交
99
        sname = self.generateString(maxTableNameLen)
100
        tdLog.info("create a super table with length %d" % len(sname))
101 102 103
        tdSql.error(
            "create table %s (ts timestamp, value int) tags(id int)" %
            sname)
B
Bomin Zhang 已提交
104

105
        # create a super table with name of max length
B
Bomin Zhang 已提交
106
        sname = self.generateString(maxTableNameLen - 1)
107
        tdLog.info("create a super table with length %d" % len(sname))
108 109 110
        tdSql.execute(
            "create table %s (ts timestamp, value int) tags(id int)" %
            sname)
111 112 113 114 115
        tdLog.info("check table count, should be one")
        tdSql.query('show stables')
        tdSql.checkRows(1)

        # create a child table with name exceed max length
B
Bomin Zhang 已提交
116
        name = self.generateString(maxTableNameLen)
117 118
        tdLog.info("create a child table with length %d" % len(name))
        tdSql.error("create table %s using %s tags(0)" % (name, sname))
B
Bomin Zhang 已提交
119

120
        # create a child table with name of max length
B
Bomin Zhang 已提交
121
        name = self.generateString(maxTableNameLen - 1)
122 123
        tdLog.info("create a child table with length %d" % len(name))
        tdSql.execute("create table %s using %s tags(0)" % (name, sname))
S
Shuduo Sang 已提交
124 125
        tdSql.query('show tables')
        tdSql.checkRows(1)
B
Bomin Zhang 已提交
126

127 128 129 130 131 132
        # insert one row
        tdLog.info("insert one row of data")
        tdSql.execute("insert into %s values(now, 0)" % name)
        tdSql.query("select * from " + name)
        tdSql.checkRows(1)
        tdSql.query("select * from " + sname)
S
Shuduo Sang 已提交
133
        tdSql.checkRows(1)
B
Bomin Zhang 已提交
134

135 136 137 138
        name = name[:len(name) - 1]
        tdSql.error("select * from " + name)
        tdSql.checkRows(0)

S
Shuduo Sang 已提交
139 140
    def checkRowBoundaries(self):
        tdLog.debug("checking row boundaries")
B
Bomin Zhang 已提交
141 142 143
        tdSql.prepare()

        # 8 bytes for timestamp
B
Bomin Zhang 已提交
144
        maxRowSize = self.getLimitFromSourceCode('TSDB_MAX_BYTES_PER_ROW') - 8
S
Shuduo Sang 已提交
145
        maxCols = self.getLimitFromSourceCode('TSDB_MAX_COLUMNS') - 1
B
Bomin Zhang 已提交
146 147 148 149 150 151

        # for binary cols, 2 bytes are used for length
        colLen = (maxRowSize - maxCols * 2) // maxCols
        firstColLen = maxRowSize - 2 * maxCols - colLen * (maxCols - 1)

        sql = "create table cars (ts timestamp, c0 binary(%d)" % firstColLen
S
Shuduo Sang 已提交
152
        for i in range(1, maxCols):
B
Bomin Zhang 已提交
153 154
            sql += ", c%d binary(%d)" % (i, colLen)
        sql += ");"
S
Shuduo Sang 已提交
155 156 157
        tdSql.execute(sql)
        tdSql.query('show tables')
        tdSql.checkRows(1)
B
Bomin Zhang 已提交
158

S
Shuduo Sang 已提交
159
        col = self.generateString(firstColLen)
B
Bomin Zhang 已提交
160
        sql = "insert into cars values (now, '%s'" % col
S
Shuduo Sang 已提交
161 162 163
        col = self.generateString(colLen)
        for i in range(1, maxCols):
            sql += ", '%s'" % col
B
Bomin Zhang 已提交
164
        sql += ");"
S
Shuduo Sang 已提交
165 166 167 168
        tdLog.info(sql)
        tdSql.execute(sql)
        tdSql.query("select * from cars")
        tdSql.checkRows(1)
B
Bomin Zhang 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182

    def run(self):
        self.checkTagBoundaries()
        self.checkColumnBoundaries()
        self.checkTableNameBoundaries()
        self.checkRowBoundaries()

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


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