From 522f359c79eff307e6b7feefce093b2d31ad918e Mon Sep 17 00:00:00 2001 From: wenwen luo Date: Tue, 21 May 2024 14:18:23 +0800 Subject: [PATCH] R api 3.0.0.1 release --- DESCRIPTION | 4 +- R/RDolphinDB.R | 1 + R/RDolphinDB_impl.R | 37 ++- R/RcppExports.R | 12 + README_CN.md | 3 +- man/dbConnect.Rd | 2 + src/RcppExports.cpp | 36 +++ src/connector_cppapi.cpp | 56 ++++ src/include/Matrixx.h | 2 +- src/include/R_CPP_Connector.h | 23 ++ src/include/R_Type.h | 2 +- src/include/Utill.h | 6 + src/include/Vectorr.h | 71 ++++- test/Main.R | 16 ++ test/TestClose.R | 39 ++- test/TestConnect.R | 56 ++-- test/TestRpc.R | 62 +++++ test/TestRunMatrix.R | 280 +++++++++++++------ test/TestRunPair.R | 339 ++++++++++++++++------ test/TestRunScalar.R | 454 +++++++++++++++++++++++------- test/TestRunSet.R | 319 ++++++++++++++++----- test/TestRunTable.R | 511 +++++++++++++++++++++++----------- test/TestRunVector.R | 427 ++++++++++++++++++++++------ test/TestUploadMatrix.R | 108 ++++--- test/TestUploadScalar.R | 120 ++++---- test/TestUploadTable.R | 313 +++++++++++++-------- test/TestUploadVector.R | 111 +++++--- test/pub/Test.R | 128 +++++++++ test/setup/Settings.R | 12 +- 29 files changed, 2629 insertions(+), 921 deletions(-) create mode 100644 test/Main.R create mode 100644 test/TestRpc.R create mode 100644 test/pub/Test.R diff --git a/DESCRIPTION b/DESCRIPTION index a2b9163..43452b3 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,8 +1,8 @@ Package: RDolphinDB Type: Package Title: Connecting to DolphinDB server with R -Version: 2.0.11.1 -Date: 2024-03-19 +Version: 3.0.0.1 +Date: 2024-05-17 Author: mrDrivingDuck Maintainer: mrDrivingDuck Description: The R API of DolphinDB. diff --git a/R/RDolphinDB.R b/R/RDolphinDB.R index a6445a6..6460f3e 100644 --- a/R/RDolphinDB.R +++ b/R/RDolphinDB.R @@ -22,6 +22,7 @@ setClass("RDolphinDB", slots = list(connected = "logical")) #' #' @description Method of getting connection with DolphinDB server. #' @description (If the input contains 'username' and 'password', a log-in job will be done) +#' @description Currently, there is at most one TCP connection between the R API and dolphindb. If you call dbConnect multiple times, it closes the previously created connection and creates a new one #' @param conn The connector object. #' @param host The host running the DolphinDB server. #' @param port The port running the DolphinDB server. diff --git a/R/RDolphinDB_impl.R b/R/RDolphinDB_impl.R index 7c31a73..afdbdde 100644 --- a/R/RDolphinDB_impl.R +++ b/R/RDolphinDB_impl.R @@ -231,7 +231,11 @@ DDB_GetEntity <- function(xxdb_type) { } else if (xxdb_type == 12) { # Character Matrix - return (NULL) + result <- ReturnMatrixString() + result <- DDB_SetReceiveVectorNA(result, ReturnMatrixNAIndex()) + result <- DDB_ReceiveMatrixLable(result) + Clear() + return (result) } else if (xxdb_type == 13) { # DataFrame @@ -276,6 +280,10 @@ DDB_GetEntity <- function(xxdb_type) { clm[NAIndex] <- NA result <- cbind(result, clm) + } else if (typelist[i] == 21) { + # factor + clm <- ReturnTableColumnFactor(i) + result <- cbind(result, clm) } else { print("error in DataFrame") Clear() @@ -387,6 +395,10 @@ DDB_GetEntity <- function(xxdb_type) { result <- DDB_SetReceiveVectorNA(result, ReturnVectorNAIndex(i)) result <- result + } else if (anytypelist[i] == 21) { + # Factor + result <- ReturnVectorFactor(i) + } else if (anytypelist[i] == 9) { # Logical Matrix result <- ReturnMatrixBool(i) @@ -462,6 +474,10 @@ DDB_GetEntity <- function(xxdb_type) { clm[NAIndex] <- NA result <- cbind(result, clm) + } else if (typelist[k] == 21) { + # factor + clm <- ReturnTableColumnFactor(k, i) + result <- cbind(result, clm) } else { print("error in DataFrame") Clear() @@ -470,6 +486,10 @@ DDB_GetEntity <- function(xxdb_type) { } result <- result[,-1] + if(class(result)[1] != "data.frame"){ + # if the result only contains one column, then must convert it to dataFrame Explicitly, or it will be a vecor + result = data.frame(result) + } colnames(result) <- ReturnTableColumeName(i) } else { @@ -481,6 +501,12 @@ DDB_GetEntity <- function(xxdb_type) { Clear() return (anyVector) + } else if (xxdb_type == 21) { + # Factor + result <- ReturnVectorFactor() + Clear() + return (result) + } else { # print("Error") Clear() @@ -572,11 +598,15 @@ DDB_UploadVector <- function(vec) { NAIndex <- DDB_SetUploadVectorNA(vec) UploadVectorDouble(vec, NAIndex) - } else if (is.character(vec) || is.factor(vec)) { + } else if (is.character(vec)) { NAIndex <- DDB_SetUploadVectorNA(vec) UploadVectorString(vec, NAIndex) + } else if(is.factor(vec)){ + + UploadVectorSymbol(vec) + } else if (length(class(vec)) > 1 && class(vec)[1] == "POSIXct") { @@ -691,6 +721,7 @@ DDB_UploadObjectCheck <- function(args) { } else if (class(args[[i]])[1] == "POSIXct" && length(args[[i]]) > 1) { + } else if (is.factor(args[[i]])){ } else { print("Data form not support yet.") return (FALSE) @@ -713,6 +744,8 @@ DDB_UploadEntity <- function(args) { DDB_UploadMatrix(args[[i]]) } else if (is.data.frame(args[[i]])) { DDB_UploadTable(args[[i]]) + } else if (is.factor(args[[i]])){ + UploadVectorSymbol(args[[i]]) } else if (is.vector(args[[i]]) && length(args[[i]]) > 1) { DDB_UploadVector(args[[i]]) } else if (is.vector(args[[i]]) && length(args[[i]]) == 1) { diff --git a/R/RcppExports.R b/R/RcppExports.R index 060f02e..7553739 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -69,6 +69,10 @@ UploadMatrixDouble <- function(R_mtx, R_NAIndex) { invisible(.Call('_RDolphinDB_UploadMatrixDouble', PACKAGE = 'RDolphinDB', R_mtx, R_NAIndex)) } +UploadVectorSymbol <- function(R_vec) { + invisible(.Call('_RDolphinDB_UploadVectorSymbol', PACKAGE = 'RDolphinDB', R_vec)) +} + UploadVectorString <- function(R_vec, R_NAIndex) { invisible(.Call('_RDolphinDB_UploadVectorString', PACKAGE = 'RDolphinDB', R_vec, R_NAIndex)) } @@ -153,6 +157,10 @@ ReturnVectorBool <- function(index = -1L) { .Call('_RDolphinDB_ReturnVectorBool', PACKAGE = 'RDolphinDB', index) } +ReturnVectorFactor <- function(index = -1L) { + .Call('_RDolphinDB_ReturnVectorFactor', PACKAGE = 'RDolphinDB', index) +} + ReturnVectorInt <- function(index = -1L) { .Call('_RDolphinDB_ReturnVectorInt', PACKAGE = 'RDolphinDB', index) } @@ -241,6 +249,10 @@ ReturnTableColumnLogical <- function(index, entity_index = -1L) { .Call('_RDolphinDB_ReturnTableColumnLogical', PACKAGE = 'RDolphinDB', index, entity_index) } +ReturnTableColumnFactor <- function(index, entity_index = -1L) { + .Call('_RDolphinDB_ReturnTableColumnFactor', PACKAGE = 'RDolphinDB', index, entity_index) +} + ReturnTableColumnInteger <- function(index, entity_index = -1L) { .Call('_RDolphinDB_ReturnTableColumnInteger', PACKAGE = 'RDolphinDB', index, entity_index) } diff --git a/README_CN.md b/README_CN.md index a108692..50ab711 100644 --- a/README_CN.md +++ b/README_CN.md @@ -108,4 +108,5 @@ help("dbClose") |NANOTIMESTAMP|2012.06.13T13:30:10.008007006|POSIXct|2012-06-13 13:30:10| |FLOAT|2.1f|Numeric|2.1| |DOUBLE|2.1|Numeric|2.1| -|STRING|"123"|Character|"123"| \ No newline at end of file +|STRING|"123"|Character|"123"| +|SYMBOL|"123"|Factor|"123"| \ No newline at end of file diff --git a/man/dbConnect.Rd b/man/dbConnect.Rd index 165b0a5..382c83d 100644 --- a/man/dbConnect.Rd +++ b/man/dbConnect.Rd @@ -24,6 +24,8 @@ Return the connector object with status of connection. Method of getting connection with DolphinDB server. (If the input contains 'username' and 'password', a log-in job will be done) + +Currently, there is at most one TCP connection between the R API and dolphindb. If you call dbConnect multiple times, it closes the previously created connection and creates a new one } \examples{ conn <- dbConnect(DolphinDB(), "localhost", 8848) # Without log-in diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 50265e5..2a1b579 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -189,6 +189,16 @@ BEGIN_RCPP return R_NilValue; END_RCPP } +// UploadVectorSymbol +void UploadVectorSymbol(IntegerVector R_vec); +RcppExport SEXP _RDolphinDB_UploadVectorSymbol(SEXP R_vecSEXP) { +BEGIN_RCPP + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< IntegerVector >::type R_vec(R_vecSEXP); + UploadVectorSymbol(R_vec); + return R_NilValue; +END_RCPP +} // UploadVectorString void UploadVectorString(CharacterVector R_vec, IntegerVector R_NAIndex); RcppExport SEXP _RDolphinDB_UploadVectorString(SEXP R_vecSEXP, SEXP R_NAIndexSEXP) { @@ -409,6 +419,17 @@ BEGIN_RCPP return rcpp_result_gen; END_RCPP } +// ReturnVectorFactor +IntegerVector ReturnVectorFactor(int index); +RcppExport SEXP _RDolphinDB_ReturnVectorFactor(SEXP indexSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< int >::type index(indexSEXP); + rcpp_result_gen = Rcpp::wrap(ReturnVectorFactor(index)); + return rcpp_result_gen; +END_RCPP +} // ReturnVectorInt IntegerVector ReturnVectorInt(int index); RcppExport SEXP _RDolphinDB_ReturnVectorInt(SEXP indexSEXP) { @@ -659,6 +680,18 @@ BEGIN_RCPP return rcpp_result_gen; END_RCPP } +// ReturnTableColumnFactor +IntegerVector ReturnTableColumnFactor(int index, int entity_index); +RcppExport SEXP _RDolphinDB_ReturnTableColumnFactor(SEXP indexSEXP, SEXP entity_indexSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< int >::type index(indexSEXP); + Rcpp::traits::input_parameter< int >::type entity_index(entity_indexSEXP); + rcpp_result_gen = Rcpp::wrap(ReturnTableColumnFactor(index, entity_index)); + return rcpp_result_gen; +END_RCPP +} // ReturnTableColumnInteger IntegerVector ReturnTableColumnInteger(int index, int entity_index); RcppExport SEXP _RDolphinDB_ReturnTableColumnInteger(SEXP indexSEXP, SEXP entity_indexSEXP) { @@ -750,6 +783,7 @@ static const R_CallMethodDef CallEntries[] = { {"_RDolphinDB_UploadMatrixBool", (DL_FUNC) &_RDolphinDB_UploadMatrixBool, 2}, {"_RDolphinDB_UploadMatrixInt", (DL_FUNC) &_RDolphinDB_UploadMatrixInt, 2}, {"_RDolphinDB_UploadMatrixDouble", (DL_FUNC) &_RDolphinDB_UploadMatrixDouble, 2}, + {"_RDolphinDB_UploadVectorSymbol", (DL_FUNC) &_RDolphinDB_UploadVectorSymbol, 1}, {"_RDolphinDB_UploadVectorString", (DL_FUNC) &_RDolphinDB_UploadVectorString, 2}, {"_RDolphinDB_UploadVectorDouble", (DL_FUNC) &_RDolphinDB_UploadVectorDouble, 2}, {"_RDolphinDB_UploadVectorBool", (DL_FUNC) &_RDolphinDB_UploadVectorBool, 2}, @@ -771,6 +805,7 @@ static const R_CallMethodDef CallEntries[] = { {"_RDolphinDB_ReturnScalarTime", (DL_FUNC) &_RDolphinDB_ReturnScalarTime, 1}, {"_RDolphinDB_ReturnScalarDate", (DL_FUNC) &_RDolphinDB_ReturnScalarDate, 1}, {"_RDolphinDB_ReturnVectorBool", (DL_FUNC) &_RDolphinDB_ReturnVectorBool, 1}, + {"_RDolphinDB_ReturnVectorFactor", (DL_FUNC) &_RDolphinDB_ReturnVectorFactor, 1}, {"_RDolphinDB_ReturnVectorInt", (DL_FUNC) &_RDolphinDB_ReturnVectorInt, 1}, {"_RDolphinDB_ReturnVectorDouble", (DL_FUNC) &_RDolphinDB_ReturnVectorDouble, 1}, {"_RDolphinDB_ReturnVectorString", (DL_FUNC) &_RDolphinDB_ReturnVectorString, 1}, @@ -793,6 +828,7 @@ static const R_CallMethodDef CallEntries[] = { {"_RDolphinDB_ReturnTableColumeName", (DL_FUNC) &_RDolphinDB_ReturnTableColumeName, 1}, {"_RDolphinDB_ReturnEmptyDataFrame", (DL_FUNC) &_RDolphinDB_ReturnEmptyDataFrame, 1}, {"_RDolphinDB_ReturnTableColumnLogical", (DL_FUNC) &_RDolphinDB_ReturnTableColumnLogical, 2}, + {"_RDolphinDB_ReturnTableColumnFactor", (DL_FUNC) &_RDolphinDB_ReturnTableColumnFactor, 2}, {"_RDolphinDB_ReturnTableColumnInteger", (DL_FUNC) &_RDolphinDB_ReturnTableColumnInteger, 2}, {"_RDolphinDB_ReturnTableColumnDouble", (DL_FUNC) &_RDolphinDB_ReturnTableColumnDouble, 2}, {"_RDolphinDB_ReturnTableColumnTime", (DL_FUNC) &_RDolphinDB_ReturnTableColumnTime, 2}, diff --git a/src/connector_cppapi.cpp b/src/connector_cppapi.cpp index 1a71c9e..f3620dd 100644 --- a/src/connector_cppapi.cpp +++ b/src/connector_cppapi.cpp @@ -165,6 +165,43 @@ void UploadMatrixDouble(NumericMatrix R_mtx, IntegerVector R_NAIndex) cnt.Rcpp_UploadEntity(mtx, NAIndex, R_mtx.nrow(), R_mtx.ncol()); } +//[[Rcpp::export]] +void UploadVectorSymbol(IntegerVector R_vec) +{ + std::vector symbolBase; + std::vector index; + + CharacterVector level = R_vec.attr("levels"); + LogicalVector isNaVec = is_na(level); + int naIndex = INT_MAX; + + //dolphindb中空值固定在首位,需要找到R中的空值位置并跳过 + symbolBase.push_back(""); + for(int i = 0; i < level.size(); ++i){ + if(isNaVec[i]){ + naIndex = i == INT_MAX ? i : i + 1; + } + else{ + symbolBase.push_back(std::string(level[i])); + } + } + + index.reserve(R_vec.size()); + for(int i = 0; i < R_vec.size(); ++i){ + if(R_vec[i] == naIndex){ + index.push_back(0); + } + else if(R_vec[i] > naIndex){ + index.push_back(R_vec[i] - 1); + } + else{ + index.push_back(R_vec[i]); + } + } + + cnt.Rcpp_UploadSymbolVector(symbolBase, index); +} + //[[Rcpp::export]] void UploadVectorString(CharacterVector R_vec, IntegerVector R_NAIndex) { @@ -347,6 +384,15 @@ LogicalVector ReturnVectorBool(int index = -1) ); } +//[[Rcpp::export]] +IntegerVector ReturnVectorFactor(int index = -1) +{ + void* tmp = cnt.Rcpp_GetEntity(index)-> + getVector()-> + getVector(); + return wrap(*((IntegerVector*)tmp)); +} + //[[Rcpp::export]] IntegerVector ReturnVectorInt(int index = -1) { @@ -721,6 +767,16 @@ LogicalVector ReturnTableColumnLogical(int index, int entity_index = -1) ); } +//[[Rcpp::export]] +IntegerVector ReturnTableColumnFactor(int index, int entity_index = -1) +{ + void* tmp = cnt.Rcpp_GetEntity(entity_index)-> + getTable()-> + getTableClm(index-1)-> + getVector(); + return wrap(*((IntegerVector*)tmp)); +} + //[[Rcpp::export]] IntegerVector ReturnTableColumnInteger(int index, int entity_index = -1) { diff --git a/src/include/Matrixx.h b/src/include/Matrixx.h index bf52d47..97f7104 100644 --- a/src/include/Matrixx.h +++ b/src/include/Matrixx.h @@ -58,7 +58,7 @@ class Matrixx void* getRowLable() {return lable_row->getVector();} void* getClmLable() {return lable_clm->getVector();} void* getMatrix() { - if(mtx->isDate()) { + if(mtx->isDate() || mtx->isString()) { return mtx->getStringVector(); } return mtx->getVector(); diff --git a/src/include/R_CPP_Connector.h b/src/include/R_CPP_Connector.h index 92e4cf4..9396025 100644 --- a/src/include/R_CPP_Connector.h +++ b/src/include/R_CPP_Connector.h @@ -120,6 +120,7 @@ class Rcpp_Connector void Rcpp_UploadEntity(vector & mtx, vector & NAIndex, int row, int clm); void Rcpp_UploadEntity(vector & mtx, vector & NAIndex, int row, int clm); void Rcpp_UploadEntity(vector & mtx, vector & NAIndex, int row, int clm); + void Rcpp_UploadSymbolVector(const std::vector& symbolBase, const std::vector& index); }; void Rcpp_Connector::Rcpp_UploadDateVector(vector & vec, vector & NAIndex) @@ -340,6 +341,28 @@ void Rcpp_Connector::Rcpp_UploadMatrixBasic(int type) socket->write(buffer.getBuffer(), buffer.size(), actual); } +void Rcpp_Connector::Rcpp_UploadSymbolVector(const std::vector& symbolBase, const std::vector& index){ + Buffer buffer; + int flag = (DATA_FORM::DF_VECTOR << 8) + DATA_TYPE::DT_SYMBOL + 128; + buffer.write((short) flag); + buffer.write((int) index.size()); + buffer.write((int) 1); + + //write symbol Base + buffer.write((int) 0); //symbolBaseID + buffer.write((int) symbolBase.size()); //symbolBaseSize + for(const auto& base : symbolBase){ + buffer.write(base); + } + //write index + for (auto ind : index) + { + buffer.write(ind); + } + size_t actual = 0; + socket->write(buffer.getBuffer(), buffer.size(), actual); +} + void Rcpp_Connector::Rcpp_UploadEntity(vector & vec, vector & NAIndex) { for (unsigned int i = 0; i < NAIndex.size(); i++) diff --git a/src/include/R_Type.h b/src/include/R_Type.h index ad0041c..c118fab 100644 --- a/src/include/R_Type.h +++ b/src/include/R_Type.h @@ -31,7 +31,7 @@ enum R_TYPE { SCALAR_DATE, SCALAR_DATETIME, VECTOR_DATE, VECTOR_DATETIME, MATRIX_DATE, MATRIX_DATETIME, - VECTOR_ANY + VECTOR_ANY, VECTOR_FACTOR }; #define DDB_NULL_INTEGER -2147483648 diff --git a/src/include/Utill.h b/src/include/Utill.h index 3e7a3f3..29fbf52 100644 --- a/src/include/Utill.h +++ b/src/include/Utill.h @@ -165,6 +165,8 @@ int Utill::ReturnRType(int data_form, int data_type) return VECTOR_DATETIME; case DATA_TYPE::DT_ANY: return VECTOR_ANY; + case DATA_TYPE::DT_SYMBOL + 128: + return VECTOR_FACTOR; default: cout << Utill::ErrorTypeNotSupport << endl; return 0; @@ -194,6 +196,10 @@ int Utill::ReturnRType(int data_form, int data_type) return MATRIX_DATE; case DATA_TYPE::DT_DATETIME: return MATRIX_DATETIME; + case DATA_TYPE::DT_SYMBOL: + case DATA_TYPE::DT_STRING: + case DATA_TYPE::DT_SYMBOL + 128: + return MATRIX_CHARACTER; case DATA_TYPE::DT_MINUTE: return MATRIX_DATETIME; case DATA_TYPE::DT_SECOND: diff --git a/src/include/Vectorr.h b/src/include/Vectorr.h index 3984f1b..220100c 100644 --- a/src/include/Vectorr.h +++ b/src/include/Vectorr.h @@ -55,6 +55,7 @@ class Vectorr Vectorr() {} virtual ~Vectorr() {} virtual bool isDate() { return false; } + virtual bool isString() { return false; } int getRow() {return row;} int getClm() {return clm;} vector & getNAIndex() {return NA_INDEX;} @@ -137,6 +138,63 @@ class VectorDouble : public Vectorr void* getVector() {return (void *)(&vec);} }; +class VectorSymbol : public Vectorr{ +private: + Rcpp::IntegerVector _vec; + std::vector str_vec; + std::vector _levels; + +public: + VectorSymbol(DataInputStream& in) + :Vectorr(in) + { + int size = Vectorr::getRow() * Vectorr::getClm(); + int symbaseId_ = -1; + in.readInt(symbaseId_); + int levelSize; + in.readInt(levelSize); + _levels.reserve(levelSize); + for(int i = 0; i < levelSize; i++){ + std::string s; + in.readString(s); + _levels.push_back(s); + } + + std::vector vec; + vec.reserve(size); + for (int i = 0; i < size; i++) + { + int temp; + in.readInt(temp); + vec.push_back(temp + 1); + } + + Rcpp::CharacterVector v = wrap(_levels); + if(v.size() > 0){ + v[0] = NA_STRING; + } + _vec = wrap(vec); + _vec.attr("class") = "factor"; + _vec.attr("levels") = v; + } + bool isString() override{ return true; } + void* getVector() override{return (void *)(&_vec);} + void* getStringVector() override + { + int size = _vec.size(); + str_vec.reserve(size); + for (int i = 0; i < size; ++i) + { + str_vec.push_back(_levels[_vec[i] - 1]); + if (_vec[i] == 1) + { + Vectorr::getNAIndex().push_back(i+1); + } + } + return (void *)(&str_vec); + } +}; + class VectorString : public Vectorr { private: @@ -160,7 +218,9 @@ class VectorString : public Vectorr } ~VectorString() {} // Free automatically - void* getVector() {return (void *)(&vec);} + bool isString() override{ return true; } + void* getVector() override{return (void *)(&vec);} + void* getStringVector() override{return (void *)(&vec);} }; class VectorChar : public Vectorr @@ -317,12 +377,12 @@ class VectorMonth : public Vectorr { private: //TODO - Rcpp::NumericVector _vec; + Rcpp::DateVector _vec; vector str_vec; vector origin_v; public: VectorMonth(DataInputStream &in) - :Vectorr(in) + :Vectorr(in), _vec(0) { int size = Vectorr::getRow() * Vectorr::getClm(); int y, m, d; @@ -746,7 +806,10 @@ int CreateVector(Vectorr*& vector_ptr, int data_type, DataInputStream& in) case DATA_TYPE::DT_NANOTIMESTAMP: vector_ptr = new VectorNanotimestamp(in); return VECTOR_DATETIME; - + + case DATA_TYPE::DT_SYMBOL + 128: + vector_ptr = new VectorSymbol(in); + return VECTOR_FACTOR; default: // cout << "UNSURPORT VECTOR TYPE" << endl; break; diff --git a/test/Main.R b/test/Main.R new file mode 100644 index 0000000..7207eac --- /dev/null +++ b/test/Main.R @@ -0,0 +1,16 @@ +source("./TestClose.R") +source("./TestConnect.R") +source("./TestRunScalar.R") +source("./TestRunPair.R") +source("./TestRunVector.R") +source("./TestRunSet.R") +source("./TestRunMatrix.R") +source("./TestRunTable.R") +source("./TestRpc.R") +source("./TestUploadScalar.R") +source("./TestUploadVector.R") +source("./TestUploadMatrix.R") +source("./TestUploadTable.R") + +. <- test$run() +. <- test$testResult() \ No newline at end of file diff --git a/test/TestClose.R b/test/TestClose.R index 25f2cea..3218be8 100644 --- a/test/TestClose.R +++ b/test/TestClose.R @@ -1,18 +1,27 @@ -source("setup/Settings.R") -source("pub/Assert.R") +if (!exists("TESTCLOSE_R__", envir = .GlobalEnv)){ + TESTCLOSE_R__ <- NULL + source("pub/Test.R") -library(RDolphinDB) + library(RDolphinDB) -record <- c(0L,0L) -# close -conn <- dbConnect(DolphinDB(),HOST,PORT) -conn <- dbClose(conn) -record <- assert(record,"connect close",conn@connected,FALSE) + # close + test$add_case( + "connect close", + function(conn){ + .conn <- dbConnect(DolphinDB(), HOST, PORT) + .conn <- dbClose(.conn) + test$assert(.conn@connected, FALSE) + } + ) -# already closed -conn <- dbConnect(DolphinDB(),HOST,PORT) -conn <- dbClose(conn) -conn <- dbClose(conn) -record <- assert(record,"connect close",conn@connected,FALSE) - -printTestResult(record) \ No newline at end of file + # already closed + test$add_case( + "already closed", + function(conn){ + .conn <- dbConnect(DolphinDB(), HOST, PORT) + .conn <- dbClose(.conn) + .conn <- dbClose(.conn) + test$assert(.conn@connected, FALSE) + } + ) +} \ No newline at end of file diff --git a/test/TestConnect.R b/test/TestConnect.R index e537d27..a5ae9fb 100644 --- a/test/TestConnect.R +++ b/test/TestConnect.R @@ -1,25 +1,43 @@ -source("setup/Settings.R") -source("pub/Assert.R") +if (!exists("TESTCONNECT_R__", envir = .GlobalEnv)){ + TESTCONNECT_R__ <- NULL + source("pub/Test.R") -library(RDolphinDB) + library(RDolphinDB) -record <- c(0L,0L) -# host error -conn <- dbConnect(DolphinDB(),"192.1.1.1",PORT) -record <- assert(record,"connect host error",conn@connected,FALSE) + # host error + test$add_case( + "host error", + function(conn){ + .conn <- dbConnect(DolphinDB(), "192.1.1.1", PORT) + test$assert(.conn@connected, FALSE) + } + ) -# port error -conn <- dbConnect(DolphinDB(),HOST,22) -record <- assert(record,"connect port error",conn@connected,FALSE) + # port error + test$add_case( + "port error", + function(conn){ + .conn <- dbConnect(DolphinDB(), HOST, 22) + test$assert(.conn@connected, FALSE) + } + ) -# user error -conn <- dbConnect(DolphinDB(),HOST,PORT,"dolphindb",PASSWD) -record <- assert(record,"connect user error",conn@connected,TRUE) -conn <- dbClose(conn) + # user error + test$add_case( + "user error", + function(conn){ + .conn <- dbConnect(DolphinDB(), HOST, PORT, "dolphindb", PASSWD) -# password error -conn <- dbConnect(DolphinDB(),HOST,PORT,USER,"000000") -record <- assert(record,"connect password error",conn@connected,TRUE) -conn <- dbClose(conn) + test$assert(.conn@connected, TRUE) + } + ) -printTestResult(record) \ No newline at end of file + # password error + test$add_case( + "password error", + function(conn){ + .conn <- dbConnect(DolphinDB(), HOST, PORT, USER, "111111") + test$assert(.conn@connected, TRUE) + } + ) +} \ No newline at end of file diff --git a/test/TestRpc.R b/test/TestRpc.R new file mode 100644 index 0000000..4be7b90 --- /dev/null +++ b/test/TestRpc.R @@ -0,0 +1,62 @@ +if (!exists("TESTRPCBASIC_R__", envir = .GlobalEnv)){ + TESTRPCBASIC_R__ <- NULL + source("pub/Test.R") + + library(RDolphinDB) + + # 0 params + test$add_case( + "test dbRpc 0 params", + function(conn){ + . <- dbRun(conn, " + def test_rpc0(){ + return true + } + ") + result <- dbRpc(conn, "test_rpc0", list()) + test$assert(result, TRUE) + } + ) + + # 1 params + test$add_case( + "test dbRpc 1 params", + function(conn){ + . <- dbRun(conn, " + def test_rpc1(x){ + return true + } + ") + result <- dbRpc(conn, "test_rpc1", list(1)) + test$assert(result, TRUE) + } + ) + + # 2 params + test$add_case( + "test dbRpc 2 params", + function(conn){ + . <- dbRun(conn, " + def test_rpc2(x,y){ + return true + } + ") + result <- dbRpc(conn, "test_rpc2", list(1, 2)) + test$assert(result, TRUE) + } + ) + + # part apply + test$add_case( + "test dbRpc part apply", + function(conn){ + . <- dbRun(conn, " + def test_rpc_part_apply(x,y){ + return true + } + ") + result <- dbRpc(conn, "test_rpc_part_apply{0}", list(1)) + test$assert(result, TRUE) + } + ) +} \ No newline at end of file diff --git a/test/TestRunMatrix.R b/test/TestRunMatrix.R index 9514fe6..02a6af7 100644 --- a/test/TestRunMatrix.R +++ b/test/TestRunMatrix.R @@ -1,125 +1,223 @@ -source("setup/Settings.R") -source("pub/Assert.R") +if (!exists("TESTRUNMATRIX_R__", envir = .GlobalEnv)){ + TESTRUNMATRIX_R__ <- NULL + source("pub/Test.R") -library(RDolphinDB) + library(RDolphinDB) -conn <- dbConnect(DolphinDB(),HOST,PORT,USER,PASSWD) -if (conn@connected){ - record <- c(0L,0L) # bool - result <- dbRun(conn,"x=matrix([true,false,00b],[true,false,00b]);x.rename!(`row1`row2`row3,`col1`col2);x") - expect <- matrix(c(TRUE,FALSE,NA,TRUE,FALSE,NA),nrow=3,byrow=FALSE) - colnames(expect) <- c("col1","col2") - rownames(expect) <- c("row1","row2","row3") - record <- assert(record,"download matrix bool 1",result,expect) + test$add_case( + "download matrix bool", + function(conn){ + result <- dbRun(conn, "x=matrix([true,false,00b],[true,false,00b]);x.rename!(`row1`row2`row3,`col1`col2);x") + expect <- matrix(c(TRUE, FALSE, NA, TRUE, FALSE, NA), nrow = 3, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2", "row3") + test$assert(result, expect) + } + ) # char - result <- dbRun(conn,"x=matrix([0c,127c,00c],[0c,127c,00c]);x.rename!(`row1`row2`row3,`col1`col2);x") - expect <- matrix(c(0L,127L,NA_integer_,0L,127L,NA_integer_),nrow=3,byrow=FALSE) - colnames(expect) <- c("col1","col2") - rownames(expect) <- c("row1","row2","row3") - record <- assert(record,"download matrix char 1",result,expect) + test$add_case( + "download matrix char", + function(conn){ + result <- dbRun(conn, "x=matrix([0c,127c,00c],[0c,127c,00c]);x.rename!(`row1`row2`row3,`col1`col2);x") + expect <- matrix(c(0L, 127L, NA_integer_, 0L, 127L, NA_integer_), nrow = 3, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2", "row3") + test$assert(result, expect) + } + ) # short - result <- dbRun(conn,"x=matrix([0h,32767h,00h],[0h,32767h,00h]);x.rename!(`row1`row2`row3,`col1`col2);x") - expect <- matrix(c(0L,32767L,NA_integer_,0L,32767L,NA_integer_),nrow=3,byrow=FALSE) - colnames(expect) <- c("col1","col2") - rownames(expect) <- c("row1","row2","row3") - record <- assert(record,"download matrix short 1",result,expect) + test$add_case( + "download matrix short", + function(conn){ + result <- dbRun(conn, "x=matrix([0h,32767h,00h],[0h,32767h,00h]);x.rename!(`row1`row2`row3,`col1`col2);x") + expect <- matrix(c(0L, 32767L, NA_integer_, 0L, 32767L, NA_integer_), nrow = 3, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2", "row3") + test$assert(result, expect) + } + ) # int - result <- dbRun(conn,"x=matrix([0i,2147483647i,00i],[0i,2147483647i,00i]);x.rename!(`row1`row2`row3,`col1`col2);x") - expect <- matrix(c(0L,2147483647L,NA_integer_,0L,2147483647L,NA_integer_),nrow=3,byrow=FALSE) - colnames(expect) <- c("col1","col2") - rownames(expect) <- c("row1","row2","row3") - record <- assert(record,"download matrix int 1",result,expect) + test$add_case( + "download matrix int", + function(conn){ + result <- dbRun(conn, "x=matrix([0i,2147483647i,00i],[0i,2147483647i,00i]);x.rename!(`row1`row2`row3,`col1`col2);x") + expect <- matrix(c(0L, 2147483647L, NA_integer_, 0L, 2147483647L, NA_integer_), nrow = 3, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2", "row3") + test$assert(result, expect) + } + ) # long - result <- dbRun(conn,"x=matrix([0l,9223372036854775807l,00l],[0l,9223372036854775807l,00l]);x.rename!(`row1`row2`row3,`col1`col2);x") - expect <- matrix(c(0L,9223372036854775807,NA_real_,0L,9223372036854775807,NA_real_),nrow=3,byrow=FALSE) - colnames(expect) <- c("col1","col2") - rownames(expect) <- c("row1","row2","row3") - record <- assert(record,"download matrix long 1",result,expect) + test$add_case( + "download matrix long", + function(conn){ + result <- dbRun(conn, "x=matrix([0l,9223372036854775807l,00l],[0l,9223372036854775807l,00l]);x.rename!(`row1`row2`row3,`col1`col2);x") + expect <- matrix(c(0L, 9223372036854775807, NA_real_, 0L, 9223372036854775807, NA_real_), nrow = 3, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2", "row3") + test$assert(result, expect) + } + ) # date - result <- dbRun(conn,"x=matrix([1970.01.01d,00d],[1970.01.01d,00d]);x.rename!(`row1`row2,`col1`col2);x") - expect <- matrix(c("1970-01-01",NA,"1970-01-01",NA),nrow=2,byrow=FALSE) - colnames(expect) <- c("col1","col2") - rownames(expect) <- c("row1","row2") - record <- assert(record,"download matrix date 1",result,expect) + test$add_case( + "download matrix date", + function(conn){ + result <- dbRun(conn, "x=matrix([1970.01.01d,00d],[1970.01.01d,00d]);x.rename!(`row1`row2,`col1`col2);x") + expect <- matrix(c("1970-01-01", NA, "1970-01-01", NA), nrow = 2, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2") + test$assert(result, expect) + } + ) # month - result <- dbRun(conn,"x=matrix([1970.01M,00M],[1970.01M,00M]);x.rename!(`row1`row2,`col1`col2);x") - expect <- matrix(c("1970-01-01",NA,"1970-01-01",NA),nrow=2,byrow=FALSE) - colnames(expect) <- c("col1","col2") - rownames(expect) <- c("row1","row2") - record <- assert(record,"download matrix month 1",result,expect) + test$add_case( + "download matrix month", + function(conn){ + result <- dbRun(conn, "x=matrix([1970.01M,00M],[1970.01M,00M]);x.rename!(`row1`row2,`col1`col2);x") + expect <- matrix(c("1970-01-01", NA, "1970-01-01", NA), nrow = 2, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2") + test$assert(result, expect) + } + ) # time - result <- dbRun(conn,"x=matrix([13:30:10.008t,00t],[13:30:10.008t,00t]);x.rename!(`row1`row2,`col1`col2);x") - expect <- matrix(c("1970-01-01 13:30:10",NA,"1970-01-01 13:30:10",NA),nrow=2,byrow=FALSE) - colnames(expect) <- c("col1","col2") - rownames(expect) <- c("row1","row2") - record <- assert(record,"download matrix time 1",result,expect) + test$add_case( + "download matrix time", + function(conn){ + result <- dbRun(conn, "x=matrix([13:30:10.008t,00t],[13:30:10.008t,00t]);x.rename!(`row1`row2,`col1`col2);x") + expect <- matrix(c("1970-01-01 13:30:10", NA, "1970-01-01 13:30:10", NA), nrow = 2, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2") + test$assert(result, expect) + } + ) # minute - result <- dbRun(conn,"x=matrix([13:30m,00m],[13:30m,00m]);x.rename!(`row1`row2,`col1`col2);x") - expect <- matrix(c("1970-01-01 13:30:00",NA,"1970-01-01 13:30:00",NA),nrow=2,byrow=FALSE) - colnames(expect) <- c("col1","col2") - rownames(expect) <- c("row1","row2") - record <- assert(record,"download matrix minute 1",result,expect) + test$add_case( + "download matrix minute", + function(conn){ + result <- dbRun(conn, "x=matrix([13:30m,00m],[13:30m,00m]);x.rename!(`row1`row2,`col1`col2);x") + expect <- matrix(c("1970-01-01 13:30:00", NA, "1970-01-01 13:30:00", NA), nrow = 2, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2") + test$assert(result, expect) + } + ) # second - result <- dbRun(conn,"x=matrix([13:30:10s,00s],[13:30:10s,00s]);x.rename!(`row1`row2,`col1`col2);x") - expect <- matrix(c("1970-01-01 13:30:10",NA,"1970-01-01 13:30:10",NA),nrow=2,byrow=FALSE) - colnames(expect) <- c("col1","col2") - rownames(expect) <- c("row1","row2") - record <- assert(record,"download matrix second 1",result,expect) + test$add_case( + "download matrix second", + function(conn){ + result <- dbRun(conn, "x=matrix([13:30:10s,00s],[13:30:10s,00s]);x.rename!(`row1`row2,`col1`col2);x") + expect <- matrix(c("1970-01-01 13:30:10", NA, "1970-01-01 13:30:10", NA), nrow = 2, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2") + test$assert(result, expect) + } + ) # datetime - result <- dbRun(conn,"x=matrix([2012.06.13T13:30:10D,00D],[2012.06.13T13:30:10D,00D]);x.rename!(`row1`row2,`col1`col2);x") - expect <- matrix(c("2012-06-13 13:30:10",NA,"2012-06-13 13:30:10",NA),nrow=2,byrow=FALSE) - colnames(expect) <- c("col1","col2") - rownames(expect) <- c("row1","row2") - record <- assert(record,"download matrix datetime 1",result,expect) + test$add_case( + "download matrix datetime", + function(conn){ + result <- dbRun(conn, "x=matrix([2012.06.13T13:30:10D,00D],[2012.06.13T13:30:10D,00D]);x.rename!(`row1`row2,`col1`col2);x") + expect <- matrix(c("2012-06-13 13:30:10", NA, "2012-06-13 13:30:10", NA), nrow = 2, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2") + test$assert(result, expect) + } + ) # timestamp - result <- dbRun(conn,"x=matrix([2012.06.13T13:30:10.008T,00T],[2012.06.13T13:30:10.008T,00T]);x.rename!(`row1`row2,`col1`col2);x") - expect <- matrix(c("2012-06-13 13:30:10",NA,"2012-06-13 13:30:10",NA),nrow=2,byrow=FALSE) - colnames(expect) <- c("col1","col2") - rownames(expect) <- c("row1","row2") - record <- assert(record,"download matrix timestamp 1",result,expect) + test$add_case( + "download matrix timestamp", + function(conn){ + result <- dbRun(conn, "x=matrix([2012.06.13T13:30:10.008T,00T],[2012.06.13T13:30:10.008T,00T]);x.rename!(`row1`row2,`col1`col2);x") + expect <- matrix(c("2012-06-13 13:30:10", NA, "2012-06-13 13:30:10", NA), nrow = 2, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2") + test$assert(result, expect) + } + ) # nanotime - result <- dbRun(conn,"x=matrix([13:30:10.008007006n,00n],[13:30:10.008007006n,00n]);x.rename!(`row1`row2,`col1`col2);x") - expect <- matrix(c("1970-01-01 13:30:10",NA,"1970-01-01 13:30:10",NA),nrow=2,byrow=FALSE) - colnames(expect) <- c("col1","col2") - rownames(expect) <- c("row1","row2") - record <- assert(record,"download matrix nanotime 1",result,expect) + test$add_case( + "download matrix nanotime", + function(conn){ + result <- dbRun(conn, "x=matrix([13:30:10.008007006n,00n],[13:30:10.008007006n,00n]);x.rename!(`row1`row2,`col1`col2);x") + expect <- matrix(c("1970-01-01 13:30:10", NA, "1970-01-01 13:30:10", NA), nrow = 2, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2") + test$assert(result, expect) + } + ) # nanotimestamp - result <- dbRun(conn,"x=matrix([2012.06.13T13:30:10.008007006N,00N],[2012.06.13T13:30:10.008007006N,00N]);x.rename!(`row1`row2,`col1`col2);x") - expect <- matrix(c("2012-06-13 13:30:10",NA,"2012-06-13 13:30:10",NA),nrow=2,byrow=FALSE) - colnames(expect) <- c("col1","col2") - rownames(expect) <- c("row1","row2") - record <- assert(record,"download matrix nanotimestamp 1",result,expect) + test$add_case( + "download matrix nanotimestamp", + function(conn){ + result <- dbRun(conn, "x=matrix([2012.06.13T13:30:10.008007006N,00N],[2012.06.13T13:30:10.008007006N,00N]);x.rename!(`row1`row2,`col1`col2);x") + expect <- matrix(c("2012-06-13 13:30:10", NA, "2012-06-13 13:30:10", NA), nrow = 2, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2") + test$assert(result, expect) + } + ) # float - result <- dbRun(conn,"x=matrix([0.0f,float('nan'),float('inf'),00f],[0.0f,float('nan'),float('inf'),00f]);x.rename!(`row1`row2`row3`row4,`col1`col2);x") - expect <- matrix(c(0,NaN,Inf,NA_real_,0,NaN,Inf,NA_real_),nrow=4,byrow=FALSE) - colnames(expect) <- c("col1","col2") - rownames(expect) <- c("row1","row2","row3","row4") - record <- assert(record,"download matrix float 1",result,expect) + test$add_case( + "download matrix float", + function(conn){ + result <- dbRun(conn, "x=matrix([0.0f,float('nan'),float('inf'),00f],[0.0f,float('nan'),float('inf'),00f]);x.rename!(`row1`row2`row3`row4,`col1`col2);x") + expect <- matrix(c(0, NaN, Inf, NA_real_, 0, NaN, Inf, NA_real_), nrow = 4, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2", "row3", "row4") + test$assert(result, expect) + } + ) # double - result <- dbRun(conn,"x=matrix([0.0F,00F],[0.0F,00F]);x.rename!(`row1`row2,`col1`col2);x") - expect <- matrix(c(0,NA_real_,0,NA_real_),nrow=2,byrow=FALSE) - colnames(expect) <- c("col1","col2") - rownames(expect) <- c("row1","row2") - record <- assert(record,"download matrix double 1",result,expect) - - printTestResult(record) - conn <- dbClose(conn) -} else { - stop("connect error in TestDownloadMatrix.R") + test$add_case( + "download matrix double", + function(conn){ + result <- dbRun(conn, "x=matrix([0.0F,00F],[0.0F,00F]);x.rename!(`row1`row2,`col1`col2);x") + expect <- matrix(c(0, NA_real_, 0, NA_real_), nrow = 2, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2") + test$assert(result, expect) + } + ) + + # string + test$add_case( + "download matrix string", + function(conn){ + result <- dbRun(conn, "x=matrix([`a,\"\"],[`a,\"\"]);x.rename!(`row1`row2,`col1`col2);x") + expect <- matrix(c("a", NA_character_, "a", NA_character_), nrow = 2, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2") + test$assert(result, expect) + } + ) + + # symbol + test$add_case( + "download matrix symbol", + function(conn){ + result <- dbRun(conn, "x=symbol([`a,\"\",`a,\"\"])$2:2;x.rename!(`row1`row2,`col1`col2);x") + expect <- matrix(c("a", NA_character_, "a", NA_character_), nrow = 2, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2") + test$assert(result, expect) + } + ) + } \ No newline at end of file diff --git a/test/TestRunPair.R b/test/TestRunPair.R index 0df58bf..e72cec4 100644 --- a/test/TestRunPair.R +++ b/test/TestRunPair.R @@ -1,119 +1,292 @@ -source("setup/Settings.R") -source("pub/Assert.R") +if (!exists("TESTRUNPAIR_R__", envir = .GlobalEnv)){ + TESTRUNPAIR_R__ <- NULL + source("pub/Test.R") -library(RDolphinDB) + library(RDolphinDB) -conn <- dbConnect(DolphinDB(),HOST,PORT,USER,PASSWD) -if (conn@connected){ - record <- c(0L,0L) # bool - result <- dbRun(conn,"true:false") - record <- assert(record,"download pair bool true:false",result,c(TRUE,FALSE)) - result <- dbRun(conn,"true:00b") - record <- assert(record,"download pair bool true:00b",result,c(TRUE,NA)) + test$add_case( + "download pair bool true:false", + function(conn){ + result <- dbRun(conn, "true:false") + test$assert(result, c(TRUE, FALSE)) + } + ) + test$add_case( + "download pair bool true:00b", + function(conn){ + result <- dbRun(conn, "true:00b") + test$assert(result, c(TRUE, NA)) + } + ) # char - result <- dbRun(conn,"0c:127c") - record <- assert(record,"download pair char 0c:127c",result,c(0L,127L)) - result <- dbRun(conn,"0c:00c") - record <- assert(record,"download pair char 0c:00c",result,c(0L,NA_integer_)) + test$add_case( + "download pair char 0c:127c", + function(conn){ + result <- dbRun(conn, "0c:127c") + test$assert(result, c(0L, 127L)) + } + ) + test$add_case( + "download pair char 0c:00c", + function(conn){ + result <- dbRun(conn, "0c:00c") + test$assert(result, c(0L, NA_integer_)) + } + ) # short - result <- dbRun(conn,"0h:32767h") - record <- assert(record,"download pair short 0h:32767h",result,c(0L,32767L)) - result <- dbRun(conn,"0h:00h") - record <- assert(record,"download pair short 0h:00h",result,c(0L,NA_integer_)) + test$add_case( + "download pair short 0h:32767h", + function(conn){ + result <- dbRun(conn, "0h:32767h") + test$assert(result, c(0L, 32767L)) + } + ) + test$add_case( + "download pair short 0h:00h", + function(conn){ + result <- dbRun(conn, "0h:00h") + test$assert(result, c(0L, NA_integer_)) + } + ) # int - result <- dbRun(conn,"0i:2147483647i") - record <- assert(record,"download pair int 0i:2147483647i1",result,c(0L,2147483647L)) - result <- dbRun(conn,"0i:00i") - record <- assert(record,"download pair int 0i:00i",result,c(0L,NA_integer_)) + test$add_case( + "download pair int 0i:2147483647i", + function(conn){ + result <- dbRun(conn, "0i:2147483647i") + test$assert(result, c(0L, 2147483647L)) + } + ) + test$add_case( + "download pair int 0i:00i", + function(conn){ + result <- dbRun(conn, "0i:00i") + test$assert(result, c(0L, NA_integer_)) + } + ) # long - result <- dbRun(conn,"0l:9223372036854775807l") - record <- assert(record,"download pair long 0l:9223372036854775807l",result,c(0L,9223372036854775807)) - result <- dbRun(conn,"0l:00l") - record <- assert(record,"download pair long 0l:00l",result,c(0,NA_real_)) + test$add_case( + "download pair long 0l:9223372036854775807l", + function(conn){ + result <- dbRun(conn, "0l:9223372036854775807l") + test$assert(result, c(0L, 9223372036854775807)) + } + ) + test$add_case( + "download pair long 0l:00l", + function(conn){ + result <- dbRun(conn, "0l:00l") + test$assert(result, c(0, NA_real_)) + } + ) # date - result <- dbRun(conn,"1970.01.01d:1970.01.02d") - record <- assert(record,"download pair date 1970.01.01d:1970.01.02d",result,c(as.Date("1970-01-01"),as.Date("1970-01-02"))) - result <- dbRun(conn,"1970.01.01d:00d") - record <- assert(record,"download pair date 1970.01.01d:00d",result,c(as.Date("1970-01-01"),NA)) + test$add_case( + "download pair date 1970.01.01d:1970.01.02d", + function(conn){ + result <- dbRun(conn, "1970.01.01d:1970.01.02d") + test$assert(result, c(as.Date("1970-01-01"), as.Date("1970-01-02"))) + } + ) + test$add_case( + "download pair date 1970.01.01d:00d", + function(conn){ + result <- dbRun(conn, "1970.01.01d:00d") + test$assert(result, c(as.Date("1970-01-01"), NA)) + } + ) # month - result <- dbRun(conn,"1970.01M:1970.02M") - record <- assert(record,"download pair month 1970.01M:1970.02M",result,c(as.Date("1970-01-01"),as.Date("1970-02-01"))) - result <- dbRun(conn,"1970.01M:00M") - record <- assert(record,"download pair month 1970.01M:00M",result,c(as.Date("1970-01-01"),NA)) + test$add_case( + "download pair month 1970.01M:1970.02M", + function(conn){ + result <- dbRun(conn, "1970.01M:1970.02M") + test$assert(result, c(as.Date("1970-01-01"), as.Date("1970-02-01"))) + } + ) + test$add_case( + "download pair month 1970.01M:00M", + function(conn){ + result <- dbRun(conn, "1970.01M:00M") + test$assert(result, c(as.Date("1970-01-01"), NA)) + } + ) # time - result <- dbRun(conn,"13:30:10.008t : 13:30:10.008t") - record <- assert(record,"download pair time 13:30:10.008t : 13:30:10.008t",result,c(as.POSIXct("1970-01-01 13:30:10.008",tz="UTC"),as.POSIXct("1970-01-01 13:30:10.008",tz="UTC"))) - result <- dbRun(conn,"1970.01M:00M") - record <- assert(record,"download pair time 1970.01M:00M",result,c(as.Date("1970-01-01"),NA)) + test$add_case( + "download pair time 13:30:10.008t : 13:30:10.008t", + function(conn){ + result <- dbRun(conn, "13:30:10.008t : 13:30:10.008t") + test$assert(result, c(as.POSIXct("1970-01-01 13:30:10.008", tz = "UTC"), as.POSIXct("1970-01-01 13:30:10.008", tz = "UTC"))) + } + ) + test$add_case( + "download pair time 13:30:10.008t : 00t", + function(conn){ + result <- dbRun(conn, "13:30:10.008t : 00t") + test$assert(result, c(as.POSIXct("1970-01-01 13:30:10.008", tz = "UTC"), as.POSIXct(NA, tz = "UTC"))) + } + ) # minute - result <- dbRun(conn,"13:30m : 13:30m") - record <- assert(record,"download pair minute 13:30m : 13:30m",result,c(as.POSIXct("1970-01-01 13:30:00",tz="UTC"),as.POSIXct("1970-01-01 13:30:00",tz="UTC"))) - result <- dbRun(conn,"1970.01M:00M") - record <- assert(record,"download pair minute 1970.01M:00M",result,c(as.Date("1970-01-01"),NA)) + test$add_case( + "download pair minute 13:30m : 13:30m", + function(conn){ + result <- dbRun(conn, "13:30m : 13:30m") + test$assert(result, c(as.POSIXct("1970-01-01 13:30:00", tz = "UTC"), as.POSIXct("1970-01-01 13:30:00", tz = "UTC"))) + } + ) + test$add_case( + "download pair minute 13:30m : 00m", + function(conn){ + result <- dbRun(conn, "13:30m : 00m") + test$assert(result, c(as.POSIXct("1970-01-01 13:30:00", tz = "UTC"), as.POSIXct(NA, tz = "UTC"))) + } + ) # second - result <- dbRun(conn,"13:30:10s : 13:30:11s") - record <- assert(record,"download pair second 13:30:10s : 13:30:11s",result,c(as.POSIXct("1970-01-01 13:30:10",tz="UTC"),as.POSIXct("1970-01-01 13:30:11",tz="UTC"))) - result <- dbRun(conn,"13:30:10s : 00s") - record <- assert(record,"download pair second 13:30:10s : 00s",result,c(as.POSIXct("1970-01-01 13:30:10",tz="UTC"),as.POSIXct(NA,tz="UTC"))) + test$add_case( + "download pair second 13:30:10s : 13:30:11s", + function(conn){ + result <- dbRun(conn, "13:30:10s : 13:30:11s") + test$assert(result, c(as.POSIXct("1970-01-01 13:30:10", tz = "UTC"), as.POSIXct("1970-01-01 13:30:11", tz = "UTC"))) + } + ) + test$add_case( + "download pair second 13:30:10s : 00s", + function(conn){ + result <- dbRun(conn, "13:30:10s : 00s") + test$assert(result, c(as.POSIXct("1970-01-01 13:30:10", tz = "UTC"), as.POSIXct(NA, tz = "UTC"))) + } + ) # datetime - result <- dbRun(conn,"2012.06.13T13:30:10D : 2012.06.13T13:30:11D") - record <- assert(record,"download pair datetime 2012.06.13T13:30:10D : 2012.06.13T13:30:11D",result,c(as.POSIXct("2012-06-13 13:30:10",tz="UTC"),as.POSIXct("2012-06-13 13:30:11",tz="UTC"))) - result <- dbRun(conn,"2012.06.13T13:30:10D : 00D") - record <- assert(record,"download pair datetime 2012.06.13T13:30:10D : 00D",result,c(as.POSIXct("2012-06-13 13:30:10",tz="UTC"),as.POSIXct(NA,tz="UTC"))) + test$add_case( + "download pair datetime 2012.06.13T13:30:10D : 2012.06.13T13:30:11D", + function(conn){ + result <- dbRun(conn, "2012.06.13T13:30:10D : 2012.06.13T13:30:11D") + test$assert(result, c(as.POSIXct("2012-06-13 13:30:10", tz = "UTC"), as.POSIXct("2012-06-13 13:30:11", tz = "UTC"))) + } + ) + test$add_case( + "download pair datetime 2012.06.13T13:30:10D : 00D", + function(conn){ + result <- dbRun(conn, "2012.06.13T13:30:10D : 00D") + test$assert(result, c(as.POSIXct("2012-06-13 13:30:10", tz = "UTC"), as.POSIXct(NA, tz = "UTC"))) + } + ) # timestamp - result <- dbRun(conn,"2012.06.13T13:30:10.008T : 2012.06.13T13:30:10.009T") - record <- assert(record,"download pair timestamp 2012.06.13T13:30:10.008T : 2012.06.13T13:30:10.009T",result,c(as.POSIXct("2012-06-13 13:30:10.008",tz="UTC"),as.POSIXct("2012-06-13 13:30:10.009",tz="UTC"))) - result <- dbRun(conn,"2012.06.13T13:30:10.008T : 00T") - record <- assert(record,"download pair timestamp 2012.06.13T13:30:10.008T : 00T",result,c(as.POSIXct("2012-06-13 13:30:10.008",tz="UTC"),as.POSIXct(NA,tz="UTC"))) + test$add_case( + "download pair timestamp 2012.06.13T13:30:10.008T : 2012.06.13T13:30:10.009T", + function(conn){ + result <- dbRun(conn, "2012.06.13T13:30:10.008T : 2012.06.13T13:30:10.009T") + test$assert(result, c(as.POSIXct("2012-06-13 13:30:10.008", tz = "UTC"), as.POSIXct("2012-06-13 13:30:10.009", tz = "UTC"))) + } + ) + test$add_case( + "download pair timestamp 2012.06.13T13:30:10.008T : 00T", + function(conn){ + result <- dbRun(conn, "2012.06.13T13:30:10.008T : 00T") + test$assert(result, c(as.POSIXct("2012-06-13 13:30:10.008", tz = "UTC"), as.POSIXct(NA, tz = "UTC"))) + } + ) # nanotime - result <- dbRun(conn,"13:30:10.008007006n : 13:30:10.008007007n") - record <- assert(record,"download pair nanotime 13:30:10.008007006n : 13:30:10.008007007n",result,c(as.POSIXct("1970-01-01 13:30:10.008007006",tz="UTC"),as.POSIXct("1970-01-01 13:30:10.008007007",tz="UTC"))) - result <- dbRun(conn,"13:30:10.008007006n : 00n") - record <- assert(record,"download pair nanotime 13:30:10.008007006n : 00n",result,c(as.POSIXct("1970-01-01 13:30:10.008007006",tz="UTC"),as.POSIXct(NA,tz="UTC"))) + test$add_case( + "download pair nanotime 13:30:10.008007006n : 13:30:10.008007007n", + function(conn){ + result <- dbRun(conn, "13:30:10.008007006n : 13:30:10.008007007n") + test$assert(result, c(as.POSIXct("1970-01-01 13:30:10.008007006", tz = "UTC"), as.POSIXct("1970-01-01 13:30:10.008007007", tz = "UTC"))) + } + ) + test$add_case( + "download pair nanotime 13:30:10.008007006n : 00n", + function(conn){ + result <- dbRun(conn, "13:30:10.008007006n : 00n") + test$assert(result, c(as.POSIXct("1970-01-01 13:30:10.008007006", tz = "UTC"), as.POSIXct(NA, tz = "UTC"))) + } + ) # nanotimestamp - result <- dbRun(conn,"2012.06.13T13:30:10.008007006N : 2012.06.13T13:30:10.008007007N") - record <- assert(record,"download pair nanotimestamp 2012.06.13T13:30:10.008007006N : 2012.06.13T13:30:10.008007007N",result,c(as.POSIXct("2012-06-13 13:30:10.008007006",tz="UTC"),as.POSIXct("2012-06-13 13:30:10.008007007",tz="UTC"))) - result <- dbRun(conn,"2012.06.13T13:30:10.008007006N : 00N") - record <- assert(record,"download pair nanotimestamp 2012.06.13T13:30:10.008007006N : 00N",result,c(as.POSIXct("2012-06-13 13:30:10.008007006",tz="UTC"),as.POSIXct(NA,tz="UTC"))) + test$add_case( + "download pair nanotimestamp 2012.06.13T13:30:10.008007006N : 2012.06.13T13:30:10.008007007N", + function(conn){ + result <- dbRun(conn, "2012.06.13T13:30:10.008007006N : 2012.06.13T13:30:10.008007007N") + test$assert(result, c(as.POSIXct("2012-06-13 13:30:10.008007006", tz = "UTC"), as.POSIXct("2012-06-13 13:30:10.008007007", tz = "UTC"))) + } + ) + test$add_case( + "download pair nanotimestamp 2012.06.13T13:30:10.008007006N : 00N", + function(conn){ + result <- dbRun(conn, "2012.06.13T13:30:10.008007006N : 00N") + test$assert(result, c(as.POSIXct("2012-06-13 13:30:10.008007006", tz = "UTC"), as.POSIXct(NA, tz = "UTC"))) + } + ) # float - result <- dbRun(conn,"0.0f:0.1f") - record <- assert(record,"download pair float0.0f:0.1f",result,c(0,0.1)) - result <- dbRun(conn,"0.0f:float('nan')") - record <- assert(record,"download pair float 0.0f:float('nan')",result,c(0,NaN)) - result <- dbRun(conn,"0.0f:float('inf')") - record <- assert(record,"download pair float 0.0f:float('inf')",result,c(0,Inf)) - result <- dbRun(conn,"0.0f:00f") - record <- assert(record,"download pair float 0.0f:00f",result,c(0,NA_real_)) + test$add_case( + "download pair float 0.0f:0.1f", + function(conn){ + result <- dbRun(conn, "0.0f:0.1f") + test$assert(result, c(0, 0.1)) + } + ) + test$add_case( + "download pair float 0.0f:float('nan')", + function(conn){ + result <- dbRun(conn, "0.0f:float('nan')") + test$assert(result, c(0, NaN)) + } + ) + test$add_case( + "download pair float 0.0f:float('inf')", + function(conn){ + result <- dbRun(conn, "0.0f:float('inf')") + test$assert(result, c(0, Inf)) + } + ) + test$add_case( + "download pair float 0.0f:00f", + function(conn){ + result <- dbRun(conn, "0.0f:00f") + test$assert(result, c(0, NA_real_)) + } + ) # double - result <- dbRun(conn,"0.0F:0.1F") - record <- assert(record,"download pair double 0.0F:0.1F",result,c(0,0.1)) - result <- dbRun(conn,"0.0F:00F") - record <- assert(record,"download pair double 0.0F:00F",result,c(0,NA_real_)) + test$add_case( + "download pair double 0.0F:0.1F", + function(conn){ + result <- dbRun(conn, "0.0F:0.1F") + test$assert(result, c(0, 0.1)) + } + ) + test$add_case( + "download pair double 0.0F:00F", + function(conn){ + result <- dbRun(conn, "0.0F:00F") + test$assert(result, c(0, NA_real_)) + } + ) # string - result <- dbRun(conn,"`a:`b") - record <- assert(record,"download pair string `a:`b",result,c("a","b")) - result <- dbRun(conn,"`a:\"\"") - record <- assert(record,"download pair string `a:\"\"",result,c("a",NA_character_)) - - printTestResult(record) - conn <- dbClose(conn) -} else { - stop("connect error in TestDownloadPair.R") + test$add_case( + "download pair string `a:`b", + function(conn){ + result <- dbRun(conn, "`a:`b") + test$assert(result, c("a", "b")) + } + ) + test$add_case( + "download pair string `a:\"\"", + function(conn){ + result <- dbRun(conn, "`a:\"\"") + test$assert(result, c("a", NA_character_)) + } + ) } \ No newline at end of file diff --git a/test/TestRunScalar.R b/test/TestRunScalar.R index 42273e6..6f3e51c 100644 --- a/test/TestRunScalar.R +++ b/test/TestRunScalar.R @@ -1,143 +1,385 @@ -source("setup/Settings.R") -source("pub/Assert.R") +if (!exists("TESTRUNSCALAR_R__", envir = .GlobalEnv)){ + TESTRUNSCALAR_R__ <- NULL + source("pub/Test.R") -library(RDolphinDB) - -conn <- dbConnect(DolphinDB(),HOST,PORT,USER,PASSWD) -if (conn@connected){ - record <- c(0L,0L) # void - result <- dbRun(conn,"NULL") - record <- assert(record,"download scalar void",result,NA) + test$add_case( + "download scalar void", + function(conn){ + result <- dbRun(conn, "NULL") + test$assert(result, NA) + } + ) # bool - result <- dbRun(conn,"true") - record <- assert(record,"download scalar bool true",result,TRUE) - result <- dbRun(conn,"false") - record <- assert(record,"download scalar bool false",result,FALSE) - result <- dbRun(conn,"00b") - record <- assert(record,"download scalar bool true",result,NA) + test$add_case( + "download scalar bool true", + function(conn){ + result <- dbRun(conn, "true") + test$assert(result, TRUE) + } + ) + test$add_case( + "download scalar bool false", + function(conn){ + result <- dbRun(conn, "false") + test$assert(result, FALSE) + } + ) + test$add_case( + "download scalar bool 00b", + function(conn){ + result <- dbRun(conn, "00b") + test$assert(result, NA) + } + ) # char - result <- dbRun(conn,"0c") - record <- assert(record,"download scalar char 0c",result,0L) - result <- dbRun(conn,"127c") - record <- assert(record,"download scalar char 128c",result,127L) - result <- dbRun(conn,"-127c") - record <- assert(record,"download scalar char -127c",result,-127L) - result <- dbRun(conn,"00c") - record <- assert(record,"download scalar char 00c",result,NA_integer_) + test$add_case( + "download scalar char 0c", + function(conn){ + result <- dbRun(conn, "0c") + test$assert(result, 0L) + } + ) + test$add_case( + "download scalar char 127c", + function(conn){ + result <- dbRun(conn, "127c") + test$assert(result, 127L) + } + ) + test$add_case( + "download scalar char -127c", + function(conn){ + result <- dbRun(conn, "-127c") + test$assert(result, -127L) + } + ) + test$add_case( + "download scalar char 00c", + function(conn){ + result <- dbRun(conn, "00c") + test$assert(result, NA_integer_) + } + ) # short - result <- dbRun(conn,"0h") - record <- assert(record,"download scalar short 0h",result,0L) - result <- dbRun(conn,"32767h") - record <- assert(record,"download scalar short 32767h",result,32767L) - result <- dbRun(conn,"-32767h") - record <- assert(record,"download scalar short -32767h",result,-32767L) - result <- dbRun(conn,"00h") - record <- assert(record,"download scalar short 00h",result,NA_integer_) + test$add_case( + "download scalar short 0h", + function(conn){ + result <- dbRun(conn, "0h") + test$assert(result, 0L) + } + ) + test$add_case( + "download scalar short 32767h", + function(conn){ + result <- dbRun(conn, "32767h") + test$assert(result, 32767L) + } + ) + test$add_case( + "download scalar short -32767h", + function(conn){ + result <- dbRun(conn, "-32767h") + test$assert(result, -32767L) + } + ) + test$add_case( + "download scalar short 00h", + function(conn){ + result <- dbRun(conn, "00h") + test$assert(result, NA_integer_) + } + ) # int - result <- dbRun(conn,"0i") - record <- assert(record,"download scalar int 0i",result,0L) - result <- dbRun(conn,"2147483647i") - record <- assert(record,"download scalar int 2147483647i",result,2147483647L) - result <- dbRun(conn,"-2147483647i") - record <- assert(record,"download scalar int -2147483647i",result,-2147483647L) - result <- dbRun(conn,"00i") - record <- assert(record,"download scalar int 00i",result,NA_integer_) + test$add_case( + "download scalar int 0i", + function(conn){ + result <- dbRun(conn, "0i") + test$assert(result, 0L) + } + ) + test$add_case( + "download scalar int 2147483647i", + function(conn){ + result <- dbRun(conn, "2147483647i") + test$assert(result, 2147483647L) + } + ) + test$add_case( + "download scalar int -2147483647i", + function(conn){ + result <- dbRun(conn, "-2147483647i") + test$assert(result, -2147483647L) + } + ) + test$add_case( + "download scalar int 00i", + function(conn){ + result <- dbRun(conn, "00i") + test$assert(result, NA_integer_) + } + ) # long - result <- dbRun(conn,"0l") - record <- assert(record,"download scalar long 0l",result,0) - result <- dbRun(conn,"9223372036854775807l") - record <- assert(record,"download scalar long 9223372036854775807l",result,9223372036854775807) - result <- dbRun(conn,"-9223372036854775807l") - record <- assert(record,"download scalar long -9223372036854775807l",result,-9223372036854775807) - result <- dbRun(conn,"00l") - record <- assert(record,"download scalar long 00l",result,NA_real_) + test$add_case( + "download scalar long 0l", + function(conn){ + result <- dbRun(conn, "0l") + test$assert(result, 0) + } + ) + test$add_case( + "download scalar long 9223372036854775807l", + function(conn){ + result <- dbRun(conn, "9223372036854775807l") + test$assert(result, 9223372036854775807) + } + ) + test$add_case( + "download scalar long -9223372036854775807l", + function(conn){ + result <- dbRun(conn, "-9223372036854775807l") + test$assert(result, -9223372036854775807) + } + ) + test$add_case( + "download scalar long 00l", + function(conn){ + result <- dbRun(conn, "00l") + test$assert(result, NA_real_) + } + ) # date - result <- dbRun(conn,"1970.01.01d") - record <- assert(record,"download scalar date 1970.01.01d",result,as.Date("1970-01-01")) - result <- dbRun(conn,"00d") - record <- assert(record,"download scalar date 00d",result,as.Date(NA)) + test$add_case( + "download scalar date 1970.01.01d", + function(conn){ + result <- dbRun(conn, "1970.01.01d") + test$assert(result, as.Date("1970-01-01")) + } + ) + test$add_case( + "download scalar date 00d", + function(conn){ + result <- dbRun(conn, "00d") + test$assert(result, as.Date(NA)) + } + ) # month - result <- dbRun(conn,"1970.01M") - record <- assert(record,"download scalar month 1970.01M",result,as.Date("1970-01-01")) - result <- dbRun(conn,"00M") - record <- assert(record,"download scalar month 00m",result,as.Date(NA)) + test$add_case( + "download scalar month 1970.01M", + function(conn){ + result <- dbRun(conn, "1970.01M") + test$assert(result, as.Date("1970-01-01")) + } + ) + test$add_case( + "download scalar month 00M", + function(conn){ + result <- dbRun(conn, "00M") + test$assert(result, as.Date(NA)) + } + ) # time - result <- dbRun(conn,"13:30:10.008t") - record <- assert(record,"download scalar time 13:30:10.008t",result,as.POSIXct("1970-01-01 13:30:10.008",tz="UTC")) - result <- dbRun(conn,"00t") - record <- assert(record,"download scalar time 00t",result,as.POSIXct(NA)) + test$add_case( + "download scalar time 13:30:10.008t", + function(conn){ + result <- dbRun(conn, "13:30:10.008t") + test$assert(result, as.POSIXct("1970-01-01 13:30:10.008", tz = "UTC")) + } + ) + test$add_case( + "download scalar time 00t", + function(conn){ + result <- dbRun(conn, "00t") + test$assert(result, as.POSIXct(NA)) + } + ) + + # minute + test$add_case( + "download scalar minute 13:30m", + function(conn){ + result <- dbRun(conn, "13:30m") + test$assert(result, as.POSIXct("1970-01-01 13:30:00", tz = "UTC")) + } + ) + test$add_case( + "download scalar minute 00m", + function(conn){ + result <- dbRun(conn, "00m") + test$assert(result, as.POSIXct(NA)) + } + ) # minute - result <- dbRun(conn,"13:30m") - record <- assert(record,"download scalar minute 13:30m",result,as.POSIXct("1970-01-01 13:30:00",tz="UTC")) - result <- dbRun(conn,"00m") - record <- assert(record,"download scalar minute 00m",result,as.POSIXct(NA)) + test$add_case( + "download scalar minute 13:30m", + function(conn){ + result <- dbRun(conn, "13:30m") + test$assert(result, as.POSIXct("1970-01-01 13:30:00", tz = "UTC")) + } + ) + test$add_case( + "download scalar minute 00m", + function(conn){ + result <- dbRun(conn, "00m") + test$assert(result, as.POSIXct(NA)) + } + ) # second - result <- dbRun(conn,"13:30:10s") - record <- assert(record,"download scalar second 13:30:10s",result,as.POSIXct("1970-01-01 13:30:10",tz="UTC")) - result <- dbRun(conn,"00s") - record <- assert(record,"download scalar second 00s",result,as.POSIXct(NA)) + test$add_case( + "download scalar second 13:30:10s", + function(conn){ + result <- dbRun(conn, "13:30:10s") + test$assert(result, as.POSIXct("1970-01-01 13:30:10", tz = "UTC")) + } + ) + test$add_case( + "download scalar second 00s", + function(conn){ + result <- dbRun(conn, "00s") + test$assert(result, as.POSIXct(NA)) + } + ) # datetime - result <- dbRun(conn,"2012.06.13T13:30:10D") - record <- assert(record,"download scalar datetime 2012.06.13T13:30:10D",result,as.POSIXct("2012-06-13 13:30:10",tz="UTC")) - result <- dbRun(conn,"00D") - record <- assert(record,"download scalar datetime 00D",result,as.POSIXct(NA)) + test$add_case( + "download scalar datetime 2012.06.13T13:30:10D", + function(conn){ + result <- dbRun(conn, "2012.06.13T13:30:10D") + test$assert(result, as.POSIXct("2012-06-13 13:30:10", tz = "UTC")) + } + ) + test$add_case( + "download scalar datetime 00D", + function(conn){ + result <- dbRun(conn, "00D") + test$assert(result, as.POSIXct(NA)) + } + ) # timestamp - result <- dbRun(conn,"2012.06.13T13:30:10.008T") - record <- assert(record,"download scalar timestamp 2012.06.13T13:30:10.008T",result,as.POSIXct("2012-06-13 13:30:10.008",tz="UTC")) - result <- dbRun(conn,"00T") - record <- assert(record,"download scalar timestamp 00T",result,as.POSIXct(NA)) + test$add_case( + "download scalar timestamp 2012.06.13T13:30:10.008T", + function(conn){ + result <- dbRun(conn, "2012.06.13T13:30:10.008T") + test$assert(result, as.POSIXct("2012-06-13 13:30:10.008", tz = "UTC")) + } + ) + test$add_case( + "download scalar timestamp 00T", + function(conn){ + result <- dbRun(conn, "00T") + test$assert(result, as.POSIXct(NA)) + } + ) # nanotime - result <- dbRun(conn,"13:30:10.008007006n") - record <- assert(record,"download scalar nanotime 13:30:10.008007006n",result,as.POSIXct("1970-01-01 13:30:10.008007006",tz="UTC")) - result <- dbRun(conn,"00n") - record <- assert(record,"download scalar nanotime 00n",result,as.POSIXct(NA)) + test$add_case( + "download scalar nanotime 13:30:10.008007006n", + function(conn){ + result <- dbRun(conn, "13:30:10.008007006n") + test$assert(result, as.POSIXct("1970-01-01 13:30:10.008007006", tz = "UTC")) + } + ) + test$add_case( + "download scalar nanotime 00n", + function(conn){ + result <- dbRun(conn, "00n") + test$assert(result, as.POSIXct(NA)) + } + ) # nanotimestamp - result <- dbRun(conn,"2012.06.13T13:30:10.008007006N") - record <- assert(record,"download scalar nanotimestamp 2012.06.13T13:30:10.008007006N",result,as.POSIXct("2012-06-13 13:30:10.008007006",tz="UTC")) - result <- dbRun(conn,"00N") - record <- assert(record,"download scalar nanotimestamp 00N",result,as.POSIXct(NA)) + test$add_case( + "download scalar nanotimestamp 2012.06.13T13:30:10.008007006N", + function(conn){ + result <- dbRun(conn, "2012.06.13T13:30:10.008007006N") + test$assert(result, as.POSIXct("2012-06-13 13:30:10.008007006", tz = "UTC")) + } + ) + test$add_case( + "download scalar nanotimestamp 00N", + function(conn){ + result <- dbRun(conn, "00N") + test$assert(result, as.POSIXct(NA)) + } + ) # float - result <- dbRun(conn,"0.0f") - record <- assert(record,"download scalar float 0.0f",result,0) - result <- dbRun(conn,"float('nan')") - record <- assert(record,"download scalar float nan",result,NaN) - result <- dbRun(conn,"float('inf')") - record <- assert(record,"download scalar float inf",result,Inf) - result <- dbRun(conn,"00f") - record <- assert(record,"download scalar float 00f",result,NA_real_) + test$add_case( + "download scalar float 0.0f", + function(conn){ + result <- dbRun(conn, "0.0f") + test$assert(result, 0) + } + ) + test$add_case( + "download scalar float float('nan')", + function(conn){ + result <- dbRun(conn, "float('nan')") + test$assert(result, NaN) + } + ) + test$add_case( + "download pair float 0.0f:float('inf')", + function(conn){ + result <- dbRun(conn, "0.0f:float('inf')") + test$assert(result, c(0, Inf)) + } + ) + test$add_case( + "download scalar float 00f", + function(conn){ + result <- dbRun(conn, "00f") + test$assert(result, NA_real_) + } + ) # double - result <- dbRun(conn,"0.0F") - record <- assert(record,"download scalar double 0.0f",result,0) - result <- dbRun(conn,"pi") - record <- assert(record,"download scalar double 0.0f",result,pi) - result <- dbRun(conn,"00F") - record <- assert(record,"download scalar double 00F",result,NA_real_) + test$add_case( + "download scalar double 0.0F", + function(conn){ + result <- dbRun(conn, "0.0F") + test$assert(result, 0) + } + ) + test$add_case( + "download scalar double pi", + function(conn){ + result <- dbRun(conn, "pi") + test$assert(result, pi) + } + ) + test$add_case( + "download scalar double 00F", + function(conn){ + result <- dbRun(conn, "00F") + test$assert(result, NA_real_) + } + ) # string - result <- dbRun(conn,"'abc!@#中文 123'") - record <- assert(record,"download scalar string 'abc!@#中文 123'",result,"abc!@#中文 123") - result <- dbRun(conn,"\"\"") - record <- assert(record,"download scalar string \"\"",result,NA_character_) - - printTestResult(record) - conn <- dbClose(conn) -} else { - stop("connect error in TestDownloadScalar.R") + test$add_case( + "download scalar string 'abc!@#中文 123'", + function(conn){ + result <- dbRun(conn, "'abc!@#中文 123'") + test$assert(result, "abc!@#中文 123") + } + ) + test$add_case( + "download scalar string \"\"", + function(conn){ + result <- dbRun(conn, "\"\"") + test$assert(result, NA_character_) + } + ) } \ No newline at end of file diff --git a/test/TestRunSet.R b/test/TestRunSet.R index e14da57..02e878c 100644 --- a/test/TestRunSet.R +++ b/test/TestRunSet.R @@ -1,109 +1,278 @@ -source("setup/Settings.R") -source("pub/Assert.R") +if (!exists("TESTRUNSET_R__", envir = .GlobalEnv)){ + TESTRUNSET_R__ <- NULL + source("pub/Test.R") -library(RDolphinDB) + library(RDolphinDB) -conn <- dbConnect(DolphinDB(),HOST,PORT,USER,PASSWD) -if (conn@connected){ - record <- c(0L,0L) # char - result <- dbRun(conn,"set([0c,127c,00c])") - record <- assert(record,"download set char set([0c,127c,00c])",result,c(0L,127L,NA_integer_),mode="in") - result <- dbRun(conn,"set(array(CHAR))") - record <- assert(record,"download set char empty",result,integer(0)) + test$add_case( + "download set char set([0c,127c,00c])", + function(conn){ + result <- dbRun(conn, "set([0c,127c,00c])") + test$assert(result, c(0L, 127L, NA_integer_), mode = "in") + } + ) + test$add_case( + "download set char set(array(CHAR))", + function(conn){ + result <- dbRun(conn, "set(array(CHAR))") + test$assert(result, integer(0)) + } + ) # short - result <- dbRun(conn,"set([0h,32767h,00h])") - record <- assert(record,"download set short set([0h,32767h,00h])",result,c(0L,32767L,NA_integer_),mode="in") - result <- dbRun(conn,"set(array(SHORT))") - record <- assert(record,"download set short empty",result,integer(0)) + test$add_case( + "download set short set([0h,32767h,00h])", + function(conn){ + result <- dbRun(conn, "set([0h,32767h,00h])") + test$assert(result, c(0L, 32767L, NA_integer_), mode = "in") + } + ) + test$add_case( + "download set short set(array(SHORT))", + function(conn){ + result <- dbRun(conn, "set(array(SHORT))") + test$assert(result, integer(0)) + } + ) # int - result <- dbRun(conn,"set([0i,2147483647i,00i])") - record <- assert(record,"download set int set([0i,2147483647i,00i])",result,c(0L,2147483647L,NA_integer_),mode="in") - result <- dbRun(conn,"set(array(INT))") - record <- assert(record,"download set int empty",result,integer(0)) + test$add_case( + "download set int set([0i,2147483647i,00i])", + function(conn){ + result <- dbRun(conn, "set([0i,2147483647i,00i])") + test$assert(result, c(0L, 2147483647L, NA_integer_), mode = "in") + } + ) + test$add_case( + "download set int set(array(INT))", + function(conn){ + result <- dbRun(conn, "set(array(INT))") + test$assert(result, integer(0)) + } + ) # long - result <- dbRun(conn,"set([0l,9223372036854775807l,00l])") - record <- assert(record,"download set long set([0l,9223372036854775807l,00l])",result,c(0L,9223372036854775807,NA_real_),mode="in") - result <- dbRun(conn,"set(array(LONG))") - record <- assert(record,"download set long empty",result,numeric(0)) + test$add_case( + "download set long set([0l,9223372036854775807l,00l])", + function(conn){ + result <- dbRun(conn, "set([0l,9223372036854775807l,00l])") + test$assert(result, c(0L, 9223372036854775807, NA_real_), mode = "in") + } + ) + test$add_case( + "download set long set(array(LONG))", + function(conn){ + result <- dbRun(conn, "set(array(LONG))") + test$assert(result, numeric(0)) + } + ) # date - result <- dbRun(conn,"set([1970.01.01d,00d])") - record <- assert(record,"download set date set([1970.01.01d,00d])",result,c(as.Date("1970-01-01"),NA),mode="in") - result <- dbRun(conn,"set(array(DATE))") - record <- assert(record,"download set date empty",result,as.Date(c())) + test$add_case( + "download set date set([1970.01.01d,00d])", + function(conn){ + result <- dbRun(conn, "set([1970.01.01d,00d])") + test$assert(result, c(as.Date("1970-01-01"), NA), mode = "in") + } + ) + test$add_case( + "download set date set(array(DATE))", + function(conn){ + result <- dbRun(conn, "set(array(DATE))") + test$assert(result, as.Date(c())) + } + ) # month - result <- dbRun(conn,"set([1970.01M,00M])") - record <- assert(record,"download set month set([1970.01M,00M])",result,c(as.Date("1970-01-01"),NA),mode="in") - result <- dbRun(conn,"set(array(MONTH))") - record <- assert(record,"download set month empty",result,as.Date(c())) + test$add_case( + "download set month set([1970.01M,00M])", + function(conn){ + result <- dbRun(conn, "set([1970.01M,00M])") + test$assert(result, c(as.Date("1970-01-01"), NA), mode = "in") + } + ) + test$add_case( + "download set month set(array(MONTH))", + function(conn){ + result <- dbRun(conn, "set(array(MONTH))") + test$assert(result, as.Date(c())) + } + ) # time - result <- dbRun(conn,"set([13:30:10.008t,00t])") - record <- assert(record,"download set time set([13:30:10.008t,00t])",result,c(as.POSIXct("1970-01-01 13:30:10.008",tz="UTC"),as.POSIXct(NA,tz="UTC")),mode="in") - result <- dbRun(conn,"set(array(TIME))") - record <- assert(record,"download set time empty",result,as.POSIXct(c(),tz="UTC")) + test$add_case( + "download set time set([13:30:10.008t,00t])", + function(conn){ + result <- dbRun(conn, "set([13:30:10.008t,00t])") + test$assert(result, c(as.POSIXct("1970-01-01 13:30:10.008", tz = "UTC"), as.POSIXct(NA, tz = "UTC")), mode = "in") + } + ) + test$add_case( + "download set time set(array(TIME))", + function(conn){ + result <- dbRun(conn, "set(array(TIME))") + test$assert(result, as.POSIXct(c(), tz = "UTC")) + } + ) # minute - result <- dbRun(conn,"set([13:30m,00m])") - record <- assert(record,"download set minute set([13:30m,00m])",result,c(as.POSIXct("1970-01-01 13:30:00",tz="UTC"),as.POSIXct(NA,tz="UTC")),mode="in") - result <- dbRun(conn,"set(array(MINUTE))") - record <- assert(record,"download set minute empty",result,as.POSIXct(c(),tz="UTC")) + test$add_case( + "download set minute set([13:30m,00m])", + function(conn){ + result <- dbRun(conn, "set([13:30m,00m])") + test$assert(result, c(as.POSIXct("1970-01-01 13:30:00", tz = "UTC"), as.POSIXct(NA, tz = "UTC")), mode = "in") + } + ) + test$add_case( + "download set minute set(array(MINUTE))", + function(conn){ + result <- dbRun(conn, "set(array(MINUTE))") + test$assert(result, as.POSIXct(c(), tz = "UTC")) + } + ) # second - result <- dbRun(conn,"set([13:30:10s,00s])") - record <- assert(record,"download set second set([13:30:10s,00s])",result,c(as.POSIXct("1970-01-01 13:30:10",tz="UTC"),as.POSIXct(NA,tz="UTC")),mode="in") - result <- dbRun(conn,"set(array(MINUTE))") - record <- assert(record,"download set second empty",result,as.POSIXct(c(),tz="UTC")) + test$add_case( + "download set second set([13:30:10s,00s])", + function(conn){ + result <- dbRun(conn, "set([13:30:10s,00s])") + test$assert(result, c(as.POSIXct("1970-01-01 13:30:10", tz = "UTC"), as.POSIXct(NA, tz = "UTC")), mode = "in") + } + ) + test$add_case( + "download set second set(array(SECOND))", + function(conn){ + result <- dbRun(conn, "set(array(SECOND))") + test$assert(result, as.POSIXct(c(), tz = "UTC")) + } + ) # datetime - result <- dbRun(conn,"set([2012.06.13T13:30:10D,00D])") - record <- assert(record,"download set datetime set([2012.06.13T13:30:10D,00D])",result,c(as.POSIXct("2012-06-13 13:30:10",tz="UTC"),as.POSIXct(NA,tz="UTC")),mode="in") - result <- dbRun(conn,"set(array(DATETIME))") - record <- assert(record,"download set datetime empty",result,as.POSIXct(c(),tz="UTC")) + test$add_case( + "download set datetime set([2012.06.13T13:30:10D,00D])", + function(conn){ + result <- dbRun(conn, "set([2012.06.13T13:30:10D,00D])") + test$assert(result, c(as.POSIXct("2012-06-13 13:30:10", tz = "UTC"), as.POSIXct(NA, tz = "UTC")), mode = "in") + } + ) + test$add_case( + "download set datetime set(array(DATETIME))", + function(conn){ + result <- dbRun(conn, "set(array(DATETIME))") + test$assert(result, as.POSIXct(c(), tz = "UTC")) + } + ) # timestamp - result <- dbRun(conn,"set([2012.06.13T13:30:10.008T,00T])") - record <- assert(record,"download set timestamp set([2012.06.13T13:30:10.008T,00T])",result,c(as.POSIXct("2012-06-13 13:30:10.008",tz="UTC"),as.POSIXct(NA,tz="UTC")),mode="in") - result <- dbRun(conn,"set(array(TIMESTAMP))") - record <- assert(record,"download set timestamp empty",result,as.POSIXct(c(),tz="UTC")) + test$add_case( + "download set timestamp set([2012.06.13T13:30:10.008T,00T])", + function(conn){ + result <- dbRun(conn, "set([2012.06.13T13:30:10.008T,00T])") + test$assert(result, c(as.POSIXct("2012-06-13 13:30:10.008", tz = "UTC"), as.POSIXct(NA, tz = "UTC")), mode = "in") + } + ) + test$add_case( + "download set timestamp set(array(TIMESTAMP))", + function(conn){ + result <- dbRun(conn, "set(array(TIMESTAMP))") + test$assert(result, as.POSIXct(c(), tz = "UTC")) + } + ) # nanotime - result <- dbRun(conn,"set([13:30:10.008007006n,00n])") - record <- assert(record,"download set nanotime set([13:30:10.008007006n,00n])",result,c(as.POSIXct("1970-01-01 13:30:10.008007006",tz="UTC"),as.POSIXct(NA,tz="UTC")),mode="in") - result <- dbRun(conn,"set(array(NANOTIME))") - record <- assert(record,"download set nanotime empty",result,as.POSIXct(c(),tz="UTC")) + test$add_case( + "download set nanotime set([13:30:10.008007006n,00n])", + function(conn){ + result <- dbRun(conn, "set([13:30:10.008007006n,00n])") + test$assert(result, c(as.POSIXct("1970-01-01 13:30:10.008007006", tz = "UTC"), as.POSIXct(NA, tz = "UTC")), mode = "in") + } + ) + test$add_case( + "download set nanotime set(array(NANOTIME))", + function(conn){ + result <- dbRun(conn, "set(array(NANOTIME))") + test$assert(result, as.POSIXct(c(), tz = "UTC")) + } + ) # nanotimestamp - result <- dbRun(conn,"set([2012.06.13T13:30:10.008007006N,00N])") - record <- assert(record,"download set nanotimestamp set([2012.06.13T13:30:10.008007006N,00N])",result,c(as.POSIXct("2012-06-13 13:30:10.008007006",tz="UTC"),as.POSIXct(NA,tz="UTC")),mode="in") - result <- dbRun(conn,"set(array(NANOTIMESTAMP))") - record <- assert(record,"download set nanotimestamp empty",result,as.POSIXct(c(),tz="UTC")) + test$add_case( + "download set nanotimestamp set([2012.06.13T13:30:10.008007006N,00N])", + function(conn){ + result <- dbRun(conn, "set([2012.06.13T13:30:10.008007006N,00N])") + test$assert(result, c(as.POSIXct(NA, tz = "UTC"), as.POSIXct("2012-06-13 13:30:10.008007006", tz = "UTC"))) + } + ) + test$add_case( + "download set nanotimestamp set(array(NANOTIMESTAMP))", + function(conn){ + result <- dbRun(conn, "set(array(NANOTIMESTAMP))") + test$assert(result, as.POSIXct(c(), tz = "UTC")) + } + ) # float - result <- dbRun(conn,"set([0.0f,float('nan'),float('inf'),00f])") - record <- assert(record,"download set float set([0.0f,float('nan'),float('inf'),00f])",result,c(0,NaN,Inf,NA_real_),mode="in") - result <- dbRun(conn,"set(array(FLOAT))") - record <- assert(record,"download set float empty",result,numeric(0)) + test$add_case( + "download set float set([0.0f,float('nan'),float('inf'),00f])", + function(conn){ + result <- dbRun(conn, "set([0.0f,float('nan'),float('inf'),00f])") + test$assert(result, c(0, NaN, Inf, NA_real_), mode = "in") + } + ) + test$add_case( + "download set float set(array(FLOAT))", + function(conn){ + result <- dbRun(conn, "set(array(FLOAT))") + test$assert(result, numeric(0)) + } + ) # double - result <- dbRun(conn,"set([0.0F,00F])") - record <- assert(record,"download set double set([0.0F,00F])",result,c(0,NA_real_),mode="in") - result <- dbRun(conn,"set(array(DOUBLE))") - record <- assert(record,"download set double empty",result,numeric(0)) + test$add_case( + "download set double set([0.0F,00F])", + function(conn){ + result <- dbRun(conn, "set([0.0F,00F])") + test$assert(result, c(0, NA_real_), mode = "in") + } + ) + test$add_case( + "download set double set(array(DOUBLE))", + function(conn){ + result <- dbRun(conn, "set(array(DOUBLE))") + test$assert(result, numeric(0)) + } + ) # string - result <- dbRun(conn,"set([`a,\"\"])") - record <- assert(record,"download set string set([`a,\"\"])",result,c("a",NA_character_),mode="in") - result <- dbRun(conn,"set(array(STRING))") - record <- assert(record,"download set string empty",result,character(0)) - - printTestResult(record) - conn <- dbClose(conn) -} else { - stop("connect error in TestDownloadSet.R") + test$add_case( + "download set string set([`a,\"\"])", + function(conn){ + result <- dbRun(conn, "set([`a,\"\"])") + test$assert(result, c("a", NA_character_), mode = "in") + } + ) + test$add_case( + "download set string set(array(STRING))", + function(conn){ + result <- dbRun(conn, "set(array(STRING))") + test$assert(result, character(0)) + } + ) + + # symbol + test$add_case( + "download set symbol set(symbol(`a`a`a`a`a`a`a`a`a`a`b))", + function(conn){ + result <- dbRun(conn, "set(symbol(`a`a`a`a`a`a`a`a`a`a`b))") + test$assert(result, c("a", "b"), mode = "in") + } + ) + test$add_case( + "download set symbol set(array(SYMBOL))", + function(conn){ + result <- dbRun(conn, "set(array(SYMBOL))") + test$assert(result, character(0)) + } + ) } \ No newline at end of file diff --git a/test/TestRunTable.R b/test/TestRunTable.R index afdd416..3ccd9e7 100644 --- a/test/TestRunTable.R +++ b/test/TestRunTable.R @@ -1,217 +1,402 @@ -source("setup/Settings.R") -source("pub/Assert.R") +if (!exists("TESTRUNTABLE_R__", envir = .GlobalEnv)){ + TESTRUNTABLE_R__ <- NULL + source("pub/Test.R") -library(RDolphinDB) + library(RDolphinDB) -conn <- dbConnect(DolphinDB(),HOST,PORT,USER,PASSWD) -if (conn@connected){ - record <- c(0L,0L) # bool - result <- dbRun(conn,"table([true,false,00b] as `a)") - expect <- data.frame( - a = c(TRUE,FALSE,NA) + test$add_case( + "download table bool", + function(conn){ + result <- dbRun(conn, "table([true,false,00b] as `a)") + expect <- data.frame( + a = c(TRUE, FALSE, NA) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table bool empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[BOOL])") + expect <- data.frame( + a = logical(0) + ) + test$assert(result, expect) + } ) - record <- assert(record,"download table bool table([true,false,00b] as `a)",result,expect) - result <- dbRun(conn,"table(100:0,[`a],[BOOL])") - expect <- data.frame( - a = logical(0) - ) - record <- assert(record,"download table bool empty",result,expect) # char - result <- dbRun(conn,"table([0c,127c,00c] as `a)") - expect <- data.frame( - a = c(0L,127L,NA_integer_) - ) - record <- assert(record,"download table char table([0c,127c,00c] as `a)",result,expect) - result <- dbRun(conn,"table(100:0,[`a],[CHAR])") - expect <- data.frame( - a = integer(0) + test$add_case( + "download table char", + function(conn){ + result <- dbRun(conn, "table([0h,32767h,00h] as `a)") + expect <- data.frame( + a = c(0L, 32767L, NA_integer_) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table char empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[SHORT])") + expect <- data.frame( + a = integer(0) + ) + test$assert(result, expect) + } ) - record <- assert(record,"download table char empty",result,expect) # short - result <- dbRun(conn,"table([0h,32767h,00h] as `a)") - expect <- data.frame( - a = c(0L,32767L,NA_integer_) - ) - record <- assert(record,"download table short table([0h,32767h,00h] as `a)",result,expect) - result <- dbRun(conn,"table(100:0,[`a],[SHORT])") - expect <- data.frame( - a = integer(0) + test$add_case( + "download table short", + function(conn){ + result <- dbRun(conn, "table([0h,32767h,00h] as `a)") + expect <- data.frame( + a = c(0L, 32767L, NA_integer_) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table short empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[SHORT])") + expect <- data.frame( + a = integer(0) + ) + test$assert(result, expect) + } ) - record <- assert(record,"download table short empty",result,expect) # int - result <- dbRun(conn,"table([0i,2147483647i,00i] as `a)") - expect <- data.frame( - a = c(0L,2147483647L,NA_integer_) + test$add_case( + "download table int", + function(conn){ + result <- dbRun(conn, "table([0i,2147483647i,00i] as `a)") + expect <- data.frame( + a = c(0L, 2147483647L, NA_integer_) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table int empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[INT])") + expect <- data.frame( + a = integer(0) + ) + test$assert(result, expect) + } ) - record <- assert(record,"download table int table([0i,2147483647i,00i] as `a)",result,expect) - result <- dbRun(conn,"table(100:0,[`a],[INT])") - expect <- data.frame( - a = integer(0) - ) - record <- assert(record,"download table int empty",result,expect) # long - result <- dbRun(conn,"table([0l,9223372036854775807l,00l] as `a)") - expect <- data.frame( - a = c(0,9223372036854775807,NA_real_) - ) - record <- assert(record,"download table long table([0l,9223372036854775807l,00l] as `a)",result,expect) - result <- dbRun(conn,"table(100:0,[`a],[LONG])") - expect <- data.frame( - a = numeric(0) + test$add_case( + "download table long", + function(conn){ + result <- dbRun(conn, "table([0l,9223372036854775807l,00l] as `a)") + expect <- data.frame( + a = c(0, 9223372036854775807, NA_real_) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table long empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[LONG])") + expect <- data.frame( + a = numeric(0) + ) + test$assert(result, expect) + } ) - record <- assert(record,"download table long empty",result,expect) # date - result <- dbRun(conn,"table([1970.01.01d,00d] as `a)") - expect <- data.frame( - a = c(as.Date("1970-01-01"),NA) + test$add_case( + "download table date", + function(conn){ + result <- dbRun(conn, "table([1970.01.01d,00d] as `a)") + expect <- data.frame( + a = c(as.Date("1970-01-01"), NA) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table date empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[DATE])") + expect <- data.frame( + a = as.Date(c()) + ) + test$assert(result, expect) + } ) - record <- assert(record,"download table date table([1970.01.01d,00d] as `a)",result,expect) - result <- dbRun(conn,"table(100:0,[`a],[DATE])") - expect <- data.frame( - a = as.Date(c()) - ) - record <- assert(record,"download table date empty",result,expect) # month - result <- dbRun(conn,"table([1970.01M,00M] as `a)") - expect <- data.frame( - a = c(as.Date("1970-01-01"),NA) + test$add_case( + "download table month", + function(conn){ + result <- dbRun(conn, "table([1970.01M,00M] as `a)") + expect <- data.frame( + a = c(as.Date("1970-01-01"), NA) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table month empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[MONTH])") + expect <- data.frame( + a = as.Date(c()) + ) + test$assert(result, expect) + } ) - record <- assert(record,"download table month table([1970.01M,00M] as `a)",result,expect) - # result <- dbRun(conn,"table(100:0,[`a],[MONTH])") - # expect <- data.frame( - # a = as.Date(c()) - # ) - # record <- assert(record,"download table month empty",result,expect) # time - result <- dbRun(conn,"table([13:30:10.008t,00t] as `a)") - expect <- data.frame( - a = c(as.POSIXct("1970-01-01 13:30:10.008",tz="UTC"),as.POSIXct(NA,tz="UTC")) - ) - record <- assert(record,"download table time table([13:30:10.008t,00t] as `a)",result,expect) - result <- dbRun(conn,"table(100:0,[`a],[TIME])") - expect <- data.frame( - a = as.POSIXct(c(),tz="UTC") + test$add_case( + "download table time", + function(conn){ + result <- dbRun(conn, "table([13:30:10.008t,00t] as `a)") + expect <- data.frame( + a = c(as.POSIXct("1970-01-01 13:30:10.008", tz = "UTC"), as.POSIXct(NA, tz = "UTC")) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table time empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[TIME])") + expect <- data.frame( + a = as.POSIXct(c(), tz = "UTC") + ) + test$assert(result, expect) + } ) - record <- assert(record,"download table time empty",result,expect) # minute - result <- dbRun(conn,"table([13:30m,00m] as `a)") - expect <- data.frame( - a = c(as.POSIXct("1970-01-01 13:30:00",tz="UTC"),as.POSIXct(NA,tz="UTC")) + test$add_case( + "download table minute", + function(conn){ + result <- dbRun(conn, "table([13:30m,00m] as `a)") + expect <- data.frame( + a = c(as.POSIXct("1970-01-01 13:30:00", tz = "UTC"), as.POSIXct(NA, tz = "UTC")) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table minute empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[MINUTE])") + expect <- data.frame( + a = as.POSIXct(c(), tz = "UTC") + ) + test$assert(result, expect) + } ) - record <- assert(record,"download table minute table([13:30m,00m] as `a)",result,expect) - result <- dbRun(conn,"table(100:0,[`a],[MINUTE])") - expect <- data.frame( - a = as.POSIXct(c(),tz="UTC") - ) - record <- assert(record,"download table minute empty",result,expect) # second - result <- dbRun(conn,"table([13:30:10s,00s] as `a)") - expect <- data.frame( - a = c(as.POSIXct("1970-01-01 13:30:10",tz="UTC"),as.POSIXct(NA,tz="UTC")) - ) - record <- assert(record,"download table second table([13:30:10s,00s] as `a)",result,expect) - result <- dbRun(conn,"table(100:0,[`a],[SECOND])") - expect <- data.frame( - a = as.POSIXct(c(),tz="UTC") + test$add_case( + "download table second", + function(conn){ + result <- dbRun(conn, "table([13:30:10s,00s] as `a)") + expect <- data.frame( + a = c(as.POSIXct("1970-01-01 13:30:10", tz = "UTC"), as.POSIXct(NA, tz = "UTC")) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table second empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[SECOND])") + expect <- data.frame( + a = as.POSIXct(c(), tz = "UTC") + ) + test$assert(result, expect) + } ) - record <- assert(record,"download table second empty",result,expect) # datetime - result <- dbRun(conn,"table([2012.06.13T13:30:10D,00D] as `a)") - expect <- data.frame( - a = c(as.POSIXct("2012-06-13 13:30:10",tz="UTC"),as.POSIXct(NA,tz="UTC")) + test$add_case( + "download table datetime", + function(conn){ + result <- dbRun(conn, "table([2012.06.13T13:30:10D,00D] as `a)") + expect <- data.frame( + a = c(as.POSIXct("2012-06-13 13:30:10", tz = "UTC"), as.POSIXct(NA, tz = "UTC")) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table datetime empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[DATETIME])") + expect <- data.frame( + a = as.POSIXct(c(), tz = "UTC") + ) + test$assert(result, expect) + } ) - record <- assert(record,"download table datetime table([2012.06.13T13:30:10D,00D] as `a)",result,expect) - result <- dbRun(conn,"table(100:0,[`a],[DATETIME])") - expect <- data.frame( - a = as.POSIXct(c(),tz="UTC") - ) - record <- assert(record,"download table datetime empty",result,expect) # timestamp - result <- dbRun(conn,"table([2012.06.13T13:30:10.008T,00T] as `a)") - expect <- data.frame( - a = c(as.POSIXct("2012-06-13 13:30:10.008",tz="UTC"),as.POSIXct(NA,tz="UTC")) - ) - record <- assert(record,"download table timestamp table([2012.06.13T13:30:10.008T,00T] as `a)",result,expect) - result <- dbRun(conn,"table(100:0,[`a],[TIMESTAMP])") - expect <- data.frame( - a = as.POSIXct(c(),tz="UTC") + test$add_case( + "download table timestamp", + function(conn){ + result <- dbRun(conn, "table([2012.06.13T13:30:10.008T,00T] as `a)") + expect <- data.frame( + a = c(as.POSIXct("2012-06-13 13:30:10.008", tz = "UTC"), as.POSIXct(NA, tz = "UTC")) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table timestamp empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[TIMESTAMP])") + expect <- data.frame( + a = as.POSIXct(c(), tz = "UTC") + ) + test$assert(result, expect) + } ) - record <- assert(record,"download table timestamp empty",result,expect) # nanotime - result <- dbRun(conn,"table([13:30:10.008007006n,00n] as `a)") - expect <- data.frame( - a = c(as.POSIXct("1970-01-01 13:30:10.008007006",tz="UTC"),as.POSIXct(NA,tz="UTC")) + test$add_case( + "download table nanotime", + function(conn){ + result <- dbRun(conn, "table([13:30:10.008007006n,00n] as `a)") + expect <- data.frame( + a = c(as.POSIXct("1970-01-01 13:30:10.008007006", tz = "UTC"), as.POSIXct(NA, tz = "UTC")) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table nanotime empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[NANOTIME])") + expect <- data.frame( + a = as.POSIXct(c(), tz = "UTC") + ) + test$assert(result, expect) + } ) - record <- assert(record,"download table nanotime table([13:30:10.008007006n,00n] as `a)",result,expect) - result <- dbRun(conn,"table(100:0,[`a],[NANOTIME])") - expect <- data.frame( - a = as.POSIXct(c(),tz="UTC") - ) - record <- assert(record,"download table nanotime empty",result,expect) # nanotimestamp - result <- dbRun(conn,"table([2012.06.13T13:30:10.008007006N,00N] as `a)") - expect <- data.frame( - a = c(as.POSIXct("2012-06-13 13:30:10.008007006",tz="UTC"),as.POSIXct(NA,tz="UTC")) - ) - record <- assert(record,"download table nanotimestamp table([2012.06.13T13:30:10.008007006N,00N] as `a)",result,expect) - result <- dbRun(conn,"table(100:0,[`a],[NANOTIMESTAMP])") - expect <- data.frame( - a = as.POSIXct(c(),tz="UTC") + test$add_case( + "download table nanotimestamp", + function(conn){ + result <- dbRun(conn, "table([2012.06.13T13:30:10.008007006N,00N] as `a)") + expect <- data.frame( + a = c(as.POSIXct("2012-06-13 13:30:10.008007006", tz = "UTC"), as.POSIXct(NA, tz = "UTC")) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table nanotimestamp empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[NANOTIMESTAMP])") + expect <- data.frame( + a = as.POSIXct(c(), tz = "UTC") + ) + test$assert(result, expect) + } ) - record <- assert(record,"download table nanotimestamp empty",result,expect) # float - result <- dbRun(conn,"table([0.0f,float('nan'),float('inf'),00f] as `a)") - expect <- data.frame( - a = c(0,NaN,Inf,NA_real_) + test$add_case( + "download table float", + function(conn){ + result <- dbRun(conn, "table([0.0f,float('nan'),float('inf'),00f] as `a)") + expect <- data.frame( + a = c(0, NaN, Inf, NA_real_) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table float empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[FLOAT])") + expect <- data.frame( + a = numeric(0) + ) + test$assert(result, expect) + } ) - record <- assert(record,"download table float table([0.0f,float('nan'),float('inf'),00f] as `a)",result,expect) - result <- dbRun(conn,"table(100:0,[`a],[FLOAT])") - expect <- data.frame( - a = numeric(0) - ) - record <- assert(record,"download table float empty",result,expect) # double - result <- dbRun(conn,"table([0.0F,00F] as `a)") - expect <- data.frame( - a = c(0,NA_real_) - ) - record <- assert(record,"download table double table([0.0F,00F] as `a)",result,expect) - result <- dbRun(conn,"table(100:0,[`a],[DOUBLE])") - expect <- data.frame( - a = numeric(0) + test$add_case( + "download table double", + function(conn){ + result <- dbRun(conn, "table([0.0F,00F] as `a)") + expect <- data.frame( + a = c(0, NA_real_) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table double empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[DOUBLE])") + expect <- data.frame( + a = numeric(0) + ) + test$assert(result, expect) + } ) - record <- assert(record,"download table double empty",result,expect) # string - result <- dbRun(conn,"table([`a,\"\"] as `a)") - expect <- data.frame( - a = c("a",NA_character_) + test$add_case( + "download table string", + function(conn){ + result <- dbRun(conn, "table([`a,\"\"] as `a)") + expect <- data.frame( + a = c("a", NA_character_) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table string empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[STRING])") + expect <- data.frame( + a = character(0) + ) + test$assert(result, expect) + } ) - record <- assert(record,"download table string table([`a,\"\"] as `a)",result,expect) - result <- dbRun(conn,"table(100:0,[`a],[STRING])") - expect <- data.frame( - a = character(0) - ) - record <- assert(record,"download table string empty",result,expect) - printTestResult(record) - conn <- dbClose(conn) -} else { - stop("connect error in TestDownloadTable.R") + # symbol + test$add_case( + "download table symbol", + function(conn){ + result <- dbRun(conn, "table(symbol([`a,`a,`a,`a,`a,`a,`a,`a,`a,`a,\"\"]) as `a)") + expect <- data.frame( + a = factor(c("a", "a", "a", "a", "a", "a", "a", "a", "a", "a", NA_character_), levels = c(NA_character_, "a"), exclude = NULL) + ) + test$assert(result, expect) + } + ) + test$add_case( + "download table symbol empty", + function(conn){ + result <- dbRun(conn, "table(100:0,[`a],[SYMBOL])") + expect <- data.frame( + a = character(0) + ) + test$assert(result, expect) + } + ) } \ No newline at end of file diff --git a/test/TestRunVector.R b/test/TestRunVector.R index 99093d6..3e31c59 100644 --- a/test/TestRunVector.R +++ b/test/TestRunVector.R @@ -1,127 +1,384 @@ -source("setup/Settings.R") -source("pub/Assert.R") +if (!exists("TESTRUNVECTOR_R__", envir = .GlobalEnv)){ + TESTRUNVECTOR_R__ <- NULL + source("pub/Test.R") -library(RDolphinDB) + library(RDolphinDB) -conn <- dbConnect(DolphinDB(),HOST,PORT,USER,PASSWD) -if (conn@connected){ - record <- c(0L,0L) # bool - result <- dbRun(conn,"[true,false,00b]") - record <- assert(record,"download vector bool [true,false,00b]",result,c(TRUE,FALSE,NA)) - result <- dbRun(conn,"array(BOOL)") - record <- assert(record,"download vector bool empty",result,logical(0)) + test$add_case( + "download vector bool [true,false,00b]", + function(conn){ + result <- dbRun(conn, "[true,false,00b]") + test$assert(result, c(TRUE, FALSE, NA)) + } + ) + test$add_case( + "download vector bool array(BOOL)", + function(conn){ + result <- dbRun(conn, "array(BOOL)") + test$assert(result, logical(0)) + } + ) # char - result <- dbRun(conn,"[0c,127c,00c]") - record <- assert(record,"download vector char [0c,127c,00c]",result,c(0L,127L,NA_integer_)) - result <- dbRun(conn,"array(CHAR)") - record <- assert(record,"download vector char empty",result,integer(0)) + test$add_case( + "download vector char [0c,127c,00c]", + function(conn){ + result <- dbRun(conn, "[0c,127c,00c]") + test$assert(result, c(0L, 127L, NA_integer_)) + } + ) + test$add_case( + "download vector char array(CHAR)", + function(conn){ + result <- dbRun(conn, "array(CHAR)") + test$assert(result, integer(0)) + } + ) # short - result <- dbRun(conn,"[0h,32767h,00h]") - record <- assert(record,"download vector short [0h,32767h,00h]",result,c(0L,32767L,NA_integer_)) - result <- dbRun(conn,"array(SHORT)") - record <- assert(record,"download vector short empty",result,integer(0)) + test$add_case( + "download vector short [0h,32767h,00h]", + function(conn){ + result <- dbRun(conn, "[0h,32767h,00h]") + test$assert(result, c(0L, 32767L, NA_integer_)) + } + ) + test$add_case( + "download vector short array(SHORT)", + function(conn){ + result <- dbRun(conn, "array(SHORT)") + test$assert(result, integer(0)) + } + ) # int - result <- dbRun(conn,"[0i,2147483647i,00i]") - record <- assert(record,"download vector int [0i,2147483647i,00i]",result,c(0L,2147483647L,NA_integer_)) - result <- dbRun(conn,"array(INT)") - record <- assert(record,"download vector short empty",result,integer(0)) + test$add_case( + "download vector int [0i,2147483647i,00i]", + function(conn){ + result <- dbRun(conn, "[0i,2147483647i,00i]") + test$assert(result, c(0L, 2147483647L, NA_integer_)) + } + ) + test$add_case( + "download vector int array(INT)", + function(conn){ + result <- dbRun(conn, "array(INT)") + test$assert(result, integer(0)) + } + ) # long - result <- dbRun(conn,"[0l,9223372036854775807l,00l]") - record <- assert(record,"download vector long [0l,9223372036854775807l,00l]",result,c(0,9223372036854775807,NA_real_)) - result <- dbRun(conn,"array(LONG)") - record <- assert(record,"download vector long empty",result,numeric(0)) + test$add_case( + "download vector long [0l,9223372036854775807l,00l]", + function(conn){ + result <- dbRun(conn, "[0l,9223372036854775807l,00l]") + test$assert(result, c(0, 9223372036854775807, NA_real_)) + } + ) + test$add_case( + "download vector long array(LONG)", + function(conn){ + result <- dbRun(conn, "array(LONG)") + test$assert(result, numeric(0)) + } + ) # date - result <- dbRun(conn,"[1970.01.01d,00d]") - record <- assert(record,"download vector date [1970.01.01d,00d]",result,c(as.Date("1970-01-01"),NA)) - result <- dbRun(conn,"array(DATE)") - record <- assert(record,"download vector date empty",result,as.Date(c())) + test$add_case( + "download vector date [1970.01.01d,00d]", + function(conn){ + result <- dbRun(conn, "[1970.01.01d,00d]") + test$assert(result, c(as.Date("1970-01-01"), NA)) + } + ) + test$add_case( + "download vector date array(DATE)", + function(conn){ + result <- dbRun(conn, "array(DATE)") + test$assert(result, as.Date(c())) + } + ) # month - result <- dbRun(conn,"[1970.01M,00M]") - record <- assert(record,"download vector month [1970.01M,00M]",result,c(as.Date("1970-01-01"),NA)) - result <- dbRun(conn,"array(MONTH)") - record <- assert(record,"download vector month empty",result,as.Date(c())) + test$add_case( + "download vector month [1970.01M,00M]", + function(conn){ + result <- dbRun(conn, "[1970.01M,00M]") + test$assert(result, c(as.Date("1970-01-01"), NA)) + } + ) + test$add_case( + "download vector month array(MONTH)", + function(conn){ + result <- dbRun(conn, "array(MONTH)") + test$assert(result, as.Date(c())) + } + ) # time - result <- dbRun(conn,"[13:30:10.008t,00t]") - record <- assert(record,"download vector time [13:30:10.008t,00t]",result,c(as.POSIXct("1970-01-01 13:30:10.008",tz="UTC"),as.POSIXct(NA,tz="UTC"))) - result <- dbRun(conn,"array(TIME)") - record <- assert(record,"download vector time empty",result,as.POSIXct(c(),tz="UTC")) + test$add_case( + "download vector time [13:30:10.008t,00t]", + function(conn){ + result <- dbRun(conn, "[13:30:10.008t,00t]") + test$assert(result, c(as.POSIXct("1970-01-01 13:30:10.008", tz = "UTC"), as.POSIXct(NA, tz = "UTC"))) + } + ) + test$add_case( + "download vector time array(TIME)", + function(conn){ + result <- dbRun(conn, "array(TIME)") + test$assert(result, as.POSIXct(c(), tz = "UTC")) + } + ) # minute - result <- dbRun(conn,"[13:30m,00m]") - record <- assert(record,"download vector minute [13:30m,00m]",result,c(as.POSIXct("1970-01-01 13:30:00",tz="UTC"),as.POSIXct(NA,tz="UTC"))) - result <- dbRun(conn,"array(MINUTE)") - record <- assert(record,"download vector minute empty",result,as.POSIXct(c(),tz="UTC")) + test$add_case( + "download vector minute [13:30m,00m]", + function(conn){ + result <- dbRun(conn, "[13:30m,00m]") + test$assert(result, c(as.POSIXct("1970-01-01 13:30:00", tz = "UTC"), as.POSIXct(NA, tz = "UTC"))) + } + ) + test$add_case( + "download vector minute array(MINUTE)", + function(conn){ + result <- dbRun(conn, "array(MINUTE)") + test$assert(result, as.POSIXct(c(), tz = "UTC")) + } + ) # second - result <- dbRun(conn,"[13:30:10s,00s]") - record <- assert(record,"download vector second [13:30:10s,00s]",result,c(as.POSIXct("1970-01-01 13:30:10",tz="UTC"),as.POSIXct(NA,tz="UTC"))) - result <- dbRun(conn,"array(SECOND)") - record <- assert(record,"download vector second empty",result,as.POSIXct(c(),tz="UTC")) + test$add_case( + "download vector second [13:30:10s,00s]", + function(conn){ + result <- dbRun(conn, "[13:30:10s,00s]") + test$assert(result, c(as.POSIXct("1970-01-01 13:30:10", tz = "UTC"), as.POSIXct(NA, tz = "UTC"))) + } + ) + test$add_case( + "download vector second array(SECOND)", + function(conn){ + result <- dbRun(conn, "array(SECOND)") + test$assert(result, as.POSIXct(c(), tz = "UTC")) + } + ) # datetime - result <- dbRun(conn,"[2012.06.13T13:30:10D,00D]") - record <- assert(record,"download vector datetime [2012.06.13T13:30:10D,00D]",result,c(as.POSIXct("2012-06-13 13:30:10",tz="UTC"),as.POSIXct(NA,tz="UTC"))) - result <- dbRun(conn,"array(DATETIME)") - record <- assert(record,"download vector datetime empty",result,as.POSIXct(c(),tz="UTC")) + test$add_case( + "download vector datetime [2012.06.13T13:30:10D,00D]", + function(conn){ + result <- dbRun(conn, "[2012.06.13T13:30:10D,00D]") + test$assert(result, c(as.POSIXct("2012-06-13 13:30:10", tz = "UTC"), as.POSIXct(NA, tz = "UTC"))) + } + ) + test$add_case( + "download vector datetime array(DATETIME)", + function(conn){ + result <- dbRun(conn, "array(DATETIME)") + test$assert(result, as.POSIXct(c(), tz = "UTC")) + } + ) # timestamp - result <- dbRun(conn,"[2012.06.13T13:30:10.008T,00T]") - record <- assert(record,"download vector timestamp [2012.06.13T13:30:10.008T,00T]",result,c(as.POSIXct("2012-06-13 13:30:10.008",tz="UTC"),as.POSIXct(NA,tz="UTC"))) - result <- dbRun(conn,"array(TIMESTAMP)") - record <- assert(record,"download vector timestamp empty",result,as.POSIXct(c(),tz="UTC")) + test$add_case( + "download vector timestamp [2012.06.13T13:30:10.008T,00T]", + function(conn){ + result <- dbRun(conn, "[2012.06.13T13:30:10.008T,00T]") + test$assert(result, c(as.POSIXct("2012-06-13 13:30:10.008", tz = "UTC"), as.POSIXct(NA, tz = "UTC"))) + } + ) + test$add_case( + "download vector timestamp array(TIMESTAMP)", + function(conn){ + result <- dbRun(conn, "array(TIMESTAMP)") + test$assert(result, as.POSIXct(c(), tz = "UTC")) + } + ) # nanotime - result <- dbRun(conn,"[13:30:10.008007006n,00n]") - record <- assert(record,"download vector nanotime [13:30:10.008007006n,00n]",result,c(as.POSIXct("1970-01-01 13:30:10.008007006",tz="UTC"),as.POSIXct(NA,tz="UTC"))) - result <- dbRun(conn,"array(NANOTIME)") - record <- assert(record,"download vector nanotime empty",result,as.POSIXct(c(),tz="UTC")) + test$add_case( + "download vector nanotime [13:30:10.008007006n,00n]", + function(conn){ + result <- dbRun(conn, "[13:30:10.008007006n,00n]") + test$assert(result, c(as.POSIXct("1970-01-01 13:30:10.008007006", tz = "UTC"), as.POSIXct(NA, tz = "UTC"))) + } + ) + test$add_case( + "download vector nanotime array(NANOTIME)", + function(conn){ + result <- dbRun(conn, "array(NANOTIME)") + test$assert(result, as.POSIXct(c(), tz = "UTC")) + } + ) # nanotimestamp - result <- dbRun(conn,"[2012.06.13T13:30:10.008007006N,00N]") - record <- assert(record,"download vector nanotimestamp [2012.06.13T13:30:10.008007006N,00N]",result,c(as.POSIXct("2012-06-13 13:30:10.008007006",tz="UTC"),as.POSIXct(NA,tz="UTC"))) - result <- dbRun(conn,"array(NANOTIMESTAMP)") - record <- assert(record,"download vector nanotimestamp empty",result,as.POSIXct(c(),tz="UTC")) + test$add_case( + "download vector nanotimestamp [2012.06.13T13:30:10.008007006N,00N]", + function(conn){ + result <- dbRun(conn, "[2012.06.13T13:30:10.008007006N,00N]") + test$assert(result, c(as.POSIXct("2012-06-13 13:30:10.008007006", tz = "UTC"), as.POSIXct(NA, tz = "UTC"))) + } + ) + test$add_case( + "download vector nanotimestamp array(NANOTIMESTAMP)", + function(conn){ + result <- dbRun(conn, "array(NANOTIMESTAMP)") + test$assert(result, as.POSIXct(c(), tz = "UTC")) + } + ) # float - result <- dbRun(conn,"[0.0f,float('nan'),float('inf'),00f]") - record <- assert(record,"download vector float [0.0f,float('nan'),float('inf'),00f]",result,c(0,NaN,Inf,NA_real_)) - result <- dbRun(conn,"array(FLOAT)") - record <- assert(record,"download vector float empty",result,numeric(0)) + test$add_case( + "download vector float [0.0f,float('nan'),float('inf'),00f]", + function(conn){ + result <- dbRun(conn, "[0.0f,float('nan'),float('inf'),00f]") + test$assert(result, c(0, NaN, Inf, NA_real_)) + } + ) + test$add_case( + "download vector float array(FLOAT)", + function(conn){ + result <- dbRun(conn, "array(FLOAT)") + test$assert(result, numeric(0)) + } + ) # double - result <- dbRun(conn,"[0.0F,00F]") - record <- assert(record,"download vector double [0.0F,00F]",result,c(0,NA_real_)) - result <- dbRun(conn,"array(DOUBLE)") - record <- assert(record,"download vector double empty",result,numeric(0)) + test$add_case( + "download vector double [0.0F,00F]", + function(conn){ + result <- dbRun(conn, "[0.0F,00F]") + test$assert(result, c(0, NA_real_)) + } + ) + test$add_case( + "download vector double array(DOUBLE)", + function(conn){ + result <- dbRun(conn, "array(DOUBLE)") + test$assert(result, numeric(0)) + } + ) # string - result <- dbRun(conn,"[`a,\"\"]") - record <- assert(record,"download vector string [`a,\"\"]",result,c("a",NA_character_)) - result <- dbRun(conn,"array(STRING)") - record <- assert(record,"download vector string empty",result,character(0)) + test$add_case( + "download vector string [`a,\"\"]", + function(conn){ + result <- dbRun(conn, "[`a,\"\"]") + test$assert(result, c("a", NA_character_)) + } + ) + test$add_case( + "download vector string array(STRING)", + function(conn){ + result <- dbRun(conn, "array(STRING)") + test$assert(result, character(0)) + } + ) - # symbol not support - # result <- dbRun(conn,"symbol([`a,\"\"])") - # record <- assert(record,"download vector symbol 1",result,c("a","")) + # symbol + test$add_case( + "download vector symbol symbol([`a,`a,`a,`a,`a,`a,`a,`a,`a,`a,\"\"])", + function(conn){ + result <- dbRun(conn, "symbol([`a,`a,`a,`a,`a,`a,`a,`a,`a,`a,\"\"])") + test$assert(result, factor(c("a", "a", "a", "a", "a", "a", "a", "a", "a", "a", NA_character_), levels = c(NA_character_, "a"), exclude = NULL)) + } + ) + test$add_case( + "download vector symbol array(SYMBOL)", + function(conn){ + result <- dbRun(conn, "array(SYMBOL)") + test$assert(result, character(0)) + } + ) # any empty - result <- dbRun(conn,"array(ANY)") - record <- assert(record,"download vector empty 1",result,list()) + test$add_case( + "download vector any empty", + function(conn){ + result <- dbRun(conn, "array(ANY)") + test$assert(result, list()) + } + ) # any vector - result <- dbRun(conn,"x=array(ANY).append!(true).append!(0c).append!(0h).append!(0i);x") - record <- assert(record,"download vector any",result,list(TRUE,0L,0L,0L)) + test$add_case( + "download vector any vector", + function(conn){ + result <- dbRun(conn, "x=array(ANY).append!(true).append!(0c).append!(0h).append!(0i);x") + test$assert(result, list(TRUE, 0L, 0L, 0L)) + } + ) - printTestResult(record) - conn <- dbClose(conn) -} else { - stop("connect error in TestDownloadVector.R") + # vector of pair + test$add_case( + "download vector vector of pair", + function(conn){ + result <- dbRun(conn, "[1:2]") + test$assert(result, list(c(1L, 2L))) + } + ) + + # vector of vector + test$add_case( + "download vector vector of vector", + function(conn){ + result <- dbRun(conn, "[[1,2]]") + test$assert(result, list(c(1L, 2L))) + } + ) + + # vector of symbol + test$add_case( + "download vector vector of symbol", + function(conn){ + result <- dbRun(conn, "x=array(ANY).append!(symbol([`a,`a,`a,`a,`a,`a,`a,`a,`a,`a,\"\"]));x") + test$assert(result, list(factor(c("a", "a", "a", "a", "a", "a", "a", "a", "a", "a", NA_character_), levels = c(NA_character_, "a"), exclude = NULL))) + } + ) + + # vector of matrix + test$add_case( + "download vector vector of matrix", + function(conn){ + result <- dbRun(conn, "x=matrix([true,false,00b],[true,false,00b]);x.rename!(`row1`row2`row3,`col1`col2);[x]") + expect <- matrix(c(TRUE, FALSE, NA, TRUE, FALSE, NA), nrow = 3, byrow = FALSE) + colnames(expect) <- c("col1", "col2") + rownames(expect) <- c("row1", "row2", "row3") + test$assert(result, list(expect)) + } + ) + + # vector of set + test$add_case( + "download vector vector of set", + function(conn){ + result <- dbRun(conn, "[set([1,2])]") + test$assert(result, list(c(2L, 1L))) + } + ) + + # vector of table + test$add_case( + "download vector vector of table", + function(conn){ + result <- dbRun(conn, "[table([1] as `a)]") + expect <- data.frame( + a = c(1) + ) + test$assert(result, list(expect)) + } + ) + + # vector of symbol table + test$add_case( + "download vector vector of symbol table", + function(conn){ + result <- dbRun(conn, "[table(symbol([`a,`a,`a,`a,`a,`a,`a,`a,`a,`a,\"\"]) as `a)]") + expect <- data.frame( + a = factor(c("a", "a", "a", "a", "a", "a", "a", "a", "a", "a", NA_character_), levels = c(NA_character_, "a"), exclude = NULL) + ) + test$assert(result, list(expect)) + } + ) } \ No newline at end of file diff --git a/test/TestUploadMatrix.R b/test/TestUploadMatrix.R index dcef2e1..179bd3f 100644 --- a/test/TestUploadMatrix.R +++ b/test/TestUploadMatrix.R @@ -1,66 +1,58 @@ -source("setup/Settings.R") -source("pub/Assert.R") +if (!exists("TESTUPLOADMATRIX_R__", envir = .GlobalEnv)){ + TESTUPLOADMATRIX_R__ <- NULL + source("pub/Test.R") -library(RDolphinDB) + library(RDolphinDB) -conn <- dbConnect(DolphinDB(),HOST,PORT,USER,PASSWD) -if (conn@connected){ - record <- c(0L,0L) # bool - data <- matrix(c(TRUE,FALSE,NA,TRUE,FALSE,NA),nrow=3,byrow=FALSE) - colnames(data) <- c("col1","col2") - rownames(data) <- c("row1","row2","row3") - stat <- dbUpload(conn,c("x"),list(data)) - record <- assert(record,"upload matrix bool",stat,TRUE) - result <- dbRun(conn," - data=matrix([true false 00b,true false 00b]); - data.rename!(`row1`row2`row3,`col1`col2); - eqObj(data,x) - ") - record <- assert(record,"upload matrix bool 1",result,TRUE) + test$add_case( + "upload matrix bool", + function(conn){ + data <- matrix(c(TRUE, FALSE, NA, TRUE, FALSE, NA), nrow = 3, byrow = FALSE) + colnames(data) <- c("col1", "col2") + rownames(data) <- c("row1", "row2", "row3") + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, " + data=matrix([true false 00b,true false 00b]); + data.rename!(`row1`row2`row3,`col1`col2); + eqObj(data,x) + ") + test$assert(list(state, result), list(TRUE, TRUE)) + } + ) # int - data <- matrix(c(0L,2147483647L,-2147483647L,NA,0L,2147483647L,-2147483647L,NA),nrow=4,byrow=FALSE) - colnames(data) <- c("col1","col2") - rownames(data) <- c("row1","row2","row3","row4") - stat <- dbUpload(conn,c("x"),list(data)) - record <- assert(record,"upload matrix int",stat,TRUE) - result <- dbRun(conn," - data=matrix([0i 2147483647i -2147483647i 00i,0i 2147483647i -2147483647i 00i]); - data.rename!(`row1`row2`row3`row4,`col1`col2); - eqObj(data,x) - ") - record <- assert(record,"upload matrix int 1",result,TRUE) + test$add_case( + "upload matrix int", + function(conn){ + data <- matrix(c(0L, 2147483647L, -2147483647L, NA, 0L, 2147483647L, -2147483647L, NA), nrow = 4, byrow = FALSE) + colnames(data) <- c("col1", "col2") + rownames(data) <- c("row1", "row2", "row3", "row4") + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, " + data=matrix([0i 2147483647i -2147483647i 00i,0i 2147483647i -2147483647i 00i]); + data.rename!(`row1`row2`row3`row4,`col1`col2); + eqObj(data,x) + ") + test$assert(list(state, result), list(TRUE, TRUE)) + } + ) # double - data <- matrix(c(0,3.14,-3.14,NA,0,3.14,-3.14,NA),nrow=4,byrow=FALSE) - colnames(data) <- c("col1","col2") - rownames(data) <- c("row1","row2","row3","row4") - stat <- dbUpload(conn,c("x"),list(data)) - record <- assert(record,"upload matrix double",stat,TRUE) - result <- dbRun(conn," - data=matrix([0F 3.14F -3.14F 00F,0F 3.14F -3.14F 00F]); - data.rename!(`row1`row2`row3`row4,`col1`col2); - eqObj(data,x) - ") - record <- assert(record,"upload matrix double 1",result,TRUE) - - # string - # not support - # data <- matrix(c("abc!@#中文 123","",NA,"abc!@#中文 123","",NA),nrow=3,byrow=FALSE) - # colnames(data) <- c("col1","col2") - # rownames(data) <- c("row1","row2","row3") - # stat <- dbUpload(conn,c("x"),list(data)) - # record <- assert(record,"upload matrix string",stat,TRUE) - # result <- dbRun(conn," - # data=matrix([\"abc!@#中文 123\" \"\" \"\",\"abc!@#中文 123\" \"\" \"\"]); - # data.rename!(`row1`row2`row3,`col1`col2); - # eqObj(data,x) - # ") - # record <- assert(record,"upload matrix string 1",result,TRUE) - - printTestResult(record) - conn <- dbClose(conn) -} else { - stop("connect error in TestUploadMatrix.R") + test$add_case( + "upload matrix double", + function(conn){ + data <- matrix(c(0, 3.14, -3.14, NA, 0, 3.14, -3.14, NA), nrow = 4, byrow = FALSE) + colnames(data) <- c("col1", "col2") + rownames(data) <- c("row1", "row2", "row3", "row4") + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, " + data=matrix([0F 3.14F -3.14F 00F,0F 3.14F -3.14F 00F]); + data.rename!(`row1`row2`row3`row4,`col1`col2); + eqObj(data,x) + ") + test$assert(list(state, result), list(TRUE, TRUE)) + } + ) + # not support string } \ No newline at end of file diff --git a/test/TestUploadScalar.R b/test/TestUploadScalar.R index f238527..0be0d36 100644 --- a/test/TestUploadScalar.R +++ b/test/TestUploadScalar.R @@ -1,69 +1,83 @@ -source("setup/Settings.R") -source("pub/Assert.R") +if (!exists("TESTUPLOADSCALAR_R__", envir = .GlobalEnv)){ + TESTUPLOADSCALAR_R__ <- NULL + source("pub/Test.R") -library(RDolphinDB) + library(RDolphinDB) -conn <- dbConnect(DolphinDB(),HOST,PORT,USER,PASSWD) -if (conn@connected){ - record <- c(0L,0L) # void - stat <- dbUpload(conn,c("x1"),list(NA)) - record <- assert(record,"upload scalar void",stat,TRUE) - result1 <- dbRun(conn,"eqObj(x1,NULL)") - record <- assert(record,"upload scalar void NA",result1,TRUE) + test$add_case( + "upload scalar void", + function(conn){ + state <- dbUpload(conn, "x1", list(NA)) + result1 <- dbRun(conn, "eqObj(x1,NULL)") + test$assert(list(state, result1), list(TRUE, TRUE)) + } + ) # bool - stat <- dbUpload(conn,c("x1","x2"),list(TRUE,FALSE)) - record <- assert(record,"upload scalar bool",stat,TRUE) - result1 <- dbRun(conn,"eqObj(x1,true)") - record <- assert(record,"upload scalar bool true",result1,TRUE) - result2 <- dbRun(conn,"eqObj(x2,false)") - record <- assert(record,"upload scalar bool false",result2,TRUE) + test$add_case( + "upload scalar bool", + function(conn){ + state <- dbUpload(conn, c("x1", "x2"), list(TRUE, FALSE)) + result1 <- dbRun(conn, "eqObj(x1,true)") + result2 <- dbRun(conn, "eqObj(x2,false)") + test$assert(list(state, result1, result2), list(TRUE, TRUE, TRUE)) + } + ) # int - stat <- dbUpload(conn,c("x1","x2","x3"),list(0L,2147483647L,-2147483647L)) - record <- assert(record,"upload scalar int",stat,TRUE) - result1 <- dbRun(conn,"eqObj(x1,0i)") - record <- assert(record,"upload scalar int 0L",result1,TRUE) - result2 <- dbRun(conn,"eqObj(x2,2147483647i)") - record <- assert(record,"upload scalar int 2147483647L",result2,TRUE) - result3 <- dbRun(conn,"eqObj(x3,-2147483647i)") - record <- assert(record,"upload scalar int -2147483647L",result3,TRUE) + test$add_case( + "upload scalar int", + function(conn){ + state <- dbUpload(conn, c("x1", "x2", "x3"), list(0L, 2147483647L, -2147483647L)) + result1 <- dbRun(conn, "eqObj(x1,0i)") + result2 <- dbRun(conn, "eqObj(x2,2147483647i)") + result3 <- dbRun(conn, "eqObj(x3,-2147483647i)") + test$assert(list(state, result1, result2, result3), list(TRUE, TRUE, TRUE, TRUE)) + } + ) # date - stat <- dbUpload(conn,c("x1","x2"),list(as.Date("1970-01-01"),as.Date(NA))) - record <- assert(record,"upload scalar date",stat,TRUE) - result1 <- dbRun(conn,"eqObj(x1,1970.01.01d)") - record <- assert(record,"upload scalar date as.Date(\"1970-01-01\")",result1,TRUE) - result2 <- dbRun(conn,"eqObj(x2,00d)") - record <- assert(record,"upload scalar date as.Date(NA)",result2,TRUE) + test$add_case( + "upload scalar date", + function(conn){ + state <- dbUpload(conn, c("x1", "x2"), list(as.Date("1970-01-01"), as.Date(NA))) + result1 <- dbRun(conn, "eqObj(x1,1970.01.01d)") + result2 <- dbRun(conn, "eqObj(x2,00d)") + test$assert(list(state, result1, result2), list(TRUE, TRUE, TRUE)) + } + ) # datetime - stat <- dbUpload(conn,c("x1","x2"),list(as.POSIXct("1970-01-01 13:30:10",tz="UTC"),as.POSIXct(NA,tz="UTC"))) - record <- assert(record,"upload scalar time",stat,TRUE) - result1 <- dbRun(conn,"eqObj(x1,1970.01.01T13:30:10D)") - record <- assert(record,"upload scalar datetime as.POSIXct(\"1970-01-01 13:30:10\",tz=\"UTC\")",result1,TRUE) - result2 <- dbRun(conn,"eqObj(x2,00D)") - record <- assert(record,"upload scalar datetime as.POSIXct(NA,tz=\"UTC\")",result2,TRUE) + test$add_case( + "upload scalar datetime", + function(conn){ + state <- dbUpload(conn, c("x1", "x2"), list(as.POSIXct("1970-01-01 13:30:10", tz = "UTC"), as.POSIXct(NA, tz = "UTC"))) + result1 <- dbRun(conn, "eqObj(x1,1970.01.01T13:30:10D)") + result2 <- dbRun(conn, "eqObj(x2,00D)") + test$assert(list(state, result1, result2), list(TRUE, TRUE, TRUE)) + } + ) # double - stat <- dbUpload(conn,c("x1","x2"),list(0,NaN)) - record <- assert(record,"upload scalar double",stat,TRUE) - result1 <- dbRun(conn,"eqObj(x1,0F)") - record <- assert(record,"upload scalar double 0",result1,TRUE) - result2 <- dbRun(conn,"eqObj(x2,00F)") - record <- assert(record,"upload scalar double NaN",result2,TRUE) + test$add_case( + "upload scalar double", + function(conn){ + state <- dbUpload(conn, c("x1", "x2"), list(0, NaN)) + result1 <- dbRun(conn, "eqObj(x1,0F)") + result2 <- dbRun(conn, "eqObj(x2,00F)") + test$assert(list(state, result1, result2), list(TRUE, TRUE, TRUE)) + } + ) # string - stat <- dbUpload(conn,c("x1","x2"),list("abc!@#中文 123","")) - record <- assert(record,"upload scalar string",stat,TRUE) - result1 <- dbRun(conn,"eqObj(x1,\"abc!@#中文 123\")") - record <- assert(record,"upload scalar string \"abc!@#中文 123\"",result1,TRUE) - result2 <- dbRun(conn,"eqObj(x2,\"\")") - record <- assert(record,"upload scalar string \"\"",result2,TRUE) - - printTestResult(record) - conn <- dbClose(conn) -} else { - stop("connect error in TestUploadScalar.R") + test$add_case( + "upload scalar string", + function(conn){ + state <- dbUpload(conn, c("x1", "x2"), list("abc!@#中文 123", "")) + result1 <- dbRun(conn, "eqObj(x1,\"abc!@#中文 123\")") + result2 <- dbRun(conn, "eqObj(x2,\"\")") + test$assert(list(state, result1, result2), list(TRUE, TRUE, TRUE)) + } + ) } \ No newline at end of file diff --git a/test/TestUploadTable.R b/test/TestUploadTable.R index 8dac77a..b5d10da 100644 --- a/test/TestUploadTable.R +++ b/test/TestUploadTable.R @@ -1,147 +1,218 @@ -source("setup/Settings.R") -source("pub/Assert.R") +if (!exists("TESTUPLOADTABLE_R__", envir = .GlobalEnv)){ + TESTUPLOADTABLE_R__ <- NULL + source("pub/Test.R") -library(RDolphinDB) + library(RDolphinDB) -conn <- dbConnect(DolphinDB(),HOST,PORT,USER,PASSWD) -if (conn@connected){ - record <- c(0L,0L) # bool - data <- data.frame( - a=c(TRUE,FALSE,NA) + test$add_case( + "upload table bool", + function(conn){ + data <- data.frame( + a = c(TRUE, FALSE, NA) + ) + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, " + data=table([true,false,00b] as `a); + eqObj(data[`a],x[`a]) + ") + test$assert(list(state, result), list(TRUE, TRUE)) + } ) - stat <- dbUpload(conn,c("x"),list(data)) - record <- assert(record,"upload table bool",stat,TRUE) - result1 <- dbRun(conn," - data=table([true,false,00b] as `a); - eqObj(data[`a],x[`a]) - ") - record <- assert(record,"upload table bool 1",result1,TRUE) - data <- data.frame( - a=logical(0) + test$add_case( + "upload table bool empty", + function(conn){ + data <- data.frame( + a = logical(0) + ) + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, " + data=table(array(BOOL) as `a); + eqObj(data[`a],x[`a]) + ") + test$assert(list(state, result), list(TRUE, TRUE)) + } ) - stat <- dbUpload(conn,c("x"),list(data)) - record <- assert(record,"upload table bool",stat,TRUE) - result2 <- dbRun(conn," - data=table(array(BOOL) as `a); - eqObj(data[`a],x[`a]) - ") - record <- assert(record,"upload table bool 2",result2,TRUE) # int - data <- data.frame( - a=c(0L,2147483647L,-2147483647L) + test$add_case( + "upload table int", + function(conn){ + data <- data.frame( + a = c(0L, 2147483647L, -2147483647L) + ) + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, " + data=table([0i,2147483647i,-2147483647i] as `a); + eqObj(data[`a],x[`a]) + ") + test$assert(list(state, result), list(TRUE, TRUE)) + } ) - stat <- dbUpload(conn,c("x"),list(data)) - record <- assert(record,"upload table int",stat,TRUE) - result1 <- dbRun(conn," - data=table([0i,2147483647i,-2147483647i] as `a); - eqObj(data[`a],x[`a]) - ") - record <- assert(record,"upload table int 1",result1,TRUE) - data <- data.frame( - a=integer(0) + test$add_case( + "upload table int empty", + function(conn){ + data <- data.frame( + a = integer(0) + ) + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, " + data=table(array(INT) as `a); + eqObj(data[`a],x[`a]) + ") + test$assert(list(state, result), list(TRUE, TRUE)) + } ) - stat <- dbUpload(conn,c("x"),list(data)) - record <- assert(record,"upload table int",stat,TRUE) - result2 <- dbRun(conn," - data=table(array(INT) as `a); - eqObj(data[`a],x[`a]) - ") - record <- assert(record,"upload table int 2",result2,TRUE) # date - data <- data.frame( - a=c(as.Date("1970-01-01"),as.Date(NA)) + test$add_case( + "upload table date", + function(conn){ + data <- data.frame( + a = c(as.Date("1970-01-01"), as.Date(NA)) + ) + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, " + data=table([1970.01.01d,00d] as `a); + eqObj(data[`a],x[`a]) + ") + test$assert(list(state, result), list(TRUE, TRUE)) + } ) - stat <- dbUpload(conn,c("x"),list(data)) - record <- assert(record,"upload table date",stat,TRUE) - result1 <- dbRun(conn," - data=table([1970.01.01d,00d] as `a); - eqObj(data[`a],x[`a]) - ") - record <- assert(record,"upload table date 1",result1,TRUE) - data <- data.frame( - a=as.Date(c()) + test$add_case( + "upload table date empty", + function(conn){ + data <- data.frame( + a = as.Date(c()) + ) + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, " + data=table(array(DATE) as `a); + eqObj(data[`a],x[`a]) + ") + test$assert(list(state, result), list(TRUE, TRUE)) + } ) - stat <- dbUpload(conn,c("x"),list(data)) - record <- assert(record,"upload table date",stat,TRUE) - result2 <- dbRun(conn," - data=table(array(DATE) as `a); - eqObj(data[`a],x[`a]) - ") - record <- assert(record,"upload table date 2",result2,TRUE) # datetime - data <- data.frame( - a=c(as.POSIXct("1970-01-01 13:30:10",tz="UTC"),as.POSIXct(NA,tz="UTC")) + test$add_case( + "upload table datetime", + function(conn){ + data <- data.frame( + a = c(as.POSIXct("1970-01-01 13:30:10", tz = "UTC"), as.POSIXct(NA, tz = "UTC")) + ) + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, " + data=table([1970.01.01T13:30:10D,00D] as `a); + eqObj(data[`a],x[`a]) + ") + test$assert(list(state, result), list(TRUE, TRUE)) + } ) - stat <- dbUpload(conn,c("x"),list(data)) - record <- assert(record,"upload table datetime",stat,TRUE) - result1 <- dbRun(conn," - data=table([1970.01.01T13:30:10D,00D] as `a); - eqObj(data[`a],x[`a]) - ") - record <- assert(record,"upload table datetime 1",result1,TRUE) - data <- data.frame( - a=as.POSIXct(c()) + test$add_case( + "upload table datetime empty", + function(conn){ + data <- data.frame( + a = as.POSIXct(c()) + ) + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, " + data=table(array(DATETIME) as `a); + eqObj(data[`a],x[`a]) + ") + test$assert(list(state, result), list(TRUE, TRUE)) + } ) - stat <- dbUpload(conn,c("x"),list(data)) - record <- assert(record,"upload table datetime",stat,TRUE) - result2 <- dbRun(conn," - data=table(array(DATETIME) as `a); - eqObj(data[`a],x[`a]) - ") - record <- assert(record,"upload table datetime 2",result2,TRUE) # double - data <- data.frame( - a=c(0,NaN) + test$add_case( + "upload table double", + function(conn){ + data <- data.frame( + a = c(0, NaN) + ) + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, " + data=table([0F,00F] as `a); + eqObj(data[`a],x[`a]) + ") + test$assert(list(state, result), list(TRUE, TRUE)) + } ) - stat <- dbUpload(conn,c("x"),list(data)) - record <- assert(record,"upload table double",stat,TRUE) - result1 <- dbRun(conn," - data=table([0F,00F] as `a); - eqObj(data[`a],x[`a]) - ") - record <- assert(record,"upload table double 1",result1,TRUE) - data <- data.frame( - a=numeric(0) + test$add_case( + "upload table double empty", + function(conn){ + data <- data.frame( + a = numeric(0) + ) + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, " + data=table(array(DOUBLE) as `a); + eqObj(data[`a],x[`a]) + ") + test$assert(list(state, result), list(TRUE, TRUE)) + } ) - stat <- dbUpload(conn,c("x"),list(data)) - record <- assert(record,"upload table double",stat,TRUE) - result2 <- dbRun(conn," - data=table(array(DOUBLE) as `a); - eqObj(data[`a],x[`a]) - ") - record <- assert(record,"upload table double 2",result2,TRUE) # string - data <- data.frame( - a=c("abc!@#中文 123","",NA) + test$add_case( + "upload table string", + function(conn){ + data <- data.frame( + a = c("abc!@#中文 123", "", NA) + ) + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, " + data=table([\"abc!@#中文 123\",\"\",\"\"] as `a); + eqObj(data[`a],x[`a]) + ") + test$assert(list(state, result), list(TRUE, TRUE)) + } ) - stat <- dbUpload(conn,c("x"),list(data)) - record <- assert(record,"upload table string",stat,TRUE) - result1 <- dbRun(conn," - data=table([\"abc!@#中文 123\",\"\",\"\"] as `a); - eqObj(data[`a],x[`a]) - ") - record <- assert(record,"upload table string 1",result1,TRUE) - data <- data.frame( - a=character(0) + test$add_case( + "upload table symbol", + function(conn){ + data <- data.frame( + a = factor(c("a","a","a","a","a","a","a","a","a","a",NA_character_),levels = c(NA_character_,"a"),exclude = NULL) + ) + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, " + data=table(symbol([`a,`a,`a,`a,`a,`a,`a,`a,`a,`a,\"\"]) as `a); + eqObj(data[`a],x[`a]) + ") + test$assert(list(state, result), list(TRUE, TRUE)) + } ) - stat <- dbUpload(conn,c("x"),list(data)) - record <- assert(record,"upload table string",stat,TRUE) - result2 <- dbRun(conn," - data=table(array(STRING) as `a); - eqObj(data[`a],x[`a]) - ") - record <- assert(record,"upload table string 2",result2,TRUE) - printTestResult(record) - conn <- dbClose(conn) -} else { - stop("connect error in TestUploadTable.R") -} -# todo:arrayvector -# todo:arrayvectortable \ No newline at end of file + # symbol + test$add_case( + "upload table symbol", + function(conn){ + data <- data.frame( + a = character(0) + ) + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, " + data=table(array(STRING) as `a); + eqObj(data[`a],x[`a]) + ") + test$assert(list(state, result), list(TRUE, TRUE)) + } + ) + test$add_case( + "upload table symbol empty", + function(conn){ + data <- data.frame( + a = factor() + ) + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, " + data=table(array(SYMBOL) as `a); + eqObj(data[`a],x[`a]) + ") + test$assert(list(state, result), list(TRUE, TRUE)) + } + ) + # todo:arrayvector + # todo:arrayvectortable +} \ No newline at end of file diff --git a/test/TestUploadVector.R b/test/TestUploadVector.R index 6b490c8..591c5a7 100644 --- a/test/TestUploadVector.R +++ b/test/TestUploadVector.R @@ -1,50 +1,87 @@ -source("setup/Settings.R") -source("pub/Assert.R") +if (!exists("TESTUPLOADVECTOR_R__", envir = .GlobalEnv)){ + TESTUPLOADVECTOR_R__ <- NULL + source("pub/Test.R") -library(RDolphinDB) + library(RDolphinDB) -conn <- dbConnect(DolphinDB(),HOST,PORT,USER,PASSWD) -if (conn@connected){ - record <- c(0L,0L) # bool - stat <- dbUpload(conn,c("x"),list(c(TRUE,FALSE,NA))) - record <- assert(record,"upload vector bool",stat,TRUE) - result <- dbRun(conn,"eqObj(x,[true,false,00b])") - record <- assert(record,"upload vector bool c(TRUE,FALSE,NA)",result,TRUE) + test$add_case( + "upload vector bool", + function(conn){ + state <- dbUpload(conn, "x", list(c(TRUE, FALSE, NA))) + result <- dbRun(conn, "eqObj(x,[true,false,00b])") + test$assert(list(state, result), list(TRUE, TRUE)) + } + ) # int - stat <- dbUpload(conn,c("x"),list(c(0L,2147483647L,-2147483647L,NA))) - record <- assert(record,"upload vector int",stat,TRUE) - result <- dbRun(conn,"eqObj(x,[0i,2147483647i,-2147483647i,00i])") - record <- assert(record,"upload vector int 1",result,TRUE) + test$add_case( + "upload vector int", + function(conn){ + state <- dbUpload(conn, "x", list(c(0L, 2147483647L, -2147483647L, NA))) + result <- dbRun(conn, "eqObj(x,[0i,2147483647i,-2147483647i,00i])") + test$assert(list(state, result), list(TRUE, TRUE)) + } + ) # date - stat <- dbUpload(conn,c("x"),list(c(as.Date("1970-01-01"),as.Date(NA)))) - record <- assert(record,"upload vector date",stat,TRUE) - result <- dbRun(conn,"eqObj(x,[1970.01.01d,00d])") - record <- assert(record,"upload vector date 1",result,TRUE) + test$add_case( + "upload vector date", + function(conn){ + state <- dbUpload(conn, "x", list(c(as.Date("1970-01-01"), as.Date(NA)))) + result <- dbRun(conn, "eqObj(x,[1970.01.01d,00d])") + test$assert(list(state, result), list(TRUE, TRUE)) + } + ) # datetime - stat <- dbUpload(conn,c("x"),list(c(as.POSIXct("1970-01-01 13:30:10",tz="UTC"),as.POSIXct(NA,tz="UTC")))) - record <- assert(record,"upload vector time",stat,TRUE) - result <- dbRun(conn,"eqObj(x,[1970.01.01T13:30:10D,00D])") - record <- assert(record,"upload vector time 1",result,TRUE) + test$add_case( + "upload vector datetime", + function(conn){ + state <- dbUpload(conn, "x", list(c(as.POSIXct("1970-01-01 13:30:10", tz = "UTC"), as.POSIXct(NA, tz = "UTC")))) + result <- dbRun(conn, "eqObj(x,[1970.01.01T13:30:10D,00D])") + test$assert(list(state, result), list(TRUE, TRUE)) + } + ) # double - stat <- dbUpload(conn,c("x"),list(c(0,NaN,NA))) - record <- assert(record,"upload vector double",stat,TRUE) - result <- dbRun(conn,"eqObj(x,[0F,00F,00F])") - record <- assert(record,"upload vector double 1",result,TRUE) + test$add_case( + "upload vector double", + function(conn){ + state <- dbUpload(conn, "x", list(c(0, NaN, NA))) + result <- dbRun(conn, "eqObj(x,[0F,00F,00F])") + test$assert(list(state, result), list(TRUE, TRUE)) + } + ) # string - stat <- dbUpload(conn,c("x"),list(c("abc!@#中文 123","",NA))) - record <- assert(record,"upload vector string",stat,TRUE) - result <- dbRun(conn,"eqObj(x,[\"abc!@#中文 123\",\"\",\"\"])") - record <- assert(record,"upload vector string 1",result,TRUE) - - printTestResult(record) - conn <- dbClose(conn) -} else { - stop("connect error in TestUploadVector.R") -} -# 不支持上传空向量,不支持上传list \ No newline at end of file + test$add_case( + "upload vector string", + function(conn){ + state <- dbUpload(conn, "x", list(c("abc!@#中文 123", "", NA))) + result <- dbRun(conn, "eqObj(x,[\"abc!@#中文 123\",\"\",\"\"])") + test$assert(list(state, result), list(TRUE, TRUE)) + } + ) + + # symbol + test$add_case( + "upload vector symbol", + function(conn){ + data <- factor(c("a","a","a","a","a","a","a","a","a","a",NA_character_),levels = c(NA_character_,"a"),exclude = NULL) + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, "eqObj(x,symbol([`a,`a,`a,`a,`a,`a,`a,`a,`a,`a,\"\"]))") + test$assert(list(state, result), list(TRUE, TRUE)) + } + ) + test$add_case( + "upload vector symbol empty", + function(conn){ + data <- factor(c()) + state <- dbUpload(conn, "x", list(data)) + result <- dbRun(conn, "eqObj(x,symbol(array(SYMBOL)))") + test$assert(list(state, result), list(TRUE, TRUE)) + } + ) + # 不支持上传空向量,不支持上传list +} \ No newline at end of file diff --git a/test/pub/Test.R b/test/pub/Test.R new file mode 100644 index 0000000..71a51d5 --- /dev/null +++ b/test/pub/Test.R @@ -0,0 +1,128 @@ +if (!exists("TEST_R__", envir = .GlobalEnv)){ + TEST_R__ <- NULL + source("setup/Settings.R") + + library(RDolphinDB) + library(htmlTable) + + Test <- R6::R6Class( + "Test", + public = list( + initialize = function(host, port, user, password, auto_test){ + private$conn <- dbConnect(DolphinDB(), host, port, user, password) + private$auto_test <- auto_test + }, + finalize = function(){ + private$conn <- dbClose(private$conn) + }, + add_case = function(case_name, case_func){ + private$test_case <- append(private$test_case, list(list(case_name, case_func))) + }, + run = function(){ + for (case in private$test_case){ + case_name <- case[[1]] + tryCatch( + expr = { + case_result <- case[[2]](conn = private$conn) + result <- case_result[[1]] + error_message <- case_result[[2]] + . <- data.frame( + case_name = c(case_name), + result = c(result), + error_message = c(error_message) + ) + private$result <- rbind(private$result, .) + }, + error = function(e){ + result <- FALSE + error_message <- e$message + . <- data.frame( + case_name = c(case_name), + result = c(result), + error_message = c(error_message) + ) + private$result <- rbind(private$result, .) + } + ) + } + }, + assert = function(source, target, mode = "equal"){ + if (!(typeof(source) == typeof(target))){ + return(list(FALSE, paste("typeof error source:", typeof(source)[[1]], "target:", typeof(target)[[1]]))) + } + if (!(class(source)[[1]] == class(target)[[1]])){ + return(list(FALSE, paste("class error source:", class(source)[[1]], "target:", class(target)[[1]]))) + } + if (mode == "equal"){ + . <- all.equal(source, target) + if (inherits(., "logical")){ + return(list(TRUE, "-")) + } else{ + return(list(FALSE, paste(., collapse = "|"))) + } + } else if (mode == "in"){ + . <- all(source %in% target) && all(target %in% source) + if (.){ + return(list(TRUE, "-")) + } else{ + return(list(FALSE, paste("assert false! source:", toString(source), "target:", toString(target)))) + } + } else{ + return(list(FALSE, paste("not support", mode))) + } + }, + testResult = function(){ + case_count <- nrow(private$result) + fail_case_table <- private$result[private$result$result == FALSE,] + fail_case_count <- nrow(fail_case_table) + overview_table <- data.frame( + fail_case_count = c(fail_case_count), + case_count = c(case_count) + ) + if (private$auto_test){ + css <- " + + " + writeLines(paste(css, htmlTable(overview_table), sep = " "), "./overview.html") + writeLines(paste(css, htmlTable(fail_case_table), sep = " "), "./fail_case.html") + } + print("=====overview=====") + print(overview_table) + print("=====fail_case=====") + print(fail_case_table) + } + ), + private = list( + auto_test = FALSE, + test_case = list(), + result = data.frame( + case_name = character(0), + result = logical(0), + error_message = character(0) + ), + conn = NULL + ) + ) + test <- Test$new(HOST, PORT, USER, PASSWD, AUTO_TEST) +} diff --git a/test/setup/Settings.R b/test/setup/Settings.R index 26f5c7c..57dd259 100644 --- a/test/setup/Settings.R +++ b/test/setup/Settings.R @@ -1,4 +1,8 @@ -HOST <- "192.168.0.54" -PORT <- 8848 -USER <- "admin" -PASSWD <- "123456" \ No newline at end of file +if (!exists("SETTINGS_R__", envir = .GlobalEnv)){ + SETTINGS_R__ <- NULL + HOST <- "192.168.0.54" + PORT <- 8848 + USER <- "admin" + PASSWD <- "123456" + AUTO_TEST <- TRUE +}