importCSV.py 3.3 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
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 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
###################################################################
#       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 csv 
import random
from util.log import *
from util.cases import *
from util.sql import *
from util.dnodes import *


class TDTestCase:
    def init(self, conn, logSql):
        tdLog.debug("start to execute %s" % __file__)
        tdSql.init(conn.cursor(), logSql)
        self.csvfile = "/tmp/file.csv"
        self.rows = 10000
        self.ntables = 1
        self.startTime = 1520000010000
    def genRandomStr(self, maxLen):
        H = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
        salt = ''
        if maxLen <= 1:
            maxLen = 2
        l = random.randint(1,maxLen)
        for i in range(l):
            salt += random.choice(H)
        return salt
    def createCSVFile(self):
        f = open(self.csvfile,'w',encoding='utf-8') 
        csv_writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC) 
        for i in range(self.rows):   
            csv_writer.writerow([self.startTime + i, 
                                self.genRandomStr(5),
                                self.genRandomStr(6),
                                self.genRandomStr(7),
                                self.genRandomStr(8),
                                self.genRandomStr(9),
                                self.genRandomStr(10),
                                self.genRandomStr(11),
                                self.genRandomStr(12),
                                self.genRandomStr(13),
                                self.genRandomStr(14)])
        f.close()
    def destroyCSVFile(self):
        os.remove(self.csvfile) 
    def run(self):
        self.createCSVFile()

        tdDnodes.stop(1)
        tdDnodes.deploy(1)
        tdDnodes.start(1)

        tdSql.execute('reset query cache')
        tdSql.execute('drop database if exists db')
        tdSql.execute('create database db')
        tdSql.execute('use db')
        tdSql.execute('''create table tbx (ts TIMESTAMP, 
                         collect_area NCHAR(5), 
                         device_id BINARY(6), 
                         imsi BINARY(7),  
                         imei BINARY(8),  
                         mdn BINARY(9), 
                         net_type BINARY(10), 
                         mno NCHAR(11), 
                         province NCHAR(12), 
                         city NCHAR(13), 
                         alarm BINARY(14))''')
    
        tdSql.execute("import into tbx file \'%s\'"%(self.csvfile))
        tdSql.query('select * from tbx')
        tdSql.checkRows(self.rows)
85 86
        #TD-4447 import the same csv twice
        tdSql.execute("import into tbx file \'%s\'"%(self.csvfile))
dengyihao's avatar
dengyihao 已提交
87 88 89 90 91 92 93 94 95 96

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


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