Skip to content

Commit 6fd9782

Browse files
committed
CXX-555 add test coverage for collection::distinct
1 parent 023941b commit 6fd9782

File tree

2 files changed

+58
-39
lines changed

2 files changed

+58
-39
lines changed

src/mongocxx/test/collection.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#include "catch.hpp"
22

3+
#include <vector>
4+
35
#include <bsoncxx/builder/stream/document.hpp>
6+
#include <bsoncxx/json.hpp>
7+
#include <bsoncxx/stdx/string_view.hpp>
48
#include <bsoncxx/types.hpp>
9+
510
#include <mongocxx/collection.hpp>
611
#include <mongocxx/client.hpp>
712
#include <mongocxx/pipeline.hpp>
@@ -388,4 +393,57 @@ TEST_CASE("CRUD functionality", "[driver::collection]") {
388393

389394
auto results = coll.aggregate(p);
390395
}
396+
397+
SECTION("distinct works", "[collection]") {
398+
using bsoncxx::builder::stream::document;
399+
400+
auto distinct_cname = "distinct_coll";
401+
auto distinct_coll = db[distinct_cname];
402+
if (db.has_collection(distinct_cname)) {
403+
distinct_coll.drop();
404+
}
405+
406+
document doc1{};
407+
doc1 << "foo" << "baz" << "garply" << 1;
408+
document doc2{};
409+
doc2 << "foo" << "bar" << "garply" << 2;
410+
document doc3{};
411+
doc3 << "foo" << "baz" << "garply" << 2;
412+
document doc4{};
413+
doc4 << "foo" << "quux" << "garply" << 9;
414+
415+
bulk_write bulk{false /* unordered */};
416+
417+
bulk.append(model::insert_one{doc1});
418+
bulk.append(model::insert_one{doc2});
419+
bulk.append(model::insert_one{doc3});
420+
bulk.append(model::insert_one{doc4});
421+
422+
distinct_coll.bulk_write(bulk);
423+
424+
auto distinct_results = distinct_coll.distinct("foo", {});
425+
426+
// copy into a vector.
427+
std::vector<bsoncxx::document::value> results(distinct_results.begin(),
428+
distinct_results.end());
429+
REQUIRE(results.size() == std::size_t{1});
430+
431+
auto res_doc = results[0].view();
432+
433+
auto values_array = res_doc["values"].get_array().value;
434+
435+
std::vector<bsoncxx::stdx::string_view> distinct_values;
436+
for (auto&& value : values_array) {
437+
distinct_values.push_back(value.get_utf8().value);
438+
}
439+
440+
const auto assert_contains_one = [&](bsoncxx::stdx::string_view val) {
441+
REQUIRE(std::count(distinct_values.begin(), distinct_values.end(), val) == 1);
442+
};
443+
444+
assert_contains_one("baz");
445+
assert_contains_one("bar");
446+
assert_contains_one("quux");
447+
}
448+
391449
}

src/mongocxx/test/collection_mocked.cpp

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -213,45 +213,6 @@ TEST_CASE("Collection", "[collection]") {
213213
REQUIRE(collection_create_index_called);
214214
}
215215

216-
SECTION("Distinct" "[collection::distinct]") {
217-
auto database_command_called = false;
218-
bool success;
219-
220-
database_command->interpose([&](
221-
mongoc_database_t* db,
222-
mongoc_query_flags_t flags,
223-
uint32_t skip,
224-
uint32_t limit,
225-
uint32_t batch_size,
226-
const bson_t* command,
227-
const bson_t* fields,
228-
const mongoc_read_prefs_t* read_prefs
229-
) -> mongoc_cursor_t* {
230-
database_command_called = true;
231-
REQUIRE(db == mongo_db.implementation());
232-
// TODO: test some actual implementation details here
233-
// TODO: figure out how to return a legit mongoc_cursor_t back to the driver
234-
return nullptr;
235-
});
236-
237-
SECTION("Succeeds") {
238-
success = true;
239-
// TODO: fixme
240-
//mongo_coll.distinct("field_name", bsoncxx::document::view{});
241-
}
242-
243-
SECTION("Fails") {
244-
success = false;
245-
// TODO: fixme
246-
//REQUIRE_THROWS_AS(
247-
//mongo_coll.distinct("field_name", bsoncxx::document::view{}),
248-
//exception::operation
249-
//);
250-
}
251-
252-
//REQUIRE(database_command_called);
253-
}
254-
255216
SECTION("Drop" "[collection::drop]") {
256217
auto collection_drop_called = false;
257218
bool success;

0 commit comments

Comments
 (0)