feat: add unit test to shell

This commit is contained in:
Alex Duan 2024-10-27 18:04:21 +08:00
parent 4df8aeded9
commit 651a652376
5 changed files with 189 additions and 2 deletions

View File

@ -14,14 +14,17 @@ IF(TD_LINUX AND TD_WEBSOCKET)
ADD_DEFINITIONS(-DWEBSOCKET -I${CMAKE_BINARY_DIR}/build/include -ltaosws)
SET(LINK_WEBSOCKET "-L${CMAKE_BINARY_DIR}/build/lib -ltaosws")
ADD_DEPENDENCIES(shell taosws-rs)
ADD_DEPENDENCIES(shell_ut taosws-rs)
ELSEIF(TD_DARWIN AND TD_WEBSOCKET)
ADD_DEFINITIONS(-DWEBSOCKET -I${CMAKE_BINARY_DIR}/build/include)
SET(LINK_WEBSOCKET "${CMAKE_BINARY_DIR}/build/lib/libtaosws.dylib")
ADD_DEPENDENCIES(shell taosws-rs)
ADD_DEPENDENCIES(shell_ut taosws-rs)
ELSEIF(TD_WINDOWS AND TD_WEBSOCKET)
ADD_DEFINITIONS(-DWEBSOCKET -I${CMAKE_BINARY_DIR}/build/include)
SET(LINK_WEBSOCKET "${CMAKE_BINARY_DIR}/build/lib/taosws.lib")
ADD_DEPENDENCIES(shell taosws-rs)
ADD_DEPENDENCIES(shell_ut taosws-rs)
ELSE()
SET(LINK_WEBSOCKET "")
ENDIF()
@ -49,3 +52,50 @@ target_include_directories(
)
SET_TARGET_PROPERTIES(shell PROPERTIES OUTPUT_NAME taos)
#
# generator library shell_ut for uint test
#
# include
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc)
LIST(APPEND SHELL_UT_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/shellMain.c)
LIST(APPEND SHELL_UT_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/shellAuto.c)
LIST(APPEND SHELL_UT_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/shellTire.c)
LIST(APPEND SHELL_UT_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/shellArguments.c)
LIST(APPEND SHELL_UT_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/shellCommand.c)
LIST(APPEND SHELL_UT_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/shellEngine.c)
LIST(APPEND SHELL_UT_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/shellUtil.c)
LIST(APPEND SHELL_UT_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/shellNettest.c)
LIST(APPEND SHELL_UT_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/shellWebsocket.c)
# shell_ut library
add_library(shell_ut STATIC ${SHELL_UT_SRC})
# link public
if(TD_WINDOWS)
target_link_libraries(shell_ut PUBLIC taos_static ${LINK_WEBSOCKET})
else()
target_link_libraries(shell_ut PUBLIC taos ${LINK_WEBSOCKET} ${LINK_JEMALLOC} ${LINK_ARGP})
endif()
# link private
target_link_libraries(
shell_ut
PRIVATE os common transport geometry util
)
# util depends
target_link_directories(
shell_ut
PUBLIC "${TD_SOURCE_DIR}/contrib/lzma2"
PUBLIC "${TD_SOURCE_DIR}/contrib/pcre2"
)
# unit test
if(${BUILD_TEST})
ADD_SUBDIRECTORY(test)
endif(${BUILD_TEST})

View File

@ -47,4 +47,12 @@ void showAD(bool end);
// show all commands help
void showHelp();
//
// for unit test
//
bool fieldOptionsArea(char* p);
bool isCreateFieldsArea(char* p);
#endif

View File

@ -22,6 +22,8 @@
#include "shellAuto.h"
#include "shellInt.h"
SShellObj shell = {0};
typedef struct {
const char *sql;
bool vertical;

View File

@ -17,8 +17,7 @@
#include "shellInt.h"
#include "shellAuto.h"
SShellObj shell = {0};
extern SShellObj shell;
void shellCrashHandler(int signum, void *sigInfo, void *context) {
taosIgnSignal(SIGTERM);

View File

@ -0,0 +1,128 @@
/*
* 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 <iostream>
#include "shellAuto.h"
TEST(fieldOptionsArea, autoTabTest) {
printf("hellow world SHELL tab test\n");
// str false
const char *s0 [] = {
"create table st(ts ",
"create table st(ts timestamp, age ",
"create table st(ts timestamp, age",
"create table st(ts timestamp, age int , name ",
"create table st(ts timestamp, age int , name binary(16)",
"create table st(ts timestamp, age int , name binary(16) ) tags( ",
"create table st(ts timestamp, age int , name binary(16) ) tags( area int, addr ",
"create table st(ts timestamp, age int , name binary(16) ) tags( area int,addr varbinary",
"create table st(ts timestamp, age int, name binary(16)) tags(area int , addr varbinary(32)",
"create table st( ts timestamp, age int, name binary(16)) tags( area int, addr",
"create table st (ts timestamp , age int, name binary(16) , area int,",
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int ,addr varbinary",
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) level 'high' , no i",
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) encode 'simple8b' level 'high', no in",
};
// str true
const char *s1 [] = {
"create table st(ts timestamp ",
"create table st(ts timestamp, age int ",
"create table st(ts timestamp, age int ",
"create table st(ts timestamp, age int , name binary(16) ",
"create table st(ts timestamp, age int , name binary(16) ",
"create table st(ts timestamp, age int , name binary(16) , addr varbinary( 32 ) ",
"create table st(ts timestamp, age int , name binary(16) ,area int, addr varbinary(32) ",
"create table st(ts timestamp, age int , name binary(16), area int,addr varbinary(32) ",
"create table st(ts timestamp, age int, name binary(16) , area int,addr varbinary(32) ",
"create table st( ts timestamp, age int, name binary(16) ,area int,addr varbinary(32) ",
"create table st (ts timestamp , age int, name binary(16), area int,addr varbinary(32) ",
"create table st (ts timestamp , age int, name binary(16), area int , addr varbinary(32) compress 'zlib' ",
"create table st (ts timestamp , age int, name binary(16), area int , addr varbinary(32) level 'high' ",
"create table st (ts timestamp , age int, name binary(16) , area int , addr varbinary(32) encode 'simple8b' level 'high' ",
};
// s0 is false
for(int32_t i = 0; i < sizeof(s0)/sizeof(char*) ; i++) {
printf("s0 i=%d fieldOptionsArea %s expect false \n", i, s0[i]);
ASSERT (fieldOptionsArea((char *)s0[i]) == false);
}
// s1 is true
for(int32_t i = 0; i < sizeof(s1)/sizeof(char*) ; i++) {
printf("s1 i=%d fieldOptionsArea %s expect true \n", i, s1[i]);
ASSERT (fieldOptionsArea((char *)s1[i]) == true);
}
}
TEST(isCreateFieldsArea, autoTabTest) {
printf("hellow world SHELL tab test\n");
// str false
const char *s0 [] = {
"create table st(ts )",
"create table st(ts timestamp, age) ",
"create table st(ts timestamp, age)",
"create table st(ts timestamp, age int , name binary(16) )",
"create table st(ts timestamp, age int , name binary(16))",
"create table st(ts timestamp, age int , name binary(16) ) tags( )",
"create table st(ts timestamp, age int , name binary(16) ) tags( area int, addr )",
"create table st(ts timestamp, age int , name binary(16) ) tags( area int,addr varbinary)",
"create table st(ts timestamp, age int, name binary(16)) tags(area int , addr varbinary(32))",
"create table st( ts timestamp, age int, name binary(16)) tags( area int, addr int)",
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int,addr varbinary(32) )",
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int ,addr varbinary(14))",
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) level 'high' )",
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) encode 'simple8b' level 'high' ) ",
};
// str true
const char *s1 [] = {
"create table st(ts timestamp ",
"create table st(ts timestamp, age int ",
"create table st(ts timestamp, age int ,",
"create table st(ts timestamp, age int , name binary(16), ",
"create table st(ts timestamp, age int , name binary(16) ",
"create table st(ts timestamp, age int , name binary(16) ) tags( area int ",
"create table st(ts timestamp, age int , name binary(16) ) tags( area int, addr varbinary(32) ",
"create table st(ts timestamp, age int , name binary(16) ) tags( area int,addr varbinary(32)",
"create table st(ts timestamp, age int, name binary(16)) tags(area int,addr varbinary(32) ",
"create table st( ts timestamp, age int, name binary(16)) tags(area int,addr varbinary(32) ",
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int, addr varbinary(32) ",
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) compress 'zlib' ",
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) level 'high' ",
"create table st (ts timestamp , age int, name binary(16) ) tags ( area int , addr varbinary(32) encode 'simple8b' level 'high' ",
};
// s0 is false
for(int32_t i = 0; i < sizeof(s0)/sizeof(char*) ; i++) {
printf("s0 i=%d isCreateFieldsArea %s expect false. \n", i, s0[i]);
ASSERT (isCreateFieldsArea((char *)s0[i]) == false);
}
// s1 is true
for(int32_t i = 0; i < sizeof(s1)/sizeof(char*) ; i++) {
printf("s1 i=%d isCreateFieldsArea %s expect true. \n", i, s1[i]);
ASSERT (isCreateFieldsArea((char *)s1[i]) == true);
}
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}