Skip to content

Commit b0f3fc7

Browse files
authored
CXX-1985 Rename filter to opts for list_databases (mongodb#745)
1 parent f5d8422 commit b0f3fc7

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed

src/mongocxx/client.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ cursor client::list_databases(const client_session& session) const {
195195
return libmongoc::client_find_databases_with_opts(_get_impl().client_t, options_bson.bson());
196196
}
197197

198-
cursor client::list_databases(const bsoncxx::document::view_or_value filter) const {
199-
scoped_bson_t filter_bson{filter.view()};
200-
return libmongoc::client_find_databases_with_opts(_get_impl().client_t, filter_bson.bson());
198+
cursor client::list_databases(const bsoncxx::document::view_or_value opts) const {
199+
scoped_bson_t opts_bson{opts.view()};
200+
return libmongoc::client_find_databases_with_opts(_get_impl().client_t, opts_bson.bson());
201201
}
202202

203203
std::vector<std::string> client::list_database_names(

src/mongocxx/client.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ class MONGOCXX_API client {
247247
///
248248
/// Enumerates the databases in the client.
249249
///
250-
/// @param filter
251-
/// The filter that documents must match in order to be listed.
250+
/// @param opts
251+
/// Options passed directly to the 'listDatabases' command.
252252
///
253253
/// @return A mongocxx::cursor containing a BSON document for each
254254
/// database. Each document contains a name field with the database
@@ -260,7 +260,7 @@ class MONGOCXX_API client {
260260
///
261261
/// @see https://docs.mongodb.com/master/reference/command/listDatabases
262262
///
263-
cursor list_databases(const bsoncxx::document::view_or_value filter) const;
263+
cursor list_databases(const bsoncxx::document::view_or_value opts) const;
264264

265265
///
266266
/// Queries the MongoDB server for a list of known databases.

src/mongocxx/test/client.cpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ TEST_CASE("A client lists its databases with a filter applied", "[client]") {
5757
auto filter_doc = make_document(kvp("filter", make_document(kvp("name", "admin"))));
5858
auto filter_view = filter_doc.view();
5959

60-
auto client_list_databases = libmongoc::client_find_databases_with_opts.create_instance();
61-
client_list_databases
60+
client_find_databases_with_opts
6261
->interpose([&](mongoc_client_t*, const bson_t* opts) {
6362
REQUIRE(opts);
6463
bsoncxx::document::view opts_view{bson_get_data(opts), opts->len};
@@ -73,6 +72,47 @@ TEST_CASE("A client lists its databases with a filter applied", "[client]") {
7372
REQUIRE(client_list_databases_called);
7473
}
7574

75+
TEST_CASE("list databases passes authorizedDatabases option", "[client]") {
76+
using bsoncxx::builder::basic::kvp;
77+
using bsoncxx::builder::basic::make_document;
78+
using bsoncxx::to_json;
79+
80+
MOCK_CLIENT
81+
82+
bool called = false;
83+
stdx::optional<bsoncxx::document::value> opts_passed;
84+
85+
client_find_databases_with_opts->visit([&](mongoc_client_t*, const bson_t* opts) {
86+
called = true;
87+
if (opts) {
88+
opts_passed =
89+
bsoncxx::document::value{bsoncxx::document::view{bson_get_data(opts), opts->len}};
90+
}
91+
return nullptr;
92+
});
93+
94+
mongocxx::client client{mongocxx::uri{}};
95+
96+
SECTION("list_databases with no arguments") {
97+
client.list_databases();
98+
REQUIRE(called);
99+
}
100+
101+
SECTION("list_databases with filter") {
102+
bsoncxx::document::value opts = make_document(kvp("filter", 1));
103+
client.list_databases(opts.view());
104+
REQUIRE(called);
105+
REQUIRE_BSON_MATCHES(*opts_passed, opts);
106+
}
107+
108+
SECTION("list_databases with authorizedDatabases") {
109+
bsoncxx::document::value opts = make_document(kvp("authorizedDatabases", true));
110+
client.list_databases(opts.view());
111+
REQUIRE(called);
112+
REQUIRE_BSON_MATCHES(*opts_passed, opts);
113+
}
114+
}
115+
76116
TEST_CASE("A client constructed with a URI is truthy", "[client]") {
77117
MOCK_CLIENT
78118

src/third_party/catch/include/helpers.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ MONGOCXX_INLINE_NAMESPACE_END
9292
client_reset->interpose([](const mongoc_client_t*){}).forever(); \
9393
auto client_get_concern = libmongoc::client_get_write_concern.create_instance(); \
9494
client_get_concern->interpose([](const mongoc_client_t*) { return nullptr; }).forever(); \
95-
auto client_start_session = libmongoc::client_start_session.create_instance();
95+
auto client_start_session = libmongoc::client_start_session.create_instance(); \
96+
auto client_find_databases_with_opts = mongocxx::libmongoc::client_find_databases_with_opts.create_instance();
9697

9798
#if defined(MONGOCXX_ENABLE_SSL) && defined(MONGOC_ENABLE_SSL)
9899
#define MOCK_CLIENT \

0 commit comments

Comments
 (0)