delete.py 7.9 KB
Newer Older
A
Alex Duan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
###################################################################
#           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

from numpy.lib.function_base import insert
import taos
from util.log import *
from util.cases import *
from util.sql import *
import numpy as np

# constant define
24
WAITS = 10 # wait seconds
A
Alex Duan 已提交
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

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

    # 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


    # run case  
    def run(self):
        # insert data
51
        for i in range(10):
A
Alex Duan 已提交
52
          tbname = "t%d"%i
53
          self.insert_data(tbname, self.ts, (i+1)*10000, 20000);
A
Alex Duan 已提交
54

55
        tdLog.debug(" INSERT data 10 tables ....... [OK]")  
A
Alex Duan 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

        # test base case
        self.test_case1()
        tdLog.debug(" DELETE test_case1 ............ [OK]")
        # test advance case
        self.test_case2()
        tdLog.debug(" DELETE test_case2 ............ [OK]")


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

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

    # create table
    def create_tables(self):
        # super table
77
        tdSql.execute("create table st(ts timestamp, i1 int) tags(area int)")
A
Alex Duan 已提交
78
        # child table
79
        for i in range(10):
A
Alex Duan 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
          sql = "create table t%d using st tags(%d)"%(i, i)
          tdSql.execute(sql)
        
        return 

    # insert data1
    def insert_data(self, tbname, ts_start, count, batch_num):
        pre_insert = "insert into %s values"%tbname
        sql = pre_insert
        tdLog.debug("doing insert table %s rows=%d ..."%(tbname, count))
        for i in range(count):
            sql += " (%d,%d)"%(ts_start + i*1000, i)
            if i >0 and i%batch_num == 0:
                tdSql.execute(sql)
                sql = pre_insert
        # end sql        
        if sql != pre_insert:
            tdSql.execute(sql)

        return

    # test case1 base 
    def test_case1(self):
        # 
104
        # delete from single table
A
Alex Duan 已提交
105
        #
106 107
        
        # where <
108
        sql = "select count(*) from t0 where ts < 1500000120000"
109
        tdSql.query(sql)
110 111 112
        tdSql.checkData(0, 0, 120)
        
        sql = "delete from t0 where ts < 1500000120000"
113 114
        tdSql.execute(sql)
        tdSql.checkAffectedRows(120)
115

A
Alex Duan 已提交
116
        sql = "select count(*) from t0"
117 118
        tdSql.query(sql)
        tdSql.checkData(0, 0, 10000 - 120 )
119 120

        sql = "select * from t0 limit 1"
121
        tdSql.query(sql)
122 123
        tdSql.checkData(0, 1, 120)

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
        # where > and <
        sql = "delete from t0 where ts > 1500000240000 and ts <= 1500000300000"
        tdSql.execute(sql)
        tdSql.checkAffectedRows(60)
        sql = "select count(*) from t0"
        tdSql.query(sql)
        tdSql.checkData(0, 0, 10000 - 120 - 60)

        sql = "select * from t0 limit 2 offset 120"
        tdSql.query(sql)
        tdSql.checkData(0, 1, 240)
        tdSql.checkData(1, 1, 301)

        
        # where >  delete 1000 rows from end
        sql = "delete from t0 where ts >= 1500009000000; "
        tdSql.execute(sql)
        tdSql.checkAffectedRows(1000)
        sql = "select count(*) from t0"
        tdSql.query(sql)
        tdSql.checkData(0, 0, 10000 - 120 - 60 - 1000)
        
        sql = "select last_row(*) from t0; "
        tdSql.query(sql)
        tdSql.checkData(0, 1, 8999)

        sql = "select last(*) from t0"
        tdSql.query(sql)
        tdSql.checkData(0, 1, 8999)

        # insert last_row
        sql = "insert into t0 values(1500009999000,9999); "
        tdSql.execute(sql)

        sql = "select last_row(*) from t0; "
        tdSql.query(sql)
        tdSql.checkData(0, 1, 9999)

        sql = "select last(*) from t0"
        tdSql.query(sql)
        tdSql.checkData(0, 1, 9999)

        # insert last 
        sql = "insert into t0 values(1500010000000,10000); "
        tdSql.execute(sql)
        sql = "insert into t0 values(1500010002000,NULL); "
        tdSql.execute(sql)
        sql = "insert into t0 values(1500010001000,10001); "
        tdSql.execute(sql)
        sql = "delete from t0 where ts = 1500010001000;  "
        tdSql.execute(sql)

        sql = "select last_row(i1) from t0; "
        tdSql.query(sql)
        tdSql.checkData(0, 0, None)

        sql = "select last(i1) from t0; "
        tdSql.query(sql)
        tdSql.checkData(0, 0, 10000)

        # delete whole
        sql = "delete from t0;"
        tdSql.execute(sql)
        tdSql.checkAffectedRows(8823)
        
A
Alex Duan 已提交
189 190 191 192 193
        return 

    # test advance 
    def test_case2(self):
        # 
194
        # delete from super table
A
Alex Duan 已提交
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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
        # where <
        sql = "select count(*) from st where ts < 1500000120000;"
        tdSql.query(sql)
        tdSql.checkData(0, 0, 9*120) #1080
        
        sql = "delete from st where ts < 1500000120000;"
        tdSql.execute(sql)
        tdSql.checkAffectedRows(9*120) #1080

        sql = "select count(*) from st;"
        tdSql.query(sql)
        tdSql.checkData(0, 0, 540000 - 9*120 )

        sql = "select * from st limit 1;"
        tdSql.query(sql)
        tdSql.checkData(0, 1, 120)

        # where > and <
        sql = "delete from st where ts > 1500000240000 and ts <= 1500000300000;"
        tdSql.execute(sql)
        tdSql.checkAffectedRows(9*60)
        sql = "select count(*) from st;"
        tdSql.query(sql)
        tdSql.checkData(0, 0, 540000 - 9*120 - 9*60)

        sql = "select * from st limit 2 offset 120"
        tdSql.query(sql)
        tdSql.checkData(0, 1, 240)
        tdSql.checkData(1, 1, 301)

        
        # where >  delete 1000 rows from end
        sql = "delete from st where ts >= 1500009000000; "
        tdSql.execute(sql)
        tdSql.checkAffectedRows(459000)
        sql = "select count(*) from st;"
        tdSql.query(sql)
        tdSql.checkData(0, 0, 79380)
        
        sql = "select last_row(*) from st; "
        tdSql.query(sql)
        tdSql.checkData(0, 1, 8999)

        sql = "select last(*) from st"
        tdSql.query(sql)
        tdSql.checkData(0, 1, 8999)

        # insert last_row
        sql = "insert into t0 values(1500009999000,9999); "
        tdSql.execute(sql)

        sql = "select last_row(*) from st; "
        tdSql.query(sql)
        tdSql.checkData(0, 1, 9999)

        sql = "select last(*) from st"
        tdSql.query(sql)
        tdSql.checkData(0, 1, 9999)

        # insert last 
        sql = "insert into t0 values(1500010000000,10000); "
        tdSql.execute(sql)
        sql = "insert into t0 values(1500010002000,NULL); "
        tdSql.execute(sql)
        sql = "insert into t0 values(1500010001000,10001); "
        tdSql.execute(sql)
        sql = "delete from t0 where ts = 1500010001000;  "
        tdSql.execute(sql)

        sql = "select last_row(i1) from st; "
        tdSql.query(sql)
        tdSql.checkData(0, 0, None)

        sql = "select last(i1) from st; "
        tdSql.query(sql)
        tdSql.checkData(0, 0, 10000)

        # delete whole
        sql = "delete from st;"
        tdSql.execute(sql)
        tdSql.checkAffectedRows(79383)
        
        return 
A
Alex Duan 已提交
280 281 282 283 284 285

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