func.cpp 16.8 KB
Newer Older
S
Shengliang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/**
 * @file func.cpp
 * @author slguan (slguan@taosdata.com)
 * @brief MNODE module func tests
 * @version 1.0
 * @date 2022-01-24
 *
 * @copyright Copyright (c) 2022
 *
 */

#include "sut.h"

class MndTestFunc : public ::testing::Test {
 protected:
  static void SetUpTestSuite() { test.Init("/tmp/mnode_test_func", 9038); }
  static void TearDownTestSuite() { test.Cleanup(); }

  static Testbase test;

 public:
  void SetUp() override {}
  void TearDown() override {}
24 25

  void SetCode(SCreateFuncReq* pReq, const char* pCode);
S
slzhou 已提交
26
  void SetCode(SCreateFuncReq* pReq, char* pCode, int32_t size);
27
  void SetComment(SCreateFuncReq* pReq, const char* pComment);
S
Shengliang 已提交
28 29 30 31
};

Testbase MndTestFunc::test;

32 33 34 35
void MndTestFunc::SetCode(SCreateFuncReq* pReq, const char* pCode) {
  int32_t len = strlen(pCode);
  pReq->pCode = (char*)taosMemoryCalloc(1, len + 1);
  strcpy(pReq->pCode, pCode);
36
  pReq->codeLen = len;
37 38
}

S
slzhou 已提交
39 40 41 42 43 44
void MndTestFunc::SetCode(SCreateFuncReq *pReq, char *pCode, int32_t size) {
  pReq->pCode = (char*)taosMemoryMalloc(size);
  memcpy(pReq->pCode, pCode, size);
  pReq->codeLen = size;
}

45 46 47 48 49 50
void MndTestFunc::SetComment(SCreateFuncReq* pReq, const char* pComment) {
  int32_t len = strlen(pComment);
  pReq->pComment = (char*)taosMemoryCalloc(1, len + 1);
  strcpy(pReq->pComment, pComment);
}

S
Shengliang 已提交
51
TEST_F(MndTestFunc, 01_Show_Func) {
S
Shengliang Guan 已提交
52
  test.SendShowReq(TSDB_MGMT_TABLE_FUNC, "user_functions", "");
S
Shengliang 已提交
53 54 55 56 57
  EXPECT_EQ(test.GetShowRows(), 0);
}

TEST_F(MndTestFunc, 02_Create_Func) {
  {
S
Shengliang Guan 已提交
58 59 60 61 62 63
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "");

    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
64
    tFreeSCreateFuncReq(&createReq);
S
Shengliang 已提交
65 66 67 68 69 70 71

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_NAME);
  }

  {
S
Shengliang Guan 已提交
72 73
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
74
    SetComment(&createReq, "comment1");
S
Shengliang Guan 已提交
75 76 77 78

    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
79
    tFreeSCreateFuncReq(&createReq);
S
Shengliang 已提交
80 81 82 83 84 85 86

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_CODE);
  }

  {
S
Shengliang Guan 已提交
87 88
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
89 90
    SetCode(&createReq, "");
    SetComment(&createReq, "comment1");
S
Shengliang Guan 已提交
91 92 93 94

    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
95
    tFreeSCreateFuncReq(&createReq);
S
Shengliang 已提交
96 97 98 99 100 101 102

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_CODE);
  }

  {
S
Shengliang Guan 已提交
103 104
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
105 106
    SetCode(&createReq, "code1");
    SetComment(&createReq, "comment1");
S
Shengliang Guan 已提交
107 108 109 110

    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
111
    tFreeSCreateFuncReq(&createReq);
S
Shengliang 已提交
112 113 114 115 116 117 118

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_BUFSIZE);
  }

  {
S
Shengliang Guan 已提交
119 120
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
121 122
    SetCode(&createReq, "code1");
    SetComment(&createReq, "comment1");
S
Shengliang Guan 已提交
123 124 125 126 127
    createReq.bufSize = TSDB_FUNC_BUF_SIZE + 1;

    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
128
    tFreeSCreateFuncReq(&createReq);
S
Shengliang Guan 已提交
129

S
Shengliang 已提交
130 131 132 133 134 135
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_BUFSIZE);
  }

  for (int32_t i = 0; i < 3; ++i) {
S
Shengliang Guan 已提交
136 137
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
138 139
    SetCode(&createReq, "code1");
    SetComment(&createReq, "comment1");
S
Shengliang Guan 已提交
140 141 142 143 144 145 146 147 148
    createReq.bufSize = TSDB_FUNC_BUF_SIZE + 1;
    createReq.igExists = 0;
    if (i == 2) createReq.igExists = 1;
    createReq.funcType = 1;
    createReq.scriptType = 2;
    createReq.outputType = TSDB_DATA_TYPE_SMALLINT;
    createReq.outputLen = 12;
    createReq.bufSize = 4;
    createReq.signature = 5;
S
Shengliang 已提交
149

S
Shengliang Guan 已提交
150 151 152
    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
153
    tFreeSCreateFuncReq(&createReq);
S
Shengliang Guan 已提交
154

S
Shengliang 已提交
155 156 157 158 159 160 161 162 163
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    if (i == 0 || i == 2) {
      ASSERT_EQ(pRsp->code, 0);
    } else {
      ASSERT_EQ(pRsp->code, TSDB_CODE_MND_FUNC_ALREADY_EXIST);
    }
  }

S
Shengliang Guan 已提交
164
  test.SendShowReq(TSDB_MGMT_TABLE_FUNC, "user_functions", "");
S
Shengliang 已提交
165 166 167 168 169
  EXPECT_EQ(test.GetShowRows(), 1);
}

TEST_F(MndTestFunc, 03_Retrieve_Func) {
  {
S
Shengliang Guan 已提交
170 171 172 173
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 1;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
    taosArrayPush(retrieveReq.pFuncNames, "f1");
S
Shengliang 已提交
174

S
Shengliang Guan 已提交
175 176 177
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
178
    tFreeSRetrieveFuncReq(&retrieveReq);
S
Shengliang 已提交
179 180 181 182 183

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

S
Shengliang Guan 已提交
184 185 186 187
    SRetrieveFuncRsp retrieveRsp = {0};
    tDeserializeSRetrieveFuncRsp(pRsp->pCont, pRsp->contLen, &retrieveRsp);
    EXPECT_EQ(retrieveRsp.numOfFuncs, 1);
    EXPECT_EQ(retrieveRsp.numOfFuncs, (int32_t)taosArrayGetSize(retrieveRsp.pFuncInfos));
S
Shengliang 已提交
188

S
Shengliang Guan 已提交
189
    SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 0);
S
Shengliang 已提交
190 191 192 193 194 195 196 197

    EXPECT_STREQ(pFuncInfo->name, "f1");
    EXPECT_EQ(pFuncInfo->funcType, 1);
    EXPECT_EQ(pFuncInfo->scriptType, 2);
    EXPECT_EQ(pFuncInfo->outputType, TSDB_DATA_TYPE_SMALLINT);
    EXPECT_EQ(pFuncInfo->outputLen, 12);
    EXPECT_EQ(pFuncInfo->bufSize, 4);
    EXPECT_EQ(pFuncInfo->signature, 5);
198 199
    EXPECT_STREQ("comment1", pFuncInfo->pComment);
    EXPECT_STREQ("code1", pFuncInfo->pCode);
S
Shengliang 已提交
200

201
    tFreeSRetrieveFuncRsp(&retrieveRsp);
S
Shengliang 已提交
202 203 204
  }

  {
S
Shengliang Guan 已提交
205 206 207
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 0;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
S
Shengliang 已提交
208

S
Shengliang Guan 已提交
209 210 211
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
212
    tFreeSRetrieveFuncReq(&retrieveReq);
S
Shengliang 已提交
213 214 215 216 217 218 219

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_RETRIEVE);
  }

  {
S
Shengliang Guan 已提交
220 221 222 223 224 225
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = TSDB_FUNC_MAX_RETRIEVE + 1;
    retrieveReq.pFuncNames = taosArrayInit(TSDB_FUNC_MAX_RETRIEVE + 1, TSDB_FUNC_NAME_LEN);
    for (int32_t i = 0; i < TSDB_FUNC_MAX_RETRIEVE + 1; ++i) {
      taosArrayPush(retrieveReq.pFuncNames, "1");
    }
S
Shengliang 已提交
226

S
Shengliang Guan 已提交
227 228 229
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
230
    tFreeSRetrieveFuncReq(&retrieveReq);
S
Shengliang 已提交
231 232 233 234 235 236 237

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_RETRIEVE);
  }

  {
S
Shengliang Guan 已提交
238 239 240 241
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 1;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
    taosArrayPush(retrieveReq.pFuncNames, "f2");
S
Shengliang 已提交
242

S
Shengliang Guan 已提交
243 244 245
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
246
    tFreeSRetrieveFuncReq(&retrieveReq);
S
Shengliang 已提交
247 248 249

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
D
dapan1121 已提交
250
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_FUNC_NOT_EXIST);
S
Shengliang 已提交
251 252 253
  }

  {
S
Shengliang Guan 已提交
254 255 256 257 258 259 260 261 262
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f2");
    createReq.igExists = 1;
    createReq.funcType = 2;
    createReq.scriptType = 3;
    createReq.outputType = TSDB_DATA_TYPE_BINARY;
    createReq.outputLen = 24;
    createReq.bufSize = 6;
    createReq.signature = 18;
263 264
    SetCode(&createReq, "code2");
    SetComment(&createReq, "comment2");
S
Shengliang 已提交
265

S
Shengliang Guan 已提交
266 267 268
    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
269
    tFreeSCreateFuncReq(&createReq);
S
Shengliang Guan 已提交
270

S
Shengliang 已提交
271 272 273 274
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

S
Shengliang Guan 已提交
275
    test.SendShowReq(TSDB_MGMT_TABLE_FUNC, "user_functions", "");
S
Shengliang 已提交
276 277 278 279
    EXPECT_EQ(test.GetShowRows(), 2);
  }

  {
S
Shengliang Guan 已提交
280 281 282 283
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 1;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
    taosArrayPush(retrieveReq.pFuncNames, "f2");
S
Shengliang 已提交
284

S
Shengliang Guan 已提交
285 286 287
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
288
    tFreeSRetrieveFuncReq(&retrieveReq);
S
Shengliang 已提交
289 290 291 292 293

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

S
Shengliang Guan 已提交
294 295 296 297
    SRetrieveFuncRsp retrieveRsp = {0};
    tDeserializeSRetrieveFuncRsp(pRsp->pCont, pRsp->contLen, &retrieveRsp);
    EXPECT_EQ(retrieveRsp.numOfFuncs, 1);
    EXPECT_EQ(retrieveRsp.numOfFuncs, (int32_t)taosArrayGetSize(retrieveRsp.pFuncInfos));
S
Shengliang 已提交
298

S
Shengliang Guan 已提交
299
    SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 0);
S
Shengliang 已提交
300 301 302 303 304 305 306 307

    EXPECT_STREQ(pFuncInfo->name, "f2");
    EXPECT_EQ(pFuncInfo->funcType, 2);
    EXPECT_EQ(pFuncInfo->scriptType, 3);
    EXPECT_EQ(pFuncInfo->outputType, TSDB_DATA_TYPE_BINARY);
    EXPECT_EQ(pFuncInfo->outputLen, 24);
    EXPECT_EQ(pFuncInfo->bufSize, 6);
    EXPECT_EQ(pFuncInfo->signature, 18);
308
    EXPECT_EQ(pFuncInfo->commentSize, strlen("comment2") + 1);
S
Shengliang 已提交
309

310 311
    EXPECT_STREQ("comment2", pFuncInfo->pComment);
    EXPECT_STREQ("code2", pFuncInfo->pCode);
S
Shengliang Guan 已提交
312

313
    tFreeSRetrieveFuncRsp(&retrieveRsp);
S
Shengliang 已提交
314 315 316
  }

  {
S
Shengliang Guan 已提交
317 318 319 320 321
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 2;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
    taosArrayPush(retrieveReq.pFuncNames, "f2");
    taosArrayPush(retrieveReq.pFuncNames, "f1");
S
Shengliang 已提交
322

S
Shengliang Guan 已提交
323 324 325
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
326
    tFreeSRetrieveFuncReq(&retrieveReq);
S
Shengliang 已提交
327 328 329 330 331

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

S
Shengliang Guan 已提交
332 333 334 335
    SRetrieveFuncRsp retrieveRsp = {0};
    tDeserializeSRetrieveFuncRsp(pRsp->pCont, pRsp->contLen, &retrieveRsp);
    EXPECT_EQ(retrieveRsp.numOfFuncs, 2);
    EXPECT_EQ(retrieveRsp.numOfFuncs, (int32_t)taosArrayGetSize(retrieveRsp.pFuncInfos));
S
Shengliang 已提交
336 337

    {
S
Shengliang Guan 已提交
338
      SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 0);
S
Shengliang 已提交
339 340 341 342 343 344 345
      EXPECT_STREQ(pFuncInfo->name, "f2");
      EXPECT_EQ(pFuncInfo->funcType, 2);
      EXPECT_EQ(pFuncInfo->scriptType, 3);
      EXPECT_EQ(pFuncInfo->outputType, TSDB_DATA_TYPE_BINARY);
      EXPECT_EQ(pFuncInfo->outputLen, 24);
      EXPECT_EQ(pFuncInfo->bufSize, 6);
      EXPECT_EQ(pFuncInfo->signature, 18);
346 347 348
      EXPECT_EQ(pFuncInfo->commentSize, strlen("comment2") + 1);
      EXPECT_STREQ("comment2", pFuncInfo->pComment);
      EXPECT_STREQ("code2", pFuncInfo->pCode);
S
Shengliang 已提交
349 350 351
    }

    {
S
Shengliang Guan 已提交
352
      SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 1);
S
Shengliang 已提交
353 354 355 356 357 358 359
      EXPECT_STREQ(pFuncInfo->name, "f1");
      EXPECT_EQ(pFuncInfo->funcType, 1);
      EXPECT_EQ(pFuncInfo->scriptType, 2);
      EXPECT_EQ(pFuncInfo->outputType, TSDB_DATA_TYPE_SMALLINT);
      EXPECT_EQ(pFuncInfo->outputLen, 12);
      EXPECT_EQ(pFuncInfo->bufSize, 4);
      EXPECT_EQ(pFuncInfo->signature, 5);
360 361
      EXPECT_STREQ("comment1", pFuncInfo->pComment);
      EXPECT_STREQ("code1", pFuncInfo->pCode);
S
Shengliang 已提交
362
    }
363 364

    tFreeSRetrieveFuncRsp(&retrieveRsp);
S
Shengliang 已提交
365 366
  }

S
Shengliang Guan 已提交
367 368 369 370 371 372
  {
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 2;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
    taosArrayPush(retrieveReq.pFuncNames, "f2");
    taosArrayPush(retrieveReq.pFuncNames, "f3");
S
Shengliang 已提交
373

S
Shengliang Guan 已提交
374 375 376
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
377
    tFreeSRetrieveFuncReq(&retrieveReq);
S
Shengliang 已提交
378 379 380

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
D
dapan1121 已提交
381
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_FUNC_NOT_EXIST);
S
Shengliang Guan 已提交
382
  }
S
Shengliang 已提交
383
}
S
Shengliang 已提交
384 385 386

TEST_F(MndTestFunc, 04_Drop_Func) {
  {
S
Shengliang Guan 已提交
387 388
    SDropFuncReq dropReq = {0};
    strcpy(dropReq.name, "");
S
Shengliang 已提交
389

S
Shengliang Guan 已提交
390 391 392
    int32_t contLen = tSerializeSDropFuncReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropFuncReq(pReq, contLen, &dropReq);
S
Shengliang 已提交
393 394 395

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
S
Shengliang 已提交
396
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_NAME);
S
Shengliang 已提交
397 398 399
  }

  {
S
Shengliang Guan 已提交
400 401
    SDropFuncReq dropReq = {0};
    strcpy(dropReq.name, "f3");
S
Shengliang 已提交
402

S
Shengliang Guan 已提交
403 404 405
    int32_t contLen = tSerializeSDropFuncReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropFuncReq(pReq, contLen, &dropReq);
S
Shengliang 已提交
406 407 408 409 410 411 412

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_FUNC_NOT_EXIST);
  }

  {
S
Shengliang Guan 已提交
413 414 415
    SDropFuncReq dropReq = {0};
    strcpy(dropReq.name, "f3");
    dropReq.igNotExists = 1;
S
Shengliang 已提交
416

S
Shengliang Guan 已提交
417 418 419
    int32_t contLen = tSerializeSDropFuncReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropFuncReq(pReq, contLen, &dropReq);
S
Shengliang 已提交
420 421 422 423 424 425 426

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }

  {
S
Shengliang Guan 已提交
427 428 429
    SDropFuncReq dropReq = {0};
    strcpy(dropReq.name, "f1");
    dropReq.igNotExists = 1;
S
Shengliang 已提交
430

S
Shengliang Guan 已提交
431 432 433
    int32_t contLen = tSerializeSDropFuncReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropFuncReq(pReq, contLen, &dropReq);
S
Shengliang 已提交
434 435 436 437 438 439

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }

S
Shengliang Guan 已提交
440
  test.SendShowReq(TSDB_MGMT_TABLE_FUNC, "user_functions", "");
S
Shengliang 已提交
441
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang 已提交
442 443 444 445

  // restart
  test.Restart();

S
Shengliang Guan 已提交
446
  test.SendShowReq(TSDB_MGMT_TABLE_FUNC, "user_functions", "");
S
Shengliang 已提交
447
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang 已提交
448
}
S
slzhou 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515

TEST_F(MndTestFunc, 05_Actual_code) {
  {
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "udf1");
    char code[300] = {0};
    for (int32_t i = 0; i < sizeof(code); ++i) {
      code[i] = i % 20;
    }
    SetCode(&createReq, code, 300);
    SetComment(&createReq, "comment1");
    createReq.bufSize = 8;
    createReq.igExists = 0;
    createReq.funcType = 1;
    createReq.scriptType = 2;
    createReq.outputType = TSDB_DATA_TYPE_SMALLINT;
    createReq.outputLen = 12;
    createReq.bufSize = 4;
    createReq.signature = 5;

    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
    tFreeSCreateFuncReq(&createReq);

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);
  }

  {
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 1;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
    taosArrayPush(retrieveReq.pFuncNames, "udf1");

    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
    tFreeSRetrieveFuncReq(&retrieveReq);

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

    SRetrieveFuncRsp retrieveRsp = {0};
    tDeserializeSRetrieveFuncRsp(pRsp->pCont, pRsp->contLen, &retrieveRsp);
    EXPECT_EQ(retrieveRsp.numOfFuncs, 1);
    EXPECT_EQ(retrieveRsp.numOfFuncs, (int32_t)taosArrayGetSize(retrieveRsp.pFuncInfos));

    SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 0);

    EXPECT_STREQ(pFuncInfo->name, "udf1");
    EXPECT_EQ(pFuncInfo->funcType, 1);
    EXPECT_EQ(pFuncInfo->scriptType, 2);
    EXPECT_EQ(pFuncInfo->outputType, TSDB_DATA_TYPE_SMALLINT);
    EXPECT_EQ(pFuncInfo->outputLen, 12);
    EXPECT_EQ(pFuncInfo->bufSize, 4);
    EXPECT_EQ(pFuncInfo->signature, 5);
    EXPECT_STREQ("comment1", pFuncInfo->pComment);
    for (int32_t i = 0; i < 300; ++i) {
        EXPECT_EQ(pFuncInfo->pCode[i], i % 20);
    }
    tFreeSRetrieveFuncRsp(&retrieveRsp);
  }

}