1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | * |
16 | * \author guonix |
17 | * \date Nov 2020 |
18 | * \brief |
19 | */ |
20 | |
21 | #include "query/meta_wrapper.h" |
22 | #include <gtest/gtest.h> |
23 | #include "meta/meta_impl.h" |
24 | #include "meta/mock_meta_service.h" |
25 | |
26 | using namespace proxima::be::query; |
27 | using namespace proxima::be::meta; |
28 | |
29 | TEST(MetaWrapperTest, TestValidate) { |
30 | { // TestMetaService error |
31 | auto meta_service = std::make_shared<MockMetaService>(); |
32 | EXPECT_CALL(*meta_service, get_current_collection(_)) |
33 | .WillOnce(Return(nullptr)) // Failed |
34 | .RetiresOnSaturation(); |
35 | MetaWrapper meta(meta_service); |
36 | |
37 | std::string collection; |
38 | ColumnNameList columns{"" , "" }; |
39 | EXPECT_TRUE(meta.validate(collection, columns) != 0); |
40 | } |
41 | |
42 | { // TestMetaService success with invalid collection pointer |
43 | auto meta_service = std::make_shared<MockMetaService>(); |
44 | EXPECT_CALL(*meta_service, get_current_collection(_)) |
45 | .WillOnce(Return(nullptr)) // Failed |
46 | .RetiresOnSaturation(); |
47 | MetaWrapper meta(meta_service); |
48 | |
49 | std::string collection; |
50 | ColumnNameList columns{"" , "" }; |
51 | EXPECT_TRUE(meta.validate(collection, columns) != 0); |
52 | } |
53 | |
54 | { // TestMetaService success with valid collection pointer |
55 | CollectionMeta meta; |
56 | meta.mutable_forward_columns()->push_back("forward1" ); |
57 | meta.mutable_forward_columns()->push_back("forward2" ); |
58 | auto column1 = std::make_shared<ColumnMeta>("column1" ); |
59 | auto column2 = std::make_shared<ColumnMeta>("column2" ); |
60 | meta.append(column1); |
61 | meta.append(column2); |
62 | CollectionImplPtr collection = std::make_shared<CollectionImpl>(meta); |
63 | |
64 | auto meta_service = std::make_shared<MockMetaService>(); |
65 | EXPECT_CALL(*meta_service, get_current_collection(_)) |
66 | .WillRepeatedly( |
67 | Invoke([&collection](const std::string &) -> CollectionMetaPtr { |
68 | return collection->meta(); |
69 | })); // success |
70 | |
71 | MetaWrapper meta_wrapper(meta_service); |
72 | std::string name{"name" }; |
73 | ColumnNameList columns{"column1" , "column2" }; |
74 | // True |
75 | EXPECT_EQ(meta_wrapper.validate(name, columns), 0); |
76 | // False |
77 | columns.push_back("column3" ); |
78 | EXPECT_TRUE(meta_wrapper.validate(name, columns) != 0); |
79 | |
80 | // Test validate_column |
81 | EXPECT_EQ(meta_wrapper.validate_column(name, "column1" ), 0); |
82 | EXPECT_TRUE(meta_wrapper.validate_column(name, "column3" ) != 0); |
83 | |
84 | // Get collection failed |
85 | EXPECT_CALL(*meta_service, get_collection(_, _)) |
86 | .WillOnce(Return(nullptr)) |
87 | .WillOnce(Return(collection->meta())); |
88 | |
89 | columns.clear(); |
90 | EXPECT_TRUE(meta_wrapper.list_columns("" , 1, &columns) != 0); |
91 | |
92 | EXPECT_EQ(meta_wrapper.list_columns(name, 1, &columns), 0); |
93 | EXPECT_EQ(columns.size(), 2); |
94 | EXPECT_EQ(columns.front().compare("forward1" ), 0); |
95 | EXPECT_EQ(columns.back().compare("forward2" ), 0); |
96 | } |
97 | |
98 | { // TestMetaService validate collection |
99 | CollectionMeta meta; |
100 | meta.set_name("name" ); |
101 | auto column1 = std::make_shared<ColumnMeta>(); |
102 | auto column2 = std::make_shared<ColumnMeta>(); |
103 | meta.append(column1); |
104 | meta.append(column2); |
105 | CollectionImplPtr collection = std::make_shared<CollectionImpl>(meta); |
106 | |
107 | auto meta_service = std::make_shared<MockMetaService>(); |
108 | EXPECT_CALL(*meta_service, get_current_collection("name" )) |
109 | .WillOnce( |
110 | Invoke([&collection](const std::string &) -> CollectionMetaPtr { |
111 | return collection->meta(); |
112 | })) |
113 | .RetiresOnSaturation(); |
114 | |
115 | MetaWrapper meta_wrapper(meta_service); |
116 | std::string name{"name" }; |
117 | // True |
118 | EXPECT_EQ(meta_wrapper.validate_collection(name), 0); |
119 | |
120 | EXPECT_CALL(*meta_service, get_current_collection(_)) |
121 | .WillOnce(Return(nullptr)) |
122 | .RetiresOnSaturation(); |
123 | EXPECT_TRUE(meta_wrapper.validate_collection(name) != 0); |
124 | } |
125 | } |
126 | |