Skip to content

Commit

Permalink
fix: api server write json
Browse files Browse the repository at this point in the history
  • Loading branch information
vagetablechicken committed Jul 14, 2023
1 parent 7b7a371 commit 1aba6d4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/apiserver/api_server_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -942,12 +942,12 @@ void WriteValue(JsonWriter& ar, std::shared_ptr<hybridse::sdk::ResultSet> rs, in
case hybridse::sdk::kTypeBool: {
bool value = false;
rs->GetBool(i, &value);
ar&(value ? "true" : "false");
ar& value;
break;
}
default: {
LOG(ERROR) << "Invalid Column Type";
ar & "NA";
ar & std::string("NA");
break;
}
}
Expand Down
60 changes: 60 additions & 0 deletions src/apiserver/api_server_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,66 @@ TEST_F(APIServerTest, procedure) {
ASSERT_TRUE(env->cluster_remote->ExecuteDDL(env->db, "drop table trans;", &status));
}

TEST_F(APIServerTest, testResultType) {
// check about json write
const auto env = APIServerTestEnv::Instance();
// don't need table data
std::string ddl =
"create table if not exists multi_type(c1 bool, c2 smallint, c3 int, c4 bigint, c5 float, c6 double, c7 "
"string, c8 date, c9 timestamp, c10_str string);";
hybridse::sdk::Status status;

env->cluster_remote->ExecuteSQL(env->db, ddl, &status);
ASSERT_TRUE(status.IsOK()) << status.msg;
ASSERT_TRUE(env->cluster_sdk->Refresh());
std::string deploy = "deploy d1 select c1, c2, c3, c4, c5, c6, c7, c8, c9, c10_str from multi_type;";
env->cluster_remote->ExecuteSQL(env->db, deploy, &status);
ASSERT_TRUE(status.IsOK()) << status.msg;
ASSERT_TRUE(env->cluster_sdk->Refresh());

{
brpc::Controller cntl;
cntl.http_request().set_method(brpc::HTTP_METHOD_POST);
cntl.http_request().uri() = "http://127.0.0.1:8010/dbs/" + env->db + "/deployments/" + "d1";
cntl.request_attachment().append(R"({
"input":
[
[ true,32767,65535,9223372036854775807,1.1,2.2,"abc","2021-12-01",11111111111111,"def" ],
[ false,32767,65535,null,1.1,2.2,"abc",null,null,"def" ]
]
})"); // null must be lower case
env->http_channel.CallMethod(NULL, &cntl, NULL, NULL, NULL);
ASSERT_FALSE(cntl.Failed()) << cntl.ErrorText();

LOG(INFO) << "exec deployment resp:\n" << cntl.response_attachment().to_string();
butil::rapidjson::Document document;
// check resp data
if (document.Parse(cntl.response_attachment().to_string().c_str()).HasParseError()) {
ASSERT_TRUE(false) << "response parse failed with code " << document.GetParseError()
<< ", raw resp: " << cntl.response_attachment().to_string();
}
ASSERT_EQ(0, document["code"].GetInt());
ASSERT_STREQ("ok", document["msg"].GetString());
ASSERT_TRUE(document["data"].FindMember("schema") == document["data"].MemberEnd());
ASSERT_EQ(2, document["data"]["data"].Size());
ASSERT_EQ(0, document["data"]["common_cols_data"].Size());

// check data.data 2 results
auto& data = document["data"]["data"];
ASSERT_TRUE(data[0].IsArray());
ASSERT_TRUE(data[0][0].GetBool());
ASSERT_EQ(data[0][2].GetInt(), 65535);
ASSERT_STREQ(data[0][7].GetString(), "2021-12-1");

ASSERT_TRUE(data[1].IsArray());
ASSERT_FALSE(data[1][0].GetBool());
ASSERT_TRUE(data[1][3].IsNull());
}

env->cluster_remote->ExecuteSQL(env->db, "drop deployment d1", &status);
ASSERT_TRUE(status.IsOK()) << status.msg;
}

TEST_F(APIServerTest, no_common) {
const auto env = APIServerTestEnv::Instance();

Expand Down

0 comments on commit 1aba6d4

Please sign in to comment.