cliTest.cpp 7.1 KB
Newer Older
H
Haojun Liao 已提交
1 2
#include <gtest/gtest.h>
#include <iostream>
J
Jun Li 已提交
3
#include <inttypes.h>
H
Haojun Liao 已提交
4 5

#include "taos.h"
H
Haojun Liao 已提交
6
#include "tglobal.h"
H
Haojun Liao 已提交
7 8 9

namespace {
static int64_t start_ts = 1433955661000;
H
Haojun Liao 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

void stmtInsertTest() {
  TAOS* conn = taos_connect("ubuntu", "root", "taosdata", 0, 0);
  if (conn == NULL) {
    printf("Failed to connect to DB, reason:%s", taos_errstr(conn));
    exit(-1);
  }

  TAOS_RES* res = taos_query(conn, "use test");
  taos_free_result(res);

  const char* sql = "insert into t1 values(?, ?, ?, ?)";
  TAOS_STMT* stmt = taos_stmt_init(conn);

  int32_t ret = taos_stmt_prepare(stmt, sql, 0);
  ASSERT_EQ(ret, 0);

  //ts timestamp, k int, a binary(11), b nchar(4)
  struct {
    int64_t ts;
    int k;
    char* a;
    char* b;
  } v = {0};

  TAOS_BIND params[4];
  params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
  params[0].buffer_length = sizeof(v.ts);
  params[0].buffer = &v.ts;
  params[0].length = &params[0].buffer_length;
  params[0].is_null = NULL;

  params[1].buffer_type = TSDB_DATA_TYPE_INT;
  params[1].buffer_length = sizeof(v.k);
  params[1].buffer = &v.k;
  params[1].length = &params[1].buffer_length;
  params[1].is_null = NULL;

  params[2].buffer_type = TSDB_DATA_TYPE_BINARY;
  params[2].buffer_length = sizeof(v.a);
  params[2].buffer = &v.a;
  params[2].is_null = NULL;

  params[3].buffer_type = TSDB_DATA_TYPE_NCHAR;
  params[3].buffer_length = sizeof(v.b);
  params[3].buffer = &v.b;
  params[3].is_null = NULL;

  v.ts = start_ts + 20;
  v.k = 123;

H
Haojun Liao 已提交
61
  char str[] = "abc";
H
Haojun Liao 已提交
62 63 64 65 66 67 68
  uintptr_t len = strlen(str);

  v.a = str;
  params[2].length = &len;
  params[2].buffer_length = len;
  params[2].buffer = str;

H
Haojun Liao 已提交
69
  char nstr[] = "999";
H
Haojun Liao 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  uintptr_t len1 = strlen(nstr);

  v.b = nstr;
  params[3].buffer_length = len1;
  params[3].buffer = nstr;
  params[3].length = &len1;

  taos_stmt_bind_param(stmt, params);
  taos_stmt_add_batch(stmt);

  if (taos_stmt_execute(stmt) != 0) {
    printf("\033[31mfailed to execute insert statement.\033[0m\n");
    return;
  }

  v.ts = start_ts + 30;
  v.k = 911;

H
Haojun Liao 已提交
88 89
  char str1[] = "92";
  len = strlen(str1);
H
Haojun Liao 已提交
90 91 92

  params[2].length = &len;
  params[2].buffer_length = len;
H
Haojun Liao 已提交
93
  params[2].buffer = str1;
H
Haojun Liao 已提交
94

H
Haojun Liao 已提交
95 96
  char nstr1[] = "1920";
  len1 = strlen(nstr1);
H
Haojun Liao 已提交
97 98

  params[3].buffer_length = len1;
H
Haojun Liao 已提交
99
  params[3].buffer = nstr1;
H
Haojun Liao 已提交
100 101 102 103 104 105 106
  params[3].length = &len1;

  taos_stmt_bind_param(stmt, params);
  taos_stmt_add_batch(stmt);

  ret = taos_stmt_execute(stmt);
  if (ret != 0) {
H
Haojun Liao 已提交
107
    printf("%d\n", ret);
H
Haojun Liao 已提交
108 109 110 111 112 113
    printf("\033[31mfailed to execute insert statement.\033[0m\n");
    return;
  }

  taos_stmt_close(stmt);
  taos_close(conn);
H
Haojun Liao 已提交
114 115
}

H
Haojun Liao 已提交
116
void validateResultFields() {
H
Haojun Liao 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  TAOS* conn = taos_connect("ubuntu", "root", "taosdata", 0, 0);
  if (conn == NULL) {
    printf("Failed to connect to DB, reason:%s", taos_errstr(conn));
    exit(-1);
  }

  TAOS_RES* res = taos_query(conn, "create database if not exists test");
  ASSERT_EQ(taos_errno(res), 0);
  taos_free_result(res);

  res = taos_query(conn, "use test");
  ASSERT_EQ(taos_errno(res), 0);
  taos_free_result(res);

  res = taos_query(conn, "create table if not exists t1(ts timestamp, k int, a binary(11), b nchar(4))");
  ASSERT_EQ(taos_errno(res), 0);
  taos_free_result(res);

  char sql[512] = {0};
J
Jun Li 已提交
136
  sprintf(sql, "insert into t1 values(%" PRId64 ", 99, 'abc', 'test')", start_ts);
H
Haojun Liao 已提交
137 138 139 140 141 142 143 144 145 146 147 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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

  res = taos_query(conn, sql);
  ASSERT_EQ(taos_errno(res), 0);
  taos_free_result(res);

  res = taos_query(conn, "select count(*), spread(ts)/(1000 * 3600 * 24), first(a), last(b) from t1");
  ASSERT_EQ(taos_num_fields(res), 4);

  TAOS_FIELD* fields = taos_fetch_fields(res);
  ASSERT_EQ(fields[0].bytes, 8);
  ASSERT_EQ(fields[0].type, TSDB_DATA_TYPE_BIGINT);
  ASSERT_STREQ(fields[0].name, "count(*)");

  ASSERT_EQ(fields[1].bytes, 8);
  ASSERT_EQ(fields[1].type, TSDB_DATA_TYPE_DOUBLE);
  ASSERT_STREQ(fields[1].name, "spread(ts)/(1000 * 3600 * 24)");

  ASSERT_EQ(fields[2].bytes, 11);
  ASSERT_EQ(fields[2].type, TSDB_DATA_TYPE_BINARY);
  ASSERT_STREQ(fields[2].name, "first(a)");

  ASSERT_EQ(fields[3].bytes, 4);
  ASSERT_EQ(fields[3].type, TSDB_DATA_TYPE_NCHAR);
  ASSERT_STREQ(fields[3].name, "last(b)");

  taos_free_result(res);

  res = taos_query(conn, "select last_row(*) from t1");
  ASSERT_EQ(taos_num_fields(res), 4);

  fields = taos_fetch_fields(res);
  ASSERT_EQ(fields[0].bytes, 8);
  ASSERT_EQ(fields[0].type, TSDB_DATA_TYPE_TIMESTAMP);
  ASSERT_STREQ(fields[0].name, "last_row(ts)");

  ASSERT_EQ(fields[1].bytes, 4);
  ASSERT_EQ(fields[1].type, TSDB_DATA_TYPE_INT);
  ASSERT_STREQ(fields[1].name, "last_row(k)");

  ASSERT_EQ(fields[2].bytes, 11);
  ASSERT_EQ(fields[2].type, TSDB_DATA_TYPE_BINARY);
  ASSERT_STREQ(fields[2].name, "last_row(a)");

  ASSERT_EQ(fields[3].bytes, 4);
  ASSERT_EQ(fields[3].type, TSDB_DATA_TYPE_NCHAR);
  ASSERT_STREQ(fields[3].name, "last_row(b)");

  taos_free_result(res);
  res = taos_query(conn, "select first(*), last(*) from t1");
  ASSERT_EQ(taos_num_fields(res), 8);

  fields = taos_fetch_fields(res);
  ASSERT_EQ(fields[0].bytes, 8);
  ASSERT_EQ(fields[0].type, TSDB_DATA_TYPE_TIMESTAMP);
  ASSERT_STREQ(fields[0].name, "first(ts)");

  ASSERT_EQ(fields[1].bytes, 4);
  ASSERT_EQ(fields[1].type, TSDB_DATA_TYPE_INT);
  ASSERT_STREQ(fields[1].name, "first(k)");

  ASSERT_EQ(fields[2].bytes, 11);
  ASSERT_EQ(fields[2].type, TSDB_DATA_TYPE_BINARY);
  ASSERT_STREQ(fields[2].name, "first(a)");

  ASSERT_EQ(fields[3].bytes, 4);
  ASSERT_EQ(fields[3].type, TSDB_DATA_TYPE_NCHAR);
  ASSERT_STREQ(fields[3].name, "first(b)");

  taos_free_result(res);

  res = taos_query(conn, "select first(ts, a, k, k, b, b, ts) from t1");
  ASSERT_EQ(taos_num_fields(res), 7);

  fields = taos_fetch_fields(res);
  ASSERT_EQ(fields[0].bytes, 8);
  ASSERT_EQ(fields[0].type, TSDB_DATA_TYPE_TIMESTAMP);
  ASSERT_STREQ(fields[0].name, "first(ts)");

  ASSERT_EQ(fields[1].bytes, 11);
  ASSERT_EQ(fields[1].type, TSDB_DATA_TYPE_BINARY);
  ASSERT_STREQ(fields[1].name, "first(a)");

  ASSERT_EQ(fields[2].bytes, 4);
  ASSERT_EQ(fields[2].type, TSDB_DATA_TYPE_INT);
  ASSERT_STREQ(fields[2].name, "first(k)");

  ASSERT_EQ(fields[3].bytes, 4);
  ASSERT_EQ(fields[3].type, TSDB_DATA_TYPE_INT);
  ASSERT_STREQ(fields[3].name, "first(k)");

  ASSERT_EQ(fields[4].bytes, 4);
  ASSERT_EQ(fields[4].type, TSDB_DATA_TYPE_NCHAR);
  ASSERT_STREQ(fields[4].name, "first(b)");

  ASSERT_EQ(fields[5].bytes, 4);
  ASSERT_EQ(fields[5].type, TSDB_DATA_TYPE_NCHAR);
  ASSERT_STREQ(fields[5].name, "first(b)");

  ASSERT_EQ(fields[6].bytes, 8);
  ASSERT_EQ(fields[6].type, TSDB_DATA_TYPE_TIMESTAMP);
  ASSERT_STREQ(fields[6].name, "first(ts)");

  taos_free_result(res);
H
Haojun Liao 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

  // update the configure parameter, the result field name will be changed
  tsKeepOriginalColumnName = 1;
  res = taos_query(conn, "select first(ts, a, k, k, b, b, ts) from t1");
  ASSERT_EQ(taos_num_fields(res), 7);

  fields = taos_fetch_fields(res);
  ASSERT_EQ(fields[0].bytes, 8);
  ASSERT_EQ(fields[0].type, TSDB_DATA_TYPE_TIMESTAMP);
  ASSERT_STREQ(fields[0].name, "ts");

  ASSERT_EQ(fields[2].bytes, 4);
  ASSERT_EQ(fields[2].type, TSDB_DATA_TYPE_INT);
  ASSERT_STREQ(fields[2].name, "k");

  taos_free_result(res);

H
Haojun Liao 已提交
257 258
  taos_close(conn);
}
H
Haojun Liao 已提交
259 260 261 262 263 264 265 266 267
}
/* test parse time function */
TEST(testCase, result_field_test) {
  taos_options(TSDB_OPTION_CONFIGDIR, "~/first/cfg");
  taos_init();

  validateResultFields();
  stmtInsertTest();
}