smlTest.cpp 27.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
/*
 * 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 <taoserror.h>
#include <tglobal.h>
#include <iostream>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwrite-strings"
#pragma GCC diagnostic ignored "-Wunused-function"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wsign-compare"

#include "../src/clientSml.c"
#include "taos.h"

wmmhello's avatar
wmmhello 已提交
31
int main(int argc, char **argv) {
32 33 34 35
  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

36
TEST(testCase, smlParseInfluxString_Test) {
wmmhello's avatar
wmmhello 已提交
37
  char       msg[256] = {0};
38 39 40 41 42
  SSmlMsgBuf msgBuf;
  msgBuf.buf = msg;
  msgBuf.len = 256;
  SSmlLineInfo elements = {0};

wmmhello's avatar
wmmhello 已提交
43
  // case 1
44
  char *sql = "st,t1=3,t2=4,t3=t3 c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64 1626006833639000000    ,32,c=3";
45
  int   ret = smlParseInfluxString(sql, &elements, &msgBuf);
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(elements.measure, sql);
  ASSERT_EQ(elements.measureLen, strlen("st"));
  ASSERT_EQ(elements.measureTagsLen, strlen("st,t1=3,t2=4,t3=t3"));

  ASSERT_EQ(elements.tags, sql + elements.measureLen + 1);
  ASSERT_EQ(elements.tagsLen, strlen("t1=3,t2=4,t3=t3"));

  ASSERT_EQ(elements.cols, sql + elements.measureTagsLen + 1);
  ASSERT_EQ(elements.colsLen, strlen("c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64"));

  ASSERT_EQ(elements.timestamp, sql + elements.measureTagsLen + 1 + elements.colsLen + 1);
  ASSERT_EQ(elements.timestampLen, strlen("1626006833639000000"));

  // case 2  false
  sql = "st,t1=3,t2=4,t3=t3 c1=3i64,c3=\"passit hello,c1=2,c2=false,c4=4f64 1626006833639000000";
  memset(&elements, 0, sizeof(SSmlLineInfo));
63
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
64 65 66 67 68
  ASSERT_NE(ret, 0);

  // case 3  false
  sql = "st, t1=3,t2=4,t3=t3 c1=3i64,c3=\"passit hello,c1=2,c2=false,c4=4f64 1626006833639000000";
  memset(&elements, 0, sizeof(SSmlLineInfo));
69
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
70 71 72 73 74 75 76
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(elements.cols, sql + elements.measureTagsLen + 2);
  ASSERT_EQ(elements.colsLen, strlen("t1=3,t2=4,t3=t3"));

  // case 4  tag is null
  sql = "st, c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64 1626006833639000000";
  memset(&elements, 0, sizeof(SSmlLineInfo));
77
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(elements.measure, sql);
  ASSERT_EQ(elements.measureLen, strlen("st"));
  ASSERT_EQ(elements.measureTagsLen, strlen("st"));

  ASSERT_EQ(elements.tags, sql + elements.measureLen + 1);
  ASSERT_EQ(elements.tagsLen, 0);

  ASSERT_EQ(elements.cols, sql + elements.measureTagsLen + 2);
  ASSERT_EQ(elements.colsLen, strlen("c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64"));

  ASSERT_EQ(elements.timestamp, sql + elements.measureTagsLen + 2 + elements.colsLen + 1);
  ASSERT_EQ(elements.timestampLen, strlen("1626006833639000000"));

  // case 5 tag is null
  sql = " st   c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64  1626006833639000000 ";
  memset(&elements, 0, sizeof(SSmlLineInfo));
95
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  sql++;
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(elements.measure, sql);
  ASSERT_EQ(elements.measureLen, strlen("st"));
  ASSERT_EQ(elements.measureTagsLen, strlen("st"));

  ASSERT_EQ(elements.tags, sql + elements.measureLen);
  ASSERT_EQ(elements.tagsLen, 0);

  ASSERT_EQ(elements.cols, sql + elements.measureTagsLen + 3);
  ASSERT_EQ(elements.colsLen, strlen("c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64"));

  ASSERT_EQ(elements.timestamp, sql + elements.measureTagsLen + 3 + elements.colsLen + 2);
  ASSERT_EQ(elements.timestampLen, strlen("1626006833639000000"));

  // case 6
  sql = " st   c1=3i64,c3=\"passit hello,c1=2\",c2=false,c4=4f64   ";
  memset(&elements, 0, sizeof(SSmlLineInfo));
114
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
115 116 117 118 119
  ASSERT_EQ(ret, 0);

  // case 7
  sql = " st   ,   ";
  memset(&elements, 0, sizeof(SSmlLineInfo));
120
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
121 122 123 124 125 126 127 128
  sql++;
  ASSERT_EQ(ret, 0);
  ASSERT_EQ(elements.cols, sql + elements.measureTagsLen + 3);
  ASSERT_EQ(elements.colsLen, strlen(","));

  // case 8 false
  sql = ", st   ,   ";
  memset(&elements, 0, sizeof(SSmlLineInfo));
129
  ret = smlParseInfluxString(sql, &elements, &msgBuf);
130 131 132
  ASSERT_NE(ret, 0);
}

wmmhello's avatar
wmmhello 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146
TEST(testCase, smlParseCols_Error_Test) {
  const char *data[] = {
    "c=\"89sd",           // binary, nchar
    "c=j\"89sd\"",
    "c=\"89sd\"k",
    "c=u",                // bool
    "c=truet",
    "c=f64",              // double
    "c=8f64f",
    "c=8ef64",
    "c=f32",              // float
    "c=8f32f",
    "c=8wef32",
    "c=-3.402823466e+39f32",
147
    "c=",                 // double
wmmhello's avatar
wmmhello 已提交
148 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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    "c=8f",
    "c=8we",
    "c=i8",              // tiny int
    "c=-8i8f",
    "c=8wei8",
    "c=-999i8",
    "c=u8",              // u tiny int
    "c=8fu8",
    "c=8weu8",
    "c=999u8",
    "c=-8u8",
    "c=i16",              // small int
    "c=8fi16u",
    "c=8wei16",
    "c=-67787i16",
    "c=u16",              // u small int
    "c=8u16f",
    "c=8weu16",
    "c=-9u16",
    "c=67787u16",
    "c=i32",                // int
    "c=8i32f",
    "c=8wei32",
    "c=2147483649i32",
    "c=u32",                // u int
    "c=8u32f",
    "c=8weu32",
    "c=-4u32",
    "c=42949672958u32",
    "c=i64",                // big int
    "c=8i64i",
    "c=8wei64",
    "c=-9223372036854775809i64",
    "c=i",                  // big int
    "c=8fi",
    "c=8wei",
    "c=9223372036854775808i",
    "c=u64",                // u big int
    "c=8u64f",
    "c=8weu64",
    "c=-3.402823466e+39u64",
    "c=-339u64",
    "c=18446744073709551616u64",
191
    "c=1,c=2"
wmmhello's avatar
wmmhello 已提交
192 193
  };

194
  SHashObj *dumplicateKey = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
wmmhello's avatar
wmmhello 已提交
195
  for(int i = 0; i < sizeof(data)/sizeof(data[0]); i++){
wmmhello's avatar
wmmhello 已提交
196 197 198 199
    char       msg[256] = {0};
    SSmlMsgBuf msgBuf;
    msgBuf.buf = msg;
    msgBuf.len = 256;
wmmhello's avatar
wmmhello 已提交
200
    int32_t len = strlen(data[i]);
201
    int32_t ret = smlParseCols(data[i], len, NULL, false, dumplicateKey, &msgBuf);
wmmhello's avatar
wmmhello 已提交
202
    ASSERT_NE(ret, TSDB_CODE_SUCCESS);
203
    taosHashClear(dumplicateKey);
wmmhello's avatar
wmmhello 已提交
204
  }
205
  taosHashCleanup(dumplicateKey);
wmmhello's avatar
wmmhello 已提交
206 207
}

wmmhello's avatar
wmmhello 已提交
208 209 210 211 212 213 214
TEST(testCase, smlParseCols_tag_Test) {
  char       msg[256] = {0};
  SSmlMsgBuf msgBuf;
  msgBuf.buf = msg;
  msgBuf.len = 256;

  SArray *cols = taosArrayInit(16, POINTER_BYTES);
wmmhello's avatar
wmmhello 已提交
215
  ASSERT_NE(cols, nullptr);
216
  SHashObj *dumplicateKey = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
wmmhello's avatar
wmmhello 已提交
217 218

  const char *data =
219
      "cbin=\"passit helloc=2\",cnch=L\"iisdfsf\",cbool=false,cf64=4.31f64,cf64_=8.32,cf32=8.23f32,ci8=-34i8,cu8=89u8,ci16=233i16,cu16=898u16,ci32=98289i32,cu32=12323u32,ci64=-89238i64,ci=989i,cu64=8989323u64,cbooltrue=true,cboolt=t,cboolf=f,cnch_=l\"iuwq\"";
wmmhello's avatar
wmmhello 已提交
220
  int32_t len = strlen(data);
221
  int32_t ret = smlParseCols(data, len, cols, true, dumplicateKey, &msgBuf);
wmmhello's avatar
wmmhello 已提交
222 223 224 225 226
  ASSERT_EQ(ret, TSDB_CODE_SUCCESS);
  int32_t size = taosArrayGetSize(cols);
  ASSERT_EQ(size, 19);

  // nchar
wmmhello's avatar
wmmhello 已提交
227
  SSmlKv *kv = (SSmlKv *)taosArrayGetP(cols, 0);
wmmhello's avatar
wmmhello 已提交
228 229 230
  ASSERT_EQ(strncasecmp(kv->key, "cbin", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR);
wmmhello's avatar
wmmhello 已提交
231
  ASSERT_EQ(kv->length, 17);
wmmhello's avatar
wmmhello 已提交
232 233 234 235
  ASSERT_EQ(strncasecmp(kv->value, "\"passit", 7), 0);
  taosMemoryFree(kv);

  // nchar
wmmhello's avatar
wmmhello 已提交
236
  kv = (SSmlKv *)taosArrayGetP(cols, 3);
wmmhello's avatar
wmmhello 已提交
237 238 239
  ASSERT_EQ(strncasecmp(kv->key, "cf64", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR);
wmmhello's avatar
wmmhello 已提交
240
  ASSERT_EQ(kv->length, 7);
wmmhello's avatar
wmmhello 已提交
241 242 243 244
  ASSERT_EQ(strncasecmp(kv->value, "4.31f64", 7), 0);
  taosMemoryFree(kv);

  taosArrayClear(cols);
245 246 247


  // test tag is null
wmmhello's avatar
wmmhello 已提交
248 249 250
  data = "t=3e";
  len = 0;
  memset(msgBuf.buf, 0, msgBuf.len);
251 252
  taosHashClear(dumplicateKey);
  ret = smlParseCols(data, len, cols, true, dumplicateKey, &msgBuf);
wmmhello's avatar
wmmhello 已提交
253 254 255 256 257
  ASSERT_EQ(ret, TSDB_CODE_SUCCESS);
  size = taosArrayGetSize(cols);
  ASSERT_EQ(size, 1);

  // nchar
wmmhello's avatar
wmmhello 已提交
258
  kv = (SSmlKv *)taosArrayGetP(cols, 0);
wmmhello's avatar
wmmhello 已提交
259 260 261
  ASSERT_EQ(strncasecmp(kv->key, TAG, strlen(TAG)), 0);
  ASSERT_EQ(kv->keyLen, strlen(TAG));
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR);
wmmhello's avatar
wmmhello 已提交
262
  ASSERT_EQ(kv->length, strlen(TAG));
wmmhello's avatar
wmmhello 已提交
263 264
  ASSERT_EQ(strncasecmp(kv->value, TAG, strlen(TAG)), 0);
  taosMemoryFree(kv);
265 266 267

  taosArrayDestroy(cols);
  taosHashCleanup(dumplicateKey);
wmmhello's avatar
wmmhello 已提交
268 269
}

wmmhello's avatar
wmmhello 已提交
270 271 272 273 274 275 276
TEST(testCase, smlParseCols_Test) {
  char       msg[256] = {0};
  SSmlMsgBuf msgBuf;
  msgBuf.buf = msg;
  msgBuf.len = 256;

  SArray *cols = taosArrayInit(16, POINTER_BYTES);
wmmhello's avatar
wmmhello 已提交
277
  ASSERT_NE(cols, nullptr);
wmmhello's avatar
wmmhello 已提交
278

279 280
  SHashObj *dumplicateKey = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);

281
  const char *data = "cbin=\"passit hello,c=2\",cnch=L\"iisdfsf\",cbool=false,cf64=4.31f64,cf64_=8.32,cf32=8.23f32,ci8=-34i8,cu8=89u8,ci16=233i16,cu16=898u16,ci32=98289i32,cu32=12323u32,ci64=-89238i64,ci=989i,cu64=8989323u64,cbooltrue=true,cboolt=t,cboolf=f,cnch_=l\"iuwq\"";
wmmhello's avatar
wmmhello 已提交
282
  int32_t len = strlen(data);
283
  int32_t ret = smlParseCols(data, len, cols, false, dumplicateKey, &msgBuf);
wmmhello's avatar
wmmhello 已提交
284 285 286 287 288
  ASSERT_EQ(ret, TSDB_CODE_SUCCESS);
  int32_t size = taosArrayGetSize(cols);
  ASSERT_EQ(size, 19);

  // binary
wmmhello's avatar
wmmhello 已提交
289
  SSmlKv *kv = (SSmlKv *)taosArrayGetP(cols, 0);
wmmhello's avatar
wmmhello 已提交
290
  ASSERT_EQ(strncasecmp(kv->key, "cbin", 4), 0);
wmmhello's avatar
wmmhello 已提交
291 292
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BINARY);
wmmhello's avatar
wmmhello 已提交
293 294 295 296 297
  ASSERT_EQ(kv->length, 16);
  ASSERT_EQ(strncasecmp(kv->value, "passit", 6), 0);
  taosMemoryFree(kv);

  // nchar
wmmhello's avatar
wmmhello 已提交
298
  kv = (SSmlKv *)taosArrayGetP(cols, 1);
wmmhello's avatar
wmmhello 已提交
299 300 301 302 303 304 305 306
  ASSERT_EQ(strncasecmp(kv->key, "cnch", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR);
  ASSERT_EQ(kv->length, 7);
  ASSERT_EQ(strncasecmp(kv->value, "iisd", 4), 0);
  taosMemoryFree(kv);

  // bool
wmmhello's avatar
wmmhello 已提交
307
  kv = (SSmlKv *)taosArrayGetP(cols, 2);
wmmhello's avatar
wmmhello 已提交
308 309 310
  ASSERT_EQ(strncasecmp(kv->key, "cbool", 5), 0);
  ASSERT_EQ(kv->keyLen, 5);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BOOL);
wmmhello's avatar
wmmhello 已提交
311
  ASSERT_EQ(kv->length, 1);
wmmhello's avatar
wmmhello 已提交
312 313 314 315
  ASSERT_EQ(kv->i, false);
  taosMemoryFree(kv);

  // double
wmmhello's avatar
wmmhello 已提交
316
  kv = (SSmlKv *)taosArrayGetP(cols, 3);
wmmhello's avatar
wmmhello 已提交
317 318 319
  ASSERT_EQ(strncasecmp(kv->key, "cf64", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_DOUBLE);
wmmhello's avatar
wmmhello 已提交
320 321
  ASSERT_EQ(kv->length, 8);
  //ASSERT_EQ(kv->d, 4.31);
322
  printf("4.31 = kv->d:%f\n", kv->d);
wmmhello's avatar
wmmhello 已提交
323 324 325
  taosMemoryFree(kv);

  // float
wmmhello's avatar
wmmhello 已提交
326
  kv = (SSmlKv *)taosArrayGetP(cols, 4);
327
  ASSERT_EQ(strncasecmp(kv->key, "cf64_", 5), 0);
wmmhello's avatar
wmmhello 已提交
328
  ASSERT_EQ(kv->keyLen, 5);
329 330
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_DOUBLE);
  ASSERT_EQ(kv->length, 8);
wmmhello's avatar
wmmhello 已提交
331
  //ASSERT_EQ(kv->f, 8.32);
332
  printf("8.32 = kv->d:%f\n", kv->d);
wmmhello's avatar
wmmhello 已提交
333 334 335
  taosMemoryFree(kv);

  // float
wmmhello's avatar
wmmhello 已提交
336
  kv = (SSmlKv *)taosArrayGetP(cols, 5);
wmmhello's avatar
wmmhello 已提交
337 338 339 340
  ASSERT_EQ(strncasecmp(kv->key, "cf32", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_FLOAT);
  ASSERT_EQ(kv->length, 4);
wmmhello's avatar
wmmhello 已提交
341 342
  //ASSERT_EQ(kv->f, 8.23);
  printf("8.23 = kv->f:%f\n", kv->f);
wmmhello's avatar
wmmhello 已提交
343 344 345
  taosMemoryFree(kv);

  // tiny int
wmmhello's avatar
wmmhello 已提交
346
  kv = (SSmlKv *)taosArrayGetP(cols, 6);
wmmhello's avatar
wmmhello 已提交
347 348
  ASSERT_EQ(strncasecmp(kv->key, "ci8", 3), 0);
  ASSERT_EQ(kv->keyLen, 3);
wmmhello's avatar
wmmhello 已提交
349
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_TINYINT);
wmmhello's avatar
wmmhello 已提交
350
  ASSERT_EQ(kv->length, 1);
wmmhello's avatar
wmmhello 已提交
351
  ASSERT_EQ(kv->i, -34);
wmmhello's avatar
wmmhello 已提交
352 353 354
  taosMemoryFree(kv);

  // unsigned tiny int
wmmhello's avatar
wmmhello 已提交
355
  kv = (SSmlKv *)taosArrayGetP(cols, 7);
wmmhello's avatar
wmmhello 已提交
356 357 358 359 360 361 362 363
  ASSERT_EQ(strncasecmp(kv->key, "cu8", 3), 0);
  ASSERT_EQ(kv->keyLen, 3);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_UTINYINT);
  ASSERT_EQ(kv->length, 1);
  ASSERT_EQ(kv->u, 89);
  taosMemoryFree(kv);

  // small int
wmmhello's avatar
wmmhello 已提交
364
  kv = (SSmlKv *)taosArrayGetP(cols, 8);
wmmhello's avatar
wmmhello 已提交
365 366 367 368 369 370 371 372
  ASSERT_EQ(strncasecmp(kv->key, "ci16", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_SMALLINT);
  ASSERT_EQ(kv->length, 2);
  ASSERT_EQ(kv->u, 233);
  taosMemoryFree(kv);

  // unsigned smallint
wmmhello's avatar
wmmhello 已提交
373
  kv = (SSmlKv *)taosArrayGetP(cols, 9);
wmmhello's avatar
wmmhello 已提交
374 375 376 377 378 379 380 381
  ASSERT_EQ(strncasecmp(kv->key, "cu16", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_USMALLINT);
  ASSERT_EQ(kv->length, 2);
  ASSERT_EQ(kv->u, 898);
  taosMemoryFree(kv);

  // int
wmmhello's avatar
wmmhello 已提交
382
  kv = (SSmlKv *)taosArrayGetP(cols, 10);
wmmhello's avatar
wmmhello 已提交
383 384 385 386 387 388 389 390
  ASSERT_EQ(strncasecmp(kv->key, "ci32", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_INT);
  ASSERT_EQ(kv->length, 4);
  ASSERT_EQ(kv->u, 98289);
  taosMemoryFree(kv);

  // unsigned int
wmmhello's avatar
wmmhello 已提交
391
  kv = (SSmlKv *)taosArrayGetP(cols, 11);
wmmhello's avatar
wmmhello 已提交
392 393 394 395 396 397 398 399 400
  ASSERT_EQ(strncasecmp(kv->key, "cu32", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_UINT);
  ASSERT_EQ(kv->length, 4);
  ASSERT_EQ(kv->u, 12323);
  taosMemoryFree(kv);


  // bigint
wmmhello's avatar
wmmhello 已提交
401
  kv = (SSmlKv *)taosArrayGetP(cols, 12);
wmmhello's avatar
wmmhello 已提交
402
  ASSERT_EQ(strncasecmp(kv->key, "ci64", 4), 0);
wmmhello's avatar
wmmhello 已提交
403 404
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BIGINT);
wmmhello's avatar
wmmhello 已提交
405 406 407 408 409
  ASSERT_EQ(kv->length, 8);
  ASSERT_EQ(kv->i, -89238);
  taosMemoryFree(kv);

  // bigint
wmmhello's avatar
wmmhello 已提交
410
  kv = (SSmlKv *)taosArrayGetP(cols, 13);
wmmhello's avatar
wmmhello 已提交
411 412 413 414 415 416 417 418
  ASSERT_EQ(strncasecmp(kv->key, "ci", 2), 0);
  ASSERT_EQ(kv->keyLen, 2);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BIGINT);
  ASSERT_EQ(kv->length, 8);
  ASSERT_EQ(kv->i, 989);
  taosMemoryFree(kv);

  // unsigned bigint
wmmhello's avatar
wmmhello 已提交
419
  kv = (SSmlKv *)taosArrayGetP(cols, 14);
wmmhello's avatar
wmmhello 已提交
420 421 422 423 424 425 426 427
  ASSERT_EQ(strncasecmp(kv->key, "cu64", 4), 0);
  ASSERT_EQ(kv->keyLen, 4);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_UBIGINT);
  ASSERT_EQ(kv->length, 8);
  ASSERT_EQ(kv->u, 8989323);
  taosMemoryFree(kv);

  // bool
wmmhello's avatar
wmmhello 已提交
428
  kv = (SSmlKv *)taosArrayGetP(cols, 15);
wmmhello's avatar
wmmhello 已提交
429 430 431 432 433 434 435 436 437
  ASSERT_EQ(strncasecmp(kv->key, "cbooltrue", 9), 0);
  ASSERT_EQ(kv->keyLen, 9);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BOOL);
  ASSERT_EQ(kv->length, 1);
  ASSERT_EQ(kv->i, true);
  taosMemoryFree(kv);


  // bool
wmmhello's avatar
wmmhello 已提交
438
  kv = (SSmlKv *)taosArrayGetP(cols, 16);
wmmhello's avatar
wmmhello 已提交
439
  ASSERT_EQ(strncasecmp(kv->key, "cboolt", 6), 0);
wmmhello's avatar
wmmhello 已提交
440 441 442 443
  ASSERT_EQ(kv->keyLen, 6);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BOOL);
  ASSERT_EQ(kv->length, 1);
  ASSERT_EQ(kv->i, true);
wmmhello's avatar
wmmhello 已提交
444 445 446
  taosMemoryFree(kv);

  // bool
wmmhello's avatar
wmmhello 已提交
447
  kv = (SSmlKv *)taosArrayGetP(cols, 17);
wmmhello's avatar
wmmhello 已提交
448 449 450
  ASSERT_EQ(strncasecmp(kv->key, "cboolf", 6), 0);
  ASSERT_EQ(kv->keyLen, 6);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_BOOL);
wmmhello's avatar
wmmhello 已提交
451 452
  ASSERT_EQ(kv->length, 1);
  ASSERT_EQ(kv->i, false);
wmmhello's avatar
wmmhello 已提交
453 454
  taosMemoryFree(kv);

wmmhello's avatar
wmmhello 已提交
455
  // nchar
wmmhello's avatar
wmmhello 已提交
456
  kv = (SSmlKv *)taosArrayGetP(cols, 18);
wmmhello's avatar
wmmhello 已提交
457 458 459 460 461 462 463 464
  ASSERT_EQ(strncasecmp(kv->key, "cnch_", 5), 0);
  ASSERT_EQ(kv->keyLen, 5);
  ASSERT_EQ(kv->type, TSDB_DATA_TYPE_NCHAR);
  ASSERT_EQ(kv->length, 4);
  ASSERT_EQ(strncasecmp(kv->value, "iuwq", 4), 0);
  taosMemoryFree(kv);

  taosArrayDestroy(cols);
465
  taosHashCleanup(dumplicateKey);
wmmhello's avatar
wmmhello 已提交
466 467
}

468
TEST(testCase, smlProcess_influx_Test) {
wmmhello's avatar
wmmhello 已提交
469
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
wmmhello's avatar
wmmhello 已提交
470
  ASSERT_NE(taos, nullptr);
wmmhello's avatar
wmmhello 已提交
471

wmmhello's avatar
wmmhello 已提交
472 473 474 475 476 477
  TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use sml_db");
  taos_free_result(pRes);

wmmhello's avatar
wmmhello 已提交
478 479
  SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);
wmmhello's avatar
wmmhello 已提交
480 481

  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true);
wmmhello's avatar
wmmhello 已提交
482
  ASSERT_NE(info, nullptr);
wmmhello's avatar
wmmhello 已提交
483

484
  const char *sql[11] = {
485 486 487 488 489
    "readings,name=truck_0,fleet=South,driver=Trish,model=H-2,device_version=v2.3 load_capacity=1500,fuel_capacity=150,nominal_fuel_consumption=12,latitude=52.31854,longitude=4.72037,elevation=124,velocity=0,heading=221,grade=0 1451606400000000000",
    "readings,name=truck_0,fleet=South,driver=Trish,model=H-2,device_version=v2.3 load_capacity=1500,fuel_capacity=150,nominal_fuel_consumption=12,latitude=52.31854,longitude=4.72037,elevation=124,velocity=0,heading=221,grade=0,fuel_consumption=25 1451607400000000000",
    "readings,name=truck_0,fleet=South,driver=Trish,model=H-2,device_version=v2.3 load_capacity=1500,fuel_capacity=150,nominal_fuel_consumption=12,latitude=52.31854,longitude=4.72037,elevation=124,heading=221,grade=0,fuel_consumption=25 1451608400000000000",
    "readings,name=truck_0,fleet=South,driver=Trish,model=H-2,device_version=v2.3 fuel_capacity=150,nominal_fuel_consumption=12,latitude=52.31854,longitude=4.72037,elevation=124,velocity=0,heading=221,grade=0,fuel_consumption=25 1451609400000000000",
    "readings,name=truck_0,fleet=South,driver=Trish,model=H-2,device_version=v2.3 fuel_consumption=25,grade=0 1451619400000000000",
wmmhello's avatar
wmmhello 已提交
490
    "readings,name=truck_1,fleet=South,driver=Albert,model=F-150,device_version=v1.5 load_capacity=2000,fuel_capacity=200,nominal_fuel_consumption=15,latitude=72.45258,longitude=68.83761,elevation=255,velocity=0,heading=181,grade=0,fuel_consumption=25 1451606400000000000",
491 492
    "readings,name=truck_2,driver=Derek,model=F-150,device_version=v1.5 load_capacity=2000,fuel_capacity=200,nominal_fuel_consumption=15,latitude=24.5208,longitude=28.09377,elevation=428,velocity=0,heading=304,grade=0,fuel_consumption=25 1451606400000000000",
    "readings,name=truck_2,fleet=North,driver=Derek,model=F-150 load_capacity=2000,fuel_capacity=200,nominal_fuel_consumption=15,latitude=24.5208,longitude=28.09377,elevation=428,velocity=0,heading=304,grade=0,fuel_consumption=25 1451609400000000000",
493 494 495
    "readings,fleet=South,name=truck_0,driver=Trish,model=H-2,device_version=v2.3 fuel_consumption=25,grade=0 1451629400000000000",
    "stable,t1=t1,t2=t2,t3=t3 c1=1,c2=2,c3=3,c4=4 1451629500000000000",
    "stable,t2=t2,t1=t1,t3=t3 c1=1,c3=3,c4=4 1451629600000000000"
wmmhello's avatar
wmmhello 已提交
496
  };
497
  smlProcess(info, (char**)sql, sizeof(sql)/sizeof(sql[0]));
498

499
  TAOS_RES *res = taos_query(taos, "select * from t_6885c584b98481584ee13dac399e173d");
500 501 502 503 504 505 506 507
  ASSERT_NE(res, nullptr);
  int fieldNum = taos_field_count(res);
  ASSERT_EQ(fieldNum, 11);
  int rowNum = taos_affected_rows(res);
  ASSERT_EQ(rowNum, 2);
  for (int i = 0; i < rowNum; ++i) {
    TAOS_ROW rows = taos_fetch_row(res);
  }
508 509
  taos_free_result(res);
  smlDestroyInfo(info);
wmmhello's avatar
wmmhello 已提交
510
}
511

512
// different types
513 514
TEST(testCase, smlParseLine_error_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
wmmhello's avatar
wmmhello 已提交
515
  ASSERT_NE(taos, nullptr);
516 517 518 519 520 521 522

  TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use sml_db");
  taos_free_result(pRes);

wmmhello's avatar
wmmhello 已提交
523 524
  SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);
525 526

  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true);
wmmhello's avatar
wmmhello 已提交
527
  ASSERT_NE(info, nullptr);
528 529 530 531 532

  const char *sql[2] = {
      "measure,t1=3 c1=8",
      "measure,t2=3 c1=8u8"
  };
533
  int ret = smlProcess(info, (char **)sql, sizeof(sql)/sizeof(sql[0]));
534
  ASSERT_NE(ret, 0);
535 536
  destroyRequest(request);
  smlDestroyInfo(info);
537 538
}

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
TEST(testCase, smlGetTimestampLen_Test) {
  uint8_t len = smlGetTimestampLen(0);
  ASSERT_EQ(len, 1);

  len = smlGetTimestampLen(1);
  ASSERT_EQ(len, 1);

  len = smlGetTimestampLen(10);
  ASSERT_EQ(len, 2);

  len = smlGetTimestampLen(390);
  ASSERT_EQ(len, 3);

  len = smlGetTimestampLen(-1);
  ASSERT_EQ(len, 1);

  len = smlGetTimestampLen(-10);
  ASSERT_EQ(len, 2);

  len = smlGetTimestampLen(-390);
  ASSERT_EQ(len, 3);
}

TEST(testCase, smlProcess_telnet_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

  TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use sml_db");
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true);
  ASSERT_NE(info, nullptr);

  const char *sql[4] = {
     "sys.if.bytes.out 1479496100 1.3E0 host=web01 interface=eth0",
     "sys.if.bytes.out 1479496101 1.3E1 interface=eth0 host=web01 ",
     "sys.if.bytes.out 1479496102 1.3E3 network=tcp",
     "sys.procs.running 1479496100 42 host=web01"
  };
  int ret = smlProcess(info, (char**)sql, sizeof(sql)/sizeof(sql[0]));
  ASSERT_EQ(ret, 0);

  TAOS_RES *res = taos_query(taos, "select * from t_8c30283b3c4131a071d1e16cf6d7094a");
  ASSERT_NE(res, nullptr);
  int fieldNum = taos_field_count(res);
  ASSERT_EQ(fieldNum, 2);
  int rowNum = taos_affected_rows(res);
  ASSERT_EQ(rowNum, 1);
  for (int i = 0; i < rowNum; ++i) {
   TAOS_ROW rows = taos_fetch_row(res);
  }
  taos_free_result(pRes);

  res = taos_query(taos, "select * from t_6931529054e5637ca92c78a1ad441961");
  ASSERT_NE(res, nullptr);
  fieldNum = taos_field_count(res);
  ASSERT_EQ(fieldNum, 2);
  rowNum = taos_affected_rows(res);
  ASSERT_EQ(rowNum, 2);
  for (int i = 0; i < rowNum; ++i) {
   TAOS_ROW rows = taos_fetch_row(res);
  }
  taos_free_result(pRes);
  smlDestroyInfo(info);
}

TEST(testCase, smlProcess_json1_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

  TAOS_RES *pRes = taos_query(taos, "create database if not exists sml_db");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use sml_db");
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj *)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true);
  ASSERT_NE(info, nullptr);

  const char *sql =
     "[\n"
     "    {\n"
     "        \"metric\": \"sys.cpu.nice\",\n"
     "        \"timestamp\": 1346846400,\n"
     "        \"value\": 18,\n"
     "        \"tags\": {\n"
     "           \"host\": \"web01\",\n"
     "           \"dc\": \"lga\"\n"
     "        }\n"
     "    },\n"
     "    {\n"
     "        \"metric\": \"sys.cpu.nice\",\n"
     "        \"timestamp\": 1346846400,\n"
     "        \"value\": 9,\n"
     "        \"tags\": {\n"
     "           \"host\": \"web02\",\n"
     "           \"dc\": \"lga\"\n"
     "        }\n"
     "    }\n"
     "]";
  int ret = smlProcess(info, (char **)(&sql), -1);
  ASSERT_EQ(ret, 0);

  TAOS_RES *res = taos_query(taos, "select * from t_cb27a7198d637b4f1c6464bd73f756a7");
  ASSERT_NE(res, nullptr);
  int fieldNum = taos_field_count(res);
  ASSERT_EQ(fieldNum, 2);
  //   int rowNum = taos_affected_rows(res);
  //   ASSERT_EQ(rowNum, 1);
  //   for (int i = 0; i < rowNum; ++i) {
  //     TAOS_ROW rows = taos_fetch_row(res);
  //   }
  taos_free_result(pRes);
  smlDestroyInfo(info);
}

TEST(testCase, smlProcess_json2_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

  TAOS_RES *pRes = taos_query(taos, "create database if not exists sml_db");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use sml_db");
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj *)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true);
  ASSERT_NE(info, nullptr);
  const char *sql =
     "{\n"
     "    \"metric\": \"meter_current\",\n"
     "    \"timestamp\": {\n"
     "        \"value\"  : 1346846400,\n"
     "        \"type\"   : \"s\"\n"
     "    },\n"
     "    \"value\": {\n"
     "         \"value\" : 10.3,\n"
     "         \"type\"  : \"i64\"\n"
     "    },\n"
     "    \"tags\": {\n"
     "       \"groupid\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"bigint\"\n"
     "       },\n"
     "       \"location\": { \n"
     "           \"value\" : \"北京\",\n"
     "           \"type\"  : \"binary\"\n"
     "       },\n"
     "       \"id\": \"d1001\"\n"
     "    }\n"
     "}";
  int32_t ret = smlProcess(info, (char **)(&sql), -1);
  ASSERT_EQ(ret, 0);
  taos_free_result(pRes);
  smlDestroyInfo(info);
}

TEST(testCase, smlProcess_json3_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

  TAOS_RES *pRes = taos_query(taos, "create database if not exists sml_db");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use sml_db");
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj *)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true);
  ASSERT_NE(info, nullptr);
  const char *sql =
     "{\n"
     "    \"metric\": \"meter_current\",\n"
     "    \"timestamp\": {\n"
     "        \"value\"  : 1346846400,\n"
     "        \"type\"   : \"s\"\n"
     "    },\n"
     "    \"value\": {\n"
     "         \"value\" : 10.3,\n"
     "         \"type\"  : \"i64\"\n"
     "    },\n"
     "    \"tags\": {\n"
     "       \"t1\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"bigint\"\n"
     "       },\n"
     "       \"t2\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"int\"\n"
     "       },\n"
     "       \"t3\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"i16\"\n"
     "       },\n"
     "       \"t4\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"i8\"\n"
     "       },\n"
     "       \"t5\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"f32\"\n"
     "       },\n"
     "       \"t6\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"double\"\n"
     "       },\n"
     "       \"t7\": { \n"
     "           \"value\" : \"8323\",\n"
     "           \"type\"  : \"binary\"\n"
     "       },\n"
     "       \"t8\": { \n"
     "           \"value\" : \"北京\",\n"
     "           \"type\"  : \"nchar\"\n"
     "       },\n"
     "       \"t9\": { \n"
     "           \"value\" : true,\n"
     "           \"type\"  : \"bool\"\n"
     "       },\n"
     "       \"id\": \"d1001\"\n"
     "    }\n"
     "}";
  int32_t ret = smlProcess(info, (char **)(&sql), -1);
  ASSERT_EQ(ret, 0);
  taos_free_result(pRes);
  smlDestroyInfo(info);
}

TEST(testCase, smlProcess_json4_Test) {
  TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
  ASSERT_NE(taos, nullptr);

  TAOS_RES* pRes = taos_query(taos, "create database if not exists sml_db");
  taos_free_result(pRes);

  pRes = taos_query(taos, "use sml_db");
  taos_free_result(pRes);

  SRequestObj *request = (SRequestObj *)createRequest((STscObj*)taos, NULL, NULL, TSDB_SQL_INSERT);
  ASSERT_NE(request, nullptr);

  SSmlHandle *info = smlBuildSmlInfo(taos, request, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NANO_SECONDS, true);
  ASSERT_NE(info, nullptr);
  const char *sql = "{\n"
     "    \"metric\": \"meter_current\",\n"
     "    \"timestamp\": {\n"
     "        \"value\"  : 1346846400000,\n"
     "        \"type\"   : \"ms\"\n"
     "    },\n"
     "    \"value\": \"ni\",\n"
     "    \"tags\": {\n"
     "       \"t1\": { \n"
     "           \"value\" : 20,\n"
     "           \"type\"  : \"i64\"\n"
     "       },\n"
     "       \"t2\": { \n"
     "           \"value\" : 25,\n"
     "           \"type\"  : \"i32\"\n"
     "       },\n"
     "       \"t3\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"smallint\"\n"
     "       },\n"
     "       \"t4\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"tinyint\"\n"
     "       },\n"
     "       \"t5\": { \n"
     "           \"value\" : 2,\n"
     "           \"type\"  : \"float\"\n"
     "       },\n"
     "       \"t6\": { \n"
     "           \"value\" : 0.2,\n"
     "           \"type\"  : \"f64\"\n"
     "       },\n"
     "       \"t7\": \"nsj\",\n"
     "       \"t8\": { \n"
     "           \"value\" : \"北京\",\n"
     "           \"type\"  : \"nchar\"\n"
     "       },\n"
     "       \"t9\": false,\n"
     "       \"id\": \"d1001\"\n"
     "    }\n"
     "}";
  int32_t ret = smlProcess(info, (char**)(&sql), -1);
  ASSERT_EQ(ret, 0);
  taos_free_result(pRes);
  smlDestroyInfo(info);
}