forked from mongodb/mongo-cxx-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_index_view.cpp
258 lines (214 loc) · 9.22 KB
/
search_index_view.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <chrono>
#include <thread>
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>
#include <bsoncxx/oid.hpp>
#include <bsoncxx/test/catch.hh>
#include <mongocxx/client.hpp>
#include <mongocxx/cursor.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/search_index_view.hpp>
#include <mongocxx/test/client_helpers.hh>
namespace {
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;
using namespace mongocxx;
bool does_search_index_exist_on_cursor(cursor& c, search_index_model& model, bool with_status) {
for (auto&& doc : c) {
// check the name, that the index is queryable, and that the definition matches.
if (doc["name"].get_string().value == *model.name() && doc["queryable"].get_bool().value &&
doc["latestDefinition"].get_document().view() == model.definition()) {
// optional addition check needed if with_status is set
if (!with_status || (with_status && bsoncxx::string::to_string(
doc["status"].get_string().value) == "READY")) {
return true;
}
}
}
return false;
}
// `assert_soon` repeatedly calls `fn` and asserts `fn` eventually returns true.
// `fn` is called every five seconds. Fails if `fn` does not return true within five minutes.
void assert_soon(std::function<bool()> fn) {
auto start = std::chrono::high_resolution_clock::now();
while (std::chrono::high_resolution_clock::now() - start < std::chrono::minutes(5)) {
if (fn()) {
return;
}
std::this_thread::sleep_for(std::chrono::seconds(5));
}
FAIL("Expected function to return true within five minutes, but did not");
}
TEST_CASE("atlas search indexes prose tests", "") {
instance::current();
auto uri_getenv = std::getenv("MONGODB_URI");
if (!uri_getenv) {
WARN("Skipping - Test requires the environment variable: MONGODB_URI");
return;
}
client mongodb_client{uri{uri_getenv}};
database db = mongodb_client["test"];
SECTION("create one with name and definition") {
// use a randomly generated collection name as there's a server side limitation that
// prevents multiple search indexes with the same name, definition and collection name
// from being created.
bsoncxx::oid id;
auto coll = db.create_collection(id.to_string());
auto siv = coll.search_indexes();
// {
// name: 'test-search-index',
// definition : {
// mappings : { dynamic: false }
// }
// }
auto name = "test-search-index";
auto definition = make_document(kvp("mappings", make_document(kvp("dynamic", false))));
auto model = search_index_model(name, definition.view());
REQUIRE(siv.create_one(name, definition.view()) == "test-search-index");
assert_soon([&siv, &model](void) -> bool {
auto cursor = siv.list();
return does_search_index_exist_on_cursor(cursor, model, false);
});
std::cout << "create one with name and definition SUCCESS" << std::endl;
}
SECTION("create one with model") {
bsoncxx::oid id;
auto coll = db.create_collection(id.to_string());
auto siv = coll.search_indexes();
// {
// name: 'test-search-index',
// definition : {
// mappings : { dynamic: false }
// }
// }
auto name = "test-search-index";
auto definition = make_document(kvp("mappings", make_document(kvp("dynamic", false))));
auto model = search_index_model(name, definition.view());
REQUIRE(siv.create_one(model) == "test-search-index");
assert_soon([&siv, &model](void) -> bool {
auto cursor = siv.list();
return does_search_index_exist_on_cursor(cursor, model, false);
});
std::cout << "create one with model SUCCESS" << std::endl;
}
SECTION("create many") {
bsoncxx::oid id;
auto coll = db.create_collection(id.to_string());
auto siv = coll.search_indexes();
// {
// name: 'test-search-index-1',
// definition : {
// mappings : { dynamic: false }
// }
// }
auto name1 = "test-search-index-1";
auto definition1 = make_document(kvp("mappings", make_document(kvp("dynamic", false))));
auto model1 = search_index_model(name1, definition1.view());
// {
// name: 'test-search-index-2',
// definition : {
// mappings : { dynamic: false }
// }
// }
auto name2 = "test-search-index-2";
auto definition2 = make_document(kvp("mappings", make_document(kvp("dynamic", false))));
auto model2 = search_index_model(name2, definition2.view());
std::vector<search_index_model> models = {model1, model2};
std::vector<std::string> result = siv.create_many(models);
std::vector<std::string> expected = {"test-search-index-1", "test-search-index-2"};
REQUIRE(result == expected);
assert_soon([&siv, &model1, &model2](void) -> bool {
auto cursor = siv.list();
return does_search_index_exist_on_cursor(cursor, model1, false) &&
does_search_index_exist_on_cursor(cursor, model2, false);
});
std::cout << "create many SUCCESS" << std::endl;
}
SECTION("drop one") {
bsoncxx::oid id;
auto coll = db.create_collection(id.to_string());
auto siv = coll.search_indexes();
// {
// name: 'test-search-index',
// definition : {
// mappings : { dynamic: false }
// }
// }
auto name = "test-search-index";
auto definition = make_document(kvp("mappings", make_document(kvp("dynamic", false))));
auto model = search_index_model(name, definition.view());
REQUIRE(siv.create_one(model) == "test-search-index");
assert_soon([&siv, &model](void) -> bool {
auto cursor = siv.list();
return does_search_index_exist_on_cursor(cursor, model, false);
});
siv.drop_one(name);
assert_soon([&siv](void) -> bool {
auto cursor = siv.list();
// Return true if empty results are returned.
return cursor.begin() == cursor.end();
});
std::cout << "drop one SUCCESS" << std::endl;
}
SECTION("update one") {
bsoncxx::oid id;
auto coll = db.create_collection(id.to_string());
auto siv = coll.search_indexes();
// {
// name: 'test-search-index',
// definition : {
// mappings : { dynamic: false }
// }
// }
auto name = "test-search-index";
auto definition = make_document(kvp("mappings", make_document(kvp("dynamic", false))));
auto model = search_index_model(name, definition.view());
REQUIRE(siv.create_one(model) == "test-search-index");
assert_soon([&siv, &model](void) -> bool {
auto cursor = siv.list();
return does_search_index_exist_on_cursor(cursor, model, false);
});
// definition : {
// mappings : { dynamic: true }
// }
auto new_definition = make_document(kvp("mappings", make_document(kvp("dynamic", true))));
auto new_model = search_index_model(name, new_definition.view());
siv.update_one(name, new_definition.view());
assert_soon([&siv, &new_model](void) -> bool {
auto cursor = siv.list();
return does_search_index_exist_on_cursor(cursor, new_model, true);
});
std::cout << "update one SUCCESS" << std::endl;
}
SECTION("drop one suppress namespace not found") {
bsoncxx::oid id;
auto coll = db[id.to_string()];
coll.search_indexes().drop_one("apples");
std::cout << "drop one supress namespace not found SUCCESS" << std::endl;
}
SECTION("create and list search indexes with non-default readConcern and writeConcern") {
bsoncxx::oid id;
auto coll = db.create_collection(id.to_string());
// Apply non-default write concern.
auto nondefault_wc = mongocxx::write_concern();
nondefault_wc.nodes(1);
coll.write_concern(nondefault_wc);
// Apply non-default read concern.
auto nondefault_rc = mongocxx::read_concern();
nondefault_rc.acknowledge_level(mongocxx::read_concern::level::k_local);
coll.read_concern(nondefault_rc);
auto siv = coll.search_indexes();
auto name = "test-search-index-case6";
auto definition = make_document(kvp("mappings", make_document(kvp("dynamic", false))));
auto model = search_index_model(name, definition.view());
REQUIRE(siv.create_one(name, definition.view()) == "test-search-index-case6");
assert_soon([&siv, &model](void) -> bool {
auto cursor = siv.list();
return does_search_index_exist_on_cursor(cursor, model, false);
});
std::cout << "create and list search indexes with non-default readConcern and writeConcern "
"SUCCESS"
<< std::endl;
}
}
} // namespace