insertTest.cpp 2.6 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 30 31 32 33 34 35 36 37 38
/*
 * 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);
  }
}

// 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:
39 40 41 42
  void setDatabase(const string& db) {
    db_ = db;
  }

43 44
  void bind(const char* sql) {
    reset();
45 46 47 48 49
    cxt_.sqlLen = strlen(sql);
    strcpy(sqlBuf_, sql);
    sqlBuf_[cxt_.sqlLen] = '\0';
    cxt_.pSql = sqlBuf_;
    cxt_.pDbname = db_.c_str();
50 51 52
  }

  int32_t run() {
53 54 55
    code_ = parseInsertSql(&cxt_, &res_);
    if (code_ != TSDB_CODE_SUCCESS) {
      cout << "code:" << toString(code_) << ", msg:" << errMagBuf_ << endl;
56
    }
57
    return code_;
58 59 60
  }

  SInsertStmtInfo* reslut() {
61
    return res_;
62 63 64 65
  }

private:
  static const int max_err_len = 1024;
66
  static const int max_sql_len = 1024 * 1024;
67 68

  void reset() {
69 70 71 72 73 74
    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;
75 76
  }

77 78 79 80 81 82
  string db_;
  char errMagBuf_[max_err_len];
  char sqlBuf_[max_sql_len];
  SParseContext cxt_;
  int32_t code_;
  SInsertStmtInfo* res_;
83 84 85 86
};

// INSERT INTO tb_name VALUES (field1_value, ...)
TEST_F(InsertTest, simpleTest) {
87 88 89
  setDatabase("test");

  bind("insert into t1 values (now, 1, \"wxy\")");
90 91 92
  ASSERT_EQ(run(), TSDB_CODE_SUCCESS);
  SInsertStmtInfo* res = reslut();
  // todo check
93 94
  ASSERT_EQ(res->insertType, TSDB_QUERY_TYPE_INSERT);
  // ASSERT_EQ(taosArrayGetSize(res->pDataBlocks), 1);
95 96 97
}

TEST_F(InsertTest, toleranceTest) {
98 99
  setDatabase("test");

100 101 102 103 104
  bind("insert into");
  ASSERT_NE(run(), TSDB_CODE_SUCCESS);
  bind("insert into t");
  ASSERT_NE(run(), TSDB_CODE_SUCCESS);
}