insertTest.cpp 3.1 KB
Newer Older
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
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <gtest/gtest.h>

#include "insertParser.h"
#include "mockCatalog.h"

using namespace std;
using namespace testing;

namespace {
  string toString(int32_t code) {
    return tstrerror(code);
  }
}

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
extern "C" {

#include <execinfo.h>

void *__real_malloc(size_t);

void *__wrap_malloc(size_t c) {  
  // printf("My MALLOC called: %d\n", c);
  // void *array[32]; 
  // int size = backtrace(array, 32); 
  // char **symbols = backtrace_symbols(array, size); 
  // for (int i = 0; i < size; ++i) { 
  //   cout << symbols[i] << endl;
  // } 
  // free(symbols); 

  return __real_malloc(c);
}

}

51 52 53 54 55 56 57 58 59
// syntax:
// INSERT INTO
//   tb_name
//       [USING stb_name [(tag1_name, ...)] TAGS (tag1_value, ...)]
//       [(field1_name, ...)]
//       VALUES (field1_value, ...) [(field1_value2, ...) ...] | FILE csv_file_path
//   [...];
class InsertTest : public Test {
protected:
60 61
  void setDatabase(const string& acctId, const string& db) {
    acctId_ = acctId;
62 63 64
    db_ = db;
  }

65 66
  void bind(const char* sql) {
    reset();
67 68
    cxt_.pAcctId = acctId_.c_str();
    cxt_.pDbname = db_.c_str();
69
    strcpy(sqlBuf_, sql);
70
    cxt_.sqlLen = strlen(sql);
71 72
    sqlBuf_[cxt_.sqlLen] = '\0';
    cxt_.pSql = sqlBuf_;
73

74 75 76
  }

  int32_t run() {
77 78 79
    code_ = parseInsertSql(&cxt_, &res_);
    if (code_ != TSDB_CODE_SUCCESS) {
      cout << "code:" << toString(code_) << ", msg:" << errMagBuf_ << endl;
80
    }
81
    return code_;
82 83 84
  }

  SInsertStmtInfo* reslut() {
85
    return res_;
86 87 88 89
  }

private:
  static const int max_err_len = 1024;
90
  static const int max_sql_len = 1024 * 1024;
91 92

  void reset() {
93 94 95 96 97 98
    memset(&cxt_, 0, sizeof(cxt_));
    memset(errMagBuf_, 0, max_err_len);
    cxt_.pMsg = errMagBuf_;
    cxt_.msgLen = max_err_len;
    code_ = TSDB_CODE_SUCCESS;
    res_ = nullptr;
99 100
  }

101
  string acctId_;
102 103 104 105 106 107
  string db_;
  char errMagBuf_[max_err_len];
  char sqlBuf_[max_sql_len];
  SParseContext cxt_;
  int32_t code_;
  SInsertStmtInfo* res_;
108 109 110 111
};

// INSERT INTO tb_name VALUES (field1_value, ...)
TEST_F(InsertTest, simpleTest) {
112
  setDatabase("root", "test");
113 114

  bind("insert into t1 values (now, 1, \"wxy\")");
115 116 117
  ASSERT_EQ(run(), TSDB_CODE_SUCCESS);
  SInsertStmtInfo* res = reslut();
  // todo check
118 119
  ASSERT_EQ(res->insertType, TSDB_QUERY_TYPE_INSERT);
  // ASSERT_EQ(taosArrayGetSize(res->pDataBlocks), 1);
120 121 122
}

TEST_F(InsertTest, toleranceTest) {
123
  setDatabase("root", "test");
124

125 126 127 128 129
  bind("insert into");
  ASSERT_NE(run(), TSDB_CODE_SUCCESS);
  bind("insert into t");
  ASSERT_NE(run(), TSDB_CODE_SUCCESS);
}