func.cpp 16.5 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);
S
Shengliang 已提交
27 28 29 30
};

Testbase MndTestFunc::test;

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

37 38 39 40 41 42
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 已提交
43
TEST_F(MndTestFunc, 01_Show_Func) {
S
Shengliang Guan 已提交
44
  test.SendShowReq(TSDB_MGMT_TABLE_FUNC, "user_functions", "");
S
Shengliang 已提交
45 46 47 48 49
  EXPECT_EQ(test.GetShowRows(), 0);
}

TEST_F(MndTestFunc, 02_Create_Func) {
  {
S
Shengliang Guan 已提交
50 51 52 53 54 55
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "");

    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
56
    tFreeSCreateFuncReq(&createReq);
S
Shengliang 已提交
57 58 59 60 61 62 63

    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 已提交
64 65
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
66
    SetComment(&createReq, "comment1");
S
Shengliang Guan 已提交
67 68 69 70

    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
71
    tFreeSCreateFuncReq(&createReq);
S
Shengliang 已提交
72 73 74 75 76 77 78

    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 已提交
79 80
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
S
Shengliang Guan 已提交
81
    SetCode(&createReq, "", 1);
82
    SetComment(&createReq, "comment1");
S
Shengliang Guan 已提交
83 84 85 86

    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
87
    tFreeSCreateFuncReq(&createReq);
S
Shengliang 已提交
88 89 90 91 92 93 94

    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 已提交
95 96
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
S
Shengliang Guan 已提交
97
    SetCode(&createReq, "code1", 6);
98
    SetComment(&createReq, "comment1");
S
Shengliang Guan 已提交
99 100 101 102

    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
103
    tFreeSCreateFuncReq(&createReq);
S
Shengliang 已提交
104 105 106 107 108 109 110

    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 已提交
111 112
    SCreateFuncReq createReq = {0};
    strcpy(createReq.name, "f1");
S
Shengliang Guan 已提交
113
    SetCode(&createReq, "code1", 6);
114
    SetComment(&createReq, "comment1");
S
Shengliang Guan 已提交
115 116 117 118 119
    createReq.bufSize = TSDB_FUNC_BUF_SIZE + 1;

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

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

S
Shengliang Guan 已提交
142 143 144
    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
145
    tFreeSCreateFuncReq(&createReq);
S
Shengliang Guan 已提交
146

S
Shengliang 已提交
147 148 149 150 151 152 153 154 155
    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 已提交
156
  test.SendShowReq(TSDB_MGMT_TABLE_FUNC, "user_functions", "");
S
Shengliang 已提交
157 158 159 160 161
  EXPECT_EQ(test.GetShowRows(), 1);
}

TEST_F(MndTestFunc, 03_Retrieve_Func) {
  {
S
Shengliang Guan 已提交
162 163 164 165
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 1;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
    taosArrayPush(retrieveReq.pFuncNames, "f1");
S
Shengliang 已提交
166

S
Shengliang Guan 已提交
167 168 169
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
170
    tFreeSRetrieveFuncReq(&retrieveReq);
S
Shengliang 已提交
171 172 173 174 175

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

S
Shengliang Guan 已提交
176 177 178 179
    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 已提交
180

S
Shengliang Guan 已提交
181
    SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 0);
S
Shengliang 已提交
182 183 184 185 186 187 188 189

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

193
    tFreeSRetrieveFuncRsp(&retrieveRsp);
S
Shengliang 已提交
194 195 196
  }

  {
S
Shengliang Guan 已提交
197 198 199
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 0;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
S
Shengliang 已提交
200

S
Shengliang Guan 已提交
201 202 203
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
204
    tFreeSRetrieveFuncReq(&retrieveReq);
S
Shengliang 已提交
205 206 207 208 209 210 211

    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 已提交
212 213 214 215 216 217
    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 已提交
218

S
Shengliang Guan 已提交
219 220 221
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
222
    tFreeSRetrieveFuncReq(&retrieveReq);
S
Shengliang 已提交
223 224 225 226 227 228 229

    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 已提交
230 231 232 233
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 1;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
    taosArrayPush(retrieveReq.pFuncNames, "f2");
S
Shengliang 已提交
234

S
Shengliang Guan 已提交
235 236 237
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
238
    tFreeSRetrieveFuncReq(&retrieveReq);
S
Shengliang 已提交
239 240 241

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
D
dapan1121 已提交
242
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_FUNC_NOT_EXIST);
S
Shengliang 已提交
243 244 245
  }

  {
S
Shengliang Guan 已提交
246 247 248 249 250 251 252 253 254
    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 已提交
255
    SetCode(&createReq, "code2", 6);
256
    SetComment(&createReq, "comment2");
S
Shengliang 已提交
257

S
Shengliang Guan 已提交
258 259 260
    int32_t contLen = tSerializeSCreateFuncReq(NULL, 0, &createReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSCreateFuncReq(pReq, contLen, &createReq);
261
    tFreeSCreateFuncReq(&createReq);
S
Shengliang Guan 已提交
262

S
Shengliang 已提交
263 264 265 266
    SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
    ASSERT_EQ(pRsp->code, 0);

S
Shengliang Guan 已提交
267
    test.SendShowReq(TSDB_MGMT_TABLE_FUNC, "user_functions", "");
S
Shengliang 已提交
268 269 270 271
    EXPECT_EQ(test.GetShowRows(), 2);
  }

  {
S
Shengliang Guan 已提交
272 273 274 275
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 1;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
    taosArrayPush(retrieveReq.pFuncNames, "f2");
S
Shengliang 已提交
276

S
Shengliang Guan 已提交
277 278 279
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
280
    tFreeSRetrieveFuncReq(&retrieveReq);
S
Shengliang 已提交
281 282 283 284 285

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

S
Shengliang Guan 已提交
286 287 288 289
    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 已提交
290

S
Shengliang Guan 已提交
291
    SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 0);
S
Shengliang 已提交
292 293 294 295 296 297 298 299

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

302 303
    EXPECT_STREQ("comment2", pFuncInfo->pComment);
    EXPECT_STREQ("code2", pFuncInfo->pCode);
S
Shengliang Guan 已提交
304

305
    tFreeSRetrieveFuncRsp(&retrieveRsp);
S
Shengliang 已提交
306 307 308
  }

  {
S
Shengliang Guan 已提交
309 310 311 312 313
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 2;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
    taosArrayPush(retrieveReq.pFuncNames, "f2");
    taosArrayPush(retrieveReq.pFuncNames, "f1");
S
Shengliang 已提交
314

S
Shengliang Guan 已提交
315 316 317
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
318
    tFreeSRetrieveFuncReq(&retrieveReq);
S
Shengliang 已提交
319 320 321 322 323

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

S
Shengliang Guan 已提交
324 325 326 327
    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 已提交
328 329

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

    {
S
Shengliang Guan 已提交
344
      SFuncInfo* pFuncInfo = (SFuncInfo*)taosArrayGet(retrieveRsp.pFuncInfos, 1);
S
Shengliang 已提交
345 346 347 348 349 350 351
      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);
352 353
      EXPECT_STREQ("comment1", pFuncInfo->pComment);
      EXPECT_STREQ("code1", pFuncInfo->pCode);
S
Shengliang 已提交
354
    }
355 356

    tFreeSRetrieveFuncRsp(&retrieveRsp);
S
Shengliang 已提交
357 358
  }

S
Shengliang Guan 已提交
359 360 361 362 363 364
  {
    SRetrieveFuncReq retrieveReq = {0};
    retrieveReq.numOfFuncs = 2;
    retrieveReq.pFuncNames = taosArrayInit(1, TSDB_FUNC_NAME_LEN);
    taosArrayPush(retrieveReq.pFuncNames, "f2");
    taosArrayPush(retrieveReq.pFuncNames, "f3");
S
Shengliang 已提交
365

S
Shengliang Guan 已提交
366 367 368
    int32_t contLen = tSerializeSRetrieveFuncReq(NULL, 0, &retrieveReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSRetrieveFuncReq(pReq, contLen, &retrieveReq);
369
    tFreeSRetrieveFuncReq(&retrieveReq);
S
Shengliang 已提交
370 371 372

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_RETRIEVE_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
D
dapan1121 已提交
373
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_FUNC_NOT_EXIST);
S
Shengliang Guan 已提交
374
  }
S
Shengliang 已提交
375
}
S
Shengliang 已提交
376 377 378

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

S
Shengliang Guan 已提交
382 383 384
    int32_t contLen = tSerializeSDropFuncReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropFuncReq(pReq, contLen, &dropReq);
S
Shengliang 已提交
385 386 387

    SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_FUNC, pReq, contLen);
    ASSERT_NE(pRsp, nullptr);
S
Shengliang 已提交
388
    ASSERT_EQ(pRsp->code, TSDB_CODE_MND_INVALID_FUNC_NAME);
S
Shengliang 已提交
389 390 391
  }

  {
S
Shengliang Guan 已提交
392 393
    SDropFuncReq dropReq = {0};
    strcpy(dropReq.name, "f3");
S
Shengliang 已提交
394

S
Shengliang Guan 已提交
395 396 397
    int32_t contLen = tSerializeSDropFuncReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropFuncReq(pReq, contLen, &dropReq);
S
Shengliang 已提交
398 399 400 401 402 403 404

    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 已提交
405 406 407
    SDropFuncReq dropReq = {0};
    strcpy(dropReq.name, "f3");
    dropReq.igNotExists = 1;
S
Shengliang 已提交
408

S
Shengliang Guan 已提交
409 410 411
    int32_t contLen = tSerializeSDropFuncReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropFuncReq(pReq, contLen, &dropReq);
S
Shengliang 已提交
412 413 414 415 416 417 418

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

  {
S
Shengliang Guan 已提交
419 420 421
    SDropFuncReq dropReq = {0};
    strcpy(dropReq.name, "f1");
    dropReq.igNotExists = 1;
S
Shengliang 已提交
422

S
Shengliang Guan 已提交
423 424 425
    int32_t contLen = tSerializeSDropFuncReq(NULL, 0, &dropReq);
    void*   pReq = rpcMallocCont(contLen);
    tSerializeSDropFuncReq(pReq, contLen, &dropReq);
S
Shengliang 已提交
426 427 428 429 430 431

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

S
Shengliang Guan 已提交
432
  test.SendShowReq(TSDB_MGMT_TABLE_FUNC, "user_functions", "");
S
Shengliang 已提交
433
  EXPECT_EQ(test.GetShowRows(), 1);
S
Shengliang 已提交
434 435 436 437

  // restart
  test.Restart();

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

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 已提交
448
      code[i] = (i) % 20;
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
    }
    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 已提交
502
        EXPECT_EQ(pFuncInfo->pCode[i], (i) % 20);
S
slzhou 已提交
503 504 505 506 507
    }
    tFreeSRetrieveFuncRsp(&retrieveRsp);
  }

}