Skip to content

Commit

Permalink
refacter some tests using test_request and test_reaponse
Browse files Browse the repository at this point in the history
  • Loading branch information
t-horikawa committed Jan 17, 2025
1 parent 6f56630 commit f01c7ef
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 148 deletions.
78 changes: 3 additions & 75 deletions test/tateyama/datastore/datastore_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <gtest/gtest.h>
#include <tateyama/utils/test_utils.h>
#include <tateyama/utils/request_response.h>

namespace tateyama::datastore {

Expand All @@ -42,79 +43,6 @@ class datastore_test :
void TearDown() override {
temporary_.clean();
}

class test_request : public api::server::request {
public:
test_request(
std::size_t session_id,
std::size_t service_id,
std::string_view payload
) :
session_id_(session_id),
service_id_(service_id),
payload_(payload)
{}

[[nodiscard]] std::size_t session_id() const override {
return session_id_;
}

[[nodiscard]] std::size_t service_id() const override {
return service_id_;
}

[[nodiscard]] std::size_t local_id() const override {
return 0;
}

[[nodiscard]] std::string_view payload() const override {
return payload_;
}

tateyama::api::server::database_info const& database_info() const noexcept override {
return database_info_;
}

tateyama::api::server::session_info const& session_info() const noexcept override {
return session_info_;
}

tateyama::api::server::session_store& session_store() noexcept override {
return session_store_;
}

tateyama::session::session_variable_set& session_variable_set() noexcept override {
return session_variable_set_;
}

std::size_t session_id_{};
std::size_t service_id_{};
std::string payload_{};

tateyama::status_info::resource::database_info_impl database_info_{};
tateyama::endpoint::common::session_info_impl session_info_{};
tateyama::api::server::session_store session_store_{};
tateyama::session::session_variable_set session_variable_set_{};
};

class test_response : public api::server::response {
public:
test_response() = default;

void session_id(std::size_t id) override {
session_id_ = id;
};
status body_head(std::string_view body_head) override { return status::ok; }
status body(std::string_view body) override { body_ = body; return status::ok; }
void error(proto::diagnostics::Record const& record) override {}
status acquire_channel(std::string_view name, std::shared_ptr<api::server::data_channel>& ch) override { return status::ok; }
status release_channel(api::server::data_channel& ch) override { return status::ok; }
bool check_cancel() const override { return false; }

std::size_t session_id_{};
std::string body_{};
};

};

using namespace std::string_view_literals;
Expand Down Expand Up @@ -142,8 +70,8 @@ TEST_F(datastore_test, basic) {
str = rq.SerializeAsString();
rq.clear_backup_begin();
}
auto svrreq = std::make_shared<test_request>(10, datastore::service::bridge::tag, str);
auto svrres = std::make_shared<test_response>();
auto svrreq = std::make_shared<tateyama::utils::test_request>(10, datastore::service::bridge::tag, str);
auto svrres = std::make_shared<tateyama::utils::test_response>();

(*router)(svrreq, svrres);
EXPECT_EQ(10, svrres->session_id_);
Expand Down
81 changes: 8 additions & 73 deletions test/tateyama/framework/router_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include <gtest/gtest.h>
#include <tateyama/utils/test_utils.h>
#include <tateyama/utils/request_response.h>

namespace tateyama::framework {

Expand Down Expand Up @@ -67,79 +68,13 @@ class router_test :
bool called_{false};
};

class test_request : public api::server::request {
class test_response_for_the_test : public tateyama::utils::test_response {
public:
test_request(
std::size_t session_id,
std::size_t service_id,
std::string_view payload
) :
session_id_(session_id),
service_id_(service_id),
payload_(payload)
{}

[[nodiscard]] std::size_t session_id() const override {
return session_id_;
}

[[nodiscard]] std::size_t service_id() const override {
return service_id_;
}

[[nodiscard]] std::size_t local_id() const override {
return 0;
}

[[nodiscard]] std::string_view payload() const override {
return payload_;
}

tateyama::api::server::database_info const& database_info() const noexcept override {
return database_info_;
}

tateyama::api::server::session_info const& session_info() const noexcept override {
return session_info_;
}

tateyama::api::server::session_store& session_store() noexcept override {
return session_store_;
}

tateyama::session::session_variable_set& session_variable_set() noexcept override {
return session_variable_set_;
}

std::size_t session_id_{};
std::size_t service_id_{};
std::string payload_{};

tateyama::status_info::resource::database_info_impl database_info_{};
tateyama::endpoint::common::session_info_impl session_info_{};
tateyama::api::server::session_store session_store_{};
tateyama::session::session_variable_set session_variable_set_{};
};

class test_response : public api::server::response {
public:
test_response() = default;

void session_id(std::size_t id) override {
session_id_ = id;
};
status body_head(std::string_view body_head) override { return status::ok; }
status body(std::string_view body) override { body_ = body; return status::ok; }
void error(proto::diagnostics::Record const& record) override {
error_invoked_ = true;
error_record_ = record;
}
status acquire_channel(std::string_view name, std::shared_ptr<api::server::data_channel>& ch) override { return status::ok; }
status release_channel(api::server::data_channel& ch) override { return status::ok; }
bool check_cancel() const override { return false; }

std::size_t session_id_{};
std::string body_{};
bool error_invoked_{};
proto::diagnostics::Record error_record_{};
};
Expand All @@ -166,8 +101,8 @@ TEST_F(router_test, basic) {
ASSERT_TRUE(msg.SerializeToOstream(&ss));
}
auto str = ss.str();
auto svrreq = std::make_shared<test_request>(10, test_service::tag, str);
auto svrres = std::make_shared<test_response>();
auto svrreq = std::make_shared<tateyama::utils::test_request>(10, test_service::tag, str);
auto svrres = std::make_shared<test_response_for_the_test>();

ASSERT_FALSE(svc0->called_);
(*router)(svrreq, svrres);
Expand Down Expand Up @@ -196,8 +131,8 @@ TEST_F(router_test, update_expiration_time) {
ASSERT_TRUE(msg.SerializeToOstream(&ss));
}
auto str = ss.str();
auto svrreq = std::make_shared<test_request>(10, routing_service::tag, str);
auto svrres = std::make_shared<test_response>();
auto svrreq = std::make_shared<tateyama::utils::test_request>(10, routing_service::tag, str);
auto svrres = std::make_shared<test_response_for_the_test>();

ASSERT_FALSE(svc0->called_);
(*router)(svrreq, svrres);
Expand All @@ -221,8 +156,8 @@ TEST_F(router_test, invalid_service_id) {
ASSERT_TRUE(router);
ASSERT_EQ(framework::routing_service::tag, router->id());

auto svrreq = std::make_shared<test_request>(10, 999, ""); // 999 is an invalid service id
auto svrres = std::make_shared<test_response>();
auto svrreq = std::make_shared<tateyama::utils::test_request>(10, 999, ""); // 999 is an invalid service id
auto svrres = std::make_shared<test_response_for_the_test>();

(*router)(svrreq, svrres);

Expand Down

0 comments on commit f01c7ef

Please sign in to comment.