This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hub: complete GetBalance implementation with test
- Loading branch information
Showing
4 changed files
with
152 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#ifndef __HUB_COMMANDS_TESTS_RUNNER_H_ | ||
#define __HUB_COMMANDS_TESTS_RUNNER_H_ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "hub/commands/create_user.h" | ||
#include "hub/db/db.h" | ||
#include "hub/stats/session.h" | ||
#include "proto/hub.pb.h" | ||
|
||
namespace iota { | ||
class CommandTest : public ::testing::Test { | ||
public: | ||
virtual void SetUp() { | ||
auto db = iota::db::DBManager::get(); | ||
db.resetConnection(); | ||
db.loadSchema(true); | ||
|
||
_session = std::make_shared<ClientSession>(); | ||
} | ||
|
||
virtual void TearDown() { _session = nullptr; } | ||
|
||
std::shared_ptr<ClientSession> session() { return _session; } | ||
|
||
grpc::Status createUser(std::shared_ptr<ClientSession> session, | ||
std::string username) { | ||
rpc::CreateUserRequest req; | ||
rpc::CreateUserReply res; | ||
|
||
req.set_userid(std::move(username)); | ||
cmd::CreateUser command(std::move(session)); | ||
return command.doProcess(&req, &res); | ||
} | ||
|
||
rpc::Error errorFromStatus(grpc::Status& status) { | ||
rpc::Error err; | ||
err.ParseFromString(status.error_details()); | ||
|
||
return err; | ||
} | ||
|
||
private: | ||
std::shared_ptr<ClientSession> _session; | ||
}; | ||
|
||
} // namespace iota | ||
#endif /* __HUB_COMMANDS_TESTS_RUNNER_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <gtest/gtest.h> | ||
#include <sqlpp11/select.h> | ||
#include <sqlpp11/functions.h> | ||
|
||
#include "proto/hub.pb.h" | ||
#include "schema/schema.h" | ||
|
||
#include "hub/db/db.h" | ||
#include "hub/stats/session.h" | ||
#include "hub/commands/get_balance.h" | ||
|
||
#include "runner.h" | ||
|
||
using namespace iota; | ||
using namespace sqlpp; | ||
|
||
namespace { | ||
class GetBalanceTest : public CommandTest {}; | ||
|
||
TEST_F(GetBalanceTest, UnknownUserShouldFail) { | ||
rpc::GetBalanceRequest req; | ||
rpc::GetBalanceReply res; | ||
|
||
req.set_userid("User1"); | ||
cmd::GetBalance command(session()); | ||
|
||
auto status = command.doProcess(&req, &res); | ||
|
||
EXPECT_FALSE(status.ok()); | ||
|
||
auto err = errorFromStatus(status); | ||
EXPECT_EQ(err.code(), rpc::ErrorCode::USER_DOES_NOT_EXIST); | ||
} | ||
|
||
TEST_F(GetBalanceTest, NewUserHasZeroBalance) { | ||
rpc::GetBalanceRequest req; | ||
rpc::GetBalanceReply res; | ||
rpc::Error err; | ||
|
||
constexpr auto username = "User1"; | ||
|
||
createUser(session(), username); | ||
|
||
req.set_userid("User1"); | ||
|
||
cmd::GetBalance command(session()); | ||
|
||
EXPECT_TRUE(command.doProcess(&req, &res).ok()); | ||
|
||
EXPECT_EQ(0, res.available()); | ||
} | ||
|
||
}; // namespace |