Simplify Mocks and test

This commit is contained in:
Kostas Kyrimis 2022-12-12 19:15:49 +02:00
parent 2e4e975102
commit f04ed3c137
3 changed files with 73 additions and 63 deletions

View File

@ -2468,7 +2468,7 @@ class DistributedCreateExpandCursor : public Cursor {
std::vector<msgs::NewExpand> ExpandCreationInfoToRequests(MultiFrame &multi_frame, ExecutionContext &context) const {
std::vector<msgs::NewExpand> edge_requests;
auto reader = multi_frame.GetValidFramesConsumer();
auto reader = multi_frame.GetValidFramesModifier();
for (auto &frame : reader) {
const auto &edge_info = self_.edge_info_;

View File

@ -13,51 +13,70 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "query/v2/common.hpp"
#include "query/v2/context.hpp"
#include "query/v2/plan/operator.hpp"
#include "query/v2/request_router.hpp"
namespace memgraph {
class MockedRequestRouter : public query::v2::RequestRouterInterface {
namespace memgraph::query::v2 {
class MockedRequestRouter : public RequestRouterInterface {
public:
MOCK_METHOD1(ScanVertices, std::vector<VertexAccessor>(std::optional<std::string> label));
MOCK_METHOD1(CreateVertices, std::vector<msgs::CreateVerticesResponse>(std::vector<msgs::NewVertex>));
MOCK_METHOD1(ExpandOne, std::vector<msgs::ExpandOneResultRow>(msgs::ExpandOneRequest));
MOCK_METHOD1(CreateExpand, std::vector<msgs::CreateExpandResponse>(std::vector<msgs::NewExpand>));
MOCK_METHOD1(GetProperties, std::vector<msgs::GetPropertiesResultRow>(msgs::GetPropertiesRequest));
MOCK_METHOD0(StartTransaction, void());
MOCK_METHOD0(Commit, void());
MOCK_METHOD(std::vector<VertexAccessor>, ScanVertices, (std::optional<std::string> label));
MOCK_METHOD(std::vector<msgs::CreateVerticesResponse>, CreateVertices, (std::vector<msgs::NewVertex>));
MOCK_METHOD(std::vector<msgs::ExpandOneResultRow>, ExpandOne, (msgs::ExpandOneRequest));
MOCK_METHOD(std::vector<msgs::CreateExpandResponse>, CreateExpand, (std::vector<msgs::NewExpand>));
MOCK_METHOD(std::vector<msgs::GetPropertiesResultRow>, GetProperties, (msgs::GetPropertiesRequest));
MOCK_METHOD(void, StartTransaction, ());
MOCK_METHOD(void, Commit, ());
MOCK_CONST_METHOD1(NameToEdgeType, storage::v3::EdgeTypeId(const std::string &));
MOCK_CONST_METHOD1(NameToProperty, storage::v3::PropertyId(const std::string &));
MOCK_CONST_METHOD1(NameToLabel, storage::v3::LabelId(const std::string &));
MOCK_CONST_METHOD1(LabelToName, storage::v3::LabelId(const std::string &));
MOCK_CONST_METHOD1(PropertyToName, const std::string &(storage::v3::PropertyId));
MOCK_CONST_METHOD1(LabelToName, const std::string &(storage::v3::LabelId label));
MOCK_CONST_METHOD1(EdgeTypeToName, const std::string &(storage::v3::EdgeTypeId type));
MOCK_CONST_METHOD1(MaybeNameToProperty, std::optional<storage::v3::PropertyId>(const std::string &));
MOCK_CONST_METHOD1(MaybeNameToEdgeType, std::optional<storage::v3::EdgeTypeId>(const std::string &));
MOCK_CONST_METHOD1(MaybeNameToLabel, std::optional<storage::v3::LabelId>(const std::string &));
MOCK_CONST_METHOD1(IsPrimaryLabel, bool(storage::v3::LabelId));
MOCK_CONST_METHOD2(IsPrimaryKey, bool(storage::v3::LabelId, storage::v3::PropertyId));
MOCK_METHOD(storage::v3::EdgeTypeId, NameToEdgeType, (const std::string &), (const));
MOCK_METHOD(storage::v3::PropertyId, NameToProperty, (const std::string &), (const));
MOCK_METHOD(storage::v3::LabelId, NameToLabel, (const std::string &), (const));
MOCK_METHOD(storage::v3::LabelId, LabelToName, (const std::string &), (const));
MOCK_METHOD(const std::string &, PropertyToName, (storage::v3::PropertyId), (const));
MOCK_METHOD(const std::string &, LabelToName, (storage::v3::LabelId label), (const));
MOCK_METHOD(const std::string &, EdgeTypeToName, (storage::v3::EdgeTypeId type), (const));
MOCK_METHOD(std::optional<storage::v3::PropertyId>, MaybeNameToProperty, (const std::string &), (const));
MOCK_METHOD(std::optional<storage::v3::EdgeTypeId>, MaybeNameToEdgeType, (const std::string &), (const));
MOCK_METHOD(std::optional<storage::v3::LabelId>, MaybeNameToLabel, (const std::string &), (const));
MOCK_METHOD(bool, IsPrimaryLabel, (storage::v3::LabelId), (const));
MOCK_METHOD(bool, IsPrimaryKey, (storage::v3::LabelId, storage::v3::PropertyId), (const));
};
class MockedLogicalOperator : query::v2::plan::LogicalOperator {
class MockedLogicalOperator : public plan::LogicalOperator {
public:
MOCK_CONST_METHOD1(MakeCursor, query::v2::plan::UniqueCursorPtr(utils::MemoryResource *));
MOCK_CONST_METHOD1(OutputSymbols, std::vector<expr::Symbol>(const expr::SymbolTable &));
MOCK_CONST_METHOD1(ModifiedSymbols, std::vector<expr::Symbol>(const expr::SymbolTable &));
MOCK_CONST_METHOD0(HasSingleInput, bool());
MOCK_CONST_METHOD0(input, std::shared_ptr<LogicalOperator>());
MOCK_METHOD1(set_input, void(std::shared_ptr<LogicalOperator>));
MOCK_CONST_METHOD1(Clone, std::unique_ptr<LogicalOperator>(query::v2::AstStorage *storage));
MOCK_METHOD(plan::UniqueCursorPtr, MakeCursor, (utils::MemoryResource *), (const));
MOCK_METHOD(std::vector<expr::Symbol>, ModifiedSymbols, (const expr::SymbolTable &), (const));
MOCK_METHOD(bool, HasSingleInput, (), (const));
MOCK_METHOD(std::shared_ptr<LogicalOperator>, input, (), (const));
MOCK_METHOD(void, set_input, (std::shared_ptr<LogicalOperator>));
MOCK_METHOD(std::unique_ptr<LogicalOperator>, Clone, (AstStorage * storage), (const));
MOCK_METHOD(bool, Accept, (plan::HierarchicalLogicalOperatorVisitor & visitor));
};
class MockedCursor : memgraph::query::v2::plan::Cursor {
class MockedCursor : public plan::Cursor {
public:
MOCK_METHOD2(Pull, bool(query::v2::Frame &, expr::ExecutionContext &));
MOCK_METHOD2(PullMultiple, void(query::v2::MultiFrame &, expr::ExecutionContext &));
MOCK_METHOD0(Reset, void());
MOCK_METHOD0(Shutdown, void());
MOCK_METHOD(bool, Pull, (Frame &, expr::ExecutionContext &));
MOCK_METHOD(void, PullMultiple, (MultiFrame &, expr::ExecutionContext &));
MOCK_METHOD(void, Reset, ());
MOCK_METHOD(void, Shutdown, ());
};
} // namespace memgraph
inline expr::ExecutionContext MakeContext(const expr::AstStorage &storage, const expr::SymbolTable &symbol_table,
RequestRouterInterface *router, IdAllocator *id_alloc) {
expr::ExecutionContext context;
context.symbol_table = symbol_table;
context.evaluation_context.properties = NamesToProperties(storage.properties_, router);
context.evaluation_context.labels = NamesToLabels(storage.labels_, router);
context.edge_ids_alloc = id_alloc;
context.request_router = router;
return context;
}
inline MockedLogicalOperator &BaseToMock(plan::LogicalOperator *op) {
return *static_cast<MockedLogicalOperator *>(op);
}
inline MockedCursor &BaseToMock(plan::Cursor *cursor) { return *static_cast<MockedCursor *>(cursor); }
} // namespace memgraph::query::v2

View File

@ -9,6 +9,7 @@
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
#include <memory>
#include "mock_helpers.hpp"
#include "query/v2/bindings/frame.hpp"
@ -22,24 +23,7 @@
#include "utils/logging.hpp"
#include "utils/memory.hpp"
using namespace memgraph::query::v2;
using namespace memgraph::query::v2::plan;
namespace memgraph {
class TestTemplate : public testing::Test {
protected:
void SetUp() override {}
};
ExecutionContext MakeContext(const AstStorage &storage, const SymbolTable &symbol_table, RequestRouterInterface *router,
IdAllocator *id_alloc) {
ExecutionContext context;
context.symbol_table = symbol_table;
context.evaluation_context.properties = NamesToProperties(storage.properties_, router);
context.evaluation_context.labels = NamesToLabels(storage.labels_, router);
context.edge_ids_alloc = id_alloc;
context.request_router = router;
return context;
}
namespace memgraph::query::v2 {
MultiFrame CreateMultiFrame(const size_t max_pos, const Symbol &src, const Symbol &dst, MockedRequestRouter *router) {
static constexpr size_t frame_size = 100;
@ -60,14 +44,15 @@ MultiFrame CreateMultiFrame(const size_t max_pos, const Symbol &src, const Symbo
return multi_frame;
}
TEST_F(TestTemplate, CreateExpand) {
MockedRequestRouter router;
TEST(CreateExpandTest, Cursor) {
using testing::_;
using testing::Return;
AstStorage ast;
SymbolTable symbol_table;
query::v2::plan::NodeCreationInfo node;
query::v2::plan::EdgeCreationInfo edge;
plan::NodeCreationInfo node;
plan::EdgeCreationInfo edge;
edge.edge_type = msgs::EdgeTypeId::FromUint(1);
edge.direction = EdgeAtom::Direction::IN;
auto id_alloc = IdAllocator(0, 100);
@ -75,14 +60,20 @@ TEST_F(TestTemplate, CreateExpand) {
const auto &src = symbol_table.CreateSymbol("n", true);
node.symbol = symbol_table.CreateSymbol("u", true);
auto create_expand = query::v2::plan::CreateExpand(node, edge, nullptr, src, true);
auto once_cur = plan::MakeUniqueCursorPtr<MockedCursor>(utils::NewDeleteResource());
EXPECT_CALL(BaseToMock(once_cur.get()), PullMultiple(_, _)).Times(1);
std::shared_ptr<plan::LogicalOperator> once_op = std::make_shared<MockedLogicalOperator>();
EXPECT_CALL(BaseToMock(once_op.get()), MakeCursor(_)).Times(1).WillOnce(Return(std::move(once_cur)));
auto create_expand = plan::CreateExpand(node, edge, once_op, src, true);
auto cursor = create_expand.MakeCursor(utils::NewDeleteResource());
EXPECT_CALL(router, CreateExpand(testing::_))
.Times(1)
.WillOnce(::testing::Return(std::vector<msgs::CreateExpandResponse>{}));
MockedRequestRouter router;
EXPECT_CALL(router, CreateExpand(_)).Times(1).WillOnce(Return(std::vector<msgs::CreateExpandResponse>{}));
auto context = MakeContext(ast, symbol_table, &router, &id_alloc);
auto multi_frame = CreateMultiFrame(context.symbol_table.max_position(), src, node.symbol, &router);
cursor->PullMultiple(multi_frame, context);
}
} // namespace memgraph
} // namespace memgraph::query::v2