parInitialDTest.cpp 7.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
/*
 * 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 "parTestUtil.h"

using namespace std;

namespace ParserTest {

X
Xiaoyu Wang 已提交
22
class ParserInitialDTest : public ParserDdlTest {};
23

X
Xiaoyu Wang 已提交
24
// DELETE FROM table_name [WHERE condition]
X
Xiaoyu Wang 已提交
25 26 27 28 29 30
TEST_F(ParserInitialDTest, delete) {
  useDb("root", "test");

  run("DELETE FROM t1");

  run("DELETE FROM t1 WHERE ts > now - 2d and ts < now - 1d");
X
Xiaoyu Wang 已提交
31 32 33 34

  run("DELETE FROM st1");

  run("DELETE FROM st1 WHERE ts > now - 2d and ts < now - 1d AND tag1 = 10");
X
Xiaoyu Wang 已提交
35 36 37 38 39 40 41 42
}

TEST_F(ParserInitialDTest, deleteSemanticCheck) {
  useDb("root", "test");

  run("DELETE FROM t1 WHERE c1 > 10", TSDB_CODE_PAR_INVALID_DELETE_WHERE, PARSER_STAGE_TRANSLATE);
}

X
Xiaoyu Wang 已提交
43 44 45 46 47 48 49 50 51
// DESC table_name
TEST_F(ParserInitialDTest, describe) {
  useDb("root", "test");

  run("DESC t1");

  run("DESCRIBE st1");
}

52
// todo describe
X
Xiaoyu Wang 已提交
53
// todo DROP account
54

55
// DROP CONSUMER GROUP [ IF EXISTS ] cgroup_name ON topic_name
X
Xiaoyu Wang 已提交
56
TEST_F(ParserInitialDTest, dropConsumerGroup) {
X
Xiaoyu Wang 已提交
57 58 59 60
  useDb("root", "test");

  SMDropCgroupReq expect = {0};

61 62 63
  auto clearDropCgroupReq = [&]() { memset(&expect, 0, sizeof(SMDropCgroupReq)); };

  auto setDropCgroupReq = [&](const char* pTopicName, const char* pCGroupName, int8_t igNotExists = 0) {
X
Xiaoyu Wang 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    snprintf(expect.topic, sizeof(expect.topic), "0.%s", pTopicName);
    strcpy(expect.cgroup, pCGroupName);
    expect.igNotExists = igNotExists;
  };

  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_DROP_CGROUP_STMT);
    SMDropCgroupReq req = {0};
    ASSERT_TRUE(TSDB_CODE_SUCCESS == tDeserializeSMDropCgroupReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req));

    ASSERT_EQ(std::string(req.topic), std::string(expect.topic));
    ASSERT_EQ(std::string(req.cgroup), std::string(expect.cgroup));
    ASSERT_EQ(req.igNotExists, expect.igNotExists);
  });

79
  setDropCgroupReq("tp1", "cg1");
80
  run("DROP CONSUMER GROUP cg1 ON tp1");
81
  clearDropCgroupReq();
X
Xiaoyu Wang 已提交
82

83
  setDropCgroupReq("tp1", "cg1", 1);
84
  run("DROP CONSUMER GROUP IF EXISTS cg1 ON tp1");
85
  clearDropCgroupReq();
86 87
}

X
Xiaoyu Wang 已提交
88
// todo DROP database
89 90 91 92 93 94 95 96

TEST_F(ParserInitialDTest, dropDnode) {
  useDb("root", "test");

  SDropDnodeReq expect = {0};

  auto clearDropDnodeReq = [&]() { memset(&expect, 0, sizeof(SDropDnodeReq)); };

97 98 99 100
  auto setDropDnodeReqById = [&](int32_t dnodeId, bool force = false) {
    expect.dnodeId = dnodeId;
    expect.force = force;
  };
101

X
Xiaoyu Wang 已提交
102
  auto setDropDnodeReqByEndpoint = [&](const char* pFqdn, int32_t port = tsServerPort, bool force = false) {
103 104
    strcpy(expect.fqdn, pFqdn);
    expect.port = port;
105
    expect.force = force;
106 107 108 109 110 111 112 113 114 115
  };

  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_DROP_DNODE_STMT);
    SDropDnodeReq req = {0};
    ASSERT_TRUE(TSDB_CODE_SUCCESS == tDeserializeSDropDnodeReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req));

    ASSERT_EQ(req.dnodeId, expect.dnodeId);
    ASSERT_EQ(std::string(req.fqdn), std::string(expect.fqdn));
    ASSERT_EQ(req.port, expect.port);
116
    ASSERT_EQ(req.force, expect.force);
117 118 119 120 121 122
  });

  setDropDnodeReqById(1);
  run("DROP DNODE 1");
  clearDropDnodeReq();

123 124 125 126
  setDropDnodeReqById(2, true);
  run("DROP DNODE 2 FORCE");
  clearDropDnodeReq();

127 128 129
  setDropDnodeReqByEndpoint("host1", 7030);
  run("DROP DNODE 'host1:7030'");
  clearDropDnodeReq();
130 131 132 133

  setDropDnodeReqByEndpoint("host2", 8030, true);
  run("DROP DNODE 'host2:8030' FORCE");
  clearDropDnodeReq();
X
Xiaoyu Wang 已提交
134 135 136 137 138 139 140 141

  setDropDnodeReqByEndpoint("host1");
  run("DROP DNODE host1");
  clearDropDnodeReq();

  setDropDnodeReqByEndpoint("host2", tsServerPort, true);
  run("DROP DNODE host2 FORCE");
  clearDropDnodeReq();
142 143
}

X
Xiaoyu Wang 已提交
144
// todo DROP function
145 146 147 148

TEST_F(ParserInitialDTest, dropIndex) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
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
  SMDropSmaReq expect = {0};

  auto clearDropSmaReq = [&]() { memset(&expect, 0, sizeof(SMDropSmaReq)); };

  auto setDropSmaReq = [&](const char* pName, int8_t igNotExists = 0) {
    sprintf(expect.name, "0.test.%s", pName);
    expect.igNotExists = igNotExists;
  };

  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_DROP_INDEX_STMT);
    SMDropSmaReq req = {0};
    ASSERT_TRUE(TSDB_CODE_SUCCESS == tDeserializeSMDropSmaReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req));

    ASSERT_EQ(std::string(req.name), std::string(expect.name));
    ASSERT_EQ(req.igNotExists, expect.igNotExists);
  });

  setDropSmaReq("index1");
  run("DROP INDEX index1");
  clearDropSmaReq();

  setDropSmaReq("index2", 1);
  run("DROP INDEX IF EXISTS index2");
  clearDropSmaReq();
174 175 176 177 178
}

TEST_F(ParserInitialDTest, dropMnode) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
179
  run("DROP mnode on dnode 1");
180 181 182 183 184
}

TEST_F(ParserInitialDTest, dropQnode) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
  SMDropQnodeReq expect = {0};

  auto setDropQnodeReq = [&](int32_t dnodeId) { expect.dnodeId = dnodeId; };

  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_DROP_QNODE_STMT);
    SMDropQnodeReq req = {0};
    ASSERT_TRUE(TSDB_CODE_SUCCESS ==
                tDeserializeSCreateDropMQSNodeReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req));

    ASSERT_EQ(req.dnodeId, expect.dnodeId);
  });

  setDropQnodeReq(1);
  run("DROP QNODE ON DNODE 1");
200 201 202 203 204
}

TEST_F(ParserInitialDTest, dropSnode) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
205
  run("DROP snode on dnode 1");
206 207
}

X
Xiaoyu Wang 已提交
208 209 210 211 212 213
TEST_F(ParserInitialDTest, dropSTable) {
  useDb("root", "test");

  run("DROP STABLE st1");
}

X
Xiaoyu Wang 已提交
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
TEST_F(ParserInitialDTest, dropStream) {
  useDb("root", "test");

  SMDropStreamReq expect = {0};

  auto clearDropStreamReq = [&]() { memset(&expect, 0, sizeof(SMDropStreamReq)); };

  auto setDropStreamReq = [&](const char* pStream, int8_t igNotExists = 0) {
    sprintf(expect.name, "0.%s", pStream);
    expect.igNotExists = igNotExists;
  };

  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_DROP_STREAM_STMT);
    SMDropStreamReq req = {0};
    ASSERT_TRUE(TSDB_CODE_SUCCESS == tDeserializeSMDropStreamReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req));

    ASSERT_EQ(std::string(req.name), std::string(expect.name));
    ASSERT_EQ(req.igNotExists, expect.igNotExists);
  });

  setDropStreamReq("s1");
  run("DROP STREAM s1");
  clearDropStreamReq();

  setDropStreamReq("s2", 1);
  run("DROP STREAM IF EXISTS s2");
  clearDropStreamReq();
}
X
Xiaoyu Wang 已提交
243 244 245 246 247 248

TEST_F(ParserInitialDTest, dropTable) {
  useDb("root", "test");

  run("DROP TABLE t1");
}
249 250 251 252

TEST_F(ParserInitialDTest, dropTopic) {
  useDb("root", "test");

X
Xiaoyu Wang 已提交
253
  run("DROP topic tp1");
254

X
Xiaoyu Wang 已提交
255
  run("DROP topic if exists tp1");
256 257 258
}

TEST_F(ParserInitialDTest, dropUser) {
259
  login("root");
260 261
  useDb("root", "test");

X
Xiaoyu Wang 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275
  SDropUserReq expect = {0};

  auto setDropUserReq = [&](const char* pUser) { sprintf(expect.user, "%s", pUser); };

  setCheckDdlFunc([&](const SQuery* pQuery, ParserStage stage) {
    ASSERT_EQ(nodeType(pQuery->pRoot), QUERY_NODE_DROP_USER_STMT);
    SDropUserReq req = {0};
    ASSERT_TRUE(TSDB_CODE_SUCCESS == tDeserializeSDropUserReq(pQuery->pCmdMsg->pMsg, pQuery->pCmdMsg->msgLen, &req));

    ASSERT_EQ(std::string(req.user), std::string(expect.user));
  });

  setDropUserReq("wxy");
  run("DROP USER wxy");
276 277 278
}

}  // namespace ParserTest