func.cpp 16.7 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

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

Testbase MndTestFunc::test;

S
Shengliang Guan 已提交
32
void MndTestFunc::SetCode(SCreateFuncReq *pReq, const char *pCode, int32_t size) {
S
slzhou 已提交
33 34 35 36 37
  pReq->pCode = (char*)taosMemoryMalloc(size);
  memcpy(pReq->pCode, pCode, size);
  pReq->codeLen = size;
}

38 39 40 41 42 43
void MndTestFunc::SetComment(SCreateFuncReq* pReq, const char* pComment) {
  int32_t len = strlen(pComment);
  pReq->pComment = (char*)taosMemoryCalloc(1, len + 1);
  strcpy(pReq->pComment, pComment);
}

44 45 46 47
void MndTestFunc::SetBufSize(SCreateFuncReq* pReq, int32_t size) {
  pReq->bufSize = size;
}

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

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

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

    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 已提交
69 70
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
71
    SetComment(&createReq, "comment1");
S
Shengliang Guan 已提交
72 73 74 75

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

    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 已提交
84 85
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
S
Shengliang Guan 已提交
86
    SetCode(&createReq, "", 1);
87
    SetComment(&createReq, "comment1");
S
Shengliang Guan 已提交
88 89 90 91

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

    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 已提交
100 101
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
S
Shengliang Guan 已提交
102
    SetCode(&createReq, "code1", 6);
103
    SetComment(&createReq, "comment1");
104
    SetBufSize(&createReq, -1);
S
Shengliang Guan 已提交
105 106 107 108

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

    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 已提交
117 118
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
S
Shengliang Guan 已提交
119
    SetCode(&createReq, "code1", 6);
120
    SetComment(&createReq, "comment1");
S
Shengliang Guan 已提交
121 122 123 124 125
    createReq.bufSize = TSDB_FUNC_BUF_SIZE + 1;

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

S
Shengliang 已提交
128 129 130 131 132 133
    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 已提交
134 135
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
S
Shengliang Guan 已提交
136
    SetCode(&createReq, "code1", 6);
137
    SetComment(&createReq, "comment1");
S
Shengliang Guan 已提交
138 139 140 141 142 143 144 145 146
    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 已提交
147

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

S
Shengliang 已提交
153 154 155 156 157 158 159 160 161
    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 已提交
162
  test.SendShowReq(TSDB_MGMT_TABLE_FUNC, "user_functions", "");
S
Shengliang 已提交
163 164 165 166 167
  EXPECT_EQ(test.GetShowRows(), 1);
}

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

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

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

S
Shengliang Guan 已提交
182 183 184 185
    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 已提交
186

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

    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);
196 197
    EXPECT_STREQ("comment1", pFuncInfo->pComment);
    EXPECT_STREQ("code1", pFuncInfo->pCode);
S
Shengliang 已提交
198

199
    tFreeSRetrieveFuncRsp(&retrieveRsp);
S
Shengliang 已提交
200 201 202
  }

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

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

    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 已提交
218 219 220 221 222 223
    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 已提交
224

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

    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 已提交
236 237 238 239
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 1;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
    taosArrayPush(retrieveReq.pFuncNames, "f2");
S
Shengliang 已提交
240

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

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

  {
S
Shengliang Guan 已提交
252 253 254 255 256 257 258 259 260
    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;
S
Shengliang Guan 已提交
261
    SetCode(&createReq, "code2", 6);
262
    SetComment(&createReq, "comment2");
S
Shengliang 已提交
263

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

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

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

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

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

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

S
Shengliang Guan 已提交
292 293 294 295
    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 已提交
296

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

    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);
306
    EXPECT_EQ(pFuncInfo->commentSize, strlen("comment2") + 1);
S
Shengliang 已提交
307

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

311
    tFreeSRetrieveFuncRsp(&retrieveRsp);
S
Shengliang 已提交
312 313 314
  }

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

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

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

S
Shengliang Guan 已提交
330 331 332 333
    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 已提交
334 335

    {
S
Shengliang Guan 已提交
336
      SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 0);
S
Shengliang 已提交
337 338 339 340 341 342 343
      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);
344 345 346
      EXPECT_EQ(pFuncInfo->commentSize, strlen("comment2") + 1);
      EXPECT_STREQ("comment2", pFuncInfo->pComment);
      EXPECT_STREQ("code2", pFuncInfo->pCode);
S
Shengliang 已提交
347 348 349
    }

    {
S
Shengliang Guan 已提交
350
      SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 1);
S
Shengliang 已提交
351 352 353 354 355 356 357
      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);
358 359
      EXPECT_STREQ("comment1", pFuncInfo->pComment);
      EXPECT_STREQ("code1", pFuncInfo->pCode);
S
Shengliang 已提交
360
    }
361 362

    tFreeSRetrieveFuncRsp(&retrieveRsp);
S
Shengliang 已提交
363 364
  }

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

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

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

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

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

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

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

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

    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 已提交
411 412 413
    SDropFuncReq dropReq = {0};
    strcpy(dropReq.name, "f3");
    dropReq.igNotExists = 1;
S
Shengliang 已提交
414

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

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

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

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

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

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

  // restart
  test.Restart();

S
Shengliang Guan 已提交
444
  test.SendShowReq(TSDB_MGMT_TABLE_FUNC, "user_functions", "");
S
Shengliang 已提交
445
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang 已提交
446
}
S
slzhou 已提交
447 448 449 450 451 452 453

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) {
S
slzhou 已提交
454
      code[i] = (i) % 20;
S
slzhou 已提交
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
    }
    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) {
S
slzhou 已提交
508
        EXPECT_EQ(pFuncInfo->pCode[i], (i) % 20);
S
slzhou 已提交
509 510 511 512 513
    }
    tFreeSRetrieveFuncRsp(&retrieveRsp);
  }

}