|
| 1 | +//////////////////////////////////////////////////////////////////////////////// |
| 2 | +/// DISCLAIMER |
| 3 | +/// |
| 4 | +/// Copyright 2021-2021 ArangoDB GmbH, Cologne, Germany |
| 5 | +/// |
| 6 | +/// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +/// you may not use this file except in compliance with the License. |
| 8 | +/// You may obtain a copy of the License at |
| 9 | +/// |
| 10 | +/// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +/// |
| 12 | +/// Unless required by applicable law or agreed to in writing, software |
| 13 | +/// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +/// See the License for the specific language governing permissions and |
| 16 | +/// limitations under the License. |
| 17 | +/// |
| 18 | +/// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +/// |
| 20 | +/// @author Michael Hackstein |
| 21 | +/// @author Copyright 2021, ArangoDB GmbH, Cologne, Germany |
| 22 | +//////////////////////////////////////////////////////////////////////////////// |
| 23 | + |
| 24 | +#include "gtest/gtest.h" |
| 25 | + |
| 26 | +#include "Aql/Ast.h" |
| 27 | +#include "Aql/Query.h" |
| 28 | +#include "Aql/ShortestPathNode.h" |
| 29 | +#include "Graph/BaseOptions.h" |
| 30 | +#include "Graph/ShortestPathOptions.h" |
| 31 | +#include "Mocks/Servers.h" |
| 32 | + |
| 33 | +#include <memory> |
| 34 | + |
| 35 | +using namespace arangodb; |
| 36 | +using namespace arangodb::aql; |
| 37 | +using namespace arangodb::graph; |
| 38 | +using namespace arangodb::tests::mocks; |
| 39 | + |
| 40 | +namespace arangodb { |
| 41 | +namespace tests { |
| 42 | +namespace aql { |
| 43 | +class ShortestPathNodeTest : public ::testing::Test { |
| 44 | + MockAqlServer _server{}; |
| 45 | + std::unique_ptr<Query> _query{_server.createFakeQuery()}; |
| 46 | + std::unique_ptr<Query> _otherQuery{_server.createFakeQuery()}; |
| 47 | + std::string _startNode{"v/123"}; |
| 48 | + AstNode* _source{nullptr}; |
| 49 | + AstNode* _target{nullptr}; |
| 50 | + AstNode* _direction{nullptr}; |
| 51 | + AstNode* _graph{nullptr}; |
| 52 | + |
| 53 | + public: |
| 54 | + ShortestPathNodeTest() { |
| 55 | + auto ast = _query->ast(); |
| 56 | + _source = ast->createNodeValueString(_startNode.c_str(), _startNode.length()); |
| 57 | + _target = ast->createNodeValueString(_startNode.c_str(), _startNode.length()); |
| 58 | + _direction = ast->createNodeDirection(0, 1); |
| 59 | + AstNode* edges = ast->createNodeArray(0); |
| 60 | + _graph = ast->createNodeCollectionList(edges, _query->resolver()); |
| 61 | + } |
| 62 | + |
| 63 | + ExecutionPlan* plan() const { return _query->plan(); } |
| 64 | + ExecutionPlan* otherPlan(bool emptyQuery = false) { |
| 65 | + if (emptyQuery) { |
| 66 | + // Let us start a new blank query |
| 67 | + _otherQuery = _server.createFakeQuery(); |
| 68 | + } |
| 69 | + return _otherQuery->plan(); |
| 70 | + } |
| 71 | + |
| 72 | + ShortestPathNode createNode(ExecutionNodeId id, |
| 73 | + std::unique_ptr<ShortestPathOptions> opts) const { |
| 74 | + return ShortestPathNode{ |
| 75 | + plan(), id, &_query->vocbase(), _direction, _source, |
| 76 | + _target, _graph, std::move(opts)}; |
| 77 | + }; |
| 78 | + |
| 79 | + std::unique_ptr<ShortestPathOptions> makeOptions() const { |
| 80 | + return std::make_unique<ShortestPathOptions>(*_query); |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +TEST_F(ShortestPathNodeTest, clone_should_preserve_isSmart) { |
| 85 | + ExecutionNodeId id(12); |
| 86 | + auto opts = makeOptions(); |
| 87 | + ShortestPathNode original = createNode(id, std::move(opts)); |
| 88 | + ASSERT_EQ(original.id(), id); |
| 89 | + for (bool keepPlan : std::vector<bool>{false, true}) { |
| 90 | + for (bool value : std::vector<bool>{false, true}) { |
| 91 | + auto p = keepPlan ? plan() : otherPlan(true); |
| 92 | + original.setIsSmart(value); |
| 93 | + auto clone = |
| 94 | + ExecutionNode::castTo<ShortestPathNode*>(original.clone(p, false, !keepPlan)); |
| 95 | + if (keepPlan) { |
| 96 | + EXPECT_NE(clone->id(), original.id()) << "Clone did keep the id"; |
| 97 | + } else { |
| 98 | + EXPECT_EQ(clone->id(), original.id()) << "Clone did not keep the id"; |
| 99 | + } |
| 100 | + EXPECT_EQ(original.isSmart(), value); |
| 101 | + EXPECT_EQ(clone->isSmart(), value); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +TEST_F(ShortestPathNodeTest, clone_should_preserve_isDisjoint) { |
| 107 | + ExecutionNodeId id(12); |
| 108 | + auto opts = makeOptions(); |
| 109 | + ShortestPathNode original = createNode(id, std::move(opts)); |
| 110 | + ASSERT_EQ(original.id(), id); |
| 111 | + for (bool keepPlan : std::vector<bool>{false, true}) { |
| 112 | + for (bool value : std::vector<bool>{false, true}) { |
| 113 | + auto p = keepPlan ? plan() : otherPlan(true); |
| 114 | + original.setIsDisjoint(value); |
| 115 | + auto clone = |
| 116 | + ExecutionNode::castTo<ShortestPathNode*>(original.clone(p, false, !keepPlan)); |
| 117 | + if (keepPlan) { |
| 118 | + EXPECT_NE(clone->id(), original.id()) << "Clone did keep the id"; |
| 119 | + } else { |
| 120 | + EXPECT_EQ(clone->id(), original.id()) << "Clone did not keep the id"; |
| 121 | + } |
| 122 | + EXPECT_EQ(original.isDisjoint(), value); |
| 123 | + EXPECT_EQ(clone->isDisjoint(), value); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +} // namespace aql |
| 129 | +} // namespace tests |
| 130 | +} // namespace arangodb |
0 commit comments