recover taoscTest.cpp

This commit is contained in:
xjzhou 2024-07-05 14:23:54 +08:00
parent b8012df909
commit 48560ddf43
1 changed files with 26 additions and 175 deletions

View File

@ -30,31 +30,31 @@
#include "taos.h"
class taoscTest : public ::testing::Test {
protected:
protected:
static void SetUpTestCase() {
// printf("start test setup.\n");
// TAOS* taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
// ASSERT_TRUE(taos != nullptr);
//
// TAOS_RES* res = taos_query(taos, "drop database IF EXISTS taosc_test_db;");
// if (taos_errno(res) != 0) {
// printf("error in drop database taosc_test_db, reason:%s\n", taos_errstr(res));
// return;
// }
// taosSsleep(5);
// taos_free_result(res);
// printf("drop database taosc_test_db,finished.\n");
//
// res = taos_query(taos, "create database taosc_test_db;");
// if (taos_errno(res) != 0) {
// printf("error in create database taosc_test_db, reason:%s\n", taos_errstr(res));
// return;
// }
// taosSsleep(5);
// taos_free_result(res);
// printf("create database taosc_test_db,finished.\n");
//
// taos_close(taos);
printf("start test setup.\n");
TAOS* taos = taos_connect("localhost", "root", "taosdata", NULL, 0);
ASSERT_TRUE(taos != nullptr);
TAOS_RES* res = taos_query(taos, "drop database IF EXISTS taosc_test_db;");
if (taos_errno(res) != 0) {
printf("error in drop database taosc_test_db, reason:%s\n", taos_errstr(res));
return;
}
taosSsleep(5);
taos_free_result(res);
printf("drop database taosc_test_db,finished.\n");
res = taos_query(taos, "create database taosc_test_db;");
if (taos_errno(res) != 0) {
printf("error in create database taosc_test_db, reason:%s\n", taos_errstr(res));
return;
}
taosSsleep(5);
taos_free_result(res);
printf("create database taosc_test_db,finished.\n");
taos_close(taos);
}
static void TearDownTestCase() {}
@ -99,154 +99,6 @@ void queryCallback(void* param, void* res, int32_t code) {
taos_fetch_raw_block_a(res, fetchCallback, param);
}
/**
* @brief execute sql only.
*
* @param taos
* @param sql
*/
void executeSQL(TAOS *taos, const char *sql) {
TAOS_RES *res = taos_query(taos, sql);
int code = taos_errno(res);
if (code != 0) {
printf("%s\n", taos_errstr(res));
taos_free_result(res);
taos_close(taos);
exit(EXIT_FAILURE);
}
taos_free_result(res);
}
/**
* @brief check return status and exit program when error occur.
*
* @param stmt
* @param code
* @param msg
*/
void checkErrorCode(TAOS_STMT *stmt, int code, const char* msg) {
if (code != 0) {
printf("%s. error: %s\n", msg, taos_stmt_errstr(stmt));
taos_stmt_close(stmt);
exit(EXIT_FAILURE);
}
}
typedef struct {
int64_t ts;
float current;
int voltage;
float phase;
} Row;
/**
* @brief insert data using stmt API
*
* @param taos
*/
void insertData(TAOS *taos) {
// init
TAOS_STMT *stmt = taos_stmt_init(taos);
// prepare
// const char *sql = "INSERT INTO ?.d1001 USING meters TAGS(?, ?) values(?, ?, ?, ?)";
// const char *sql = "INSERT INTO ?.? USING meters TAGS(?, ?) values(?, ?, ?, ?)";
// const char *sql = "INSERT INTO power.? USING meters TAGS(?, ?) values(?, ?, ?, ?)";
// const char *sql = "INSERT INTO ? USING meters TAGS(?, ?) values(?, ?, ?, ?)";
// const char *sql = "INSERT INTO ? USING meters TAGS(?, ?) values(?, ?, ?, ?)";
const char *sql = "insert into huawei USING meters TAGS(?, ?) values(?, ?, ?, ?)";
int code = taos_stmt_prepare(stmt, sql, 0);
checkErrorCode(stmt, code, "failed to execute taos_stmt_prepare");
// bind table name and tags
TAOS_MULTI_BIND tags[2];
char *location = "California.SanFrancisco";
int groupId = 2;
tags[0].buffer_type = TSDB_DATA_TYPE_BINARY;
tags[0].buffer_length = strlen(location);
tags[0].length = (int32_t *)&tags[0].buffer_length;
tags[0].buffer = location;
tags[0].is_null = NULL;
tags[1].buffer_type = TSDB_DATA_TYPE_INT;
tags[1].buffer_length = sizeof(int);
tags[1].length = (int32_t *)&tags[1].buffer_length;
tags[1].buffer = &groupId;
tags[1].is_null = NULL;
// code = taos_stmt_set_tbname_tags(stmt, "duck", tags);
// checkErrorCode(stmt, code, "failed to execute taos_stmt_set_dbname_tbname_tags");
// insert two rows with multi binds
TAOS_MULTI_BIND params[4];
// values to bind
int64_t ts[] = {1648432611250, 1648432611778};
float current[] = {10.3, 12.6};
int voltage[] = {219, 218};
float phase[] = {0.31, 0.33};
// is_null array
char is_null[2] = {0};
// length array
int32_t int64Len[2] = {sizeof(int64_t)};
int32_t floatLen[2] = {sizeof(float)};
int32_t intLen[2] = {sizeof(int)};
params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
params[0].buffer_length = sizeof(int64_t);
params[0].buffer = ts;
params[0].length = int64Len;
params[0].is_null = is_null;
params[0].num = 2;
params[1].buffer_type = TSDB_DATA_TYPE_FLOAT;
params[1].buffer_length = sizeof(float);
params[1].buffer = current;
params[1].length = floatLen;
params[1].is_null = is_null;
params[1].num = 2;
params[2].buffer_type = TSDB_DATA_TYPE_INT;
params[2].buffer_length = sizeof(int);
params[2].buffer = voltage;
params[2].length = intLen;
params[2].is_null = is_null;
params[2].num = 2;
params[3].buffer_type = TSDB_DATA_TYPE_FLOAT;
params[3].buffer_length = sizeof(float);
params[3].buffer = phase;
params[3].length = floatLen;
params[3].is_null = is_null;
params[3].num = 2;
code = taos_stmt_bind_param_batch(stmt, params); // bind batch
checkErrorCode(stmt, code, "failed to execute taos_stmt_bind_param_batch");
code = taos_stmt_add_batch(stmt); // add batch
checkErrorCode(stmt, code, "failed to execute taos_stmt_add_batch");
// execute
code = taos_stmt_execute(stmt);
checkErrorCode(stmt, code, "failed to execute taos_stmt_execute");
int affectedRows = taos_stmt_affected_rows(stmt);
printf("successfully inserted %d rows\n", affectedRows);
// close
taos_stmt_close(stmt);
}
TEST_F(taoscTest, taos_stmt_test) {
TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 6030);
if (taos == NULL) {
printf("failed to connect to server");
exit(EXIT_FAILURE);
}
// executeSQL(taos, "drop database if exists power");
// executeSQL(taos, "create database power");
executeSQL(taos, "use power");
// executeSQL(taos, "create stable meters (ts timestamp, current float, voltage int, phase float) tags (location binary(64), groupId int)");
insertData(taos);
taos_close(taos);
taos_cleanup();
}
TEST_F(taoscTest, taos_query_a_test) {
char sql[1024] = {0};
int32_t code = 0;
@ -336,7 +188,7 @@ TEST_F(taoscTest, taos_query_test) {
void queryCallback2(void* param, void* res, int32_t code) {
ASSERT_TRUE(code == 0);
ASSERT_TRUE(param == pUserParam);
// After using taos_query_a to query, using taos_fetch_row in the callback will cause blocking.
// After using taos_query_a to query, using taos_fetch_row in the callback will cause blocking.
// Reason: schProcessOnCbBegin SCH_LOCK_TASK(pTask)
TAOS_ROW row;
row = taos_fetch_row(res);
@ -402,7 +254,7 @@ TEST_F(taoscTest, taos_query_a_fetch_row) {
printf("taos_query_a_fetch_row taos_fetch_row start...\n");
while ((row = taos_fetch_row(*pres))) {
getRecordCounts++;
getRecordCounts++;
}
printf("taos_query_a_fetch_row taos_fetch_row end. %p record count:%d.\n", *pres, getRecordCounts);
taos_free_result(*pres);
@ -412,4 +264,3 @@ TEST_F(taoscTest, taos_query_a_fetch_row) {
printf("taos_query_a_fetch_row test finished.\n");
}