From a5940d1c3432a607790b7db30c9d2118120aaf9a Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 1 Oct 2024 13:36:11 -0500 Subject: [PATCH 01/25] Add missing docs for mongocxx::v_noabi::uri::k_default_uri --- src/mongocxx/include/mongocxx/v_noabi/mongocxx/uri.hpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/uri.hpp b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/uri.hpp index 30e309b182..3870e2cae0 100644 --- a/src/mongocxx/include/mongocxx/v_noabi/mongocxx/uri.hpp +++ b/src/mongocxx/include/mongocxx/v_noabi/mongocxx/uri.hpp @@ -49,11 +49,14 @@ class uri { std::int32_t family; }; + /// + /// The default URI string: `"mongodb://localhost:27017"`. + /// static MONGOCXX_ABI_EXPORT const std::string k_default_uri; /// /// Constructs a uri from an optional MongoDB URI string. If no URI string is specified, - /// uses the default URI string, 'mongodb://localhost:27017'. + /// uses the default URI string: `"mongodb://localhost:27017"`. /// /// @see /// - https://mongoc.org/libmongoc/current/mongoc_uri_t.html From cd1fa09b196346f258dda9897855de06e34029a9 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 1 Oct 2024 13:36:11 -0500 Subject: [PATCH 02/25] Avoid CDRIVER-5732 --- examples/api/bsoncxx/examples/bson_errors/big_string.hh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/api/bsoncxx/examples/bson_errors/big_string.hh b/examples/api/bsoncxx/examples/bson_errors/big_string.hh index f97e4b6060..76566165e3 100644 --- a/examples/api/bsoncxx/examples/bson_errors/big_string.hh +++ b/examples/api/bsoncxx/examples/bson_errors/big_string.hh @@ -26,7 +26,8 @@ namespace examples { // Used to trigger builder append failure. struct big_string { // BSON_SIZE_MAX == 0x7FFFFFFF - std::size_t length{static_cast(std::numeric_limits::max())}; + // Leave some room for CDRIVER-5732. + std::size_t length{static_cast(std::numeric_limits::max() - 32u)}; // Allocate an UNINITIALIZED blob of data that will not be accessed due to length checks. // Leaving memory unitialized (rather than zero-init) should hopefully avoid slow and expensive From e59ec865ab82a8956957f8ed7b9574005a7c4e5e Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 1 Oct 2024 13:36:12 -0500 Subject: [PATCH 03/25] Add support for forking API examples --- .evergreen/test.sh | 3 + examples/api/runner.cpp | 159 ++++++++++++++++++++++++++++++++++------ examples/api/runner.hh | 20 +++-- examples/macros.hh | 9 ++- 4 files changed, 160 insertions(+), 31 deletions(-) diff --git a/.evergreen/test.sh b/.evergreen/test.sh index ade6af0449..54d86a2f98 100755 --- a/.evergreen/test.sh +++ b/.evergreen/test.sh @@ -297,6 +297,9 @@ else # API version to example clients, so examples will fail when requireApiVersion # is true. if [[ -z "${MONGODB_API_VERSION:-}" ]]; then + # Avoid `[__NSCFConstantString initialize] may have been in progress in another thread when fork() was called.` errors on MacOS. + export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES + echo "Running examples..." if ! "${cmake_binary:?}" --build . --target run-examples --parallel 1 >|output.txt 2>&1; then # Only emit output on failure. diff --git a/examples/api/runner.cpp b/examples/api/runner.cpp index abf80fa6c0..6bcd4b88d6 100644 --- a/examples/api/runner.cpp +++ b/examples/api/runner.cpp @@ -32,21 +32,135 @@ #include +#if !defined(_MSC_VER) + +#include + +#include + +#endif // !defined(_MSC_VER) + namespace { class runner_type { public: using fn_type = void (*)(); + struct component { + fn_type fn; + const char* name; + + component(fn_type f, const char* n) : fn(f), name(n) {} + }; + private: - std::vector fns; + std::vector components; + std::vector forking_components; std::minstd_rand::result_type seed = 0u; std::minstd_rand gen; unsigned int jobs = 0; + static void run_with_jobs(const std::vector& components, unsigned int jobs) { + if (jobs == 1) { + for (const auto& component : components) { + component.fn(); + } + } else { + std::queue threads; + + // Rudimentary job scheduler. + for (const auto& component : components) { + while (threads.size() >= jobs) { + threads.front().join(); + threads.pop(); + } + + threads.emplace(component.fn); + } + + while (!threads.empty()) { + threads.front().join(); + threads.pop(); + } + } + } + + void run_components() { + run_with_jobs(components, jobs); + } + + enum struct action { + succeed, + fail, + return_from_main, + }; + +#if !defined(_MSC_VER) + action run_forking_components() { + // Forking with threads is difficult and the number of components that require forking are + // few in number. Run forking components sequentially. + for (const auto& component : forking_components) { + const auto& fn = component.fn; + const auto& name = component.name; + + const pid_t pid = ::fork(); + + // Child: do nothing more than call the registered function. + if (pid == 0) { + fn(); + return action::return_from_main; // Return from `main()`. + } + + // Parent: wait for child and handle returned status values. + else { + int status; + + const int ret = ::waitpid(pid, &status, 0); + + // For non-zero exit codes, permit continuation for example coverage. + if (WIFEXITED(status) && WEXITSTATUS(status) != EXIT_SUCCESS) { + std::cout << __func__ << ": failed: " << name + << " exited with a non-zero exit code: " << WEXITSTATUS(status) + << std::endl; + + return action::fail; + } + + // For unexpected signals, stop immediately. + else if (WIFSIGNALED(status)) { + const int signal = WTERMSIG(status); + const char* const sigstr = ::strsignal(signal); + + std::cout << __func__ << ": failed: " << name + << " was killed by signal: " << signal << " (" + << (sigstr ? sigstr : "") << ")" << std::endl; + + std::exit(EXIT_FAILURE); + } + + // We don't expect any other failure condition. + else { + ASSERT(ret != -1); + } + } + } + + return action::succeed; + } +#else + action run_forking_components() { + std::cout << "Skipping API examples that require forked processes" << std::endl; + return action::succeed; + } +#endif // !defined(_MSC_VER) + public: - void add(fn_type fn) { - fns.push_back(fn); + void add_component(fn_type fn, const char* name) { + components.emplace_back(fn, name); + } + + void add_forking_component(fn_type fn, const char* name) { + forking_components.emplace_back(fn, name); } void set_seed(std::minstd_rand::result_type seed) { @@ -69,26 +183,21 @@ class runner_type { gen.seed(seed); // Prevent ordering dependencies across examples. - std::shuffle(fns.begin(), fns.end(), gen); - - std::queue threads; - - // Rudimentary job scheduler. - for (auto fn : fns) { - while (threads.size() >= jobs) { - threads.front().join(); - threads.pop(); - } - - threads.emplace(fn); - } - - while (!threads.empty()) { - threads.front().join(); - threads.pop(); + std::shuffle(components.begin(), components.end(), gen); + std::shuffle(forking_components.begin(), forking_components.end(), gen); + + run_components(); + + switch (run_forking_components()) { + case action::succeed: + break; // Continue example coverage. + case action::fail: + return EXIT_FAILURE; // A component failed. + case action::return_from_main: + return EXIT_SUCCESS; // Return directly from forked processes. } - return 0; + return EXIT_SUCCESS; } }; @@ -96,8 +205,12 @@ runner_type runner; } // namespace -void runner_register_fn(void (*fn)()) { - runner.add(fn); +void runner_register_component(void (*fn)(), const char* name) { + runner.add_component(fn, name); +} + +void runner_register_forking_component(void (*fn)(), const char* name) { + runner.add_forking_component(fn, name); } int EXAMPLES_CDECL main(int argc, char** argv) { diff --git a/examples/api/runner.hh b/examples/api/runner.hh index 0d4a5d6811..d07a7569e0 100644 --- a/examples/api/runner.hh +++ b/examples/api/runner.hh @@ -16,15 +16,23 @@ #include -void runner_register_fn(void (*fn)()); +void runner_register_component(void (*fn)(), const char* name); + +void runner_register_forking_component(void (*fn)(), const char* name); // Defined by examples/CMakeLists.txt. #if !defined(EXAMPLES_COMPONENT_NAME) #error "EXAMPLES_COMPONENT_NAME is not defined!" #endif // !defined(EXAMPLES_COMPONENT_NAME) -#define RUNNER_REGISTER_COMPONENT() \ - static void EXAMPLES_CONCAT(EXAMPLES_COMPONENT_NAME, _entry_point)(void); \ - static int EXAMPLES_CONCAT(EXAMPLES_COMPONENT_NAME, _registerator) = \ - (::runner_register_fn(&EXAMPLES_CONCAT(EXAMPLES_COMPONENT_NAME, _entry_point)), 0); \ - static void EXAMPLES_CONCAT(EXAMPLES_COMPONENT_NAME, _entry_point)(void) +#define RUNNER_REGISTER_COMPONENT_IMPL(name, register_fn) \ + static void EXAMPLES_CONCAT3(name, _entry_point_, __LINE__)(void); \ + static int EXAMPLES_CONCAT2(name, _registrator) = \ + ((register_fn)(&EXAMPLES_CONCAT3(name, _entry_point_, __LINE__), EXAMPLES_STR(name)), 0); \ + static void EXAMPLES_CONCAT3(EXAMPLES_COMPONENT_NAME, _entry_point_, __LINE__)(void) + +#define RUNNER_REGISTER_COMPONENT() \ + RUNNER_REGISTER_COMPONENT_IMPL(EXAMPLES_COMPONENT_NAME, ::runner_register_component) + +#define RUNNER_REGISTER_FORKING_COMPONENT() \ + RUNNER_REGISTER_COMPONENT_IMPL(EXAMPLES_COMPONENT_NAME, ::runner_register_forking_component) diff --git a/examples/macros.hh b/examples/macros.hh index f11b9e1f47..86dfaede8e 100644 --- a/examples/macros.hh +++ b/examples/macros.hh @@ -23,8 +23,13 @@ #define EXAMPLES_CDECL #endif -#define EXAMPLES_CONCAT(a, b) EXAMPLES_CONCAT_1(a, b) -#define EXAMPLES_CONCAT_1(a, b) a##b +#define EXAMPLES_CONCAT2(a, b) EXAMPLES_CONCAT_IMPL(a, b) +#define EXAMPLES_CONCAT3(a, b, c) EXAMPLES_CONCAT2(EXAMPLES_CONCAT2(a, b), c) +#define EXAMPLES_CONCAT4(a, b, c, d) \ + EXAMPLES_CONCAT2(EXAMPLES_CONCAT2(a, b), EXAMPLES_CONCAT2(c, d)) +#define EXAMPLES_CONCAT_IMPL(a, b) a##b + +#define EXAMPLES_STR(e) #e // Unconditionally define `assert()` for examples. #define ASSERT(...) \ From c6728f590950320c06b909464304f34780a5b41c Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 1 Oct 2024 13:36:12 -0500 Subject: [PATCH 04/25] Add support for running components with an instance object --- examples/api/runner.cpp | 19 +++++++++++++++++++ examples/api/runner.hh | 6 ++++++ 2 files changed, 25 insertions(+) diff --git a/examples/api/runner.cpp b/examples/api/runner.cpp index 6bcd4b88d6..88aa9d3366 100644 --- a/examples/api/runner.cpp +++ b/examples/api/runner.cpp @@ -30,6 +30,8 @@ #include #include +#include + #include #if !defined(_MSC_VER) @@ -55,6 +57,7 @@ class runner_type { private: std::vector components; + std::vector components_with_instance; std::vector forking_components; std::minstd_rand::result_type seed = 0u; std::minstd_rand gen; @@ -154,11 +157,21 @@ class runner_type { } #endif // !defined(_MSC_VER) + void run_components_with_instance() { + mongocxx::instance instance; + + run_with_jobs(components_with_instance, jobs); + } + public: void add_component(fn_type fn, const char* name) { components.emplace_back(fn, name); } + void add_component_with_instance(fn_type fn, const char* name) { + components_with_instance.emplace_back(fn, name); + } + void add_forking_component(fn_type fn, const char* name) { forking_components.emplace_back(fn, name); } @@ -197,6 +210,8 @@ class runner_type { return EXIT_SUCCESS; // Return directly from forked processes. } + run_components_with_instance(); + return EXIT_SUCCESS; } }; @@ -209,6 +224,10 @@ void runner_register_component(void (*fn)(), const char* name) { runner.add_component(fn, name); } +void runner_register_component_with_instance(void (*fn)(), const char* name) { + runner.add_component_with_instance(fn, name); +} + void runner_register_forking_component(void (*fn)(), const char* name) { runner.add_forking_component(fn, name); } diff --git a/examples/api/runner.hh b/examples/api/runner.hh index d07a7569e0..9ab709844c 100644 --- a/examples/api/runner.hh +++ b/examples/api/runner.hh @@ -18,6 +18,8 @@ void runner_register_component(void (*fn)(), const char* name); +void runner_register_component_with_instance(void (*fn)(), const char* name); + void runner_register_forking_component(void (*fn)(), const char* name); // Defined by examples/CMakeLists.txt. @@ -34,5 +36,9 @@ void runner_register_forking_component(void (*fn)(), const char* name); #define RUNNER_REGISTER_COMPONENT() \ RUNNER_REGISTER_COMPONENT_IMPL(EXAMPLES_COMPONENT_NAME, ::runner_register_component) +#define RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() \ + RUNNER_REGISTER_COMPONENT_IMPL(EXAMPLES_COMPONENT_NAME, \ + ::runner_register_component_with_instance) + #define RUNNER_REGISTER_FORKING_COMPONENT() \ RUNNER_REGISTER_COMPONENT_IMPL(EXAMPLES_COMPONENT_NAME, ::runner_register_forking_component) From 6e4485bbda90d76eb193092aecb3189efe086a85 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 1 Oct 2024 13:36:13 -0500 Subject: [PATCH 05/25] Add support for running components against a live server --- examples/api/runner.cpp | 61 +++++++++++++++++++++++++++++++++++++++++ examples/api/runner.hh | 13 +++++++++ 2 files changed, 74 insertions(+) diff --git a/examples/api/runner.cpp b/examples/api/runner.cpp index 88aa9d3366..4ad52c5f7f 100644 --- a/examples/api/runner.cpp +++ b/examples/api/runner.cpp @@ -30,7 +30,13 @@ #include #include +#include +#include + +#include +#include #include +#include #include @@ -57,8 +63,15 @@ class runner_type { private: std::vector components; + std::vector components_with_instance; + + std::vector components_for_single; + std::vector components_for_replica; + std::vector components_for_sharded; + std::vector forking_components; + std::minstd_rand::result_type seed = 0u; std::minstd_rand gen; unsigned int jobs = 0; @@ -161,6 +174,30 @@ class runner_type { mongocxx::instance instance; run_with_jobs(components_with_instance, jobs); + + try { + mongocxx::client client{mongocxx::uri{"mongodb://localhost:27017/"}}; + + const auto reply = client["admin"].run_command(bsoncxx::builder::basic::make_document( + bsoncxx::builder::basic::kvp("isMaster", 1))); + + if (reply["msg"]) { + std::cout << "Running API examples against a live sharded server" << std::endl; + run_with_jobs(components_for_sharded, jobs); + run_with_jobs(components_for_replica, jobs); + run_with_jobs(components_for_single, jobs); + } else if (reply["setName"]) { + std::cout << "Running API examples against a live replica server" << std::endl; + run_with_jobs(components_for_replica, jobs); + run_with_jobs(components_for_single, jobs); + } else { + std::cout << "Running API examples against a live single server" << std::endl; + run_with_jobs(components_for_single, jobs); + } + } catch (const mongocxx::exception& ex) { + std::cout << "Skipping API examples that require a live server: " << ex.what() + << std::endl; + } } public: @@ -172,6 +209,18 @@ class runner_type { components_with_instance.emplace_back(fn, name); } + void add_component_for_single(fn_type fn, const char* name) { + components_for_single.emplace_back(fn, name); + } + + void add_component_for_replica(fn_type fn, const char* name) { + components_for_replica.emplace_back(fn, name); + } + + void add_component_for_sharded(fn_type fn, const char* name) { + components_for_sharded.emplace_back(fn, name); + } + void add_forking_component(fn_type fn, const char* name) { forking_components.emplace_back(fn, name); } @@ -228,6 +277,18 @@ void runner_register_component_with_instance(void (*fn)(), const char* name) { runner.add_component_with_instance(fn, name); } +void runner_register_component_for_single(void (*fn)(), const char* name) { + runner.add_component_for_single(fn, name); +} + +void runner_register_component_for_replica(void (*fn)(), const char* name) { + runner.add_component_for_replica(fn, name); +} + +void runner_register_component_for_sharded(void (*fn)(), const char* name) { + runner.add_component_for_sharded(fn, name); +} + void runner_register_forking_component(void (*fn)(), const char* name) { runner.add_forking_component(fn, name); } diff --git a/examples/api/runner.hh b/examples/api/runner.hh index 9ab709844c..c40ea0dea0 100644 --- a/examples/api/runner.hh +++ b/examples/api/runner.hh @@ -20,6 +20,10 @@ void runner_register_component(void (*fn)(), const char* name); void runner_register_component_with_instance(void (*fn)(), const char* name); +void runner_register_component_for_single(void (*fn)(), const char* name); +void runner_register_component_for_replica(void (*fn)(), const char* name); +void runner_register_component_for_sharded(void (*fn)(), const char* name); + void runner_register_forking_component(void (*fn)(), const char* name); // Defined by examples/CMakeLists.txt. @@ -40,5 +44,14 @@ void runner_register_forking_component(void (*fn)(), const char* name); RUNNER_REGISTER_COMPONENT_IMPL(EXAMPLES_COMPONENT_NAME, \ ::runner_register_component_with_instance) +#define RUNNER_REGISTER_COMPONENT_FOR_SINGLE() \ + RUNNER_REGISTER_COMPONENT_IMPL(EXAMPLES_COMPONENT_NAME, ::runner_register_component_for_single) + +#define RUNNER_REGISTER_COMPONENT_FOR_REPLICA() \ + RUNNER_REGISTER_COMPONENT_IMPL(EXAMPLES_COMPONENT_NAME, ::runner_register_component_for_replica) + +#define RUNNER_REGISTER_COMPONENT_FOR_SHARDED() \ + RUNNER_REGISTER_COMPONENT_IMPL(EXAMPLES_COMPONENT_NAME, ::runner_register_component_for_sharded) + #define RUNNER_REGISTER_FORKING_COMPONENT() \ RUNNER_REGISTER_COMPONENT_IMPL(EXAMPLES_COMPONENT_NAME, ::runner_register_forking_component) From 7ced355f21af4df34567577c9ffc0395e4e50cb9 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 1 Oct 2024 13:36:13 -0500 Subject: [PATCH 06/25] Add diagnostics for a component thread terminated via uncaught exception --- examples/api/runner.hh | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/examples/api/runner.hh b/examples/api/runner.hh index c40ea0dea0..776ffc4acb 100644 --- a/examples/api/runner.hh +++ b/examples/api/runner.hh @@ -14,6 +14,8 @@ #pragma once +#include + #include void runner_register_component(void (*fn)(), const char* name); @@ -31,10 +33,19 @@ void runner_register_forking_component(void (*fn)(), const char* name); #error "EXAMPLES_COMPONENT_NAME is not defined!" #endif // !defined(EXAMPLES_COMPONENT_NAME) -#define RUNNER_REGISTER_COMPONENT_IMPL(name, register_fn) \ - static void EXAMPLES_CONCAT3(name, _entry_point_, __LINE__)(void); \ - static int EXAMPLES_CONCAT2(name, _registrator) = \ - ((register_fn)(&EXAMPLES_CONCAT3(name, _entry_point_, __LINE__), EXAMPLES_STR(name)), 0); \ +#define RUNNER_REGISTER_COMPONENT_IMPL(name, register_fn) \ + static void EXAMPLES_CONCAT3(name, _entry_point_, __LINE__)(void); \ + static void EXAMPLES_CONCAT4(name, _entry_point_, __LINE__, _guarded)(void) try { \ + EXAMPLES_CONCAT3(name, _entry_point_, __LINE__)(); \ + } catch (...) { \ + std::cout << EXAMPLES_STR(name) ":" << __LINE__ << ": failed: uncaught exception" \ + << std::endl; \ + throw; \ + } \ + static int EXAMPLES_CONCAT2(name, _registrator) = \ + ((register_fn)(&EXAMPLES_CONCAT4(name, _entry_point_, __LINE__, _guarded), \ + EXAMPLES_STR(name)), \ + 0); \ static void EXAMPLES_CONCAT3(EXAMPLES_COMPONENT_NAME, _entry_point_, __LINE__)(void) #define RUNNER_REGISTER_COMPONENT() \ From 83b6ad903c0c9eb265a6973007cccace027ab778 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 1 Oct 2024 13:36:15 -0500 Subject: [PATCH 07/25] Add db locker to avoid read-write conflicts across examples --- examples/CMakeLists.txt | 2 ++ examples/api/db_lock.cpp | 49 ++++++++++++++++++++++++++++++++++++++++ examples/api/db_lock.hh | 42 ++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 examples/api/db_lock.cpp create mode 100644 examples/api/db_lock.hh diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 162db903d6..ddf99810f1 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -170,6 +170,8 @@ function (add_abi_examples) # Avoid MAX_PATH errors on Windows due to long target names by using a single `api-runner` . add_examples_executable(api/runner.cpp LIBRARIES mongocxx_target Threads::Threads) + target_sources (api-runner PRIVATE "api/runner.cpp") + # Define a unique entry function name per component. foreach (source ${api_examples_sources}) target_sources (api-runner PRIVATE ${source}) diff --git a/examples/api/db_lock.cpp b/examples/api/db_lock.cpp new file mode 100644 index 0000000000..26cca50877 --- /dev/null +++ b/examples/api/db_lock.cpp @@ -0,0 +1,49 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +namespace { + +std::unordered_map db_locks; +std::mutex db_locks_mut; + +} // namespace + +db_lock::~db_lock() { + this->get().drop(); +} + +db_lock::db_lock(mongocxx::client& client, std::string name) : _client_ptr(&client), _name(name) { + ((void)std::lock_guard{db_locks_mut}, + _lock = std::unique_lock(db_locks[name])); + + this->get().drop(); +} + +mongocxx::database db_lock::get() const& { + ASSERT(_client_ptr); + + return _client_ptr->database(_name); +} diff --git a/examples/api/db_lock.hh b/examples/api/db_lock.hh new file mode 100644 index 0000000000..d37beb21c0 --- /dev/null +++ b/examples/api/db_lock.hh @@ -0,0 +1,42 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include +#include + +#include +#include + +// Ensure exclusive access to the associated database. +// For convenience, drops the database on both lock and unlock. +class db_lock { + private: + mongocxx::client* _client_ptr; + std::string _name; + std::unique_lock _lock; + + public: + ~db_lock(); + + db_lock(db_lock&&) = delete; + db_lock& operator=(db_lock&&) = delete; + db_lock(const db_lock&) = delete; + db_lock& operator=(const db_lock&) = delete; + + db_lock(mongocxx::client& client, std::string name); + + mongocxx::database get() const&; +}; From 0eaf9bc9fba0cc7e5bd623a7d7cb650cfdd149c0 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 1 Oct 2024 13:36:15 -0500 Subject: [PATCH 08/25] Add helper to set majority read/write concern for collections --- examples/api/concern.cpp | 48 ++++++++++++++++++++++++++++++++++++++++ examples/api/concern.hh | 27 ++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 examples/api/concern.cpp create mode 100644 examples/api/concern.hh diff --git a/examples/api/concern.cpp b/examples/api/concern.cpp new file mode 100644 index 0000000000..00af68194b --- /dev/null +++ b/examples/api/concern.cpp @@ -0,0 +1,48 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include +#include + +#include + +// Preferred default for most operations. +mongocxx::collection set_rw_concern_majority(mongocxx::collection coll) { + coll.read_concern(rc_majority()); + coll.write_concern(wc_majority()); + return coll; +} + +// Preferred default for most operations. +mongocxx::database set_rw_concern_majority(mongocxx::database db) { + db.read_concern(rc_majority()); + db.write_concern(wc_majority()); + return db; +} + +mongocxx::read_concern rc_majority() { + mongocxx::read_concern rc; + rc.acknowledge_level(mongocxx::read_concern::level::k_majority); + return rc; +} + +mongocxx::write_concern wc_majority() { + mongocxx::write_concern wc; + wc.majority(std::chrono::milliseconds(0)); + return wc; +} diff --git a/examples/api/concern.hh b/examples/api/concern.hh new file mode 100644 index 0000000000..f0c3a20186 --- /dev/null +++ b/examples/api/concern.hh @@ -0,0 +1,27 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include +#include +#include +#include + +// Preferred default for most operations. +mongocxx::collection set_rw_concern_majority(mongocxx::collection coll); +mongocxx::database set_rw_concern_majority(mongocxx::database db); + +mongocxx::read_concern rc_majority(); +mongocxx::write_concern wc_majority(); From f65dfb20d1f53b9cfa24f37719a1191b2fba628d Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 1 Oct 2024 13:36:15 -0500 Subject: [PATCH 09/25] Add EXAMPLES_COMPONENT_NAME_STR --- examples/api/runner.hh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/api/runner.hh b/examples/api/runner.hh index 776ffc4acb..684553624b 100644 --- a/examples/api/runner.hh +++ b/examples/api/runner.hh @@ -33,6 +33,9 @@ void runner_register_forking_component(void (*fn)(), const char* name); #error "EXAMPLES_COMPONENT_NAME is not defined!" #endif // !defined(EXAMPLES_COMPONENT_NAME) +#define EXAMPLES_COMPONENT_NAME_STR EXAMPLES_COMPONENT_NAME_STR_IMPL(EXAMPLES_COMPONENT_NAME) +#define EXAMPLES_COMPONENT_NAME_STR_IMPL(name) EXAMPLES_STR(name) + #define RUNNER_REGISTER_COMPONENT_IMPL(name, register_fn) \ static void EXAMPLES_CONCAT3(name, _entry_point_, __LINE__)(void); \ static void EXAMPLES_CONCAT4(name, _entry_point_, __LINE__, _guarded)(void) try { \ From c91c7f28572732b104e127cac20b687beefe8995 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 1 Oct 2024 13:36:16 -0500 Subject: [PATCH 10/25] Add mongocxx instance examples --- docs/api/mongocxx/examples/instance.md | 21 ++++++++ .../examples/instance/basic_usage.cpp | 51 ++++++++++++++++++ .../mongocxx/examples/instance/current.cpp | 53 +++++++++++++++++++ .../mongocxx/examples/instance/destroyed.cpp | 50 +++++++++++++++++ .../mongocxx/examples/instance/recreation.cpp | 48 +++++++++++++++++ src/mongocxx/include/mongocxx/doc.hpp | 8 +++ 6 files changed, 231 insertions(+) create mode 100644 docs/api/mongocxx/examples/instance.md create mode 100644 examples/api/mongocxx/examples/instance/basic_usage.cpp create mode 100644 examples/api/mongocxx/examples/instance/current.cpp create mode 100644 examples/api/mongocxx/examples/instance/destroyed.cpp create mode 100644 examples/api/mongocxx/examples/instance/recreation.cpp diff --git a/docs/api/mongocxx/examples/instance.md b/docs/api/mongocxx/examples/instance.md new file mode 100644 index 0000000000..12918aa9af --- /dev/null +++ b/docs/api/mongocxx/examples/instance.md @@ -0,0 +1,21 @@ +# Initialize the C++ Driver + +## Basic Usage + +@snippet api/mongocxx/examples/instance/basic_usage.cpp Example + +## With Static Lifetime + +@warning This pattern depends on an exit-time destructor with indeterminate order relative to other objects with static lifetime being destroyed. + +@snippet api/mongocxx/examples/instance/current.cpp Example + +# Errors + +## Instance Recreation + +@snippet api/mongocxx/examples/instance/recreation.cpp Example + +## Destroyed Instance + +@snippet api/mongocxx/examples/instance/destroyed.cpp Example diff --git a/examples/api/mongocxx/examples/instance/basic_usage.cpp b/examples/api/mongocxx/examples/instance/basic_usage.cpp new file mode 100644 index 0000000000..d63e65c3de --- /dev/null +++ b/examples/api/mongocxx/examples/instance/basic_usage.cpp @@ -0,0 +1,51 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include + +namespace { + +template +void use(Args&&...) {} + +// [Example] +void example() { + // Do not use mongocxx library interfaces at this point! + + { + // Initialize the MongoDB C++ Driver. + mongocxx::instance instance; + + ASSERT(&mongocxx::instance::current() == &instance); + + // Use mongocxx library interfaces at this point. + use(mongocxx::client{}); + + // Cleanup the MongoDB C++ Driver. + } + + // Do not use mongocxx library interfaces at this point! +} +// [Example] + +} // namespace + +RUNNER_REGISTER_FORKING_COMPONENT() { + example(); +} diff --git a/examples/api/mongocxx/examples/instance/current.cpp b/examples/api/mongocxx/examples/instance/current.cpp new file mode 100644 index 0000000000..dfdfc78958 --- /dev/null +++ b/examples/api/mongocxx/examples/instance/current.cpp @@ -0,0 +1,53 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include + +namespace { + +template +void use(Args&&...) {} + +// [Example] +void example() { + // Do not use mongocxx library interfaces at this point! + + { + // Initialize the MongoDB C++ Driver. + auto& instance = mongocxx::instance::current(); + + ASSERT(&mongocxx::instance::current() == &instance); + + // Use mongocxx library interfaces at this point. + use(mongocxx::client{}); + } + + // Use mongocxx library interfaces at this point. + use(mongocxx::client{}); +} + +// Cleanup the MongoDB C++ Driver after returning from `main()` with indeterminate order relative to +// other objects with static lifetime being destroyed. +// [Example] + +} // namespace + +RUNNER_REGISTER_FORKING_COMPONENT() { + example(); +} diff --git a/examples/api/mongocxx/examples/instance/destroyed.cpp b/examples/api/mongocxx/examples/instance/destroyed.cpp new file mode 100644 index 0000000000..0d3e8a25f0 --- /dev/null +++ b/examples/api/mongocxx/examples/instance/destroyed.cpp @@ -0,0 +1,50 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + { mongocxx::instance instance; } // Initialize and cleanup. + + try { + mongocxx::instance instance; // Throws. + + ASSERT(false && "should not reach this point"); + } catch (const mongocxx::exception& ex) { + ASSERT(ex.code() == mongocxx::error_code::k_cannot_recreate_instance); + } + + try { + auto& instance = mongocxx::instance::current(); // Throws. + + ASSERT(false && "should not reach this point"); + } catch (const mongocxx::exception& ex) { + ASSERT(ex.code() == mongocxx::error_code::k_instance_destroyed); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_FORKING_COMPONENT() { + example(); +} diff --git a/examples/api/mongocxx/examples/instance/recreation.cpp b/examples/api/mongocxx/examples/instance/recreation.cpp new file mode 100644 index 0000000000..4663f25b3a --- /dev/null +++ b/examples/api/mongocxx/examples/instance/recreation.cpp @@ -0,0 +1,48 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + { + mongocxx::instance instance; + + ASSERT(&mongocxx::instance::current() == &instance); + + try { + mongocxx::instance another_instance; // Throws. + + ASSERT(false && "should not reach this point"); + } catch (const mongocxx::exception& ex) { + ASSERT(ex.code() == mongocxx::error_code::k_cannot_recreate_instance); + } + + ASSERT(&mongocxx::instance::current() == &instance); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_FORKING_COMPONENT() { + example(); +} diff --git a/src/mongocxx/include/mongocxx/doc.hpp b/src/mongocxx/include/mongocxx/doc.hpp index 5ea25b9f2d..2a53eebc55 100644 --- a/src/mongocxx/include/mongocxx/doc.hpp +++ b/src/mongocxx/include/mongocxx/doc.hpp @@ -95,6 +95,14 @@ /// /// @page topic-mongocxx-examples How-To Guides /// @brief Examples of how to use the mongocxx library. +/// @li @subpage topic-mongocxx-examples-instance +/// + +/// +/// @page topic-mongocxx-examples-instance Instance +/// @brief How to use a MongoDB C++ Driver instance. +/// @tableofcontents +/// @include{doc} api/mongocxx/examples/instance.md /// /// From f17b848bbabc2fd56a120190b3b9402d39d8550c Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 1 Oct 2024 13:36:16 -0500 Subject: [PATCH 11/25] Add mongocxx logger examples --- docs/api/mongocxx/examples/logger.md | 7 +++ .../mongocxx/examples/logger/basic_usage.cpp | 63 +++++++++++++++++++ .../mongocxx/examples/logger/to_string.cpp | 51 +++++++++++++++ src/mongocxx/include/mongocxx/doc.hpp | 8 +++ 4 files changed, 129 insertions(+) create mode 100644 docs/api/mongocxx/examples/logger.md create mode 100644 examples/api/mongocxx/examples/logger/basic_usage.cpp create mode 100644 examples/api/mongocxx/examples/logger/to_string.cpp diff --git a/docs/api/mongocxx/examples/logger.md b/docs/api/mongocxx/examples/logger.md new file mode 100644 index 0000000000..7c9ae49a43 --- /dev/null +++ b/docs/api/mongocxx/examples/logger.md @@ -0,0 +1,7 @@ +# Basic Usage + +@snippet api/mongocxx/examples/logger/basic_usage.cpp Example + +# Convert a Log Level to a String + +@snippet api/mongocxx/examples/logger/to_string.cpp Example diff --git a/examples/api/mongocxx/examples/logger/basic_usage.cpp b/examples/api/mongocxx/examples/logger/basic_usage.cpp new file mode 100644 index 0000000000..76364fc9ee --- /dev/null +++ b/examples/api/mongocxx/examples/logger/basic_usage.cpp @@ -0,0 +1,63 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include + +#include +#include + +#include +#include + +namespace { + +// [Example] +class example_logger : public mongocxx::logger { + private: + int* counter_ptr; + + public: + explicit example_logger(int* ptr) : counter_ptr(ptr) {} + + void operator()(mongocxx::log_level level, + bsoncxx::stdx::string_view domain, + bsoncxx::stdx::string_view message) noexcept override { + ASSERT(level == mongocxx::log_level::k_info); + ASSERT(domain.compare("mongocxx") == 0); + ASSERT(message.compare("libmongoc logging callback enabled") == 0); + + *counter_ptr += 1; + } +}; + +void example() { + int counter = 0; + + // Use `std::make_unique` with C++14 or newer. + auto logger = std::unique_ptr(new example_logger{&counter}); + + // Emit the informational mongocxx log message: "libmongoc logging callback enabled". + mongocxx::instance instance{std::move(logger)}; + + ASSERT(counter == 1); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_FORKING_COMPONENT() { + example(); +} diff --git a/examples/api/mongocxx/examples/logger/to_string.cpp b/examples/api/mongocxx/examples/logger/to_string.cpp new file mode 100644 index 0000000000..43274e6077 --- /dev/null +++ b/examples/api/mongocxx/examples/logger/to_string.cpp @@ -0,0 +1,51 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include + +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + bsoncxx::stdx::string_view error = mongocxx::to_string(mongocxx::log_level::k_error); + bsoncxx::stdx::string_view critical = mongocxx::to_string(mongocxx::log_level::k_critical); + bsoncxx::stdx::string_view warning = mongocxx::to_string(mongocxx::log_level::k_warning); + bsoncxx::stdx::string_view message = mongocxx::to_string(mongocxx::log_level::k_message); + bsoncxx::stdx::string_view info = mongocxx::to_string(mongocxx::log_level::k_info); + bsoncxx::stdx::string_view debug = mongocxx::to_string(mongocxx::log_level::k_debug); + bsoncxx::stdx::string_view trace = mongocxx::to_string(mongocxx::log_level::k_trace); + + ASSERT(error.compare("error") == 0); + ASSERT(critical.compare("critical") == 0); + ASSERT(warning.compare("warning") == 0); + ASSERT(message.compare("message") == 0); + ASSERT(info.compare("info") == 0); + ASSERT(debug.compare("debug") == 0); + ASSERT(trace.compare("trace") == 0); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_FORKING_COMPONENT() { + example(); +} diff --git a/src/mongocxx/include/mongocxx/doc.hpp b/src/mongocxx/include/mongocxx/doc.hpp index 2a53eebc55..284d35d2c0 100644 --- a/src/mongocxx/include/mongocxx/doc.hpp +++ b/src/mongocxx/include/mongocxx/doc.hpp @@ -96,6 +96,7 @@ /// @page topic-mongocxx-examples How-To Guides /// @brief Examples of how to use the mongocxx library. /// @li @subpage topic-mongocxx-examples-instance +/// @li @subpage topic-mongocxx-examples-logger /// /// @@ -105,6 +106,13 @@ /// @include{doc} api/mongocxx/examples/instance.md /// +/// +/// @page topic-mongocxx-examples-logger Logger +/// @brief How to use a custom logger with a MongoDB C++ Driver instance. +/// @tableofcontents +/// @include{doc} api/mongocxx/examples/logger.md +/// + /// /// @page topic-mongocxx-about Explanation /// @brief Design decisions, history, and other technical details about the mongocxx library. From 7c0d9cf6d7cfa1287161f9ebe5858c61eb4ecf3f Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 1 Oct 2024 13:36:16 -0500 Subject: [PATCH 12/25] Add mongocxx URI examples --- docs/api/mongocxx/examples/uri.md | 33 ++++++++++ .../api/mongocxx/examples/uri/all_options.cpp | 49 ++++++++++++++ .../api/mongocxx/examples/uri/basic_usage.cpp | 40 ++++++++++++ .../mongocxx/examples/uri/default_value.cpp | 40 ++++++++++++ examples/api/mongocxx/examples/uri/hosts.cpp | 54 ++++++++++++++++ .../api/mongocxx/examples/uri/invalid.cpp | 59 +++++++++++++++++ .../api/mongocxx/examples/uri/optional.cpp | 64 +++++++++++++++++++ .../api/mongocxx/examples/uri/userpass.cpp | 50 +++++++++++++++ src/mongocxx/include/mongocxx/doc.hpp | 10 +++ 9 files changed, 399 insertions(+) create mode 100644 docs/api/mongocxx/examples/uri.md create mode 100644 examples/api/mongocxx/examples/uri/all_options.cpp create mode 100644 examples/api/mongocxx/examples/uri/basic_usage.cpp create mode 100644 examples/api/mongocxx/examples/uri/default_value.cpp create mode 100644 examples/api/mongocxx/examples/uri/hosts.cpp create mode 100644 examples/api/mongocxx/examples/uri/invalid.cpp create mode 100644 examples/api/mongocxx/examples/uri/optional.cpp create mode 100644 examples/api/mongocxx/examples/uri/userpass.cpp diff --git a/docs/api/mongocxx/examples/uri.md b/docs/api/mongocxx/examples/uri.md new file mode 100644 index 0000000000..5898baea6d --- /dev/null +++ b/docs/api/mongocxx/examples/uri.md @@ -0,0 +1,33 @@ +# Create a URI + +## Basic Usage + +@snippet api/mongocxx/examples/uri/basic_usage.cpp Example + +## Default Value + +@snippet api/mongocxx/examples/uri/default_value.cpp Example + +# Query a URI + +## User Credentials + +@snippet api/mongocxx/examples/uri/userpass.cpp Example + +## List of Hosts + +@snippet api/mongocxx/examples/uri/hosts.cpp Example + +## Optional Options + +@snippet api/mongocxx/examples/uri/optional.cpp Example + +## All URI Options + +@snippet api/mongocxx/examples/uri/all_options.cpp Example + +# Error Handling + +## Invalid URI + +@snippet api/mongocxx/examples/uri/invalid.cpp Example diff --git a/examples/api/mongocxx/examples/uri/all_options.cpp b/examples/api/mongocxx/examples/uri/all_options.cpp new file mode 100644 index 0000000000..a474dfda52 --- /dev/null +++ b/examples/api/mongocxx/examples/uri/all_options.cpp @@ -0,0 +1,49 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include + +#include + +#include +#include + +namespace { + +// [Example] +void example() { + mongocxx::uri uri{"mongodb://localhost:27017/?appName=example&tls=true&maxPoolSize=10"}; + + bsoncxx::document::view options = uri.options(); + + ASSERT(options["appname"]); + ASSERT(options["appname"].get_string().value.compare("example") == 0); + + ASSERT(options["tls"]); + ASSERT(options["tls"].get_bool().value == true); + + ASSERT(options["maxpoolsize"]); + ASSERT(options["maxpoolsize"].get_int32().value == 10); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + example(); +} diff --git a/examples/api/mongocxx/examples/uri/basic_usage.cpp b/examples/api/mongocxx/examples/uri/basic_usage.cpp new file mode 100644 index 0000000000..cb844ec350 --- /dev/null +++ b/examples/api/mongocxx/examples/uri/basic_usage.cpp @@ -0,0 +1,40 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include + +namespace { + +// [Example] +void example() { + const auto uri_str = "mongodb://bob:pwd123@localhost:27017/?tls=true"; + + mongocxx::uri uri{uri_str}; + + ASSERT(uri.to_string() == uri_str); + + ASSERT(uri.username().compare("bob") == 0); + ASSERT(uri.password() == "pwd123"); + ASSERT(uri.tls() == true); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + example(); +} diff --git a/examples/api/mongocxx/examples/uri/default_value.cpp b/examples/api/mongocxx/examples/uri/default_value.cpp new file mode 100644 index 0000000000..2227fc6dec --- /dev/null +++ b/examples/api/mongocxx/examples/uri/default_value.cpp @@ -0,0 +1,40 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include + +namespace { + +// [Example] +void example() { + const auto uri_str = "mongodb://localhost:27017"; + + mongocxx::uri a; + mongocxx::uri b{uri_str}; + mongocxx::uri c{mongocxx::uri::k_default_uri}; + + ASSERT(a.to_string() == uri_str); + ASSERT(b.to_string() == uri_str); + ASSERT(c.to_string() == uri_str); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + example(); +} diff --git a/examples/api/mongocxx/examples/uri/hosts.cpp b/examples/api/mongocxx/examples/uri/hosts.cpp new file mode 100644 index 0000000000..c587c26261 --- /dev/null +++ b/examples/api/mongocxx/examples/uri/hosts.cpp @@ -0,0 +1,54 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include + +#include +#include + +namespace { + +// [Example] +void example() { + mongocxx::uri uri{"mongodb://127.0.0.1,[::1]:27018,%2Fpath%2Fto.socket:27019"}; + + std::vector hosts = uri.hosts(); + + ASSERT(hosts.size() == 3u); + + const mongocxx::uri::host& first = hosts[0]; + const mongocxx::uri::host& second = hosts[1]; + const mongocxx::uri::host& third = hosts[2]; + + ASSERT(first.name == "127.0.0.1"); + ASSERT(first.port == 27017u); + ASSERT(first.family == 0); // AF_UNSPEC (AP_INET). + + ASSERT(second.name == "::1"); + ASSERT(second.port == 27018u); + ASSERT(second.family != 0); // AF_INET6. + + ASSERT(third.name == "/path/to.socket"); + ASSERT(third.port == 27019u); + ASSERT(third.family != 0); // AF_UNIX. +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + example(); +} diff --git a/examples/api/mongocxx/examples/uri/invalid.cpp b/examples/api/mongocxx/examples/uri/invalid.cpp new file mode 100644 index 0000000000..ca33d36a03 --- /dev/null +++ b/examples/api/mongocxx/examples/uri/invalid.cpp @@ -0,0 +1,59 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + try { + // Missing `mongodb://`. + mongocxx::uri invalid_uri{"invalid"}; + + ASSERT(false && "should not reach this point"); + } catch (const mongocxx::exception& ex) { + ASSERT(ex.code() == mongocxx::error_code::k_invalid_uri); + } + + try { + // Missing `=`. + mongocxx::uri invalid_uri{"mongodb://localhost:27017/?tls"}; + + ASSERT(false && "should not reach this point"); + } catch (const mongocxx::exception& ex) { + ASSERT(ex.code() == mongocxx::error_code::k_invalid_uri); + } + + try { + // Missing user credentials when authMechanism is provided. + mongocxx::uri invalid_uri{"mongodb://localhost:27017/?authMechanism=SCRAM-SHA-256"}; + + ASSERT(false && "should not reach this point"); + } catch (const mongocxx::exception& ex) { + ASSERT(ex.code() == mongocxx::error_code::k_invalid_uri); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + example(); +} diff --git a/examples/api/mongocxx/examples/uri/optional.cpp b/examples/api/mongocxx/examples/uri/optional.cpp new file mode 100644 index 0000000000..cc95d2d0c3 --- /dev/null +++ b/examples/api/mongocxx/examples/uri/optional.cpp @@ -0,0 +1,64 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include + +#include + +#include +#include + +namespace { + +// [Example] +void example() { + { + mongocxx::uri uri{"mongodb://localhost:27017/"}; + + std::string database = uri.database(); + ASSERT(database.empty()); + + auto try_once_opt = uri.server_selection_try_once(); + ASSERT(!try_once_opt); + + auto appname_opt = uri.appname(); + ASSERT(!appname_opt); + } + + { + mongocxx::uri uri{ + "mongodb://localhost:27017/dbName?appName=example&serverSelectionTryOnce=false"}; + + auto database = uri.database(); + ASSERT(database.compare("dbName") == 0); + + auto try_once_opt = uri.server_selection_try_once(); + ASSERT(try_once_opt); + ASSERT(*try_once_opt == false); + + auto appname_opt = uri.appname(); + ASSERT(appname_opt); + ASSERT(appname_opt->compare("example") == 0); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + example(); +} diff --git a/examples/api/mongocxx/examples/uri/userpass.cpp b/examples/api/mongocxx/examples/uri/userpass.cpp new file mode 100644 index 0000000000..77be7aa6d8 --- /dev/null +++ b/examples/api/mongocxx/examples/uri/userpass.cpp @@ -0,0 +1,50 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include + +#include +#include + +namespace { + +// [Example] +void example() { + { + mongocxx::uri uri{"mongodb://localhost:27017/"}; + + ASSERT(uri.username().empty()); + ASSERT(uri.password().empty()); + + ASSERT(uri.tls() == false); + } + + { + mongocxx::uri uri{"mongodb://bob:pwd123@localhost:27017/?tls=true"}; + + ASSERT(uri.username().compare("bob") == 0); + ASSERT(uri.password() == "pwd123"); + + ASSERT(uri.tls() == true); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + example(); +} diff --git a/src/mongocxx/include/mongocxx/doc.hpp b/src/mongocxx/include/mongocxx/doc.hpp index 284d35d2c0..800934510c 100644 --- a/src/mongocxx/include/mongocxx/doc.hpp +++ b/src/mongocxx/include/mongocxx/doc.hpp @@ -97,6 +97,7 @@ /// @brief Examples of how to use the mongocxx library. /// @li @subpage topic-mongocxx-examples-instance /// @li @subpage topic-mongocxx-examples-logger +/// @li @subpage topic-mongocxx-examples-uri /// /// @@ -113,6 +114,15 @@ /// @include{doc} api/mongocxx/examples/logger.md /// +/// +/// @page topic-mongocxx-examples-uri URI +/// @brief How to create and use URIs. +/// @tableofcontents +/// @see [Connection Strings (MongoDB +/// Manual)](https://www.mongodb.com/docs/manual/reference/connection-string/) +/// @include{doc} api/mongocxx/examples/uri.md +/// + /// /// @page topic-mongocxx-about Explanation /// @brief Design decisions, history, and other technical details about the mongocxx library. From 42f1258ddba66d346f89974ac94dbc4624ea41d8 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 1 Oct 2024 13:36:17 -0500 Subject: [PATCH 13/25] Add mongocxx client examples --- docs/api/mongocxx/examples/clients.md | 83 ++++++++++++++++ .../examples/clients/create/pool/basic.cpp | 59 +++++++++++ .../examples/clients/create/pool/options.cpp | 50 ++++++++++ .../clients/create/pool/try_acquire.cpp | 55 +++++++++++ .../examples/clients/create/single/basic.cpp | 44 +++++++++ .../clients/create/single/options/apm.cpp | 56 +++++++++++ .../create/single/options/auto_encryption.cpp | 73 ++++++++++++++ .../create/single/options/stable_api.cpp | 57 +++++++++++ .../clients/create/single/options/tls.cpp | 68 +++++++++++++ .../clients/create/single/with_uri.cpp | 37 +++++++ .../clients/errors/auto_encryption.cpp | 99 +++++++++++++++++++ .../clients/errors/invalid_client.cpp | 45 +++++++++ .../examples/clients/errors/stable_api.cpp | 50 ++++++++++ .../mongocxx/examples/clients/errors/tls.cpp | 52 ++++++++++ .../clients/errors/wait_queue_timeout.cpp | 50 ++++++++++ .../clients/use/list_database_names.cpp | 44 +++++++++ .../use/list_database_names_with_options.cpp | 45 +++++++++ .../examples/clients/use/list_databases.cpp | 50 ++++++++++ .../use/list_databases_with_options.cpp | 55 +++++++++++ mongocryptd.pid | 0 src/mongocxx/include/mongocxx/doc.hpp | 8 ++ 21 files changed, 1080 insertions(+) create mode 100644 docs/api/mongocxx/examples/clients.md create mode 100644 examples/api/mongocxx/examples/clients/create/pool/basic.cpp create mode 100644 examples/api/mongocxx/examples/clients/create/pool/options.cpp create mode 100644 examples/api/mongocxx/examples/clients/create/pool/try_acquire.cpp create mode 100644 examples/api/mongocxx/examples/clients/create/single/basic.cpp create mode 100644 examples/api/mongocxx/examples/clients/create/single/options/apm.cpp create mode 100644 examples/api/mongocxx/examples/clients/create/single/options/auto_encryption.cpp create mode 100644 examples/api/mongocxx/examples/clients/create/single/options/stable_api.cpp create mode 100644 examples/api/mongocxx/examples/clients/create/single/options/tls.cpp create mode 100644 examples/api/mongocxx/examples/clients/create/single/with_uri.cpp create mode 100644 examples/api/mongocxx/examples/clients/errors/auto_encryption.cpp create mode 100644 examples/api/mongocxx/examples/clients/errors/invalid_client.cpp create mode 100644 examples/api/mongocxx/examples/clients/errors/stable_api.cpp create mode 100644 examples/api/mongocxx/examples/clients/errors/tls.cpp create mode 100644 examples/api/mongocxx/examples/clients/errors/wait_queue_timeout.cpp create mode 100644 examples/api/mongocxx/examples/clients/use/list_database_names.cpp create mode 100644 examples/api/mongocxx/examples/clients/use/list_database_names_with_options.cpp create mode 100644 examples/api/mongocxx/examples/clients/use/list_databases.cpp create mode 100644 examples/api/mongocxx/examples/clients/use/list_databases_with_options.cpp create mode 100644 mongocryptd.pid diff --git a/docs/api/mongocxx/examples/clients.md b/docs/api/mongocxx/examples/clients.md new file mode 100644 index 0000000000..5fb3bf0d7d --- /dev/null +++ b/docs/api/mongocxx/examples/clients.md @@ -0,0 +1,83 @@ +# Create a Client + +## Single + +### Basic Usage + +@snippet api/mongocxx/examples/clients/create/single/basic.cpp Example + +### With a Custom URI + +@snippet api/mongocxx/examples/clients/create/single/with_uri.cpp Example + +### With Client Options + +#### Stable API Options + +@snippet api/mongocxx/examples/clients/create/single/options/stable_api.cpp Example + +#### TLS Options + +@snippet api/mongocxx/examples/clients/create/single/options/tls.cpp Example + +#### Automatic Encryption Options + +@snippet api/mongocxx/examples/clients/create/single/options/auto_encryption.cpp Example + +#### APM Options + +@snippet api/mongocxx/examples/clients/create/single/options/apm.cpp Example + +## Pool + +### Basic Usage + +@snippet api/mongocxx/examples/clients/create/pool/basic.cpp Example + +### With Client Options + +@snippet api/mongocxx/examples/clients/create/pool/options.cpp Example + +### Try Acquire + +@snippet api/mongocxx/examples/clients/create/pool/try_acquire.cpp Example + +# Use a Client + +## List Databases + +@snippet api/mongocxx/examples/clients/use/list_databases.cpp Example + +## List Databases With Options + +@snippet api/mongocxx/examples/clients/use/list_databases_with_options.cpp Example + +## List Database Names + +@snippet api/mongocxx/examples/clients/use/list_database_names.cpp Example + +## List Database Names With Options + +@snippet api/mongocxx/examples/clients/use/list_database_names_with_options.cpp Example + +# Error Handling + +## Invalid Client + +@snippet api/mongocxx/examples/clients/errors/invalid_client.cpp Example + +## Wait Queue Timeout + +@snippet api/mongocxx/examples/clients/errors/wait_queue_timeout.cpp Example + +## Invalid Stable API Options + +@snippet api/mongocxx/examples/clients/errors/stable_api.cpp Example + +## TLS Not Enabled + +@snippet api/mongocxx/examples/clients/errors/tls.cpp Example + +## Invalid Auto Encryption Options + +@snippet api/mongocxx/examples/clients/errors/auto_encryption.cpp Example diff --git a/examples/api/mongocxx/examples/clients/create/pool/basic.cpp b/examples/api/mongocxx/examples/clients/create/pool/basic.cpp new file mode 100644 index 0000000000..abf0c82752 --- /dev/null +++ b/examples/api/mongocxx/examples/clients/create/pool/basic.cpp @@ -0,0 +1,59 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + { + mongocxx::pool pool; + + mongocxx::pool::entry entry = pool.acquire(); + + ASSERT(entry); + + mongocxx::client& client = *entry; + + ASSERT(client); + ASSERT(client.uri().to_string() == mongocxx::uri::k_default_uri); + } + + { + mongocxx::uri uri{"mongodb://localhost:27017/?serverSelectionTryOnce=true"}; + mongocxx::pool pool{uri}; + + mongocxx::pool::entry entry = pool.acquire(); + + ASSERT(entry); + + mongocxx::client& client = *entry; + + ASSERT(client); + ASSERT(client.uri().to_string() == uri.to_string()); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + example(); +} diff --git a/examples/api/mongocxx/examples/clients/create/pool/options.cpp b/examples/api/mongocxx/examples/clients/create/pool/options.cpp new file mode 100644 index 0000000000..25a1a5606f --- /dev/null +++ b/examples/api/mongocxx/examples/clients/create/pool/options.cpp @@ -0,0 +1,50 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + mongocxx::options::client client_opts; + + // ... set client options. + + mongocxx::uri uri; + mongocxx::pool pool{uri, mongocxx::options::pool{client_opts}}; + + mongocxx::pool::entry entry = pool.acquire(); + + ASSERT(entry); + + mongocxx::client& client = *entry; + + ASSERT(client); + ASSERT(client.uri().to_string() == mongocxx::uri::k_default_uri); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + example(); +} diff --git a/examples/api/mongocxx/examples/clients/create/pool/try_acquire.cpp b/examples/api/mongocxx/examples/clients/create/pool/try_acquire.cpp new file mode 100644 index 0000000000..8fb60fe9f1 --- /dev/null +++ b/examples/api/mongocxx/examples/clients/create/pool/try_acquire.cpp @@ -0,0 +1,55 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + mongocxx::uri uri{"mongodb://localhost:27017/?maxPoolSize=1&waitQueueTimeoutMS=1"}; + mongocxx::pool pool{uri}; + + auto entry_opt = pool.try_acquire(); + + ASSERT(entry_opt); + ASSERT(*entry_opt); + + { + mongocxx::pool::entry hold = std::move(*entry_opt); + + ASSERT(hold); + + entry_opt = pool.try_acquire(); + + ASSERT(!entry_opt); + } + + entry_opt = pool.try_acquire(); + + ASSERT(entry_opt); + ASSERT(*entry_opt); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + example(); +} diff --git a/examples/api/mongocxx/examples/clients/create/single/basic.cpp b/examples/api/mongocxx/examples/clients/create/single/basic.cpp new file mode 100644 index 0000000000..8f4610ab6f --- /dev/null +++ b/examples/api/mongocxx/examples/clients/create/single/basic.cpp @@ -0,0 +1,44 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + { + mongocxx::client client; + + ASSERT(!client); + } + + { + mongocxx::client client{mongocxx::uri{}}; + + ASSERT(client); + ASSERT(client.uri().to_string() == mongocxx::uri::k_default_uri); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + example(); +} diff --git a/examples/api/mongocxx/examples/clients/create/single/options/apm.cpp b/examples/api/mongocxx/examples/clients/create/single/options/apm.cpp new file mode 100644 index 0000000000..9556c1d25c --- /dev/null +++ b/examples/api/mongocxx/examples/clients/create/single/options/apm.cpp @@ -0,0 +1,56 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include + +#include +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void on_command_started_callback(const mongocxx::events::command_started_event& event); + +void example() { + mongocxx::options::apm apm_opts; + + apm_opts.on_command_started(&on_command_started_callback); + // ... other APM options. + + mongocxx::options::client client_opts; + client_opts.apm_opts(apm_opts); + + mongocxx::client client{mongocxx::uri{}, client_opts}; + + ASSERT(client); +} +// [Example] + +void on_command_started_callback(const mongocxx::events::command_started_event& event) { + (void)event; +} + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + example(); +} diff --git a/examples/api/mongocxx/examples/clients/create/single/options/auto_encryption.cpp b/examples/api/mongocxx/examples/clients/create/single/options/auto_encryption.cpp new file mode 100644 index 0000000000..aad7e4480b --- /dev/null +++ b/examples/api/mongocxx/examples/clients/create/single/options/auto_encryption.cpp @@ -0,0 +1,73 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example(bsoncxx::document::view kms_providers) { + mongocxx::options::auto_encryption auto_encryption_opts; + + auto_encryption_opts.key_vault_namespace({"keyvault", "datakeys"}); + auto_encryption_opts.kms_providers(kms_providers); + // ... other automatic encryption options. + + mongocxx::options::client client_opts; + client_opts.auto_encryption_opts(auto_encryption_opts); + + mongocxx::uri uri; + mongocxx::client client{uri, client_opts}; + + ASSERT(client); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + using bsoncxx::builder::basic::kvp; + using bsoncxx::builder::basic::make_document; + + try { + std::uint8_t local_key[96]{}; + + example(make_document(kvp( + "local", + make_document(kvp( + "key", + bsoncxx::types::b_binary{bsoncxx::binary_sub_type::k_binary, 96, local_key}))))); + } catch (const mongocxx::exception& ex) { + if (std::strstr(ex.what(), "ENABLE_CLIENT_SIDE_ENCRYPTION") != nullptr) { + // Library may not be configured with TLS/SSL support enabled. + } else { + throw; + } + } +} diff --git a/examples/api/mongocxx/examples/clients/create/single/options/stable_api.cpp b/examples/api/mongocxx/examples/clients/create/single/options/stable_api.cpp new file mode 100644 index 0000000000..3d01091bfd --- /dev/null +++ b/examples/api/mongocxx/examples/clients/create/single/options/stable_api.cpp @@ -0,0 +1,57 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +mongocxx::client example() { + mongocxx::options::server_api server_api_opts{ + mongocxx::options::server_api::version::k_version_1}; + + server_api_opts.strict(true); + // ... other Stable API options. + + mongocxx::options::client client_opts; + + client_opts.server_api_opts(server_api_opts); + + mongocxx::uri uri; + mongocxx::client client{uri, client_opts}; + + ASSERT(client); + ASSERT(client.uri().to_string() == mongocxx::uri::k_default_uri); + + return client; +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + auto client = example(); + + auto reply = client["admin"].run_command(bsoncxx::from_json(R"({"ping": 1})")); + + ASSERT(reply["ok"] && reply["ok"].get_double().value == 1.0); +} diff --git a/examples/api/mongocxx/examples/clients/create/single/options/tls.cpp b/examples/api/mongocxx/examples/clients/create/single/options/tls.cpp new file mode 100644 index 0000000000..5069f11f9b --- /dev/null +++ b/examples/api/mongocxx/examples/clients/create/single/options/tls.cpp @@ -0,0 +1,68 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +mongocxx::client example() { + mongocxx::options::tls tls_opts; + + // ... set TLS options. + + mongocxx::options::client client_opts; + client_opts.tls_opts(tls_opts); + + mongocxx::uri uri{"mongodb://bob:pwd123@localhost:27017/?tls=true"}; + mongocxx::client client{uri, client_opts}; + + ASSERT(client); + ASSERT(client.uri().to_string() == uri.to_string()); + + return client; +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + try { + auto client = example(); + + auto reply = client["admin"].run_command(bsoncxx::from_json(R"({"ping": 1})")); + + ASSERT(reply["ok"] && reply["ok"].get_double().value == 1.0); + } catch (const mongocxx::exception& ex) { + if (ex.code() == mongocxx::error_code::k_ssl_not_supported) { + // Library may not be configured with TLS/SSL support enabled. + } else if (std::strstr(ex.what(), "suitable server") != nullptr) { + // Authentication may not be supported by the live server. + } else { + throw; + } + } +} diff --git a/examples/api/mongocxx/examples/clients/create/single/with_uri.cpp b/examples/api/mongocxx/examples/clients/create/single/with_uri.cpp new file mode 100644 index 0000000000..644a4053f8 --- /dev/null +++ b/examples/api/mongocxx/examples/clients/create/single/with_uri.cpp @@ -0,0 +1,37 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + mongocxx::uri uri{"mongodb://localhost:27017/?serverSelectionTryOnce=true"}; + mongocxx::client client{uri}; + + ASSERT(client); + ASSERT(client.uri().to_string() == uri.to_string()); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + example(); +} diff --git a/examples/api/mongocxx/examples/clients/errors/auto_encryption.cpp b/examples/api/mongocxx/examples/clients/errors/auto_encryption.cpp new file mode 100644 index 0000000000..489a0dae8b --- /dev/null +++ b/examples/api/mongocxx/examples/clients/errors/auto_encryption.cpp @@ -0,0 +1,99 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + // Missing keyvault namespace. + try { + mongocxx::client client{mongocxx::uri{}, + mongocxx::options::client{}.auto_encryption_opts( + mongocxx::options::auto_encryption{})}; // Throws. + + ASSERT(false && "should not reach this point"); + } catch (const mongocxx::exception& ex) { + // CXX-834: libmongoc error code. + ASSERT(ex.code().category() == mongocxx::server_error_category()); + ASSERT(ex.code().value() == 58); // MONGOC_ERROR_CLIENT_INVALID_ENCRYPTION_ARG + } + + // Invalid KMS providers. + try { + mongocxx::client client{ + mongocxx::uri{}, + mongocxx::options::client{}.auto_encryption_opts( + mongocxx::options::auto_encryption{} + .key_vault_namespace({"keyvault", "datakeys"}) + .kms_providers(bsoncxx::from_json(R"({"invalid": 1})")))}; // Throws. + + ASSERT(false && "should not reach this point"); + } catch (const mongocxx::exception& ex) { + // CXX-834: libmongocrypt error code. + ASSERT(ex.code().category() == mongocxx::server_error_category()); + ASSERT(ex.code().value() == 1); // MONGOCRYPT_GENERIC_ERROR_CODE + } + + // Incompatible options. + try { + mongocxx::client client{ + mongocxx::uri{}, + mongocxx::options::client{}.auto_encryption_opts( + mongocxx::options::auto_encryption{}.key_vault_client(nullptr).key_vault_pool( + nullptr))}; // Throws. + + ASSERT(false && "should not reach this point"); + } catch (const mongocxx::exception& ex) { + ASSERT(ex.code() == mongocxx::error_code::k_invalid_parameter); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + using bsoncxx::builder::basic::sub_document; + + try { + (void)mongocxx::client{ + mongocxx::uri{}, + mongocxx::options::client{}.auto_encryption_opts(mongocxx::options::auto_encryption{})}; + + ASSERT(false && "should not reach this point"); + } catch (const mongocxx::exception& ex) { + if (std::strstr(ex.what(), "ENABLE_CLIENT_SIDE_ENCRYPTION") != nullptr) { + // Library may not be configured with TLS/SSL support enabled. + } else { + example(); + } + } +} diff --git a/examples/api/mongocxx/examples/clients/errors/invalid_client.cpp b/examples/api/mongocxx/examples/clients/errors/invalid_client.cpp new file mode 100644 index 0000000000..8348b471d7 --- /dev/null +++ b/examples/api/mongocxx/examples/clients/errors/invalid_client.cpp @@ -0,0 +1,45 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + mongocxx::client client; + + ASSERT(!client); + + try { + mongocxx::uri uri = client.uri(); // DO NOT DO THIS. Throws. + + ASSERT(false && "should not reach this point"); + } catch (const mongocxx::exception& ex) { + ASSERT(ex.code() == mongocxx::error_code::k_invalid_client_object); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + example(); +} diff --git a/examples/api/mongocxx/examples/clients/errors/stable_api.cpp b/examples/api/mongocxx/examples/clients/errors/stable_api.cpp new file mode 100644 index 0000000000..8a1e0173d3 --- /dev/null +++ b/examples/api/mongocxx/examples/clients/errors/stable_api.cpp @@ -0,0 +1,50 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + try { + mongocxx::options::server_api::version version = + mongocxx::options::server_api::version_from_string("0"); // Throws. + + ASSERT(false && "should not reach this point"); + } catch (const mongocxx::exception& ex) { + ASSERT(ex.code() == mongocxx::error_code::k_invalid_parameter); + } + + try { + std::string version = mongocxx::options::server_api::version_to_string( + static_cast(1)); // Throws. + + ASSERT(false && "should not reach this point"); + } catch (const mongocxx::exception& ex) { + ASSERT(ex.code() == mongocxx::error_code::k_invalid_parameter); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + example(); +} diff --git a/examples/api/mongocxx/examples/clients/errors/tls.cpp b/examples/api/mongocxx/examples/clients/errors/tls.cpp new file mode 100644 index 0000000000..7ec5cc8a9c --- /dev/null +++ b/examples/api/mongocxx/examples/clients/errors/tls.cpp @@ -0,0 +1,52 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + try { + mongocxx::client client{ + mongocxx::uri{}, + mongocxx::options::client{}.tls_opts(mongocxx::options::tls{})}; // Throws. + + ASSERT(false && "should not reach this point"); + } catch (const mongocxx::exception& ex) { + ASSERT( + // When TLS/SSL is enabled for both mongocxx and libmongoc. + ex.code() == mongocxx::error_code::k_invalid_parameter || + + // When TLS/SSL is not enabled for either mongocxx or libmongoc. + ex.code() == mongocxx::error_code::k_ssl_not_supported || + + false); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + example(); +} diff --git a/examples/api/mongocxx/examples/clients/errors/wait_queue_timeout.cpp b/examples/api/mongocxx/examples/clients/errors/wait_queue_timeout.cpp new file mode 100644 index 0000000000..c144327dcb --- /dev/null +++ b/examples/api/mongocxx/examples/clients/errors/wait_queue_timeout.cpp @@ -0,0 +1,50 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example() { + try { + mongocxx::pool pool{ + mongocxx::uri{"mongodb://localhost:27017/?maxPoolSize=1&waitQueueTimeoutMS=1"}}; + + mongocxx::pool::entry hold = pool.acquire(); + + ASSERT(hold); + + mongocxx::pool::entry entry = pool.acquire(); // Throws. + + ASSERT(false && "should not reach this point"); + } catch (const mongocxx::exception& ex) { + ASSERT(ex.code() == mongocxx::error_code::k_pool_wait_queue_timeout); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + example(); +} diff --git a/examples/api/mongocxx/examples/clients/use/list_database_names.cpp b/examples/api/mongocxx/examples/clients/use/list_database_names.cpp new file mode 100644 index 0000000000..26e0b14041 --- /dev/null +++ b/examples/api/mongocxx/examples/clients/use/list_database_names.cpp @@ -0,0 +1,44 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example(mongocxx::client client) { + std::vector names = client.list_database_names(); + + ASSERT(std::count(names.begin(), names.end(), "admin") == 1); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + example(mongocxx::client{mongocxx::uri{}}); +} diff --git a/examples/api/mongocxx/examples/clients/use/list_database_names_with_options.cpp b/examples/api/mongocxx/examples/clients/use/list_database_names_with_options.cpp new file mode 100644 index 0000000000..9d3aa7efb8 --- /dev/null +++ b/examples/api/mongocxx/examples/clients/use/list_database_names_with_options.cpp @@ -0,0 +1,45 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include + +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example(mongocxx::client client) { + auto opts = bsoncxx::from_json(R"({"authorizedDatabases": true})"); + // ... other listDatabases options. + + std::vector names = client.list_database_names(opts.view()); + + ASSERT(std::count(names.begin(), names.end(), "admin") == 0); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + example(mongocxx::client{mongocxx::uri{}}); +} diff --git a/examples/api/mongocxx/examples/clients/use/list_databases.cpp b/examples/api/mongocxx/examples/clients/use/list_databases.cpp new file mode 100644 index 0000000000..20b6252516 --- /dev/null +++ b/examples/api/mongocxx/examples/clients/use/list_databases.cpp @@ -0,0 +1,50 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example(mongocxx::client client) { + mongocxx::cursor databases = client.list_databases(); + + int count = 0; + + for (bsoncxx::document::view doc : databases) { + ASSERT(doc["name"]); + ASSERT(doc["sizeOnDisk"]); + ASSERT(doc["empty"]); + + if (doc["name"].get_string().value.compare("admin") == 0) { + ++count; + } + } + + ASSERT(count == 1); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + example(mongocxx::client{mongocxx::uri{}}); +} diff --git a/examples/api/mongocxx/examples/clients/use/list_databases_with_options.cpp b/examples/api/mongocxx/examples/clients/use/list_databases_with_options.cpp new file mode 100644 index 0000000000..b1b2b34b25 --- /dev/null +++ b/examples/api/mongocxx/examples/clients/use/list_databases_with_options.cpp @@ -0,0 +1,55 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example(mongocxx::client client) { + auto opts = bsoncxx::from_json(R"({"nameOnly": true})"); + // ... other listDatabases options. + + mongocxx::cursor databases = client.list_databases(opts.view()); + + int count = 0; + + for (bsoncxx::document::view doc : databases) { + ASSERT(doc["name"]); + ASSERT(!doc["sizeOnDisk"]); + ASSERT(!doc["empty"]); + + if (doc["name"].get_string().value.compare("admin") == 0) { + ++count; + } + } + + ASSERT(count == 1); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + example(mongocxx::client{mongocxx::uri{}}); +} diff --git a/mongocryptd.pid b/mongocryptd.pid new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/mongocxx/include/mongocxx/doc.hpp b/src/mongocxx/include/mongocxx/doc.hpp index 800934510c..70363747c8 100644 --- a/src/mongocxx/include/mongocxx/doc.hpp +++ b/src/mongocxx/include/mongocxx/doc.hpp @@ -98,6 +98,7 @@ /// @li @subpage topic-mongocxx-examples-instance /// @li @subpage topic-mongocxx-examples-logger /// @li @subpage topic-mongocxx-examples-uri +/// @li @subpage topic-mongocxx-examples-clients /// /// @@ -123,6 +124,13 @@ /// @include{doc} api/mongocxx/examples/uri.md /// +/// +/// @page topic-mongocxx-examples-clients Clients +/// @brief How to use clients and client pools. +/// @tableofcontents +/// @include{doc} api/mongocxx/examples/clients.md +/// + /// /// @page topic-mongocxx-about Explanation /// @brief Design decisions, history, and other technical details about the mongocxx library. From 4b0f1e29781b2799d38e5ecea2fe10259c015460 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 1 Oct 2024 13:36:17 -0500 Subject: [PATCH 14/25] Add mongocxx database examples --- docs/api/mongocxx/examples/databases.md | 43 +++++++++ .../examples/databases/create_collection.cpp | 49 ++++++++++ .../create_collection_with_options.cpp | 53 +++++++++++ .../api/mongocxx/examples/databases/drop.cpp | 61 ++++++++++++ .../examples/databases/has_collection.cpp | 46 +++++++++ .../databases/list_collection_names.cpp | 73 ++++++++++++++ .../examples/databases/list_collections.cpp | 95 +++++++++++++++++++ .../mongocxx/examples/databases/obtain.cpp | 39 ++++++++ .../api/mongocxx/examples/databases/rc.cpp | 60 ++++++++++++ .../api/mongocxx/examples/databases/rp.cpp | 57 +++++++++++ .../examples/databases/run_command.cpp | 44 +++++++++ .../api/mongocxx/examples/databases/wc.cpp | 63 ++++++++++++ src/mongocxx/include/mongocxx/doc.hpp | 8 ++ 13 files changed, 691 insertions(+) create mode 100644 docs/api/mongocxx/examples/databases.md create mode 100644 examples/api/mongocxx/examples/databases/create_collection.cpp create mode 100644 examples/api/mongocxx/examples/databases/create_collection_with_options.cpp create mode 100644 examples/api/mongocxx/examples/databases/drop.cpp create mode 100644 examples/api/mongocxx/examples/databases/has_collection.cpp create mode 100644 examples/api/mongocxx/examples/databases/list_collection_names.cpp create mode 100644 examples/api/mongocxx/examples/databases/list_collections.cpp create mode 100644 examples/api/mongocxx/examples/databases/obtain.cpp create mode 100644 examples/api/mongocxx/examples/databases/rc.cpp create mode 100644 examples/api/mongocxx/examples/databases/rp.cpp create mode 100644 examples/api/mongocxx/examples/databases/run_command.cpp create mode 100644 examples/api/mongocxx/examples/databases/wc.cpp diff --git a/docs/api/mongocxx/examples/databases.md b/docs/api/mongocxx/examples/databases.md new file mode 100644 index 0000000000..c1574d6e09 --- /dev/null +++ b/docs/api/mongocxx/examples/databases.md @@ -0,0 +1,43 @@ +# Obtain a Database + +@snippet examples/api/mongocxx/examples/databases/obtain.cpp Example + +# Database Operations + +## Drop a Database + +@snippet examples/api/mongocxx/examples/databases/drop.cpp Example + +## Run a Command + +@snippet examples/api/mongocxx/examples/databases/run_command.cpp Example + +## Set a Read Concern + +@snippet examples/api/mongocxx/examples/databases/rc.cpp Example + +## Set a Write Concern + +@snippet examples/api/mongocxx/examples/databases/wc.cpp Example + +# Collection Operations + +## Create a Collection + +@snippet examples/api/mongocxx/examples/databases/create_collection.cpp Example + +## Create a Collection With Options + +@snippet examples/api/mongocxx/examples/databases/create_collection_with_options.cpp Example + +## Query a Collection Exists + +@snippet examples/api/mongocxx/examples/databases/has_collection.cpp Example + +## List Collections in the Database + +@snippet examples/api/mongocxx/examples/databases/list_collections.cpp Example + +## List Collection Names in the Database + +@snippet examples/api/mongocxx/examples/databases/list_collection_names.cpp Example diff --git a/examples/api/mongocxx/examples/databases/create_collection.cpp b/examples/api/mongocxx/examples/databases/create_collection.cpp new file mode 100644 index 0000000000..2857cb14ff --- /dev/null +++ b/examples/api/mongocxx/examples/databases/create_collection.cpp @@ -0,0 +1,49 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +void example(mongocxx::database db) { + ASSERT(!db.has_collection("coll")); + + mongocxx::collection coll = db.create_collection("coll"); + + ASSERT(coll); + + ASSERT(db.has_collection("coll")); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + example(set_rw_concern_majority(guard.get())); + } +} diff --git a/examples/api/mongocxx/examples/databases/create_collection_with_options.cpp b/examples/api/mongocxx/examples/databases/create_collection_with_options.cpp new file mode 100644 index 0000000000..f42a41a5f4 --- /dev/null +++ b/examples/api/mongocxx/examples/databases/create_collection_with_options.cpp @@ -0,0 +1,53 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +void example(mongocxx::database db) { + ASSERT(!db.has_collection("coll")); + + auto opts = bsoncxx::from_json(R"({"validationLevel": "strict", "validationAction": "error"})"); + // ... other create options. + + mongocxx::collection coll = db.create_collection("coll", opts.view()); + + ASSERT(coll); + + ASSERT(db.has_collection("coll")); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + example(set_rw_concern_majority(guard.get())); + } +} diff --git a/examples/api/mongocxx/examples/databases/drop.cpp b/examples/api/mongocxx/examples/databases/drop.cpp new file mode 100644 index 0000000000..18da841590 --- /dev/null +++ b/examples/api/mongocxx/examples/databases/drop.cpp @@ -0,0 +1,61 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +namespace { + +// [Example] +void example(mongocxx::client& client) { + mongocxx::database db = client["db"]; + + { + std::vector names = client.list_database_names(); + + ASSERT(std::count(names.begin(), names.end(), "db") == 1); + } + + db.drop(); + + { + std::vector names = client.list_database_names(); + + ASSERT(std::count(names.begin(), names.end(), "db") == 0); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, "db"}; + + (void)guard.get().create_collection("coll"); + + example(client); + } +} diff --git a/examples/api/mongocxx/examples/databases/has_collection.cpp b/examples/api/mongocxx/examples/databases/has_collection.cpp new file mode 100644 index 0000000000..4f1e09c2dc --- /dev/null +++ b/examples/api/mongocxx/examples/databases/has_collection.cpp @@ -0,0 +1,46 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include +#include + +namespace { + +// [Example] +void example(mongocxx::database db) { + ASSERT(db.has_collection("present")); + ASSERT(!db.has_collection("missing")); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto db = guard.get(); + + (void)db.create_collection("present"); + + example(db); + } +} diff --git a/examples/api/mongocxx/examples/databases/list_collection_names.cpp b/examples/api/mongocxx/examples/databases/list_collection_names.cpp new file mode 100644 index 0000000000..1dd03ab23f --- /dev/null +++ b/examples/api/mongocxx/examples/databases/list_collection_names.cpp @@ -0,0 +1,73 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +void example(mongocxx::database db) { + // Basic usage. + { + std::vector names = db.list_collection_names(); + + ASSERT(std::count(names.begin(), names.end(), "a") == 1); // Present. + ASSERT(std::count(names.begin(), names.end(), "b") == 1); // Present. + ASSERT(std::count(names.begin(), names.end(), "c") == 0); // Missing. + } + + // With a filter. + { + auto filter = bsoncxx::from_json(R"({"name": {"$ne": "b"}})"); + + std::vector names = db.list_collection_names(filter.view()); + + ASSERT(std::count(names.begin(), names.end(), "a") == 1); // Present. + ASSERT(std::count(names.begin(), names.end(), "b") == 0); // Filtered. + ASSERT(std::count(names.begin(), names.end(), "c") == 0); // Missing. + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto db = set_rw_concern_majority(guard.get()); + + (void)db.create_collection("a"); + (void)db.create_collection("b"); + // (void)db.create_collection("c"); + + example(db); + } +} diff --git a/examples/api/mongocxx/examples/databases/list_collections.cpp b/examples/api/mongocxx/examples/databases/list_collections.cpp new file mode 100644 index 0000000000..f462047aa3 --- /dev/null +++ b/examples/api/mongocxx/examples/databases/list_collections.cpp @@ -0,0 +1,95 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +namespace { + +// [Example] +void example(mongocxx::database db) { + int a = 0; + int b = 0; + int c = 0; + + auto count_fields = [&a, &b, &c](mongocxx::cursor cursor) { + a = 0; + b = 0; + c = 0; + + for (bsoncxx::document::view doc : cursor) { + ASSERT(doc["name"]); + + auto name = bsoncxx::string::to_string(doc["name"].get_string().value); + + if (name == "a") { + ++a; + } else if (name == "b") { + ++b; + } else if (name == "c") { + ++c; + } + } + }; + + // Basic usage. + { + count_fields(db.list_collections()); + + ASSERT(a == 1); // Present. + ASSERT(b == 1); // Present. + ASSERT(c == 0); // Missing. + } + + // With a filter. + { + auto filter = bsoncxx::from_json(R"({"name": {"$ne": "b"}})"); + + count_fields(db.list_collections(filter.view())); + + ASSERT(a == 1); // Present. + ASSERT(b == 0); // Filtered. + ASSERT(c == 0); // Missing. + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto db = guard.get(); + + (void)db.create_collection("a"); + (void)db.create_collection("b"); + // (void)db.create_collection("c"); + + example(db); + } +} diff --git a/examples/api/mongocxx/examples/databases/obtain.cpp b/examples/api/mongocxx/examples/databases/obtain.cpp new file mode 100644 index 0000000000..1be8db63b6 --- /dev/null +++ b/examples/api/mongocxx/examples/databases/obtain.cpp @@ -0,0 +1,39 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example(mongocxx::client client) { + mongocxx::database db = client["db"]; + + ASSERT(db); + ASSERT(db.name().compare("db") == 0); + + ASSERT(client.database("db").name().compare("db") == 0); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + example(mongocxx::client{mongocxx::uri{}}); +} diff --git a/examples/api/mongocxx/examples/databases/rc.cpp b/examples/api/mongocxx/examples/databases/rc.cpp new file mode 100644 index 0000000000..6023c032ac --- /dev/null +++ b/examples/api/mongocxx/examples/databases/rc.cpp @@ -0,0 +1,60 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#include +#include +#include + +namespace { + +// [Example] +void example(mongocxx::database db) { + using rc_level = mongocxx::read_concern::level; + + // Default. + { + mongocxx::read_concern rc = db.read_concern(); + + ASSERT(rc.acknowledge_level() == rc_level::k_server_default); + } + + // Explicit. + { + mongocxx::read_concern rc; + + rc.acknowledge_level(rc_level::k_majority); + // ... other read concern options. + + db.read_concern(rc); + + ASSERT(db.read_concern() == rc); + ASSERT(db.read_concern().acknowledge_level() == rc_level::k_majority); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + mongocxx::client client{mongocxx::uri{}}; + + ASSERT(client.read_concern() == mongocxx::read_concern{}); + + example(client["db"]); +} diff --git a/examples/api/mongocxx/examples/databases/rp.cpp b/examples/api/mongocxx/examples/databases/rp.cpp new file mode 100644 index 0000000000..6556c0caf9 --- /dev/null +++ b/examples/api/mongocxx/examples/databases/rp.cpp @@ -0,0 +1,57 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example(mongocxx::database db) { + // Default. + { + mongocxx::read_preference rp; + + ASSERT(db.read_preference() == rp); + ASSERT(db.read_preference().mode() == mongocxx::read_preference::read_mode::k_primary); + } + + // Explicit. + { + mongocxx::read_preference rp; + + rp.mode(mongocxx::read_preference::read_mode::k_secondary); + // ... other read preference options. + + db.read_preference(rp); + + ASSERT(db.read_preference() == rp); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + mongocxx::client client{mongocxx::uri{}}; + + ASSERT(client.read_preference() == mongocxx::read_preference{}); + + example(client["db"]); +} diff --git a/examples/api/mongocxx/examples/databases/run_command.cpp b/examples/api/mongocxx/examples/databases/run_command.cpp new file mode 100644 index 0000000000..175164b960 --- /dev/null +++ b/examples/api/mongocxx/examples/databases/run_command.cpp @@ -0,0 +1,44 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example(mongocxx::database db) { + auto cmd = bsoncxx::from_json(R"({"ping": 1})"); + + bsoncxx::document::value reply = db.run_command(cmd.view()); + + ASSERT(reply["ok"]); + ASSERT(reply["ok"].get_double().value == 1.0); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + example(client["db"]); +} diff --git a/examples/api/mongocxx/examples/databases/wc.cpp b/examples/api/mongocxx/examples/databases/wc.cpp new file mode 100644 index 0000000000..fade393213 --- /dev/null +++ b/examples/api/mongocxx/examples/databases/wc.cpp @@ -0,0 +1,63 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example(mongocxx::database db) { + using wc_level = mongocxx::write_concern::level; + + // Default. + { + mongocxx::write_concern wc = db.write_concern(); + + ASSERT(wc.acknowledge_level() == wc_level::k_default); + ASSERT(wc.timeout() == std::chrono::milliseconds(0)); + } + + // Explicit. + { + mongocxx::write_concern wc; + + wc.majority(std::chrono::milliseconds(5000)); + // ... other write concern options. + + db.write_concern(wc); + + ASSERT(db.write_concern() == wc); + ASSERT(db.write_concern().acknowledge_level() == wc_level::k_majority); + ASSERT(db.write_concern().timeout() == std::chrono::seconds(5)); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + mongocxx::client client{mongocxx::uri{}}; + + ASSERT(client.write_concern() == mongocxx::write_concern{}); + + example(client["db"]); +} diff --git a/src/mongocxx/include/mongocxx/doc.hpp b/src/mongocxx/include/mongocxx/doc.hpp index 70363747c8..eedaf695a1 100644 --- a/src/mongocxx/include/mongocxx/doc.hpp +++ b/src/mongocxx/include/mongocxx/doc.hpp @@ -99,6 +99,7 @@ /// @li @subpage topic-mongocxx-examples-logger /// @li @subpage topic-mongocxx-examples-uri /// @li @subpage topic-mongocxx-examples-clients +/// @li @subpage topic-mongocxx-examples-databases /// /// @@ -131,6 +132,13 @@ /// @include{doc} api/mongocxx/examples/clients.md /// +/// +/// @page topic-mongocxx-examples-databases Databases +/// @brief How to obtain and use databases. +/// @tableofcontents +/// @include{doc} api/mongocxx/examples/databases.md +/// + /// /// @page topic-mongocxx-about Explanation /// @brief Design decisions, history, and other technical details about the mongocxx library. From bf9e0aace9f20d8378380c029b0322ed86fad9c3 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 1 Oct 2024 13:36:17 -0500 Subject: [PATCH 15/25] Add mongocxx collection examples --- docs/api/mongocxx/examples/collections.md | 113 ++++++++++++++++++ .../examples/collections/aggregate.cpp | 92 ++++++++++++++ .../examples/collections/bulk_write.cpp | 70 +++++++++++ .../collections/bulk_write_with_options.cpp | 76 ++++++++++++ .../mongocxx/examples/collections/count.cpp | 78 ++++++++++++ .../examples/collections/create_index.cpp | 64 ++++++++++ .../collections/create_index_with_options.cpp | 88 ++++++++++++++ .../examples/collections/delete_many.cpp | 98 +++++++++++++++ .../examples/collections/delete_one.cpp | 96 +++++++++++++++ .../examples/collections/distinct.cpp | 98 +++++++++++++++ .../mongocxx/examples/collections/drop.cpp | 53 ++++++++ .../examples/collections/estimate_count.cpp | 73 +++++++++++ .../mongocxx/examples/collections/find.cpp | 104 ++++++++++++++++ .../examples/collections/find_one.cpp | 91 ++++++++++++++ .../collections/find_one_and_delete.cpp | 104 ++++++++++++++++ .../collections/find_one_and_replace.cpp | 103 ++++++++++++++++ .../collections/find_one_and_update.cpp | 110 +++++++++++++++++ .../examples/collections/insert_many.cpp | 93 ++++++++++++++ .../examples/collections/insert_one.cpp | 74 ++++++++++++ .../examples/collections/list_indexes.cpp | 58 +++++++++ .../mongocxx/examples/collections/obtain.cpp | 42 +++++++ .../api/mongocxx/examples/collections/rc.cpp | 57 +++++++++ .../mongocxx/examples/collections/rename.cpp | 59 +++++++++ .../examples/collections/replace_one.cpp | 110 +++++++++++++++++ .../api/mongocxx/examples/collections/rp.cpp | 55 +++++++++ .../examples/collections/update_many.cpp | 109 +++++++++++++++++ .../examples/collections/update_one.cpp | 107 +++++++++++++++++ .../api/mongocxx/examples/collections/wc.cpp | 59 +++++++++ .../mongocxx/examples/collections/write.cpp | 75 ++++++++++++ .../create_collection_with_options.cpp | 1 - src/mongocxx/include/mongocxx/doc.hpp | 8 ++ 31 files changed, 2417 insertions(+), 1 deletion(-) create mode 100644 docs/api/mongocxx/examples/collections.md create mode 100644 examples/api/mongocxx/examples/collections/aggregate.cpp create mode 100644 examples/api/mongocxx/examples/collections/bulk_write.cpp create mode 100644 examples/api/mongocxx/examples/collections/bulk_write_with_options.cpp create mode 100644 examples/api/mongocxx/examples/collections/count.cpp create mode 100644 examples/api/mongocxx/examples/collections/create_index.cpp create mode 100644 examples/api/mongocxx/examples/collections/create_index_with_options.cpp create mode 100644 examples/api/mongocxx/examples/collections/delete_many.cpp create mode 100644 examples/api/mongocxx/examples/collections/delete_one.cpp create mode 100644 examples/api/mongocxx/examples/collections/distinct.cpp create mode 100644 examples/api/mongocxx/examples/collections/drop.cpp create mode 100644 examples/api/mongocxx/examples/collections/estimate_count.cpp create mode 100644 examples/api/mongocxx/examples/collections/find.cpp create mode 100644 examples/api/mongocxx/examples/collections/find_one.cpp create mode 100644 examples/api/mongocxx/examples/collections/find_one_and_delete.cpp create mode 100644 examples/api/mongocxx/examples/collections/find_one_and_replace.cpp create mode 100644 examples/api/mongocxx/examples/collections/find_one_and_update.cpp create mode 100644 examples/api/mongocxx/examples/collections/insert_many.cpp create mode 100644 examples/api/mongocxx/examples/collections/insert_one.cpp create mode 100644 examples/api/mongocxx/examples/collections/list_indexes.cpp create mode 100644 examples/api/mongocxx/examples/collections/obtain.cpp create mode 100644 examples/api/mongocxx/examples/collections/rc.cpp create mode 100644 examples/api/mongocxx/examples/collections/rename.cpp create mode 100644 examples/api/mongocxx/examples/collections/replace_one.cpp create mode 100644 examples/api/mongocxx/examples/collections/rp.cpp create mode 100644 examples/api/mongocxx/examples/collections/update_many.cpp create mode 100644 examples/api/mongocxx/examples/collections/update_one.cpp create mode 100644 examples/api/mongocxx/examples/collections/wc.cpp create mode 100644 examples/api/mongocxx/examples/collections/write.cpp diff --git a/docs/api/mongocxx/examples/collections.md b/docs/api/mongocxx/examples/collections.md new file mode 100644 index 0000000000..16dcece429 --- /dev/null +++ b/docs/api/mongocxx/examples/collections.md @@ -0,0 +1,113 @@ +# Obtain a Collection + +@snippet examples/api/mongocxx/examples/collections/obtain.cpp Example + +# Collection Operations + +## Drop a Collection + +@snippet examples/api/mongocxx/examples/collections/drop.cpp Example + +## Rename a Collection + +@snippet examples/api/mongocxx/examples/collections/rename.cpp Example + +## Set a Read Concern + +@snippet examples/api/mongocxx/examples/collections/rc.cpp Example + +## Set a Write Concern + +@snippet examples/api/mongocxx/examples/collections/wc.cpp Example + +## Set a Read Preference + +@snippet examples/api/mongocxx/examples/collections/rp.cpp Example + +# Index Operations + +## List Indexes + +@snippet examples/api/mongocxx/examples/collections/list_indexes.cpp Example + +## Create an Index + +@snippet examples/api/mongocxx/examples/collections/create_index.cpp Example + +## Create an Index With Options + +@snippet examples/api/mongocxx/examples/collections/create_index_with_options.cpp Example + +# Document Operations + +## Query the Number of Documents + +@snippet examples/api/mongocxx/examples/collections/count.cpp Example + +## Estimate the Number of Documents + +@snippet examples/api/mongocxx/examples/collections/estimate_count.cpp Example + +## Find a Document + +@snippet examples/api/mongocxx/examples/collections/find_one.cpp Example + +## Find Multiple Documents + +@snippet examples/api/mongocxx/examples/collections/find.cpp Example + +## Delete a Document + +@snippet examples/api/mongocxx/examples/collections/delete_one.cpp Example + +## Delete Many Documents + +@snippet examples/api/mongocxx/examples/collections/delete_many.cpp Example + +## Insert a Document + +@snippet examples/api/mongocxx/examples/collections/insert_one.cpp Example + +## Insert Many Documents + +@snippet examples/api/mongocxx/examples/collections/insert_many.cpp Example + +## Replace a Document + +@snippet examples/api/mongocxx/examples/collections/replace_one.cpp Example + +## Update a Document + +@snippet examples/api/mongocxx/examples/collections/update_one.cpp Example + +## Update Multiple Documents + +@snippet examples/api/mongocxx/examples/collections/update_many.cpp Example + +## Find and Delete a Document + +@snippet examples/api/mongocxx/examples/collections/find_one_and_delete.cpp Example + +## Find and Replace a Document + +@snippet examples/api/mongocxx/examples/collections/find_one_and_replace.cpp Example + +## Find and Update a Document + +@snippet examples/api/mongocxx/examples/collections/find_one_and_update.cpp Example + +## Find Distinct Values + +@snippet examples/api/mongocxx/examples/collections/distinct.cpp Example + +## Execute a Single Bulk Write Operation + +@snippet examples/api/mongocxx/examples/collections/write.cpp Example + +## Execute Multiple Bulk Write Operations + +@snippet examples/api/mongocxx/examples/collections/bulk_write.cpp Example + +## Execute an Aggregation Operation + +@snippet examples/api/mongocxx/examples/collections/aggregate.cpp Example diff --git a/examples/api/mongocxx/examples/collections/aggregate.cpp b/examples/api/mongocxx/examples/collections/aggregate.cpp new file mode 100644 index 0000000000..f71b51ba51 --- /dev/null +++ b/examples/api/mongocxx/examples/collections/aggregate.cpp @@ -0,0 +1,92 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +// [ +// {"x": 1}, +// {"x": 2}, +// ] +void example(mongocxx::collection coll) { + // Basic usage. + { + mongocxx::pipeline pipeline; + + pipeline.match(bsoncxx::from_json(R"({"x": 1})")); + pipeline.sample(1); + // ... other pipeline stages. + + mongocxx::cursor cursor = coll.aggregate(pipeline); + + auto has_field_x = [](bsoncxx::document::view doc) -> bool { + return doc.find("x") != doc.end(); + }; + + ASSERT(std::count_if(cursor.begin(), cursor.end(), has_field_x) == 1); + } + + // With options. + { + mongocxx::options::aggregate opts; + + // ... set aggregate options. + + mongocxx::cursor cursor = coll.aggregate(mongocxx::pipeline{}, opts); + + ASSERT(std::distance(cursor.begin(), cursor.end()) == 2); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto coll = set_rw_concern_majority(guard.get().create_collection("coll")); + + using insert = mongocxx::model::insert_one; + + ASSERT(coll.create_bulk_write() + .append(insert{bsoncxx::from_json(R"({"x": 1})")}) + .append(insert{bsoncxx::from_json(R"({"x": 2})")}) + .execute()); + + example(coll); + } +} diff --git a/examples/api/mongocxx/examples/collections/bulk_write.cpp b/examples/api/mongocxx/examples/collections/bulk_write.cpp new file mode 100644 index 0000000000..fd91014069 --- /dev/null +++ b/examples/api/mongocxx/examples/collections/bulk_write.cpp @@ -0,0 +1,70 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +void example(mongocxx::collection coll) { + ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$exists": 1}})")) == 0); + + mongocxx::model::insert_one insert{bsoncxx::from_json(R"({"x": 10})")}; + mongocxx::model::update_one update{bsoncxx::from_json(R"({"x": {"$exists": 1}})"), + bsoncxx::from_json(R"({"$set": {"x": 20}})")}; + + mongocxx::bulk_write bulk_write = coll.create_bulk_write(); + + bulk_write.append(insert); + bulk_write.append(update); + // ... other bulk write operations. + + auto result_opt = bulk_write.execute(); + + ASSERT(result_opt); + + mongocxx::result::bulk_write& result = *result_opt; + + ASSERT(result.inserted_count() == 1); + ASSERT(result.modified_count() == 1); + + ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": 20})")) == 1); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + example(set_rw_concern_majority(guard.get().create_collection("coll"))); + } +} diff --git a/examples/api/mongocxx/examples/collections/bulk_write_with_options.cpp b/examples/api/mongocxx/examples/collections/bulk_write_with_options.cpp new file mode 100644 index 0000000000..fb24f51a6a --- /dev/null +++ b/examples/api/mongocxx/examples/collections/bulk_write_with_options.cpp @@ -0,0 +1,76 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +void example(mongocxx::collection coll) { + ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$exists": 1}})")) == 0); + + mongocxx::options::bulk_write opts; + + opts.ordered(true); + // ... other bulk write options. + + auto result_opt = + coll.create_bulk_write(opts) + .append(mongocxx::model::insert_one{bsoncxx::from_json(R"({"x": 10})")}) + .append(mongocxx::model::update_one{bsoncxx::from_json(R"({"x": {"$exists": 1}})"), + bsoncxx::from_json(R"({"$set": {"x": 20}})")}) + .execute(); + + ASSERT(result_opt); + + mongocxx::result::bulk_write& result = *result_opt; + + ASSERT(result.inserted_count() == 1); + ASSERT(result.modified_count() == 1); + + ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": 20})")) == 1); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto coll = set_rw_concern_majority(guard.get().create_collection("coll")); + + example(coll); + } +} diff --git a/examples/api/mongocxx/examples/collections/count.cpp b/examples/api/mongocxx/examples/collections/count.cpp new file mode 100644 index 0000000000..9818b1c1a0 --- /dev/null +++ b/examples/api/mongocxx/examples/collections/count.cpp @@ -0,0 +1,78 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +// [ +// {"x": 1}, +// {"x": 2}, +// {"x": 3}, +// ] +void example(mongocxx::collection coll) { + // Basic usage. + { + ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$exists": 1}})")) == 3); + ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$gt": 1}})")) == 2); + ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$gt": 2}})")) == 1); + } + + // With options. + { + mongocxx::options::count opts; + + opts.limit(1); + // ... other count options. + + ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$exists": 1}})"), opts) == 1); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto db = set_rw_concern_majority(guard.get()); + + auto coll = db.create_collection("coll"); + + using insert = mongocxx::model::insert_one; + + ASSERT(coll.create_bulk_write() + .append(insert{bsoncxx::from_json(R"({"x": 1})")}) + .append(insert{bsoncxx::from_json(R"({"x": 2})")}) + .append(insert{bsoncxx::from_json(R"({"x": 3})")}) + .execute()); + + example(coll); + } +} diff --git a/examples/api/mongocxx/examples/collections/create_index.cpp b/examples/api/mongocxx/examples/collections/create_index.cpp new file mode 100644 index 0000000000..33edd52067 --- /dev/null +++ b/examples/api/mongocxx/examples/collections/create_index.cpp @@ -0,0 +1,64 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +void example(mongocxx::collection coll) { + bsoncxx::document::value result = coll.create_index(bsoncxx::from_json(R"({"key": 1})")); + + ASSERT(result["name"]); + ASSERT(result["name"].get_string().value.compare("key_1") == 0); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto coll = set_rw_concern_majority(guard.get().create_collection("coll")); + + auto count_indexes = [&coll] { + auto cursor = coll.list_indexes(); + + return std::distance(cursor.begin(), cursor.end()); + }; + + ASSERT(count_indexes() == 1); // _id + + example(coll); + + ASSERT(count_indexes() == 2); // _id, key_1 + } +} diff --git a/examples/api/mongocxx/examples/collections/create_index_with_options.cpp b/examples/api/mongocxx/examples/collections/create_index_with_options.cpp new file mode 100644 index 0000000000..943f7240f5 --- /dev/null +++ b/examples/api/mongocxx/examples/collections/create_index_with_options.cpp @@ -0,0 +1,88 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +void example(mongocxx::collection coll) { + // Index options. + { + auto opts = bsoncxx::from_json(R"({"name": "custom_name", "unique": true})"); + // ... other index options. + + bsoncxx::document::value result = + coll.create_index(bsoncxx::from_json(R"({"key_a": 1})"), opts.view()); + + ASSERT(result["name"]); + ASSERT(result["name"].get_string().value.compare("custom_name") == 0); + } + + // Create index options. + { + auto opts = bsoncxx::builder::basic::make_document(); + + mongocxx::options::index_view create_opts; + + // ... set create index options. + + bsoncxx::document::value result = + coll.create_index(bsoncxx::from_json(R"({"key_b": 1})"), opts.view()); + + ASSERT(result["name"]); + ASSERT(result["name"].get_string().value.compare("key_b_1") == 0); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto db = set_rw_concern_majority(guard.get()); + + example(db.create_collection("coll")); + + for (auto doc : db["coll"].list_indexes()) { + ASSERT(doc["name"]); + + if (doc["name"].get_string().value.compare("custom_name") == 0) { + ASSERT(doc["unique"]); + ASSERT(doc["unique"].get_bool().value == true); + } + } + } +} diff --git a/examples/api/mongocxx/examples/collections/delete_many.cpp b/examples/api/mongocxx/examples/collections/delete_many.cpp new file mode 100644 index 0000000000..298365077c --- /dev/null +++ b/examples/api/mongocxx/examples/collections/delete_many.cpp @@ -0,0 +1,98 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +// [ +// {"x": 1}, +// {"x": 2}, +// {"x": 3}, +// ] +void example(mongocxx::collection coll) { + auto x1 = bsoncxx::from_json(R"({"x": 1})"); + auto x2 = bsoncxx::from_json(R"({"x": 2})"); + auto x3 = bsoncxx::from_json(R"({"x": 3})"); + + // Basic usage. + { + ASSERT(coll.count_documents(x1.view()) == 1); + ASSERT(coll.count_documents(x2.view()) == 1); + ASSERT(coll.count_documents(x3.view()) == 1); + + auto result_opt = coll.delete_many(bsoncxx::from_json(R"({"x": {"$gt": 1}})")); + + ASSERT(result_opt); + + mongocxx::result::delete_result& result = *result_opt; + + ASSERT(result.deleted_count() == 2); + ASSERT(result.result().deleted_count() == 2); + + ASSERT(coll.count_documents(x1.view()) == 1); + ASSERT(coll.count_documents(x2.view()) == 0); + ASSERT(coll.count_documents(x3.view()) == 0); + } + + // With options. + { + ASSERT(coll.count_documents(x1.view()) == 1); + + mongocxx::options::delete_options opts; + + // ... set delete options. + + ASSERT(coll.delete_many(bsoncxx::from_json(R"({"x": {"$exists": 1}})"), opts)); + + ASSERT(coll.count_documents(x1.view()) == 0); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto db = set_rw_concern_majority(guard.get()); + + auto coll = db.create_collection("coll"); + + using insert = mongocxx::model::insert_one; + + ASSERT(coll.create_bulk_write() + .append(insert{bsoncxx::from_json(R"({"x": 1})")}) + .append(insert{bsoncxx::from_json(R"({"x": 2})")}) + .append(insert{bsoncxx::from_json(R"({"x": 3})")}) + .execute()); + + example(coll); + } +} diff --git a/examples/api/mongocxx/examples/collections/delete_one.cpp b/examples/api/mongocxx/examples/collections/delete_one.cpp new file mode 100644 index 0000000000..08adcd6443 --- /dev/null +++ b/examples/api/mongocxx/examples/collections/delete_one.cpp @@ -0,0 +1,96 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +// [ +// {"x": 1}, +// {"x": 2}, +// {"x": 3}, +// ] +void example(mongocxx::collection coll) { + auto x1 = bsoncxx::from_json(R"({"x": 1})"); + auto x2 = bsoncxx::from_json(R"({"x": 2})"); + auto x3 = bsoncxx::from_json(R"({"x": 3})"); + + // Basic usage. + { + ASSERT(coll.count_documents(x1.view()) == 1); + + auto result_opt = coll.delete_one(x1.view()); + + ASSERT(result_opt); + + mongocxx::result::delete_result& result = *result_opt; + + ASSERT(result.deleted_count() == 1); + ASSERT(result.result().deleted_count() == 1); + + ASSERT(coll.count_documents(x1.view()) == 0); + } + + // With options. + { + ASSERT(coll.count_documents(x2.view()) == 1); + + mongocxx::options::delete_options opts; + + // ... set delete options. + + ASSERT(coll.delete_one(x2.view(), opts)); + + ASSERT(coll.count_documents(x2.view()) == 0); + } + + ASSERT(coll.count_documents(x3.view()) == 1); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto db = set_rw_concern_majority(guard.get()); + + auto coll = db.create_collection("coll"); + + using insert = mongocxx::model::insert_one; + + ASSERT(coll.create_bulk_write() + .append(insert{bsoncxx::from_json(R"({"x": 1})")}) + .append(insert{bsoncxx::from_json(R"({"x": 2})")}) + .append(insert{bsoncxx::from_json(R"({"x": 3})")}) + .execute()); + + example(coll); + } +} diff --git a/examples/api/mongocxx/examples/collections/distinct.cpp b/examples/api/mongocxx/examples/collections/distinct.cpp new file mode 100644 index 0000000000..fdc8112499 --- /dev/null +++ b/examples/api/mongocxx/examples/collections/distinct.cpp @@ -0,0 +1,98 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +// [ +// {"x": 1, "flag": 1}, +// {"x": 2, "flag": 1}, +// {"x": 3, "flag": 0}, +// ] +void example(mongocxx::collection coll) { + // Basic usage. + { + mongocxx::cursor cursor = coll.distinct("x", bsoncxx::from_json(R"({"flag": 1})")); + + std::vector docs{cursor.begin(), cursor.end()}; + + ASSERT(docs.size() == 1u); + + auto doc = docs[0].view(); + + ASSERT(doc["values"]); + ASSERT(doc["values"].type() == bsoncxx::type::k_array); + + auto arr = doc["values"].get_array().value; + + ASSERT(std::distance(arr.begin(), arr.end()) == 2); + + ASSERT(arr[0].get_int32().value == 1); + ASSERT(arr[1].get_int32().value == 2); + } + + // With options. + { + mongocxx::options::distinct opts; + + // ... set distinct options. + + mongocxx::cursor cursor = coll.distinct("x", bsoncxx::from_json(R"({"flag": 1})"), opts); + + ASSERT(std::distance(cursor.begin(), cursor.end()) == 1); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto coll = set_rw_concern_majority(guard.get().create_collection("coll")); + + using insert = mongocxx::model::insert_one; + + ASSERT(coll.create_bulk_write() + .append(insert{bsoncxx::from_json(R"({"x": 1, "flag": 1})")}) + .append(insert{bsoncxx::from_json(R"({"x": 2, "flag": 1})")}) + .append(insert{bsoncxx::from_json(R"({"x": 3, "flag": 0})")}) + .execute()); + + example(coll); + } +} diff --git a/examples/api/mongocxx/examples/collections/drop.cpp b/examples/api/mongocxx/examples/collections/drop.cpp new file mode 100644 index 0000000000..fb33605c84 --- /dev/null +++ b/examples/api/mongocxx/examples/collections/drop.cpp @@ -0,0 +1,53 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +void example(mongocxx::database db) { + mongocxx::collection coll = db["coll"]; + + ASSERT(db.has_collection("coll")); + + coll.drop(); + + ASSERT(!db.has_collection("coll")); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto db = set_rw_concern_majority(guard.get()); + + (void)db.create_collection("coll"); + + example(db); + } +} diff --git a/examples/api/mongocxx/examples/collections/estimate_count.cpp b/examples/api/mongocxx/examples/collections/estimate_count.cpp new file mode 100644 index 0000000000..07be9b090d --- /dev/null +++ b/examples/api/mongocxx/examples/collections/estimate_count.cpp @@ -0,0 +1,73 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +// [ +// {"x": 1}, +// {"x": 2}, +// {"x": 3}, +// ] +void example(mongocxx::collection coll) { + // Basic usage. + ASSERT(coll.estimated_document_count() == 3); + + // With options. + { + mongocxx::options::estimated_document_count opts; + + // ... set estimated scount options. + + ASSERT(coll.estimated_document_count(opts) == 3); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto coll = set_rw_concern_majority(guard.get().create_collection("coll")); + + using insert = mongocxx::model::insert_one; + + ASSERT(coll.create_bulk_write() + .append(insert{bsoncxx::from_json(R"({"x": 1})")}) + .append(insert{bsoncxx::from_json(R"({"x": 2})")}) + .append(insert{bsoncxx::from_json(R"({"x": 3})")}) + .execute()); + + example(coll); + } +} diff --git a/examples/api/mongocxx/examples/collections/find.cpp b/examples/api/mongocxx/examples/collections/find.cpp new file mode 100644 index 0000000000..dfec92a460 --- /dev/null +++ b/examples/api/mongocxx/examples/collections/find.cpp @@ -0,0 +1,104 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example]A +// [ +// {"x": 1, "found": true}, +// {"x": 2, "found": false}, +// {"x": 3, "found": true}, +// ] +void example(mongocxx::collection coll) { + // Basic usage. + { + auto filter = bsoncxx::from_json(R"({"found": true})"); + + mongocxx::cursor cursor = coll.find(filter.view()); + + int count = 0; + + for (bsoncxx::document::view doc : cursor) { + ASSERT(doc["_id"]); + + ASSERT(doc["x"]); + ASSERT(doc["x"].get_int32().value != 2); + + ++count; + } + + ASSERT(count == 2); + } + + // With options. + { + mongocxx::options::find opts; + + opts.projection(bsoncxx::from_json(R"({"_id": 0, "x": 1})")); + opts.sort(bsoncxx::from_json(R"({"x": 1})")); + // ... other find options. + + mongocxx::cursor cursor = coll.find(bsoncxx::builder::basic::make_document(), opts); + + std::vector docs{cursor.begin(), cursor.end()}; + + ASSERT(docs.size() == 3u); + + ASSERT(docs[0] == bsoncxx::from_json(R"({"x": 1})")); + ASSERT(docs[1] == bsoncxx::from_json(R"({"x": 2})")); + ASSERT(docs[2] == bsoncxx::from_json(R"({"x": 3})")); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto db = set_rw_concern_majority(guard.get()); + + auto coll = db.create_collection("coll"); + + using insert = mongocxx::model::insert_one; + + ASSERT(coll.create_bulk_write() + .append(insert{bsoncxx::from_json(R"({"x": 1, "found": true})")}) + .append(insert{bsoncxx::from_json(R"({"x": 2, "found": false})")}) + .append(insert{bsoncxx::from_json(R"({"x": 3, "found": true})")}) + .execute()); + + example(coll); + } +} diff --git a/examples/api/mongocxx/examples/collections/find_one.cpp b/examples/api/mongocxx/examples/collections/find_one.cpp new file mode 100644 index 0000000000..418dafd99e --- /dev/null +++ b/examples/api/mongocxx/examples/collections/find_one.cpp @@ -0,0 +1,91 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +// [ +// {"x": 1, "found": false}, +// {"x": 2, "found": true}, +// {"x": 3, "found": false}, +// ] +void example(mongocxx::collection coll) { + // Basic usage. + { + auto filter = bsoncxx::from_json(R"({"found": true})"); + + auto result_opt = coll.find_one(filter.view()); + + ASSERT(result_opt); + + bsoncxx::document::view doc = result_opt->view(); + + ASSERT(doc["_id"]); + + ASSERT(doc["x"]); + ASSERT(doc["x"].get_int32().value == 2); + } + + // With options. + { + mongocxx::options::find opts; + + opts.projection(bsoncxx::from_json(R"({"_id": 0, "x": 1})")); + opts.sort(bsoncxx::from_json(R"({"x": 1})")); + // ... other find options. + + auto result_opt = coll.find_one(bsoncxx::builder::basic::make_document(), opts); + + ASSERT(result_opt); + ASSERT(*result_opt == bsoncxx::from_json(R"({"x": 1})")); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto db = set_rw_concern_majority(guard.get()); + + auto coll = db.create_collection("coll"); + + using insert = mongocxx::model::insert_one; + + ASSERT(coll.create_bulk_write() + .append(insert{bsoncxx::from_json(R"({"x": 1, "found": false})")}) + .append(insert{bsoncxx::from_json(R"({"x": 2, "found": true})")}) + .append(insert{bsoncxx::from_json(R"({"x": 3, "found": false})")}) + .execute()); + + example(coll); + } +} diff --git a/examples/api/mongocxx/examples/collections/find_one_and_delete.cpp b/examples/api/mongocxx/examples/collections/find_one_and_delete.cpp new file mode 100644 index 0000000000..a29e0f1a15 --- /dev/null +++ b/examples/api/mongocxx/examples/collections/find_one_and_delete.cpp @@ -0,0 +1,104 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +// [ +// {"x": 1}, +// {"x": 2}, +// {"x": 3}, +// ] +void example(mongocxx::collection coll) { + auto has_field_x = bsoncxx::from_json(R"({"x": {"$exists": true}})"); + + auto x1 = bsoncxx::from_json(R"({"x": 1})"); + auto x2 = bsoncxx::from_json(R"({"x": 2})"); + auto x3 = bsoncxx::from_json(R"({"x": 3})"); + + // Basic usage. + { + ASSERT(coll.count_documents(has_field_x.view()) == 3); + + auto result_opt = coll.find_one_and_delete(x2.view()); + + ASSERT(result_opt); + + bsoncxx::document::view doc = result_opt->view(); + + ASSERT(doc["_id"]); + + ASSERT(doc["x"]); + ASSERT(doc["x"].get_int32().value == 2); + + ASSERT(coll.count_documents(has_field_x.view()) == 2); + } + + // With options. + { + ASSERT(coll.count_documents(has_field_x.view()) == 2); + + mongocxx::options::find_one_and_delete opts; + + opts.projection(bsoncxx::from_json(R"({"_id": 0, "x": 1})")); + // ... other findOneAndDelete options. + + auto result_opt = coll.find_one_and_delete(x3.view(), opts); + + ASSERT(result_opt); + ASSERT(*result_opt == x3); + + ASSERT(coll.count_documents(has_field_x.view()) == 1); + } + + ASSERT(coll.count_documents(x1.view()) == 1); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto db = set_rw_concern_majority(guard.get()); + + auto coll = db.create_collection("coll"); + + using insert = mongocxx::model::insert_one; + + ASSERT(coll.create_bulk_write() + .append(insert{bsoncxx::from_json(R"({"x": 1})")}) + .append(insert{bsoncxx::from_json(R"({"x": 2})")}) + .append(insert{bsoncxx::from_json(R"({"x": 3})")}) + .execute()); + + example(coll); + } +} diff --git a/examples/api/mongocxx/examples/collections/find_one_and_replace.cpp b/examples/api/mongocxx/examples/collections/find_one_and_replace.cpp new file mode 100644 index 0000000000..d9c96748ae --- /dev/null +++ b/examples/api/mongocxx/examples/collections/find_one_and_replace.cpp @@ -0,0 +1,103 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +// [ +// {"x": 1}, +// {"x": 2}, +// {"x": 3}, +// ] +void example(mongocxx::collection coll) { + auto x0 = bsoncxx::from_json(R"({"x": 0})"); + auto x1 = bsoncxx::from_json(R"({"x": 1})"); + auto x2 = bsoncxx::from_json(R"({"x": 2})"); + auto x3 = bsoncxx::from_json(R"({"x": 3})"); + + // Basic usage. + { + ASSERT(coll.count_documents(x0.view()) == 0); + + auto result_opt = coll.find_one_and_replace(x2.view(), x0.view()); + + ASSERT(result_opt); + + bsoncxx::document::view doc = result_opt->view(); + + ASSERT(doc["_id"]); + + ASSERT(doc["x"]); + ASSERT(doc["x"].get_int32().value == 2); + + ASSERT(coll.count_documents(x0.view()) == 1); + } + + // With options. + { + ASSERT(coll.count_documents(x0.view()) == 1); + + mongocxx::options::find_one_and_replace opts; + + opts.projection(bsoncxx::from_json(R"({"_id": 0, "x": 1})")); + // ... other findOneAndReplace options. + + auto result_opt = coll.find_one_and_replace(x3.view(), x0.view(), opts); + + ASSERT(result_opt); + ASSERT(*result_opt == x3); + + ASSERT(coll.count_documents(x0.view()) == 2); + } + + ASSERT(coll.count_documents(x1.view()) == 1); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto db = set_rw_concern_majority(guard.get()); + + auto coll = db.create_collection("coll"); + + using insert = mongocxx::model::insert_one; + + ASSERT(coll.create_bulk_write() + .append(insert{bsoncxx::from_json(R"({"x": 1})")}) + .append(insert{bsoncxx::from_json(R"({"x": 2})")}) + .append(insert{bsoncxx::from_json(R"({"x": 3})")}) + .execute()); + + example(coll); + } +} diff --git a/examples/api/mongocxx/examples/collections/find_one_and_update.cpp b/examples/api/mongocxx/examples/collections/find_one_and_update.cpp new file mode 100644 index 0000000000..36734776cf --- /dev/null +++ b/examples/api/mongocxx/examples/collections/find_one_and_update.cpp @@ -0,0 +1,110 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +// [ +// {"x": 1, "updated": false}, +// {"x": 2, "updated": false}, +// {"x": 3, "updated": false}, +// ] +void example(mongocxx::collection coll) { + auto updated = bsoncxx::from_json(R"({"updated": true})"); + + auto x1 = bsoncxx::from_json(R"({"x": 1})"); + auto x2 = bsoncxx::from_json(R"({"x": 2})"); + auto x3 = bsoncxx::from_json(R"({"x": 3})"); + + auto update = bsoncxx::from_json(R"({"$set": {"updated": true}})"); + + // Basic usage. + { + ASSERT(coll.count_documents(x2.view()) == 1); + ASSERT(coll.count_documents(updated.view()) == 0); + + auto result_opt = coll.find_one_and_update(x2.view(), update.view()); + + ASSERT(result_opt); + + bsoncxx::document::view doc = result_opt->view(); + + ASSERT(doc["_id"]); + + ASSERT(doc["x"]); + ASSERT(doc["x"].get_int32().value == 2); + + ASSERT(coll.count_documents(x2.view()) == 1); + ASSERT(coll.count_documents(updated.view()) == 1); + } + + // With options. + { + ASSERT(coll.count_documents(x3.view()) == 1); + ASSERT(coll.count_documents(updated.view()) == 1); + + mongocxx::options::find_one_and_update opts; + + opts.projection(bsoncxx::from_json(R"({"_id": 0, "x": 1})")); + // ... other findOneAndReplace options. + + auto result_opt = coll.find_one_and_update(x3.view(), update.view(), opts); + + ASSERT(result_opt); + ASSERT(*result_opt == x3); + + ASSERT(coll.count_documents(x3.view()) == 1); + ASSERT(coll.count_documents(updated.view()) == 2); + } + + ASSERT(coll.count_documents(x1.view()) == 1); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto db = set_rw_concern_majority(guard.get()); + + auto coll = db.create_collection("coll"); + + using insert = mongocxx::model::insert_one; + + ASSERT(coll.create_bulk_write() + .append(insert{bsoncxx::from_json(R"({"x": 1, "updated": false})")}) + .append(insert{bsoncxx::from_json(R"({"x": 2, "updated": false})")}) + .append(insert{bsoncxx::from_json(R"({"x": 3, "updated": false})")}) + .execute()); + + example(coll); + } +} diff --git a/examples/api/mongocxx/examples/collections/insert_many.cpp b/examples/api/mongocxx/examples/collections/insert_many.cpp new file mode 100644 index 0000000000..db22fc413f --- /dev/null +++ b/examples/api/mongocxx/examples/collections/insert_many.cpp @@ -0,0 +1,93 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +void example(mongocxx::collection coll) { + auto x1 = bsoncxx::from_json(R"({"x": 1})"); + auto x2 = bsoncxx::from_json(R"({"x": 2})"); + auto y1 = bsoncxx::from_json(R"({"y": 1})"); + auto y2 = bsoncxx::from_json(R"({"y": 2})"); + auto z1 = bsoncxx::from_json(R"({"z": 1})"); + auto z2 = bsoncxx::from_json(R"({"z": 2})"); + + // Basic usage. + { + std::vector docs = {x1.view(), x2.view()}; + + auto result_opt = coll.insert_many(docs); + + ASSERT(result_opt); + + mongocxx::result::insert_many& result = *result_opt; + + ASSERT(result.inserted_count() == 2); + ASSERT(result.result().inserted_count() == 2); + } + + // Iterators. + { + bsoncxx::document::view docs[] = {y1.view(), y2.view()}; + + auto result_opt = coll.insert_many(std::begin(docs), std::end(docs)); + + ASSERT(result_opt); + + mongocxx::result::insert_many& result = *result_opt; + + ASSERT(result.inserted_count() == 2); + ASSERT(result.result().inserted_count() == 2); + } + + // With options. + { + std::array docs = {z1.view(), z2.view()}; + + mongocxx::options::insert opts; + + // ... set insert options. + + ASSERT(coll.insert_many(docs, opts)); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + example(set_rw_concern_majority(guard.get().create_collection("coll"))); + } +} diff --git a/examples/api/mongocxx/examples/collections/insert_one.cpp b/examples/api/mongocxx/examples/collections/insert_one.cpp new file mode 100644 index 0000000000..4a1f520908 --- /dev/null +++ b/examples/api/mongocxx/examples/collections/insert_one.cpp @@ -0,0 +1,74 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +void example(mongocxx::collection coll) { + auto x1 = bsoncxx::from_json(R"({"x": 1})"); + auto x2 = bsoncxx::from_json(R"({"x": 2})"); + + // Basic usage. + { + ASSERT(coll.count_documents(x1.view()) == 0); + + auto result_opt = coll.insert_one(x1.view()); + + ASSERT(result_opt); + + mongocxx::result::insert_one& result = *result_opt; + + ASSERT(result.result().inserted_count() == 1); + + ASSERT(coll.count_documents(x1.view()) == 1); + } + + // With options. + { + ASSERT(coll.count_documents(x2.view()) == 0); + + mongocxx::options::insert opts; + + // ... set insert options. + + ASSERT(coll.insert_one(x2.view(), opts)); + + ASSERT(coll.count_documents(x2.view()) == 1); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + example(set_rw_concern_majority(guard.get().create_collection("coll"))); + } +} diff --git a/examples/api/mongocxx/examples/collections/list_indexes.cpp b/examples/api/mongocxx/examples/collections/list_indexes.cpp new file mode 100644 index 0000000000..0f24fd35d6 --- /dev/null +++ b/examples/api/mongocxx/examples/collections/list_indexes.cpp @@ -0,0 +1,58 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +void example(mongocxx::collection coll) { + for (bsoncxx::document::view doc : coll.list_indexes()) { + ASSERT(doc["name"]); + ASSERT(doc["name"].type() == bsoncxx::type::k_string); + + ASSERT(doc["key"]); + ASSERT(doc["key"].type() == bsoncxx::type::k_document); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto coll = set_rw_concern_majority(guard.get().create_collection("coll")); + + (void)coll.create_index(bsoncxx::from_json(R"({"key": 1})")); + + example(coll); + } +} diff --git a/examples/api/mongocxx/examples/collections/obtain.cpp b/examples/api/mongocxx/examples/collections/obtain.cpp new file mode 100644 index 0000000000..55031f23c6 --- /dev/null +++ b/examples/api/mongocxx/examples/collections/obtain.cpp @@ -0,0 +1,42 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example(mongocxx::database db) { + mongocxx::collection coll = db["coll"]; + + ASSERT(coll); + ASSERT(coll.name().compare("coll") == 0); + + ASSERT(db.collection("coll").name().compare("coll") == 0); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + mongocxx::client client{mongocxx::uri{}}; + + example(client["db"]); +} diff --git a/examples/api/mongocxx/examples/collections/rc.cpp b/examples/api/mongocxx/examples/collections/rc.cpp new file mode 100644 index 0000000000..c6c94af1d2 --- /dev/null +++ b/examples/api/mongocxx/examples/collections/rc.cpp @@ -0,0 +1,57 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example(mongocxx::collection coll) { + using rc_level = mongocxx::read_concern::level; + + // Default. + { + mongocxx::read_concern rc = coll.read_concern(); + + ASSERT(rc.acknowledge_level() == rc_level::k_server_default); + } + + // Explicit. + { + mongocxx::read_concern rc; + + rc.acknowledge_level(rc_level::k_majority); + // ... other read concern options. + + coll.read_concern(rc); + + ASSERT(coll.read_concern() == rc); + ASSERT(coll.read_concern().acknowledge_level() == rc_level::k_majority); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + mongocxx::client client{mongocxx::uri{}}; + + example(client["db"]["coll"]); +} diff --git a/examples/api/mongocxx/examples/collections/rename.cpp b/examples/api/mongocxx/examples/collections/rename.cpp new file mode 100644 index 0000000000..9945681391 --- /dev/null +++ b/examples/api/mongocxx/examples/collections/rename.cpp @@ -0,0 +1,59 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +void example(mongocxx::database db) { + mongocxx::collection coll = db["old"]; + + ASSERT(db.has_collection("old")); + ASSERT(!db.has_collection("new")); + + ASSERT(coll.name().compare("old") == 0); + + coll.rename("new"); + + ASSERT(coll.name().compare("new") == 0); + + ASSERT(!db.has_collection("old")); + ASSERT(db.has_collection("new")); +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto db = set_rw_concern_majority(guard.get()); + + (void)db.create_collection("old"); + + example(db); + } +} diff --git a/examples/api/mongocxx/examples/collections/replace_one.cpp b/examples/api/mongocxx/examples/collections/replace_one.cpp new file mode 100644 index 0000000000..13ac2f5dee --- /dev/null +++ b/examples/api/mongocxx/examples/collections/replace_one.cpp @@ -0,0 +1,110 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +// [ +// {"x": 1, "replaced": false}, +// {"x": 2, "replaced": false}, +// {"x": 3, "replaced": false}, +// ] +void example(mongocxx::collection coll) { + auto is_original = bsoncxx::from_json(R"({"replaced": false})"); + auto is_replaced = bsoncxx::from_json(R"({"replaced": true})"); + + auto x0 = bsoncxx::from_json(R"({"x": 0, "replaced": true})"); + + // Basic usage. + { + ASSERT(coll.count_documents(x0.view()) == 0); + + auto filter = bsoncxx::from_json(R"({"x": 2})"); + + auto result_opt = coll.replace_one(filter.view(), x0.view()); + + ASSERT(result_opt); + + mongocxx::result::replace_one& result = *result_opt; + + ASSERT(result.matched_count() == 1); + ASSERT(result.modified_count() == 1); + ASSERT(result.result().matched_count() == 1); + ASSERT(result.result().modified_count() == 1); + + ASSERT(coll.count_documents(x0.view()) == 1); + } + + // With options. + { + ASSERT(coll.count_documents(is_original.view()) == 2); + ASSERT(coll.count_documents(is_replaced.view()) == 1); + + mongocxx::options::replace opts; + + opts.upsert(true); + // ... other replace options. + + auto filter = bsoncxx::from_json(R"({"replaced": false})"); + + ASSERT(coll.replace_one(filter.view(), x0.view(), opts)); + ASSERT(coll.count_documents(is_original.view()) == 1); + ASSERT(coll.count_documents(is_replaced.view()) == 2); + + ASSERT(coll.replace_one(filter.view(), x0.view(), opts)); + ASSERT(coll.count_documents(is_original.view()) == 0); + ASSERT(coll.count_documents(is_replaced.view()) == 3); + + ASSERT(coll.replace_one(filter.view(), x0.view(), opts)); + ASSERT(coll.count_documents(is_original.view()) == 0); + ASSERT(coll.count_documents(is_replaced.view()) == 4); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto db = set_rw_concern_majority(guard.get()); + + auto coll = db.create_collection("coll"); + + using insert = mongocxx::model::insert_one; + + ASSERT(coll.create_bulk_write() + .append(insert(bsoncxx::from_json(R"({"x": 1, "replaced": false})"))) + .append(insert(bsoncxx::from_json(R"({"x": 2, "replaced": false})"))) + .append(insert(bsoncxx::from_json(R"({"x": 3, "replaced": false})"))) + .execute()); + + example(coll); + } +} diff --git a/examples/api/mongocxx/examples/collections/rp.cpp b/examples/api/mongocxx/examples/collections/rp.cpp new file mode 100644 index 0000000000..9911b4f9fc --- /dev/null +++ b/examples/api/mongocxx/examples/collections/rp.cpp @@ -0,0 +1,55 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example(mongocxx::collection coll) { + // Default. + { + mongocxx::read_preference rp; + + ASSERT(coll.read_preference() == rp); + ASSERT(coll.read_preference().mode() == mongocxx::read_preference::read_mode::k_primary); + } + + // Explicit. + { + mongocxx::read_preference rp; + + rp.mode(mongocxx::read_preference::read_mode::k_secondary); + // ... other read preference options. + + coll.read_preference(rp); + + ASSERT(coll.read_preference() == rp); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + mongocxx::client client{mongocxx::uri{}}; + + example(client["db"]["coll"]); +} diff --git a/examples/api/mongocxx/examples/collections/update_many.cpp b/examples/api/mongocxx/examples/collections/update_many.cpp new file mode 100644 index 0000000000..4c8dadcaae --- /dev/null +++ b/examples/api/mongocxx/examples/collections/update_many.cpp @@ -0,0 +1,109 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +// [ +// {"x": 1, "updated": false}, +// {"x": 2, "updated": false}, +// {"x": 3, "updated": false}, +// ] +void example(mongocxx::collection coll) { + auto original = bsoncxx::from_json(R"({"updated": false})"); + auto updated = bsoncxx::from_json(R"({"updated": true})"); + + auto update = bsoncxx::from_json(R"({"$set": {"updated": true}})"); + + // Basic usage. + { + ASSERT(coll.count_documents(updated.view()) == 0); + + auto filter = bsoncxx::from_json(R"({"x": {"$gt": 1}})"); + + auto result_opt = coll.update_many(filter.view(), update.view()); + + ASSERT(result_opt); + + mongocxx::result::update& result = *result_opt; + + ASSERT(result.matched_count() == 2); + ASSERT(result.modified_count() == 2); + ASSERT(result.result().matched_count() == 2); + ASSERT(result.result().modified_count() == 2); + + ASSERT(coll.count_documents(updated.view()) == 2); + ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": 1, "updated": false})")) == 1); + } + + // With options. + { + ASSERT(coll.count_documents(original.view()) == 1); + ASSERT(coll.count_documents(updated.view()) == 2); + + mongocxx::options::update opts; + + opts.upsert(true); + // ... other update options. + + ASSERT(coll.update_many(original.view(), update.view(), opts)); + + ASSERT(coll.count_documents(original.view()) == 0); + ASSERT(coll.count_documents(updated.view()) == 3); + + ASSERT(coll.update_many(original.view(), update.view(), opts)); + + ASSERT(coll.count_documents(original.view()) == 0); + ASSERT(coll.count_documents(updated.view()) == 4); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto db = set_rw_concern_majority(guard.get()); + + auto coll = db.create_collection("coll"); + + using insert = mongocxx::model::insert_one; + + ASSERT(coll.create_bulk_write() + .append(insert{bsoncxx::from_json(R"({"x": 1, "updated": false})")}) + .append(insert{bsoncxx::from_json(R"({"x": 2, "updated": false})")}) + .append(insert{bsoncxx::from_json(R"({"x": 3, "updated": false})")}) + .execute()); + + example(coll); + } +} diff --git a/examples/api/mongocxx/examples/collections/update_one.cpp b/examples/api/mongocxx/examples/collections/update_one.cpp new file mode 100644 index 0000000000..f541d56f49 --- /dev/null +++ b/examples/api/mongocxx/examples/collections/update_one.cpp @@ -0,0 +1,107 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +// [ +// {"x": 1, "updated": false}, +// {"x": 2, "updated": false}, +// {"x": 3, "updated": false}, +// ] +void example(mongocxx::collection coll) { + auto updated = bsoncxx::from_json(R"({"updated": true})"); + + // Basic usage. + { + ASSERT(coll.count_documents(updated.view()) == 0); + + auto filter = bsoncxx::from_json(R"({"x": 2})"); + auto update = bsoncxx::from_json(R"({"$set": {"updated": true}})"); + + auto result_opt = coll.update_one(filter.view(), update.view()); + + ASSERT(result_opt); + + mongocxx::result::update& result = *result_opt; + + ASSERT(result.matched_count() == 1); + ASSERT(result.modified_count() == 1); + ASSERT(result.result().matched_count() == 1); + ASSERT(result.result().modified_count() == 1); + + ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": 2, "updated": true})")) == 1); + ASSERT(coll.count_documents(updated.view()) == 1); + } + + // With options. + { + ASSERT(coll.count_documents(updated.view()) == 1); + + auto x4 = bsoncxx::from_json(R"({"x": 4, "updated": true})"); + + ASSERT(coll.count_documents(x4.view()) == 0); + + mongocxx::options::update opts; + + opts.upsert(true); + // ... other update options. + + ASSERT(coll.update_one(bsoncxx::from_json(R"({"x": 4})"), + bsoncxx::from_json(R"({"$set": {"updated": true}})"), + opts)); + + ASSERT(coll.count_documents(x4.view()) == 1); + ASSERT(coll.count_documents(updated.view()) == 2); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + auto db = set_rw_concern_majority(guard.get()); + + auto coll = db.create_collection("coll"); + + using insert = mongocxx::model::insert_one; + + ASSERT(coll.create_bulk_write() + .append(insert{bsoncxx::from_json(R"({"x": 1, "updated": false})")}) + .append(insert{bsoncxx::from_json(R"({"x": 2, "updated": false})")}) + .append(insert{bsoncxx::from_json(R"({"x": 3, "updated": false})")}) + .execute()); + + example(coll); + } +} diff --git a/examples/api/mongocxx/examples/collections/wc.cpp b/examples/api/mongocxx/examples/collections/wc.cpp new file mode 100644 index 0000000000..f3c9c2534c --- /dev/null +++ b/examples/api/mongocxx/examples/collections/wc.cpp @@ -0,0 +1,59 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example(mongocxx::collection coll) { + using wc_level = mongocxx::write_concern::level; + + // Default. + { + mongocxx::write_concern wc = coll.write_concern(); + + ASSERT(wc.acknowledge_level() == wc_level::k_default); + ASSERT(wc.timeout() == std::chrono::milliseconds(0)); + } + + // Explicit. + { + mongocxx::write_concern wc; + + wc.majority(std::chrono::milliseconds(5000)); + // ... other write concern options. + + coll.write_concern(wc); + + ASSERT(coll.write_concern() == wc); + ASSERT(coll.write_concern().acknowledge_level() == wc_level::k_majority); + ASSERT(coll.write_concern().timeout() == std::chrono::seconds(5)); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { + mongocxx::client client{mongocxx::uri{}}; + + example(client["db"]["coll"]); +} diff --git a/examples/api/mongocxx/examples/collections/write.cpp b/examples/api/mongocxx/examples/collections/write.cpp new file mode 100644 index 0000000000..71bb51cd7e --- /dev/null +++ b/examples/api/mongocxx/examples/collections/write.cpp @@ -0,0 +1,75 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +namespace { + +// [Example] +void example(mongocxx::collection coll) { + { + ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": 1})")) == 0); + + mongocxx::model::insert_one insert{bsoncxx::from_json(R"({"x": 1})")}; + + auto result_opt = coll.write(insert); + + ASSERT(result_opt); + ASSERT(result_opt->inserted_count() == 1); + + ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": 1})")) == 1); + } + + { + ASSERT(coll.count_documents(bsoncxx::from_json(R"({"y": 1})")) == 0); + + mongocxx::options::bulk_write opts; + + opts.ordered(true); + // ... other bulk write options. + + ASSERT(coll.write(mongocxx::model::insert_one{bsoncxx::from_json(R"({"y": 1})")}, opts)); + + ASSERT(coll.count_documents(bsoncxx::from_json(R"({"y": 1})")) == 1); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + { + db_lock guard{client, EXAMPLES_COMPONENT_NAME_STR}; + + example(set_rw_concern_majority(guard.get().create_collection("coll"))); + } +} diff --git a/examples/api/mongocxx/examples/databases/create_collection_with_options.cpp b/examples/api/mongocxx/examples/databases/create_collection_with_options.cpp index f42a41a5f4..c9f2b99530 100644 --- a/examples/api/mongocxx/examples/databases/create_collection_with_options.cpp +++ b/examples/api/mongocxx/examples/databases/create_collection_with_options.cpp @@ -35,7 +35,6 @@ void example(mongocxx::database db) { mongocxx::collection coll = db.create_collection("coll", opts.view()); ASSERT(coll); - ASSERT(db.has_collection("coll")); } // [Example] diff --git a/src/mongocxx/include/mongocxx/doc.hpp b/src/mongocxx/include/mongocxx/doc.hpp index eedaf695a1..3805053b75 100644 --- a/src/mongocxx/include/mongocxx/doc.hpp +++ b/src/mongocxx/include/mongocxx/doc.hpp @@ -100,6 +100,7 @@ /// @li @subpage topic-mongocxx-examples-uri /// @li @subpage topic-mongocxx-examples-clients /// @li @subpage topic-mongocxx-examples-databases +/// @li @subpage topic-mongocxx-examples-collections /// /// @@ -139,6 +140,13 @@ /// @include{doc} api/mongocxx/examples/databases.md /// +/// +/// @page topic-mongocxx-examples-collections Collections +/// @brief How to obtain and use collections. +/// @tableofcontents +/// @include{doc} api/mongocxx/examples/collections.md +/// + /// /// @page topic-mongocxx-about Explanation /// @brief Design decisions, history, and other technical details about the mongocxx library. From 47602e4277524c490c54a50a6db4f2781bb7f0e6 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 1 Oct 2024 13:36:18 -0500 Subject: [PATCH 16/25] Add mongocxx operation exception examples --- .../mongocxx/examples/operation_exceptions.md | 11 +++ .../operation_exceptions/operation.cpp | 73 +++++++++++++++++++ .../examples/operation_exceptions/regular.cpp | 57 +++++++++++++++ src/mongocxx/include/mongocxx/doc.hpp | 8 ++ 4 files changed, 149 insertions(+) create mode 100644 docs/api/mongocxx/examples/operation_exceptions.md create mode 100644 examples/api/mongocxx/examples/operation_exceptions/operation.cpp create mode 100644 examples/api/mongocxx/examples/operation_exceptions/regular.cpp diff --git a/docs/api/mongocxx/examples/operation_exceptions.md b/docs/api/mongocxx/examples/operation_exceptions.md new file mode 100644 index 0000000000..1c63ad8e0d --- /dev/null +++ b/docs/api/mongocxx/examples/operation_exceptions.md @@ -0,0 +1,11 @@ +# As a Regular Exception + +@warning The @ref mongocxx::server_error_category error category is overloaded ([CXX-834](https://jira.mongodb.org/browse/CXX-834)). The error code value may belong to the server, libmongoc, or libmongocrypt depending on the context. Use error code values with caution. + +@snippet examples/api/mongocxx/examples/operation_exceptions/regular.cpp Example + +# As an Operation Exception + +@note Using @ref mongocxx::exception for error handling is recommended for forward compatibility. ([CXX-2377](https://jira.mongodb.org/browse/CXX-2377)) + +@snippet examples/api/mongocxx/examples/operation_exceptions/operation.cpp Example diff --git a/examples/api/mongocxx/examples/operation_exceptions/operation.cpp b/examples/api/mongocxx/examples/operation_exceptions/operation.cpp new file mode 100644 index 0000000000..c293bcf466 --- /dev/null +++ b/examples/api/mongocxx/examples/operation_exceptions/operation.cpp @@ -0,0 +1,73 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example(mongocxx::database db) { + ASSERT(db.name().compare("db") == 0); + + // The `getParameter` command can only be run in the `admin` database. + auto cmd = bsoncxx::from_json(R"({"getParameter": "*"})"); + + // This error handling pattern applies to all commands which may throw a + // `mongocxx::operation_exception` exception. + try { + auto reply = db.run_command(cmd.view()); + + ASSERT(false && "should not reach this point"); + } catch (const mongocxx::operation_exception& ex) { + ASSERT(ex.code().category() == mongocxx::server_error_category()); + ASSERT(ex.code().value() == 13); // Unauthorized + ASSERT(std::strstr(ex.what(), "admin") != nullptr); + + if (auto server_error_opt = ex.raw_server_error()) { + bsoncxx::document::view server_error = server_error_opt->view(); + + ASSERT(server_error["ok"]); + ASSERT(server_error["ok"].get_double().value == 0.0); + + ASSERT(server_error["code"]); + ASSERT(server_error["code"].get_int32().value == ex.code().value()); + + ASSERT(server_error["codeName"]); + ASSERT(server_error["errmsg"]); + // ... other error fields. + } + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + example(client["db"]); +} diff --git a/examples/api/mongocxx/examples/operation_exceptions/regular.cpp b/examples/api/mongocxx/examples/operation_exceptions/regular.cpp new file mode 100644 index 0000000000..0b3fb0ecf0 --- /dev/null +++ b/examples/api/mongocxx/examples/operation_exceptions/regular.cpp @@ -0,0 +1,57 @@ +// Copyright 2009-present MongoDB, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include + +#include +#include +#include +#include +#include + +#include +#include + +namespace { + +// [Example] +void example(mongocxx::database db) { + ASSERT(db.name().compare("db") == 0); + + // The `getParameter` command can only be run in the `admin` database. + auto cmd = bsoncxx::from_json(R"({"getParameter": "*"})"); + + // This error handling pattern applies to all commands which may throw a + // `mongocxx::operation_exception` exception. + try { + auto reply = db.run_command(cmd.view()); + + ASSERT(false && "should not reach this point"); + } catch (const mongocxx::exception& ex) { + ASSERT(ex.code().category() == mongocxx::server_error_category()); + ASSERT(ex.code().value() == 13); // Unauthorized + ASSERT(std::strstr(ex.what(), "admin") != nullptr); + } +} +// [Example] + +} // namespace + +RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { + mongocxx::client client{mongocxx::uri{}}; + + example(client["db"]); +} diff --git a/src/mongocxx/include/mongocxx/doc.hpp b/src/mongocxx/include/mongocxx/doc.hpp index 3805053b75..b37dc903dc 100644 --- a/src/mongocxx/include/mongocxx/doc.hpp +++ b/src/mongocxx/include/mongocxx/doc.hpp @@ -101,6 +101,7 @@ /// @li @subpage topic-mongocxx-examples-clients /// @li @subpage topic-mongocxx-examples-databases /// @li @subpage topic-mongocxx-examples-collections +/// @li @subpage topic-mongocxx-examples-operation-exceptions /// /// @@ -147,6 +148,13 @@ /// @include{doc} api/mongocxx/examples/collections.md /// +/// +/// @page topic-mongocxx-examples-operation-exceptions Operation Exceptions +/// @brief How to handle exceptions thrown by database and collection operations. +/// @tableofcontents +/// @include{doc} api/mongocxx/examples/operation_exceptions.md +/// + /// /// @page topic-mongocxx-about Explanation /// @brief Design decisions, history, and other technical details about the mongocxx library. From d327820b2092e39a6759e962cda51b0c1968bbfa Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Tue, 1 Oct 2024 15:18:46 -0500 Subject: [PATCH 17/25] Revert adding a redundant call to target_sources --- examples/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index ddf99810f1..162db903d6 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -170,8 +170,6 @@ function (add_abi_examples) # Avoid MAX_PATH errors on Windows due to long target names by using a single `api-runner` . add_examples_executable(api/runner.cpp LIBRARIES mongocxx_target Threads::Threads) - target_sources (api-runner PRIVATE "api/runner.cpp") - # Define a unique entry function name per component. foreach (source ${api_examples_sources}) target_sources (api-runner PRIVATE ${source}) From 6259e716ccae0232fae3bcc803f049f6c16b69b4 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Sat, 5 Oct 2024 11:06:09 -0500 Subject: [PATCH 18/25] CXX-3082 Rename ASSERT to EXPECT in API examples --- .../access_array/algorithms.cpp | 8 +- .../bson_documents/access_array/basic.cpp | 12 +- .../bson_documents/access_array/find.cpp | 10 +- .../bson_documents/access_array/iterators.cpp | 20 +-- .../bson_documents/access_array/subscript.cpp | 8 +- .../bson_documents/access_doc/algorithms.cpp | 8 +- .../bson_documents/access_doc/basic.cpp | 12 +- .../bson_documents/access_doc/find.cpp | 10 +- .../bson_documents/access_doc/iterators.cpp | 20 +-- .../bson_documents/access_doc/subscript.cpp | 8 +- .../create_array/builder_append.cpp | 6 +- .../create_array/builder_basic.cpp | 6 +- .../create_array/builder_bson_type.cpp | 12 +- .../create_array/builder_bson_value.cpp | 12 +- .../create_array/builder_concatenate.cpp | 2 +- .../create_array/builder_make_document.cpp | 6 +- .../create_array/builder_raw_value.cpp | 4 +- .../create_array/builder_raw_view.cpp | 4 +- .../create_array/builder_reset.cpp | 8 +- .../create_array/builder_sub_array.cpp | 8 +- .../create_array/builder_sub_array_append.cpp | 6 +- .../create_array/builder_sub_document.cpp | 2 +- .../builder_sub_document_append.cpp | 6 +- .../create_array/builder_value_type.cpp | 12 +- .../create_array/json_basic.cpp | 6 +- .../create_array/json_extended.cpp | 6 +- .../create_array/json_sub_array.cpp | 4 +- .../create_array/json_sub_document.cpp | 2 +- .../bson_documents/create_array/json_udl.cpp | 2 +- .../create_doc/builder_append.cpp | 6 +- .../create_doc/builder_basic.cpp | 6 +- .../create_doc/builder_bson_type.cpp | 12 +- .../create_doc/builder_bson_value.cpp | 12 +- .../create_doc/builder_concatenate.cpp | 2 +- .../create_doc/builder_make_document.cpp | 6 +- .../create_doc/builder_raw_value.cpp | 2 +- .../create_doc/builder_raw_view.cpp | 2 +- .../create_doc/builder_reset.cpp | 8 +- .../create_doc/builder_sub_array.cpp | 8 +- .../create_doc/builder_sub_array_append.cpp | 6 +- .../create_doc/builder_sub_document.cpp | 2 +- .../builder_sub_document_append.cpp | 6 +- .../create_doc/builder_value_type.cpp | 12 +- .../bson_documents/create_doc/json_basic.cpp | 6 +- .../create_doc/json_extended.cpp | 6 +- .../create_doc/json_sub_array.cpp | 4 +- .../create_doc/json_sub_document.cpp | 2 +- .../bson_documents/create_doc/json_udl.cpp | 2 +- .../bson_documents/elements/arr_multi.cpp | 18 +-- .../bson_documents/elements/arr_single.cpp | 8 +- .../elements/cmp_bson_value.cpp | 8 +- .../bson_documents/elements/cmp_type.cpp | 8 +- .../bson_documents/elements/doc_multi.cpp | 18 +-- .../bson_documents/elements/doc_single.cpp | 8 +- .../examples/bson_documents/json/array.cpp | 8 +- .../examples/bson_documents/json/document.cpp | 8 +- .../bson_documents/values/arr_value.cpp | 10 +- .../bson_documents/values/arr_view.cpp | 14 +-- .../bson_documents/values/bson_type_value.cpp | 16 +-- .../bson_documents/values/bson_type_view.cpp | 16 +-- .../bson_documents/values/doc_value.cpp | 10 +- .../bson_documents/values/doc_view.cpp | 14 +-- .../bson_documents/values/make_value.cpp | 12 +- .../bson_documents/values/value_type.cpp | 16 +-- .../bson_errors/access_arr_iter_end.cpp | 8 +- .../bson_errors/access_arr_iter_invalid.cpp | 4 +- .../bson_errors/access_arr_key_missing.cpp | 4 +- .../bson_errors/access_doc_iter_end.cpp | 8 +- .../bson_errors/access_doc_iter_invalid.cpp | 4 +- .../bson_errors/access_doc_key_missing.cpp | 6 +- .../bson_errors/create_arr_append.cpp | 6 +- .../create_arr_append_sub_array.cpp | 12 +- .../create_arr_append_sub_document.cpp | 12 +- .../create_bson_value_invalid_element.cpp | 4 +- .../create_bson_value_invalid_type.cpp | 4 +- .../bson_errors/create_doc_append.cpp | 6 +- .../create_doc_append_sub_array.cpp | 12 +- .../create_doc_append_sub_document.cpp | 12 +- .../examples/bson_errors/create_json.cpp | 4 +- .../query_bson_value_invalid_type.cpp | 12 +- .../bson_errors/query_element_arr_invalid.cpp | 10 +- .../query_element_arr_invalid_type.cpp | 14 +-- .../bson_errors/query_element_doc_invalid.cpp | 10 +- .../query_element_doc_invalid_type.cpp | 14 +-- .../bson_errors/to_json_invalid_arr.cpp | 4 +- .../bson_errors/to_json_invalid_doc.cpp | 4 +- .../examples/decimal128/basic_usage.cpp | 118 +++++++++--------- .../bsoncxx/examples/decimal128/errors.cpp | 4 +- .../examples/decimal128/from_bytes.cpp | 34 ++--- .../api/bsoncxx/examples/oid/basic_usage.cpp | 32 ++--- examples/api/bsoncxx/examples/oid/errors.cpp | 8 +- .../examples/validation/basic_usage.cpp | 18 +-- .../bsoncxx/examples/validation/validator.cpp | 16 +-- examples/api/db_lock.cpp | 2 +- .../examples/clients/create/pool/basic.cpp | 12 +- .../examples/clients/create/pool/options.cpp | 6 +- .../clients/create/pool/try_acquire.cpp | 12 +- .../examples/clients/create/single/basic.cpp | 6 +- .../clients/create/single/options/apm.cpp | 2 +- .../create/single/options/auto_encryption.cpp | 2 +- .../create/single/options/stable_api.cpp | 6 +- .../clients/create/single/options/tls.cpp | 6 +- .../clients/create/single/with_uri.cpp | 4 +- .../clients/errors/auto_encryption.cpp | 18 +-- .../clients/errors/invalid_client.cpp | 6 +- .../examples/clients/errors/stable_api.cpp | 8 +- .../mongocxx/examples/clients/errors/tls.cpp | 4 +- .../clients/errors/wait_queue_timeout.cpp | 6 +- .../clients/use/list_database_names.cpp | 2 +- .../use/list_database_names_with_options.cpp | 2 +- .../examples/clients/use/list_databases.cpp | 8 +- .../use/list_databases_with_options.cpp | 8 +- .../examples/collections/aggregate.cpp | 6 +- .../examples/collections/bulk_write.cpp | 10 +- .../collections/bulk_write_with_options.cpp | 10 +- .../mongocxx/examples/collections/count.cpp | 10 +- .../examples/collections/create_index.cpp | 8 +- .../collections/create_index_with_options.cpp | 14 +-- .../examples/collections/delete_many.cpp | 26 ++-- .../examples/collections/delete_one.cpp | 20 +-- .../examples/collections/distinct.cpp | 16 +-- .../mongocxx/examples/collections/drop.cpp | 4 +- .../examples/collections/estimate_count.cpp | 6 +- .../mongocxx/examples/collections/find.cpp | 18 +-- .../examples/collections/find_one.cpp | 14 +-- .../collections/find_one_and_delete.cpp | 24 ++-- .../collections/find_one_and_replace.cpp | 24 ++-- .../collections/find_one_and_update.cpp | 32 ++--- .../examples/collections/insert_many.cpp | 14 +-- .../examples/collections/insert_one.cpp | 14 +-- .../examples/collections/list_indexes.cpp | 8 +- .../mongocxx/examples/collections/obtain.cpp | 6 +- .../api/mongocxx/examples/collections/rc.cpp | 6 +- .../mongocxx/examples/collections/rename.cpp | 12 +- .../examples/collections/replace_one.cpp | 38 +++--- .../api/mongocxx/examples/collections/rp.cpp | 6 +- .../examples/collections/update_many.cpp | 34 ++--- .../examples/collections/update_one.cpp | 28 ++--- .../api/mongocxx/examples/collections/wc.cpp | 10 +- .../mongocxx/examples/collections/write.cpp | 14 +-- .../examples/databases/create_collection.cpp | 6 +- .../create_collection_with_options.cpp | 6 +- .../api/mongocxx/examples/databases/drop.cpp | 4 +- .../examples/databases/has_collection.cpp | 4 +- .../databases/list_collection_names.cpp | 12 +- .../examples/databases/list_collections.cpp | 14 +-- .../mongocxx/examples/databases/obtain.cpp | 6 +- .../api/mongocxx/examples/databases/rc.cpp | 8 +- .../api/mongocxx/examples/databases/rp.cpp | 8 +- .../examples/databases/run_command.cpp | 4 +- .../api/mongocxx/examples/databases/wc.cpp | 12 +- .../examples/instance/basic_usage.cpp | 2 +- .../mongocxx/examples/instance/current.cpp | 2 +- .../mongocxx/examples/instance/destroyed.cpp | 8 +- .../mongocxx/examples/instance/recreation.cpp | 8 +- .../mongocxx/examples/logger/basic_usage.cpp | 8 +- .../mongocxx/examples/logger/to_string.cpp | 14 +-- .../operation_exceptions/operation.cpp | 22 ++-- .../examples/operation_exceptions/regular.cpp | 10 +- .../api/mongocxx/examples/uri/all_options.cpp | 12 +- .../api/mongocxx/examples/uri/basic_usage.cpp | 8 +- .../mongocxx/examples/uri/default_value.cpp | 6 +- examples/api/mongocxx/examples/uri/hosts.cpp | 20 +-- .../api/mongocxx/examples/uri/invalid.cpp | 12 +- .../api/mongocxx/examples/uri/optional.cpp | 16 +-- .../api/mongocxx/examples/uri/userpass.cpp | 12 +- examples/api/runner.cpp | 4 +- examples/macros.hh | 16 +-- 168 files changed, 876 insertions(+), 876 deletions(-) diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp index e7d8902b25..a5ece39d31 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/algorithms.cpp @@ -28,7 +28,7 @@ namespace { // [Example] // [1, 2.0, "three"] void example(bsoncxx::array::view arr) { - ASSERT(std::distance(arr.begin(), arr.end()) == 3); + EXPECT(std::distance(arr.begin(), arr.end()) == 3); std::vector elements; @@ -37,9 +37,9 @@ void example(bsoncxx::array::view arr) { return e.key().compare("0") == 0 || e.type() == bsoncxx::type::k_string; }); - ASSERT(elements.size() == 2u); - ASSERT(elements[0].key().compare("0") == 0); - ASSERT(elements[1].key().compare("2") == 0); + EXPECT(elements.size() == 2u); + EXPECT(elements[0].key().compare("0") == 0); + EXPECT(elements[1].key().compare("2") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp index b1d4c873a2..1e4157bbfa 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/basic.cpp @@ -28,16 +28,16 @@ void example(bsoncxx::array::view arr) { for (bsoncxx::array::element e : arr) { switch (e.type()) { case bsoncxx::type::k_int32: - ASSERT(e.key().compare("0") == 0); - ASSERT(e.get_int32().value == 1); + EXPECT(e.key().compare("0") == 0); + EXPECT(e.get_int32().value == 1); break; case bsoncxx::type::k_double: - ASSERT(e.key().compare("1") == 0); - ASSERT(e.get_double().value == 2.0); + EXPECT(e.key().compare("1") == 0); + EXPECT(e.get_double().value == 2.0); break; case bsoncxx::type::k_string: - ASSERT(e.key().compare("2") == 0); - ASSERT(e.get_string().value.compare("three") == 0); + EXPECT(e.key().compare("2") == 0); + EXPECT(e.get_string().value.compare("three") == 0); break; } } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp index ccfad22af9..789d9d0eb3 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/find.cpp @@ -24,17 +24,17 @@ namespace { // [Example] // [1, 2] void example(bsoncxx::array::view arr) { - ASSERT(arr.find(0) == arr.begin()); + EXPECT(arr.find(0) == arr.begin()); { auto iter = arr.find(1); - ASSERT(iter != arr.end()); - ASSERT(iter->key().compare("1") == 0); - ASSERT(iter->get_int32().value == 2); + EXPECT(iter != arr.end()); + EXPECT(iter->key().compare("1") == 0); + EXPECT(iter->get_int32().value == 2); } - ASSERT(arr.find(2) == arr.end()); + EXPECT(arr.find(2) == arr.end()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp index f4cae8f739..24d468459c 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/iterators.cpp @@ -25,32 +25,32 @@ namespace { // [Example] // [1, 2] void example(bsoncxx::array::view arr) { - ASSERT(arr.begin() != arr.end()); + EXPECT(arr.begin() != arr.end()); auto iter = arr.begin(); - ASSERT(iter == arr.begin()); + EXPECT(iter == arr.begin()); { bsoncxx::array::element e = *iter; - ASSERT(e.key().compare("0") == 0); - ASSERT(e.get_int32().value == 1); + EXPECT(e.key().compare("0") == 0); + EXPECT(e.get_int32().value == 1); } ++iter; - ASSERT(iter->key().compare("1") == 0); - ASSERT(iter->get_int32().value == 2); + EXPECT(iter->key().compare("1") == 0); + EXPECT(iter->get_int32().value == 2); { auto iter_copy = iter++; - ASSERT(iter_copy != iter); - ASSERT(iter_copy->key().compare("1") == 0); - ASSERT(iter_copy->get_int32() == 2); + EXPECT(iter_copy != iter); + EXPECT(iter_copy->key().compare("1") == 0); + EXPECT(iter_copy->get_int32() == 2); } - ASSERT(iter == arr.end()); + EXPECT(iter == arr.end()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp b/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp index bc7c5d2119..69a163acc7 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_array/subscript.cpp @@ -25,16 +25,16 @@ namespace { // [Example] // [1, 2] void example(bsoncxx::array::view arr) { - ASSERT(arr[0]); + EXPECT(arr[0]); { bsoncxx::array::element e = arr[1]; - ASSERT(e.key().compare("1") == 0); - ASSERT(e.get_int32().value == 2); + EXPECT(e.key().compare("1") == 0); + EXPECT(e.get_int32().value == 2); } - ASSERT(!arr[2]); // Invalid element. + EXPECT(!arr[2]); // Invalid element. } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp index f24e42a98d..5f141321b3 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/algorithms.cpp @@ -29,7 +29,7 @@ namespace { // [Example] // {"a": 1, "b": 2.0, "c": "three"} void example(bsoncxx::document::view doc) { - ASSERT(std::distance(doc.begin(), doc.end()) == 3); + EXPECT(std::distance(doc.begin(), doc.end()) == 3); std::vector elements; @@ -40,9 +40,9 @@ void example(bsoncxx::document::view doc) { return e.key().compare("a") == 0 || e.type() == bsoncxx::type::k_string; }); - ASSERT(elements.size() == 2u); - ASSERT(elements[0].key().compare("a") == 0); - ASSERT(elements[1].key().compare("c") == 0); + EXPECT(elements.size() == 2u); + EXPECT(elements[0].key().compare("a") == 0); + EXPECT(elements[1].key().compare("c") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp index 95aa123912..7af279e40b 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/basic.cpp @@ -28,16 +28,16 @@ void example(bsoncxx::document::view doc) { for (bsoncxx::document::element e : doc) { switch (e.type()) { case bsoncxx::type::k_int32: - ASSERT(e.key().compare("a") == 0); - ASSERT(e.get_int32().value == 1); + EXPECT(e.key().compare("a") == 0); + EXPECT(e.get_int32().value == 1); break; case bsoncxx::type::k_double: - ASSERT(e.key().compare("b") == 0); - ASSERT(e.get_double().value == 2.0); + EXPECT(e.key().compare("b") == 0); + EXPECT(e.get_double().value == 2.0); break; case bsoncxx::type::k_string: - ASSERT(e.key().compare("c") == 0); - ASSERT(e.get_string().value.compare("three") == 0); + EXPECT(e.key().compare("c") == 0); + EXPECT(e.get_string().value.compare("three") == 0); break; } } diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp index 03afcb3af7..ec224c2437 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/find.cpp @@ -24,17 +24,17 @@ namespace { // [Example] // {"a": 1, "b": 2} void example(bsoncxx::document::view doc) { - ASSERT(doc.find("a") == doc.begin()); + EXPECT(doc.find("a") == doc.begin()); { auto iter = doc.find("b"); - ASSERT(iter != doc.end()); - ASSERT(iter->key().compare("b") == 0); - ASSERT(iter->get_int32().value == 2); + EXPECT(iter != doc.end()); + EXPECT(iter->key().compare("b") == 0); + EXPECT(iter->get_int32().value == 2); } - ASSERT(doc.find("x") == doc.end()); + EXPECT(doc.find("x") == doc.end()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp index ff8cbcc5ce..d496c2e5f8 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/iterators.cpp @@ -25,32 +25,32 @@ namespace { // [Example] // {"a": 1, "b": 2} void example(bsoncxx::document::view doc) { - ASSERT(doc.begin() != doc.end()); + EXPECT(doc.begin() != doc.end()); auto iter = doc.begin(); - ASSERT(iter == doc.begin()); + EXPECT(iter == doc.begin()); { bsoncxx::document::element e = *iter; - ASSERT(e.key().compare("a") == 0); - ASSERT(e.get_int32().value == 1); + EXPECT(e.key().compare("a") == 0); + EXPECT(e.get_int32().value == 1); } ++iter; - ASSERT(iter->key().compare("b") == 0); - ASSERT(iter->get_int32().value == 2); + EXPECT(iter->key().compare("b") == 0); + EXPECT(iter->get_int32().value == 2); { auto iter_copy = iter++; - ASSERT(iter_copy != iter); - ASSERT(iter_copy->key().compare("b") == 0); - ASSERT(iter_copy->get_int32() == 2); + EXPECT(iter_copy != iter); + EXPECT(iter_copy->key().compare("b") == 0); + EXPECT(iter_copy->get_int32() == 2); } - ASSERT(iter == doc.end()); + EXPECT(iter == doc.end()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp b/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp index 398558dd52..e039fad21b 100644 --- a/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/access_doc/subscript.cpp @@ -25,16 +25,16 @@ namespace { // [Example] // {"a": 1, "b": 2} void example(bsoncxx::document::view doc) { - ASSERT(doc["a"]); + EXPECT(doc["a"]); { bsoncxx::document::element e = doc["b"]; - ASSERT(e.key().compare("b") == 0); - ASSERT(e.get_int32().value == 2); + EXPECT(e.key().compare("b") == 0); + EXPECT(e.get_int32().value == 2); } - ASSERT(!doc["c"]); // Invalid element. + EXPECT(!doc["c"]); // Invalid element. } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp index ad56ae3c5a..0c99ba6f2e 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_append.cpp @@ -33,9 +33,9 @@ void example() { bsoncxx::array::value owner = builder.extract(); bsoncxx::array::view arr = owner.view(); - ASSERT(arr[0].get_int32().value == 1); - ASSERT(arr[1].get_double().value == 2.0); - ASSERT(arr[2].get_string().value.compare("three") == 0); + EXPECT(arr[0].get_int32().value == 1); + EXPECT(arr[1].get_double().value == 2.0); + EXPECT(arr[2].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp index 53ffd5facf..06d135c97f 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_basic.cpp @@ -31,9 +31,9 @@ void example() { bsoncxx::array::value owner = builder.extract(); bsoncxx::array::view arr = owner.view(); - ASSERT(arr[0].get_int32().value == 1); - ASSERT(arr[1].get_double().value == 2.0); - ASSERT(arr[2].get_string().value.compare("three") == 0); + EXPECT(arr[0].get_int32().value == 1); + EXPECT(arr[1].get_double().value == 2.0); + EXPECT(arr[2].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp index f15f76e480..87ebd4a738 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_type.cpp @@ -31,13 +31,13 @@ void example() { bsoncxx::array::value owner = bsoncxx::builder::basic::make_array(a, b, c); bsoncxx::array::view arr = owner.view(); - ASSERT(arr[0].type() == bsoncxx::type::k_int32); - ASSERT(arr[1].type() == bsoncxx::type::k_double); - ASSERT(arr[2].type() == bsoncxx::type::k_string); + EXPECT(arr[0].type() == bsoncxx::type::k_int32); + EXPECT(arr[1].type() == bsoncxx::type::k_double); + EXPECT(arr[2].type() == bsoncxx::type::k_string); - ASSERT(arr[0].get_int32().value == 1); - ASSERT(arr[1].get_double().value == 2.0); - ASSERT(arr[2].get_string().value.compare("three") == 0); + EXPECT(arr[0].get_int32().value == 1); + EXPECT(arr[1].get_double().value == 2.0); + EXPECT(arr[2].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp index 4a66341d01..6e44b20a98 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_bson_value.cpp @@ -40,13 +40,13 @@ void example() { bsoncxx::builder::basic::make_array(values[0], values[1], values[2]); bsoncxx::array::view arr = owner.view(); - ASSERT(arr[0].type() == bsoncxx::type::k_int32); - ASSERT(arr[1].type() == bsoncxx::type::k_double); - ASSERT(arr[2].type() == bsoncxx::type::k_string); + EXPECT(arr[0].type() == bsoncxx::type::k_int32); + EXPECT(arr[1].type() == bsoncxx::type::k_double); + EXPECT(arr[2].type() == bsoncxx::type::k_string); - ASSERT(arr[0].get_value() == values[0]); - ASSERT(arr[1].get_value() == values[1]); - ASSERT(arr[2].get_value() == values[2]); + EXPECT(arr[0].get_value() == values[0]); + EXPECT(arr[1].get_value() == values[1]); + EXPECT(arr[2].get_value() == values[2]); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_concatenate.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_concatenate.cpp index 13df983a5b..31129c34f7 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_concatenate.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_concatenate.cpp @@ -32,7 +32,7 @@ void example(bsoncxx::array::view a, bsoncxx::array::view b) { builder.append(bsoncxx::builder::concatenate(a)); builder.append(bsoncxx::builder::concatenate(b)); - ASSERT(builder.view() == bsoncxx::builder::basic::make_array(1, 2)); + EXPECT(builder.view() == bsoncxx::builder::basic::make_array(1, 2)); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp index b36ade8f15..e5f35c213a 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_make_document.cpp @@ -30,9 +30,9 @@ void example() { bsoncxx::builder::basic::make_array(std::int32_t{1}, 2.0, "three"); bsoncxx::array::view arr = owner.view(); - ASSERT(arr[0].get_int32().value == 1); - ASSERT(arr[1].get_double().value == 2.0); - ASSERT(arr[2].get_string().value.compare("three") == 0); + EXPECT(arr[0].get_int32().value == 1); + EXPECT(arr[1].get_double().value == 2.0); + EXPECT(arr[2].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp index c7289389a0..5ebd3f1551 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_value.cpp @@ -39,8 +39,8 @@ void example(const std::uint8_t* data, std::size_t length) { bsoncxx::array::value owner{raw, length, deleter}; bsoncxx::array::view arr = owner.view(); - ASSERT(arr[0].get_int32().value == 1); - ASSERT(arr[1].get_int32().value == 2); + EXPECT(arr[0].get_int32().value == 1); + EXPECT(arr[1].get_int32().value == 2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp index 822d4eab35..7c598dfdb5 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_raw_view.cpp @@ -30,8 +30,8 @@ namespace { void example(const std::uint8_t* data, std::size_t length) { bsoncxx::array::view arr{data, length}; - ASSERT(arr[0].get_int32().value == 1); - ASSERT(arr[1].get_int32().value == 2); + EXPECT(arr[0].get_int32().value == 1); + EXPECT(arr[1].get_int32().value == 2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_reset.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_reset.cpp index 20938e8b48..c26fbfd215 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_reset.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_reset.cpp @@ -39,11 +39,11 @@ void example() { bsoncxx::array::view a = a_owner.view(); bsoncxx::array::view b = b_owner.view(); - ASSERT(a[0].type() == bsoncxx::type::k_int32); - ASSERT(b[0].type() == bsoncxx::type::k_int64); + EXPECT(a[0].type() == bsoncxx::type::k_int32); + EXPECT(b[0].type() == bsoncxx::type::k_int64); - ASSERT(a[0].get_int32().value == 1); - ASSERT(b[0].get_int64().value == 2); + EXPECT(a[0].get_int32().value == 1); + EXPECT(b[0].get_int64().value == 2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp index 84698f7cdd..fa35e05b2f 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array.cpp @@ -30,11 +30,11 @@ void example() { bsoncxx::array::value owner = make_array(make_array(std::int32_t{1}, std::int64_t{2})); bsoncxx::array::view v = owner.view()[0].get_array().value; - ASSERT(v[0].type() == bsoncxx::type::k_int32); - ASSERT(v[1].type() == bsoncxx::type::k_int64); + EXPECT(v[0].type() == bsoncxx::type::k_int32); + EXPECT(v[1].type() == bsoncxx::type::k_int64); - ASSERT(v[0].get_int32().value == 1); - ASSERT(v[1].get_int64().value == 2); + EXPECT(v[0].get_int32().value == 1); + EXPECT(v[1].get_int64().value == 2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array_append.cpp index aea40ad1ea..ceb0e90794 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array_append.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_array_append.cpp @@ -35,9 +35,9 @@ void example() { }); bsoncxx::array::view v = owner.view()[0].get_array().value; - ASSERT(v[0].get_int32().value == 1); - ASSERT(v[1].get_int32().value == 2); - ASSERT(v[2].get_int32().value == 3); + EXPECT(v[0].get_int32().value == 1); + EXPECT(v[1].get_int32().value == 2); + EXPECT(v[2].get_int32().value == 3); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp index 412eab43dd..e18b4b2f44 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document.cpp @@ -31,7 +31,7 @@ void example() { bsoncxx::array::value owner = make_array(make_document(kvp("key", "value"))); bsoncxx::document::view v = owner.view()[0].get_document().value; - ASSERT(v["key"].get_string().value.compare("value") == 0); + EXPECT(v["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document_append.cpp index 77f158152f..92f5f18b11 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document_append.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_sub_document_append.cpp @@ -40,9 +40,9 @@ void example() { }); bsoncxx::document::view v = owner.view()[0].get_document().value; - ASSERT(v["a"].get_int32().value == 1); - ASSERT(v["b"].get_int32().value == 2); - ASSERT(v["c"].get_int32().value == 3); + EXPECT(v["a"].get_int32().value == 1); + EXPECT(v["b"].get_int32().value == 2); + EXPECT(v["c"].get_int32().value == 3); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp index d577099be6..9c9281deff 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/builder_value_type.cpp @@ -30,13 +30,13 @@ void example() { bsoncxx::builder::basic::make_array(std::int32_t{1}, 2.0, "three"); bsoncxx::array::view arr = owner.view(); - ASSERT(arr[0].type() == bsoncxx::type::k_int32); - ASSERT(arr[1].type() == bsoncxx::type::k_double); - ASSERT(arr[2].type() == bsoncxx::type::k_string); + EXPECT(arr[0].type() == bsoncxx::type::k_int32); + EXPECT(arr[1].type() == bsoncxx::type::k_double); + EXPECT(arr[2].type() == bsoncxx::type::k_string); - ASSERT(arr[0].get_int32().value == 1); - ASSERT(arr[1].get_double().value == 2.0); - ASSERT(arr[2].get_string().value.compare("three") == 0); + EXPECT(arr[0].get_int32().value == 1); + EXPECT(arr[1].get_double().value == 2.0); + EXPECT(arr[2].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp index b52cdfb24b..302cea0e25 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_basic.cpp @@ -29,9 +29,9 @@ void example() { )"); bsoncxx::document::view doc = owner.view(); - ASSERT(doc["0"].get_int32().value == 1); - ASSERT(doc["1"].get_double().value == 2.0); - ASSERT(doc["2"].get_string().value.compare("three") == 0); + EXPECT(doc["0"].get_int32().value == 1); + EXPECT(doc["1"].get_double().value == 2.0); + EXPECT(doc["2"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_extended.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_extended.cpp index 1cbb9cddb0..cb268eda18 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/json_extended.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_extended.cpp @@ -33,9 +33,9 @@ void example() { )"); bsoncxx::document::view doc = owner.view(); - ASSERT(doc["0"].get_int32().value == 1); - ASSERT(doc["1"].get_int64().value == 2); - ASSERT(doc["2"].get_double().value == 3.0); + EXPECT(doc["0"].get_int32().value == 1); + EXPECT(doc["1"].get_int64().value == 2); + EXPECT(doc["2"].get_double().value == 3.0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp index ab5f41743a..3664ab4411 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_array.cpp @@ -31,8 +31,8 @@ void example() { )"); bsoncxx::array::view sub = owner.view()["0"].get_array().value; - ASSERT(sub[0].get_int32().value == 1); - ASSERT(sub[1].get_int32().value == 2); + EXPECT(sub[0].get_int32().value == 1); + EXPECT(sub[1].get_int32().value == 2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp index 4efcb1c9fd..3157fd9c45 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_sub_document.cpp @@ -31,7 +31,7 @@ void example() { )"); bsoncxx::document::view v = owner.view()["0"].get_document().value; - ASSERT(v["key"].get_string().value.compare("value") == 0); + EXPECT(v["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_array/json_udl.cpp b/examples/api/bsoncxx/examples/bson_documents/create_array/json_udl.cpp index 544674d790..969ca70634 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_array/json_udl.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_array/json_udl.cpp @@ -27,7 +27,7 @@ void example() { bsoncxx::document::value a = R"([1, 2])"_bson; bsoncxx::document::value b = bsoncxx::from_json(R"([1, 2])"); - ASSERT(a == b); + EXPECT(a == b); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp index fc7aea9fc2..d9f594547e 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_append.cpp @@ -36,9 +36,9 @@ void example() { bsoncxx::document::value owner = builder.extract(); bsoncxx::document::view doc = owner.view(); - ASSERT(doc["a"].get_int32().value == 1); - ASSERT(doc["b"].get_double().value == 2.0); - ASSERT(doc["c"].get_string().value.compare("three") == 0); + EXPECT(doc["a"].get_int32().value == 1); + EXPECT(doc["b"].get_double().value == 2.0); + EXPECT(doc["c"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp index c523665e4d..0b9b9523fa 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_basic.cpp @@ -34,9 +34,9 @@ void example() { bsoncxx::document::value owner = builder.extract(); bsoncxx::document::view doc = owner.view(); - ASSERT(doc["a"].get_int32().value == 1); - ASSERT(doc["b"].get_double().value == 2.0); - ASSERT(doc["c"].get_string().value.compare("three") == 0); + EXPECT(doc["a"].get_int32().value == 1); + EXPECT(doc["b"].get_double().value == 2.0); + EXPECT(doc["c"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp index 3beeaa2eeb..7d7d88868e 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_type.cpp @@ -37,13 +37,13 @@ void example() { bsoncxx::builder::basic::make_document(kvp("a", a), kvp("b", b), kvp("c", c)); bsoncxx::document::view doc = owner.view(); - ASSERT(doc["a"].type() == bsoncxx::type::k_int32); - ASSERT(doc["b"].type() == bsoncxx::type::k_double); - ASSERT(doc["c"].type() == bsoncxx::type::k_string); + EXPECT(doc["a"].type() == bsoncxx::type::k_int32); + EXPECT(doc["b"].type() == bsoncxx::type::k_double); + EXPECT(doc["c"].type() == bsoncxx::type::k_string); - ASSERT(doc["a"].get_int32() == a); - ASSERT(doc["b"].get_double() == b); - ASSERT(doc["c"].get_string() == c); + EXPECT(doc["a"].get_int32() == a); + EXPECT(doc["b"].get_double() == b); + EXPECT(doc["c"].get_string() == c); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp index eec1f9a05e..5434ca352c 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_bson_value.cpp @@ -40,13 +40,13 @@ void example() { kvp("a", values[0]), kvp("b", values[1]), kvp("c", values[2])); bsoncxx::document::view doc = owner.view(); - ASSERT(doc["a"].type() == bsoncxx::type::k_int32); - ASSERT(doc["b"].type() == bsoncxx::type::k_double); - ASSERT(doc["c"].type() == bsoncxx::type::k_string); + EXPECT(doc["a"].type() == bsoncxx::type::k_int32); + EXPECT(doc["b"].type() == bsoncxx::type::k_double); + EXPECT(doc["c"].type() == bsoncxx::type::k_string); - ASSERT(doc["a"].get_value() == values[0]); - ASSERT(doc["b"].get_value() == values[1]); - ASSERT(doc["c"].get_value() == values[2]); + EXPECT(doc["a"].get_value() == values[0]); + EXPECT(doc["b"].get_value() == values[1]); + EXPECT(doc["c"].get_value() == values[2]); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_concatenate.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_concatenate.cpp index 0180df59fd..a93da9ea4b 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_concatenate.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_concatenate.cpp @@ -31,7 +31,7 @@ void example(bsoncxx::document::view a, bsoncxx::document::view b) { builder.append(bsoncxx::builder::concatenate(a)); builder.append(bsoncxx::builder::concatenate(b)); - ASSERT(builder.view() == bsoncxx::from_json(R"({"a": 1, "b": 2})")); + EXPECT(builder.view() == bsoncxx::from_json(R"({"a": 1, "b": 2})")); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp index 36440ddcba..81b69f8453 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_make_document.cpp @@ -33,9 +33,9 @@ void example() { kvp("a", std::int32_t{1}), kvp("b", 2.0), kvp("c", "three")); bsoncxx::document::view doc = owner.view(); - ASSERT(doc["a"].get_int32().value == 1); - ASSERT(doc["b"].get_double().value == 2.0); - ASSERT(doc["c"].get_string().value.compare("three") == 0); + EXPECT(doc["a"].get_int32().value == 1); + EXPECT(doc["b"].get_double().value == 2.0); + EXPECT(doc["c"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp index 90d263daa1..5b4c37d9ab 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_value.cpp @@ -38,7 +38,7 @@ void example(const std::uint8_t* data, std::size_t length) { bsoncxx::document::value owner{raw, length, deleter}; bsoncxx::document::view doc = owner.view(); - ASSERT(doc["key"].get_string().value.compare("value") == 0); + EXPECT(doc["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp index 0f45aa72c1..6f2787f706 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_raw_view.cpp @@ -30,7 +30,7 @@ namespace { void example(const std::uint8_t* data, std::size_t length) { bsoncxx::document::view doc{data, length}; - ASSERT(doc["key"].get_string().value.compare("value") == 0); + EXPECT(doc["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_reset.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_reset.cpp index a508694604..575892bbc7 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_reset.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_reset.cpp @@ -42,11 +42,11 @@ void example() { bsoncxx::document::view a = a_owner.view(); bsoncxx::document::view b = b_owner.view(); - ASSERT(a["v"].type() == bsoncxx::type::k_int32); - ASSERT(b["v"].type() == bsoncxx::type::k_int64); + EXPECT(a["v"].type() == bsoncxx::type::k_int32); + EXPECT(b["v"].type() == bsoncxx::type::k_int64); - ASSERT(a["v"].get_int32().value == 1); - ASSERT(b["v"].get_int64().value == 2); + EXPECT(a["v"].get_int32().value == 1); + EXPECT(b["v"].get_int64().value == 2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp index 94f2ac8639..2750b8d416 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array.cpp @@ -36,11 +36,11 @@ void example() { make_document(kvp("v", make_array(std::int32_t{1}, std::int64_t{2}))); bsoncxx::array::view v = owner.view()["v"].get_array().value; - ASSERT(v[0].type() == bsoncxx::type::k_int32); - ASSERT(v[1].type() == bsoncxx::type::k_int64); + EXPECT(v[0].type() == bsoncxx::type::k_int32); + EXPECT(v[1].type() == bsoncxx::type::k_int64); - ASSERT(v[0].get_int32().value == 1); - ASSERT(v[1].get_int64().value == 2); + EXPECT(v[0].get_int32().value == 1); + EXPECT(v[1].get_int64().value == 2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array_append.cpp index f5c62e1967..e287d03713 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array_append.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_array_append.cpp @@ -40,9 +40,9 @@ void example() { })); bsoncxx::array::view v = owner.view()["v"].get_array().value; - ASSERT(v[0].get_int32().value == 1); - ASSERT(v[1].get_int32().value == 2); - ASSERT(v[2].get_int32().value == 3); + EXPECT(v[0].get_int32().value == 1); + EXPECT(v[1].get_int32().value == 2); + EXPECT(v[2].get_int32().value == 3); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp index c5b2da0cec..03230c2e59 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document.cpp @@ -31,7 +31,7 @@ void example() { bsoncxx::document::value owner = make_document(kvp("v", make_document(kvp("key", "value")))); bsoncxx::document::view v = owner.view()["v"].get_document().value; - ASSERT(v["key"].get_string().value.compare("value") == 0); + EXPECT(v["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document_append.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document_append.cpp index 6537488a0b..dd1aafc775 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document_append.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_sub_document_append.cpp @@ -40,9 +40,9 @@ void example() { })); bsoncxx::document::view v = owner.view()["v"].get_document().value; - ASSERT(v["a"].get_int32().value == 1); - ASSERT(v["b"].get_int32().value == 2); - ASSERT(v["c"].get_int32().value == 3); + EXPECT(v["a"].get_int32().value == 1); + EXPECT(v["b"].get_int32().value == 2); + EXPECT(v["c"].get_int32().value == 3); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp index 22398aa81a..1c38225698 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/builder_value_type.cpp @@ -33,13 +33,13 @@ void example() { kvp("a", std::int32_t{1}), kvp("b", 2.0), kvp("c", "three")); bsoncxx::document::view doc = owner.view(); - ASSERT(doc["a"].type() == bsoncxx::type::k_int32); - ASSERT(doc["b"].type() == bsoncxx::type::k_double); - ASSERT(doc["c"].type() == bsoncxx::type::k_string); + EXPECT(doc["a"].type() == bsoncxx::type::k_int32); + EXPECT(doc["b"].type() == bsoncxx::type::k_double); + EXPECT(doc["c"].type() == bsoncxx::type::k_string); - ASSERT(doc["a"].get_int32().value == 1); - ASSERT(doc["b"].get_double().value == 2.0); - ASSERT(doc["c"].get_string().value.compare("three") == 0); + EXPECT(doc["a"].get_int32().value == 1); + EXPECT(doc["b"].get_double().value == 2.0); + EXPECT(doc["c"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp index c9eccc453e..2e864f21d6 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_basic.cpp @@ -33,9 +33,9 @@ void example() { )"); bsoncxx::document::view doc = owner.view(); - ASSERT(doc["a"].get_int32().value == 1); - ASSERT(doc["b"].get_double().value == 2.0); - ASSERT(doc["c"].get_string().value.compare("three") == 0); + EXPECT(doc["a"].get_int32().value == 1); + EXPECT(doc["b"].get_double().value == 2.0); + EXPECT(doc["c"].get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_extended.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_extended.cpp index 0cad361a20..3ae281365b 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_extended.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_extended.cpp @@ -33,9 +33,9 @@ void example() { )"); bsoncxx::document::view doc = owner.view(); - ASSERT(doc["a"].get_int32().value == 1); - ASSERT(doc["b"].get_int64().value == 2); - ASSERT(doc["c"].get_double().value == 3.0); + EXPECT(doc["a"].get_int32().value == 1); + EXPECT(doc["b"].get_int64().value == 2); + EXPECT(doc["c"].get_double().value == 3.0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_array.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_array.cpp index 07c4e74d24..8474175fea 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_array.cpp @@ -31,8 +31,8 @@ void example() { )"); bsoncxx::array::view sub = owner.view()["v"].get_array().value; - ASSERT(sub[0].get_int32().value == 1); - ASSERT(sub[1].get_int32().value == 2); + EXPECT(sub[0].get_int32().value == 1); + EXPECT(sub[1].get_int32().value == 2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp index 3296d7c2c0..593709dd54 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_sub_document.cpp @@ -31,7 +31,7 @@ void example() { )"); bsoncxx::document::view v = doc["v"].get_document().value; - ASSERT(v["key"].get_string().value.compare("value") == 0); + EXPECT(v["key"].get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_udl.cpp b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_udl.cpp index 28753a48fd..1f88bb5c5d 100644 --- a/examples/api/bsoncxx/examples/bson_documents/create_doc/json_udl.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/create_doc/json_udl.cpp @@ -27,7 +27,7 @@ void example() { bsoncxx::document::value a = R"({"key": "value"})"_bson; bsoncxx::document::value b = bsoncxx::from_json(R"({"key": "value"})"); - ASSERT(a == b); + EXPECT(a == b); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp index 4a317c495d..03782bd2ed 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/arr_multi.cpp @@ -26,32 +26,32 @@ namespace { void example(bsoncxx::array::element e) { switch (e.type()) { case bsoncxx::type::k_int32: { - ASSERT(e.key().compare("0") == 0); + EXPECT(e.key().compare("0") == 0); bsoncxx::types::b_int32 v = e.get_int32(); - ASSERT(v.type_id == bsoncxx::type::k_int32); - ASSERT(v.value == 1); + EXPECT(v.type_id == bsoncxx::type::k_int32); + EXPECT(v.value == 1); break; } case bsoncxx::type::k_double: { - ASSERT(e.key().compare("1") == 0); + EXPECT(e.key().compare("1") == 0); bsoncxx::types::b_double v = e.get_double(); - ASSERT(v.type_id == bsoncxx::type::k_double); - ASSERT(v.value == 2.0); + EXPECT(v.type_id == bsoncxx::type::k_double); + EXPECT(v.value == 2.0); break; } case bsoncxx::type::k_string: { - ASSERT(e.key().compare("2") == 0); + EXPECT(e.key().compare("2") == 0); bsoncxx::types::b_string v = e.get_string(); - ASSERT(v.type_id == bsoncxx::type::k_string); - ASSERT(v.value.compare("three") == 0); + EXPECT(v.type_id == bsoncxx::type::k_string); + EXPECT(v.value.compare("three") == 0); break; } diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp index 392d735936..40f43cce61 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/arr_single.cpp @@ -25,14 +25,14 @@ namespace { // [1, 2.0, "three"] void example(bsoncxx::array::element e) { if (e.type() == bsoncxx::type::k_int32) { - ASSERT(e.key().compare("0") == 0); + EXPECT(e.key().compare("0") == 0); bsoncxx::types::b_int32 v = e.get_int32(); - ASSERT(v.type_id == bsoncxx::type::k_int32); - ASSERT(v.value == 1); + EXPECT(v.type_id == bsoncxx::type::k_int32); + EXPECT(v.value == 1); } else { - ASSERT(e.key().compare("0") != 0); + EXPECT(e.key().compare("0") != 0); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp index 44c2d9dba6..88996d6ece 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_bson_value.cpp @@ -29,18 +29,18 @@ void example(bsoncxx::document::element e) { bsoncxx::types::b_int64 b{2}; if (e.get_value() == a) { - ASSERT(e.key().compare("a") == 0); + EXPECT(e.key().compare("a") == 0); } else if (e.get_value() == b) { - ASSERT(e.key().compare("b") == 0); + EXPECT(e.key().compare("b") == 0); } bsoncxx::types::bson_value::view va{a}; bsoncxx::types::bson_value::view vb{b}; if (e == va) { - ASSERT(e.key().compare("a") == 0); + EXPECT(e.key().compare("a") == 0); } else if (e == vb) { - ASSERT(e.key().compare("b") == 0); + EXPECT(e.key().compare("b") == 0); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp index 29c7a0b1ea..8d10f6d75a 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/cmp_type.cpp @@ -30,11 +30,11 @@ void example(bsoncxx::document::element e) { std::int64_t b{2}; if (e.type() == bsoncxx::type::k_int32) { - ASSERT(e.key().compare("a") == 0); - ASSERT(e.get_int32().value == a); + EXPECT(e.key().compare("a") == 0); + EXPECT(e.get_int32().value == a); } else if (e.type() == bsoncxx::type::k_int64) { - ASSERT(e.key().compare("b") == 0); - ASSERT(e.get_int64().value == b); + EXPECT(e.key().compare("b") == 0); + EXPECT(e.get_int64().value == b); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp index fe5c323736..05438236bf 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/doc_multi.cpp @@ -26,32 +26,32 @@ namespace { void example(bsoncxx::document::element e) { switch (e.type()) { case bsoncxx::type::k_int32: { - ASSERT(e.key().compare("a") == 0); + EXPECT(e.key().compare("a") == 0); bsoncxx::types::b_int32 v = e.get_int32(); - ASSERT(v.type_id == bsoncxx::type::k_int32); - ASSERT(v.value == 1); + EXPECT(v.type_id == bsoncxx::type::k_int32); + EXPECT(v.value == 1); break; } case bsoncxx::type::k_double: { - ASSERT(e.key().compare("b") == 0); + EXPECT(e.key().compare("b") == 0); bsoncxx::types::b_double v = e.get_double(); - ASSERT(v.type_id == bsoncxx::type::k_double); - ASSERT(v.value == 2.0); + EXPECT(v.type_id == bsoncxx::type::k_double); + EXPECT(v.value == 2.0); break; } case bsoncxx::type::k_string: { - ASSERT(e.key().compare("c") == 0); + EXPECT(e.key().compare("c") == 0); bsoncxx::types::b_string v = e.get_string(); - ASSERT(v.type_id == bsoncxx::type::k_string); - ASSERT(v.value.compare("three") == 0); + EXPECT(v.type_id == bsoncxx::type::k_string); + EXPECT(v.value.compare("three") == 0); break; } diff --git a/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp b/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp index c8bf3ac940..eace5e5548 100644 --- a/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/elements/doc_single.cpp @@ -25,14 +25,14 @@ namespace { // {"a": 1, "b": 2.0, "c": "three"} void example(bsoncxx::document::element e) { if (e.type() == bsoncxx::type::k_int32) { - ASSERT(e.key().compare("a") == 0); + EXPECT(e.key().compare("a") == 0); bsoncxx::types::b_int32 v = e.get_int32(); - ASSERT(v.type_id == bsoncxx::type::k_int32); - ASSERT(v.value == 1); + EXPECT(v.type_id == bsoncxx::type::k_int32); + EXPECT(v.value == 1); } else { - ASSERT(e.key().compare("a") != 0); + EXPECT(e.key().compare("a") != 0); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/json/array.cpp b/examples/api/bsoncxx/examples/bson_documents/json/array.cpp index f74104ecb4..3bc7ee8ef4 100644 --- a/examples/api/bsoncxx/examples/bson_documents/json/array.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/json/array.cpp @@ -54,7 +54,7 @@ void example() { // } // ] std::string json = bsoncxx::to_json(arr, ExtendedJsonMode::k_canonical); - ASSERT( + EXPECT( json == R"([ { "$numberInt" : "1" }, { "$numberLong" : "2" }, { "$binary" : { "base64" : "dGhyZWU=", "subType" : "00" } } ])"); } @@ -72,7 +72,7 @@ void example() { // } // ] std::string json = bsoncxx::to_json(arr, ExtendedJsonMode::k_relaxed); - ASSERT(json == R"([ 1, 2, { "$binary" : { "base64" : "dGhyZWU=", "subType" : "00" } } ])"); + EXPECT(json == R"([ 1, 2, { "$binary" : { "base64" : "dGhyZWU=", "subType" : "00" } } ])"); } { @@ -86,14 +86,14 @@ void example() { // } // ] std::string json = bsoncxx::to_json(arr); - ASSERT(json == R"([ 1, 2, { "$binary" : "dGhyZWU=", "$type" : "00" } ])"); + EXPECT(json == R"([ 1, 2, { "$binary" : "dGhyZWU=", "$type" : "00" } ])"); } { std::string a = bsoncxx::to_json(arr); std::string b = bsoncxx::to_json(arr, ExtendedJsonMode::k_legacy); - ASSERT(a == b); + EXPECT(a == b); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/json/document.cpp b/examples/api/bsoncxx/examples/bson_documents/json/document.cpp index d7055e6a98..9a3e9a268e 100644 --- a/examples/api/bsoncxx/examples/bson_documents/json/document.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/json/document.cpp @@ -56,7 +56,7 @@ void example() { // } // } std::string json = bsoncxx::to_json(doc, ExtendedJsonMode::k_canonical); - ASSERT( + EXPECT( json == R"({ "a" : { "$numberInt" : "1" }, "b" : { "$numberLong" : "2" }, "c" : { "$binary" : { "base64" : "dGhyZWU=", "subType" : "00" } } })"); } @@ -74,7 +74,7 @@ void example() { // } // } std::string json = bsoncxx::to_json(doc, ExtendedJsonMode::k_relaxed); - ASSERT( + EXPECT( json == R"({ "a" : 1, "b" : 2, "c" : { "$binary" : { "base64" : "dGhyZWU=", "subType" : "00" } } })"); } @@ -90,14 +90,14 @@ void example() { // } // } std::string json = bsoncxx::to_json(doc); - ASSERT(json == R"({ "a" : 1, "b" : 2, "c" : { "$binary" : "dGhyZWU=", "$type" : "00" } })"); + EXPECT(json == R"({ "a" : 1, "b" : 2, "c" : { "$binary" : "dGhyZWU=", "$type" : "00" } })"); } { std::string a = bsoncxx::to_json(doc); std::string b = bsoncxx::to_json(doc, ExtendedJsonMode::k_legacy); - ASSERT(a == b); + EXPECT(a == b); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp index 979bb557a0..40a3b31925 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/arr_value.cpp @@ -25,19 +25,19 @@ namespace { void example() { bsoncxx::types::bson_value::value v = nullptr; - ASSERT(v.view().type() == bsoncxx::type::k_null); - ASSERT(v.view().get_null() == bsoncxx::types::b_null{}); + EXPECT(v.view().type() == bsoncxx::type::k_null); + EXPECT(v.view().get_null() == bsoncxx::types::b_null{}); v = bsoncxx::from_json(R"({"v": ["value"]})") // Temporary object. ["v"] .get_owning_value(); // Copy: no dangling. - ASSERT(v.view().type() == bsoncxx::type::k_array); + EXPECT(v.view().type() == bsoncxx::type::k_array); v = v.view().get_array().value[0].get_string(); // Copy: no dangling. - ASSERT(v.view().type() == bsoncxx::type::k_string); - ASSERT(v.view().get_string().value.compare("value") == 0); + EXPECT(v.view().type() == bsoncxx::type::k_string); + EXPECT(v.view().get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp b/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp index f5600d3ae5..c314e6cfac 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/arr_view.cpp @@ -26,20 +26,20 @@ namespace { // [1, 2.0, "three"] void example(bsoncxx::array::element e) { bsoncxx::types::bson_value::view v = e.get_value(); - ASSERT(v.type() == e.type()); + EXPECT(v.type() == e.type()); switch (v.type()) { case bsoncxx::type::k_int32: - ASSERT(e.key().compare("0") == 0); - ASSERT(v.get_int32() == e.get_int32()); + EXPECT(e.key().compare("0") == 0); + EXPECT(v.get_int32() == e.get_int32()); break; case bsoncxx::type::k_double: - ASSERT(e.key().compare("1") == 0); - ASSERT(v.get_double() == e.get_double()); + EXPECT(e.key().compare("1") == 0); + EXPECT(v.get_double() == e.get_double()); break; case bsoncxx::type::k_string: - ASSERT(e.key().compare("2") == 0); - ASSERT(v.get_string() == e.get_string()); + EXPECT(e.key().compare("2") == 0); + EXPECT(v.get_string() == e.get_string()); break; } } diff --git a/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp index 51ce072c61..4edfd3c93b 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_value.cpp @@ -24,20 +24,20 @@ namespace { void example() { bsoncxx::types::bson_value::value v = nullptr; - ASSERT(v.view().type() == bsoncxx::type::k_null); - ASSERT(v.view().get_null() == bsoncxx::types::b_null{}); + EXPECT(v.view().type() == bsoncxx::type::k_null); + EXPECT(v.view().get_null() == bsoncxx::types::b_null{}); v = bsoncxx::types::b_int32{1}; - ASSERT(v.view().type() == bsoncxx::type::k_int32); - ASSERT(v.view().get_int32().value == 1); + EXPECT(v.view().type() == bsoncxx::type::k_int32); + EXPECT(v.view().get_int32().value == 1); v = bsoncxx::types::b_double{2.0}; - ASSERT(v.view().type() == bsoncxx::type::k_double); - ASSERT(v.view().get_double().value == 2.0); + EXPECT(v.view().type() == bsoncxx::type::k_double); + EXPECT(v.view().get_double().value == 2.0); v = bsoncxx::types::b_string{"three"}; - ASSERT(v.view().type() == bsoncxx::type::k_string); - ASSERT(v.view().get_string().value.compare("three") == 0); + EXPECT(v.view().type() == bsoncxx::type::k_string); + EXPECT(v.view().get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/values/bson_type_view.cpp b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_view.cpp index 1d66ac44ee..6d89c4a866 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/bson_type_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/bson_type_view.cpp @@ -29,20 +29,20 @@ void example() { bsoncxx::types::b_string v2{"three"}; view_type v; - ASSERT(v.type() == bsoncxx::type::k_null); - ASSERT(v.get_null() == bsoncxx::types::b_null{}); + EXPECT(v.type() == bsoncxx::type::k_null); + EXPECT(v.get_null() == bsoncxx::types::b_null{}); v = view_type(v0); - ASSERT(v.type() == v0.type_id); - ASSERT(v.get_int32() == v0); + EXPECT(v.type() == v0.type_id); + EXPECT(v.get_int32() == v0); v = view_type(v1); - ASSERT(v.type() == v1.type_id); - ASSERT(v.get_double() == v1); + EXPECT(v.type() == v1.type_id); + EXPECT(v.get_double() == v1); v = view_type(v2); - ASSERT(v.type() == v2.type_id); - ASSERT(v.get_string() == v2); + EXPECT(v.type() == v2.type_id); + EXPECT(v.get_string() == v2); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp index ead44f8128..2e686d7ce1 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/doc_value.cpp @@ -25,19 +25,19 @@ namespace { void example() { bsoncxx::types::bson_value::value v = nullptr; - ASSERT(v.view().type() == bsoncxx::type::k_null); - ASSERT(v.view().get_null() == bsoncxx::types::b_null{}); + EXPECT(v.view().type() == bsoncxx::type::k_null); + EXPECT(v.view().get_null() == bsoncxx::types::b_null{}); v = bsoncxx::from_json(R"({"v": {"key": "value"}})") // Temporary object. ["v"] .get_owning_value(); // Copy: no dangling. - ASSERT(v.view().type() == bsoncxx::type::k_document); + EXPECT(v.view().type() == bsoncxx::type::k_document); v = v.view().get_document().value["key"].get_string(); // Copy: no dangling. - ASSERT(v.view().type() == bsoncxx::type::k_string); - ASSERT(v.view().get_string().value.compare("value") == 0); + EXPECT(v.view().type() == bsoncxx::type::k_string); + EXPECT(v.view().get_string().value.compare("value") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp b/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp index 362c233f38..3efeecf094 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/doc_view.cpp @@ -26,20 +26,20 @@ namespace { // {"a": 1, "b": 2.0, "c": "three"} void example(bsoncxx::document::element e) { bsoncxx::types::bson_value::view v = e.get_value(); - ASSERT(v.type() == e.type()); + EXPECT(v.type() == e.type()); switch (v.type()) { case bsoncxx::type::k_int32: - ASSERT(e.key().compare("a") == 0); - ASSERT(v.get_int32() == e.get_int32()); + EXPECT(e.key().compare("a") == 0); + EXPECT(v.get_int32() == e.get_int32()); break; case bsoncxx::type::k_double: - ASSERT(e.key().compare("b") == 0); - ASSERT(v.get_double() == e.get_double()); + EXPECT(e.key().compare("b") == 0); + EXPECT(v.get_double() == e.get_double()); break; case bsoncxx::type::k_string: - ASSERT(e.key().compare("c") == 0); - ASSERT(v.get_string() == e.get_string()); + EXPECT(e.key().compare("c") == 0); + EXPECT(v.get_string() == e.get_string()); break; } } diff --git a/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp b/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp index f2837d5e42..993e975814 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/make_value.cpp @@ -33,24 +33,24 @@ void example() { value_type owner = bsoncxx::types::bson_value::make_value(1); view_type v = owner.view(); - ASSERT(v.type() == bsoncxx::type::k_int32); - ASSERT(v.get_int32().value == 1); + EXPECT(v.type() == bsoncxx::type::k_int32); + EXPECT(v.get_int32().value == 1); } { value_type owner = bsoncxx::types::bson_value::make_value(bsoncxx::types::b_int64{2}); view_type v = owner.view(); - ASSERT(v.type() == bsoncxx::type::k_int64); - ASSERT(v.get_int64().value == 2); + EXPECT(v.type() == bsoncxx::type::k_int64); + EXPECT(v.get_int64().value == 2); } { value_type owner = bsoncxx::types::bson_value::make_value(std::string("three")); view_type v = owner.view(); - ASSERT(v.type() == bsoncxx::type::k_string); - ASSERT(v.get_string().value.compare("three") == 0); + EXPECT(v.type() == bsoncxx::type::k_string); + EXPECT(v.get_string().value.compare("three") == 0); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp b/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp index 34f17aaa18..4e18d186e0 100644 --- a/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp +++ b/examples/api/bsoncxx/examples/bson_documents/values/value_type.cpp @@ -26,23 +26,23 @@ namespace { void example() { bsoncxx::types::bson_value::value v = nullptr; - ASSERT(v.view().type() == bsoncxx::type::k_null); - ASSERT(v.view().get_null() == bsoncxx::types::b_null{}); + EXPECT(v.view().type() == bsoncxx::type::k_null); + EXPECT(v.view().get_null() == bsoncxx::types::b_null{}); v = std::int32_t{1}; - ASSERT(v.view().type() == bsoncxx::type::k_int32); - ASSERT(v.view().get_int32().value == 1); + EXPECT(v.view().type() == bsoncxx::type::k_int32); + EXPECT(v.view().get_int32().value == 1); v = 2.0; - ASSERT(v.view().type() == bsoncxx::type::k_double); - ASSERT(v.view().get_double().value == 2.0); + EXPECT(v.view().type() == bsoncxx::type::k_double); + EXPECT(v.view().get_double().value == 2.0); v = std::string("three"); - ASSERT(v.view().type() == bsoncxx::type::k_string); - ASSERT(v.view().get_string().value.compare("three") == 0); + EXPECT(v.view().type() == bsoncxx::type::k_string); + EXPECT(v.view().get_string().value.compare("three") == 0); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_end.cpp b/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_end.cpp index ccfa4a9a70..0e53d4b530 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_end.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_end.cpp @@ -25,7 +25,7 @@ namespace { // [Example] // [1, 2, 3] void example(bsoncxx::array::view arr) { - ASSERT(arr.begin() != arr.end()); + EXPECT(arr.begin() != arr.end()); auto iter = arr.begin(); // 1 @@ -33,15 +33,15 @@ void example(bsoncxx::array::view arr) { ++iter; // 3 ++iter; // End iterator. - ASSERT(iter == arr.end()); + EXPECT(iter == arr.end()); ++iter; // DO NOT DO THIS - ASSERT(iter == arr.end()); // Incrementing an end iterator results in an end iterator. + EXPECT(iter == arr.end()); // Incrementing an end iterator results in an end iterator. bsoncxx::array::element e = *iter; // DO NOT DO THIS - ASSERT(!e); // An end iterator returns an invalid element. + EXPECT(!e); // An end iterator returns an invalid element. } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_invalid.cpp b/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_invalid.cpp index 92d5ef7f24..3ec1731122 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_invalid.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_arr_iter_invalid.cpp @@ -34,11 +34,11 @@ void example() { auto iter = arr.begin(); - ASSERT(iter == arr.end()); // An invalid BSON document returns an end iterator. + EXPECT(iter == arr.end()); // An invalid BSON document returns an end iterator. bsoncxx::array::element e = *iter; // DO NOT DO THIS - ASSERT(!e); // An end iterator returns an invalid element. + EXPECT(!e); // An end iterator returns an invalid element. } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/access_arr_key_missing.cpp b/examples/api/bsoncxx/examples/bson_errors/access_arr_key_missing.cpp index 10508f49b7..f63a1cdc03 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_arr_key_missing.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_arr_key_missing.cpp @@ -27,11 +27,11 @@ namespace { // [Example] // [1, 2, 3] void example(bsoncxx::array::view arr) { - ASSERT(std::distance(arr.begin(), arr.end()) == 3); + EXPECT(std::distance(arr.begin(), arr.end()) == 3); bsoncxx::array::element e = arr[3]; - ASSERT(!e); // A missing element is represented as an invalid element. + EXPECT(!e); // A missing element is represented as an invalid element. } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_end.cpp b/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_end.cpp index 27f235a654..0bb78f20d8 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_end.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_end.cpp @@ -25,22 +25,22 @@ namespace { // [Example] // {"a": 1, "b": 2} void example(bsoncxx::document::view doc) { - ASSERT(doc.begin() != doc.end()); + EXPECT(doc.begin() != doc.end()); auto iter = doc.begin(); // "a": 1 ++iter; // "b": 2 ++iter; // End iterator. - ASSERT(iter == doc.end()); + EXPECT(iter == doc.end()); ++iter; // DO NOT DO THIS - ASSERT(iter == doc.end()); // Incrementing an end iterator results in an end iterator. + EXPECT(iter == doc.end()); // Incrementing an end iterator results in an end iterator. bsoncxx::document::element e = *iter; // DO NOT DO THIS - ASSERT(!e); // An end iterator returns an invalid element. + EXPECT(!e); // An end iterator returns an invalid element. } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_invalid.cpp b/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_invalid.cpp index 5079e48ee1..11fc5713af 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_invalid.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_doc_iter_invalid.cpp @@ -35,11 +35,11 @@ void example() { auto iter = doc.begin(); - ASSERT(iter == doc.end()); // An invalid BSON document returns an end iterator. + EXPECT(iter == doc.end()); // An invalid BSON document returns an end iterator. bsoncxx::document::element e = *iter; // DO NOT DO THIS - ASSERT(!e); // An end iterator returns an invalid element. + EXPECT(!e); // An end iterator returns an invalid element. } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/access_doc_key_missing.cpp b/examples/api/bsoncxx/examples/bson_errors/access_doc_key_missing.cpp index e7a3db6268..7d55ba0377 100644 --- a/examples/api/bsoncxx/examples/bson_errors/access_doc_key_missing.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/access_doc_key_missing.cpp @@ -25,12 +25,12 @@ namespace { // [Example] // {"a": 1, "b": 2} void example(bsoncxx::document::view doc) { - ASSERT(doc["a"]); - ASSERT(doc["b"]); + EXPECT(doc["a"]); + EXPECT(doc["b"]); bsoncxx::document::element e = doc["c"]; - ASSERT(!e); // A missing element is represented as an invalid element. + EXPECT(!e); // A missing element is represented as an invalid element. } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_arr_append.cpp b/examples/api/bsoncxx/examples/bson_errors/create_arr_append.cpp index 48e2be60f6..1213d56243 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_arr_append.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_arr_append.cpp @@ -40,12 +40,12 @@ void example(bsoncxx::stdx::string_view big_string) { try { builder.append(big_string); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_cannot_append_string); + EXPECT(ex.code() == bsoncxx::error_code::k_cannot_append_string); } - ASSERT(builder.view() == original.view()); + EXPECT(builder.view() == original.view()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_array.cpp b/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_array.cpp index 7f8c019919..bd696bad9d 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_array.cpp @@ -42,28 +42,28 @@ void example(bsoncxx::stdx::string_view big_string) { arr.append(big_string); // Throws. }); - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_cannot_append_string); + EXPECT(ex.code() == bsoncxx::error_code::k_cannot_append_string); } // Builder is in an erroneous state. try { builder.view(); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); + EXPECT(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); } // Reset builder to a usable state. builder.clear(); - ASSERT(builder.view().empty()); + EXPECT(builder.view().empty()); // Restore the original state prior to the append failure. builder.append(bsoncxx::builder::concatenate_array{original.view()}); - ASSERT(builder.view() == original.view()); + EXPECT(builder.view() == original.view()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_document.cpp b/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_document.cpp index acc2ddb3d1..51a4e1af6d 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_arr_append_sub_document.cpp @@ -45,28 +45,28 @@ void example(bsoncxx::stdx::string_view big_string) { doc.append(kvp("too big", big_string)); // Throws. }); - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_cannot_append_string); + EXPECT(ex.code() == bsoncxx::error_code::k_cannot_append_string); } // Builder is in an erroneous state. try { builder.view(); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); + EXPECT(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); } // Reset builder to a usable state. builder.clear(); - ASSERT(builder.view().empty()); + EXPECT(builder.view().empty()); // Restore the original state prior to the append failure. builder.append(bsoncxx::builder::concatenate_array{original.view()}); - ASSERT(builder.view() == original.view()); + EXPECT(builder.view() == original.view()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_element.cpp b/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_element.cpp index b47eac9c3a..df9d5fdead 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_element.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_element.cpp @@ -28,9 +28,9 @@ void example() { bsoncxx::document::element e; bsoncxx::types::bson_value::view v = e.get_value(); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_unset_element); + EXPECT(ex.code() == bsoncxx::error_code::k_unset_element); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_type.cpp index c02b75331d..70439777d6 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_type.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_bson_value_invalid_type.cpp @@ -27,9 +27,9 @@ void example() { try { bsoncxx::types::bson_value::value value{bsoncxx::type::k_null}; // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_invalid_bson_type_id); + EXPECT(ex.code() == bsoncxx::error_code::k_invalid_bson_type_id); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_doc_append.cpp b/examples/api/bsoncxx/examples/bson_errors/create_doc_append.cpp index cd97ea327b..b4b760ec35 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_doc_append.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_doc_append.cpp @@ -43,12 +43,12 @@ void example(bsoncxx::stdx::string_view big_string) { try { builder.append(kvp("too big", big_string)); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_cannot_append_string); + EXPECT(ex.code() == bsoncxx::error_code::k_cannot_append_string); } - ASSERT(builder.view() == original.view()); + EXPECT(builder.view() == original.view()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_array.cpp b/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_array.cpp index 8e5ea16e21..0393a93ceb 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_array.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_array.cpp @@ -47,28 +47,28 @@ void example(bsoncxx::stdx::string_view big_string) { arr.append(big_string); // Throws. })); - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_cannot_append_string); + EXPECT(ex.code() == bsoncxx::error_code::k_cannot_append_string); } // Builder is in an erroneous state. try { builder.view(); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); + EXPECT(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); } // Reset builder to a usable state. builder.clear(); - ASSERT(builder.view().empty()); + EXPECT(builder.view().empty()); // Restore the original state prior to the append failure. builder.append(bsoncxx::builder::concatenate_doc{original.view()}); - ASSERT(builder.view() == original.view()); + EXPECT(builder.view() == original.view()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_document.cpp b/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_document.cpp index e45ebb5cd0..6a3ce423d1 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_document.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_doc_append_sub_document.cpp @@ -46,28 +46,28 @@ void example(bsoncxx::stdx::string_view big_string) { doc.append(kvp("too big", big_string)); // Throws. })); - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_cannot_append_string); + EXPECT(ex.code() == bsoncxx::error_code::k_cannot_append_string); } // Builder is in an erroneous state. try { builder.view(); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); + EXPECT(ex.code() == bsoncxx::error_code::k_unmatched_key_in_builder); } // Reset builder to a usable state. builder.clear(); - ASSERT(builder.view().empty()); + EXPECT(builder.view().empty()); // Restore the original state prior to the append failure. builder.append(bsoncxx::builder::concatenate_doc{original.view()}); - ASSERT(builder.view() == original.view()); + EXPECT(builder.view() == original.view()); } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/create_json.cpp b/examples/api/bsoncxx/examples/bson_errors/create_json.cpp index 3621351f5e..a9d3df4f63 100644 --- a/examples/api/bsoncxx/examples/bson_errors/create_json.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/create_json.cpp @@ -26,9 +26,9 @@ void example() { try { const auto doc = bsoncxx::from_json(R"(invalid json)"); - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_json_parse_failure); + EXPECT(ex.code() == bsoncxx::error_code::k_json_parse_failure); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/query_bson_value_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/query_bson_value_invalid_type.cpp index b89f67d7ab..5e2a491c55 100644 --- a/examples/api/bsoncxx/examples/bson_errors/query_bson_value_invalid_type.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/query_bson_value_invalid_type.cpp @@ -25,23 +25,23 @@ namespace { // [Example] void example() { bsoncxx::types::bson_value::view v; - ASSERT(v.type() == bsoncxx::type::k_null); - ASSERT(v.get_null() == bsoncxx::types::b_null{}); + EXPECT(v.type() == bsoncxx::type::k_null); + EXPECT(v.get_null() == bsoncxx::types::b_null{}); try { bsoncxx::types::b_int32 i = v.get_int32(); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_need_element_type_k_int32); + EXPECT(ex.code() == bsoncxx::error_code::k_need_element_type_k_int32); } try { bsoncxx::types::b_int64 i = v.get_int64(); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_need_element_type_k_int64); + EXPECT(ex.code() == bsoncxx::error_code::k_need_element_type_k_int64); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid.cpp index a1dd6a6af0..8b2df5e6f1 100644 --- a/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid.cpp @@ -35,22 +35,22 @@ void example() { bsoncxx::array::element e = arr[0]; - ASSERT(!e); // An invalid BSON array returns an invalid element. + EXPECT(!e); // An invalid BSON array returns an invalid element. try { bsoncxx::stdx::string_view key = e.key(); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_unset_element); + EXPECT(ex.code() == bsoncxx::error_code::k_unset_element); } try { bsoncxx::type type = e.type(); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_unset_element); + EXPECT(ex.code() == bsoncxx::error_code::k_unset_element); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp index 7ec31ef9a1..db802cd32d 100644 --- a/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_arr_invalid_type.cpp @@ -26,24 +26,24 @@ namespace { // [Example] // [1] void example(bsoncxx::array::element e) { - ASSERT(e.key().compare("0") == 0); - ASSERT(e.type() == bsoncxx::type::k_int32); - ASSERT(e.get_int32().value == 1); + EXPECT(e.key().compare("0") == 0); + EXPECT(e.type() == bsoncxx::type::k_int32); + EXPECT(e.get_int32().value == 1); try { bsoncxx::types::b_double d = e.get_double(); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_need_element_type_k_double); + EXPECT(ex.code() == bsoncxx::error_code::k_need_element_type_k_double); } try { bsoncxx::types::b_string str = e.get_string(); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_need_element_type_k_string); + EXPECT(ex.code() == bsoncxx::error_code::k_need_element_type_k_string); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid.cpp index 987e607833..b54804a220 100644 --- a/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid.cpp @@ -38,22 +38,22 @@ void example() { bsoncxx::document::element e = doc["x"]; - ASSERT(!e); // An invalid BSON document returns an invalid element. + EXPECT(!e); // An invalid BSON document returns an invalid element. try { bsoncxx::stdx::string_view key = e.key(); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_unset_element); + EXPECT(ex.code() == bsoncxx::error_code::k_unset_element); } try { bsoncxx::type type = e.type(); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_unset_element); + EXPECT(ex.code() == bsoncxx::error_code::k_unset_element); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp index 58b8abd72e..3f7fe409fc 100644 --- a/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/query_element_doc_invalid_type.cpp @@ -26,24 +26,24 @@ namespace { // [Example] // {"x": 1} void example(bsoncxx::document::element e) { - ASSERT(e.key().compare("x") == 0); - ASSERT(e.type() == bsoncxx::type::k_int32); - ASSERT(e.get_int32().value == 1); + EXPECT(e.key().compare("x") == 0); + EXPECT(e.type() == bsoncxx::type::k_int32); + EXPECT(e.get_int32().value == 1); try { bsoncxx::types::b_double d = e.get_double(); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_need_element_type_k_double); + EXPECT(ex.code() == bsoncxx::error_code::k_need_element_type_k_double); } try { bsoncxx::types::b_string str = e.get_string(); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_need_element_type_k_string); + EXPECT(ex.code() == bsoncxx::error_code::k_need_element_type_k_string); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_arr.cpp b/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_arr.cpp index afc36e1fd4..9325c7f785 100644 --- a/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_arr.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_arr.cpp @@ -40,9 +40,9 @@ void example() { try { std::string json = bsoncxx::to_json(doc); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_failed_converting_bson_to_json); + EXPECT(ex.code() == bsoncxx::error_code::k_failed_converting_bson_to_json); } } // [Example] diff --git a/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_doc.cpp b/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_doc.cpp index 85eee021a0..6d9b8466ed 100644 --- a/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_doc.cpp +++ b/examples/api/bsoncxx/examples/bson_errors/to_json_invalid_doc.cpp @@ -40,9 +40,9 @@ void example() { try { std::string json = bsoncxx::to_json(doc); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_failed_converting_bson_to_json); + EXPECT(ex.code() == bsoncxx::error_code::k_failed_converting_bson_to_json); } } // [Example] diff --git a/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp b/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp index db2ae6c899..1e70994988 100644 --- a/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp +++ b/examples/api/bsoncxx/examples/decimal128/basic_usage.cpp @@ -26,72 +26,72 @@ void example() { using d128 = bsoncxx::decimal128; // For brevity. // Default ctor. - ASSERT(d128{}.to_string() == "0E-6176"); + EXPECT(d128{}.to_string() == "0E-6176"); - ASSERT(d128{"0"}.to_string() == "0"); - ASSERT(d128{"12"}.to_string() == "12"); - ASSERT(d128{"-76"}.to_string() == "-76"); - ASSERT(d128{"12.70"}.to_string() == "12.70"); - ASSERT(d128{"+0.003"}.to_string() == "0.003"); - ASSERT(d128{"017."}.to_string() == "17"); - ASSERT(d128{".5"}.to_string() == "0.5"); - ASSERT(d128{"4E+9"}.to_string() == "4E+9"); - ASSERT(d128{"0.73e-7"}.to_string() == "7.3E-8"); - ASSERT(d128{"Inf"}.to_string() == "Infinity"); - ASSERT(d128{"-infinity"}.to_string() == "-Infinity"); - ASSERT(d128{"NaN"}.to_string() == "NaN"); + EXPECT(d128{"0"}.to_string() == "0"); + EXPECT(d128{"12"}.to_string() == "12"); + EXPECT(d128{"-76"}.to_string() == "-76"); + EXPECT(d128{"12.70"}.to_string() == "12.70"); + EXPECT(d128{"+0.003"}.to_string() == "0.003"); + EXPECT(d128{"017."}.to_string() == "17"); + EXPECT(d128{".5"}.to_string() == "0.5"); + EXPECT(d128{"4E+9"}.to_string() == "4E+9"); + EXPECT(d128{"0.73e-7"}.to_string() == "7.3E-8"); + EXPECT(d128{"Inf"}.to_string() == "Infinity"); + EXPECT(d128{"-infinity"}.to_string() == "-Infinity"); + EXPECT(d128{"NaN"}.to_string() == "NaN"); // "NaN8275" is not a supported string representation. - ASSERT(d128{"123"}.to_string() == "123"); - ASSERT(d128{"-123"}.to_string() == "-123"); - ASSERT(d128{"1.23E+3"}.to_string() == "1.23E+3"); - ASSERT(d128{"1.23E+5"}.to_string() == "1.23E+5"); - ASSERT(d128{"12.3"}.to_string() == "12.3"); - ASSERT(d128{"0.00123"}.to_string() == "0.00123"); - ASSERT(d128{"1.23E-8"}.to_string() == "1.23E-8"); - ASSERT(d128{"-1.23E-10"}.to_string() == "-1.23E-10"); - ASSERT(d128{"0"}.to_string() == "0"); - ASSERT(d128{"0.00"}.to_string() == "0.00"); - ASSERT(d128{"0E+2"}.to_string() == "0E+2"); - ASSERT(d128{"-0"}.to_string() == "-0"); - ASSERT(d128{"0.000005"}.to_string() == "0.000005"); - ASSERT(d128{"0.0000050"}.to_string() == "0.0000050"); - ASSERT(d128{"5E-7"}.to_string() == "5E-7"); - ASSERT(d128{"Infinity"}.to_string() == "Infinity"); - ASSERT(d128{"-Infinity"}.to_string() == "-Infinity"); - ASSERT(d128{"NaN"}.to_string() == "NaN"); + EXPECT(d128{"123"}.to_string() == "123"); + EXPECT(d128{"-123"}.to_string() == "-123"); + EXPECT(d128{"1.23E+3"}.to_string() == "1.23E+3"); + EXPECT(d128{"1.23E+5"}.to_string() == "1.23E+5"); + EXPECT(d128{"12.3"}.to_string() == "12.3"); + EXPECT(d128{"0.00123"}.to_string() == "0.00123"); + EXPECT(d128{"1.23E-8"}.to_string() == "1.23E-8"); + EXPECT(d128{"-1.23E-10"}.to_string() == "-1.23E-10"); + EXPECT(d128{"0"}.to_string() == "0"); + EXPECT(d128{"0.00"}.to_string() == "0.00"); + EXPECT(d128{"0E+2"}.to_string() == "0E+2"); + EXPECT(d128{"-0"}.to_string() == "-0"); + EXPECT(d128{"0.000005"}.to_string() == "0.000005"); + EXPECT(d128{"0.0000050"}.to_string() == "0.0000050"); + EXPECT(d128{"5E-7"}.to_string() == "5E-7"); + EXPECT(d128{"Infinity"}.to_string() == "Infinity"); + EXPECT(d128{"-Infinity"}.to_string() == "-Infinity"); + EXPECT(d128{"NaN"}.to_string() == "NaN"); // "NaN123" is not a supported string representation. // "-sNaN" is not a supported string representation. - ASSERT(d128{"1.23E+3"}.to_string() == "1.23E+3"); - ASSERT(d128{"123E+3"}.to_string() == "1.23E+5"); - ASSERT(d128{"12.3E-9"}.to_string() == "1.23E-8"); - ASSERT(d128{"-123E-12"}.to_string() == "-1.23E-10"); - ASSERT(d128{"700E-9"}.to_string() == "7.00E-7"); - ASSERT(d128{"70"}.to_string() == "70"); - ASSERT(d128{"0.00E+3"}.to_string() == "0E+1"); + EXPECT(d128{"1.23E+3"}.to_string() == "1.23E+3"); + EXPECT(d128{"123E+3"}.to_string() == "1.23E+5"); + EXPECT(d128{"12.3E-9"}.to_string() == "1.23E-8"); + EXPECT(d128{"-123E-12"}.to_string() == "-1.23E-10"); + EXPECT(d128{"700E-9"}.to_string() == "7.00E-7"); + EXPECT(d128{"70"}.to_string() == "70"); + EXPECT(d128{"0.00E+3"}.to_string() == "0E+1"); - ASSERT(d128{"0"}.to_string() == "0"); - ASSERT(d128{"0.00"}.to_string() == "0.00"); - ASSERT(d128{"123"}.to_string() == "123"); - ASSERT(d128{"-123"}.to_string() == "-123"); - ASSERT(d128{"1.23E3"}.to_string() == "1.23E+3"); - ASSERT(d128{"1.23E+3"}.to_string() == "1.23E+3"); - ASSERT(d128{"12.3E+7"}.to_string() == "1.23E+8"); - ASSERT(d128{"12.0"}.to_string() == "12.0"); - ASSERT(d128{"12.3"}.to_string() == "12.3"); - ASSERT(d128{"0.00123"}.to_string() == "0.00123"); - ASSERT(d128{"-1.23E-12"}.to_string() == "-1.23E-12"); - ASSERT(d128{"1234.5E-4"}.to_string() == "0.12345"); - ASSERT(d128{"-0"}.to_string() == "-0"); - ASSERT(d128{"-0.00"}.to_string() == "-0.00"); - ASSERT(d128{"0E+7"}.to_string() == "0E+7"); - ASSERT(d128{"-0E-7"}.to_string() == "-0E-7"); - ASSERT(d128{"inf"}.to_string() == "Infinity"); - ASSERT(d128{"+inFiniTy"}.to_string() == "Infinity"); - ASSERT(d128{"-Infinity"}.to_string() == "-Infinity"); - ASSERT(d128{"NaN"}.to_string() == "NaN"); - ASSERT(d128{"-NAN"}.to_string() == "NaN"); + EXPECT(d128{"0"}.to_string() == "0"); + EXPECT(d128{"0.00"}.to_string() == "0.00"); + EXPECT(d128{"123"}.to_string() == "123"); + EXPECT(d128{"-123"}.to_string() == "-123"); + EXPECT(d128{"1.23E3"}.to_string() == "1.23E+3"); + EXPECT(d128{"1.23E+3"}.to_string() == "1.23E+3"); + EXPECT(d128{"12.3E+7"}.to_string() == "1.23E+8"); + EXPECT(d128{"12.0"}.to_string() == "12.0"); + EXPECT(d128{"12.3"}.to_string() == "12.3"); + EXPECT(d128{"0.00123"}.to_string() == "0.00123"); + EXPECT(d128{"-1.23E-12"}.to_string() == "-1.23E-12"); + EXPECT(d128{"1234.5E-4"}.to_string() == "0.12345"); + EXPECT(d128{"-0"}.to_string() == "-0"); + EXPECT(d128{"-0.00"}.to_string() == "-0.00"); + EXPECT(d128{"0E+7"}.to_string() == "0E+7"); + EXPECT(d128{"-0E-7"}.to_string() == "-0E-7"); + EXPECT(d128{"inf"}.to_string() == "Infinity"); + EXPECT(d128{"+inFiniTy"}.to_string() == "Infinity"); + EXPECT(d128{"-Infinity"}.to_string() == "-Infinity"); + EXPECT(d128{"NaN"}.to_string() == "NaN"); + EXPECT(d128{"-NAN"}.to_string() == "NaN"); // "SNaN" is not a supported string representation. // "Fred" is not a supported string representation. } diff --git a/examples/api/bsoncxx/examples/decimal128/errors.cpp b/examples/api/bsoncxx/examples/decimal128/errors.cpp index da6c1f24e0..c852a93f93 100644 --- a/examples/api/bsoncxx/examples/decimal128/errors.cpp +++ b/examples/api/bsoncxx/examples/decimal128/errors.cpp @@ -26,9 +26,9 @@ void example() { try { bsoncxx::decimal128 d{"invalid"}; // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_invalid_decimal128); + EXPECT(ex.code() == bsoncxx::error_code::k_invalid_decimal128); } } // [Example] diff --git a/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp b/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp index 86f1ec0999..d29b46d5d1 100644 --- a/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp +++ b/examples/api/bsoncxx/examples/decimal128/from_bytes.cpp @@ -26,55 +26,55 @@ void example() { using d128 = bsoncxx::decimal128; // For brevity. // Default ctor. - ASSERT((d128{0u, 0u}) == d128{}); + EXPECT((d128{0u, 0u}) == d128{}); // [0,0,0] - ASSERT((d128{0x3040000000000000, 0x0000000000000000}) == d128{"0"}); + EXPECT((d128{0x3040000000000000, 0x0000000000000000}) == d128{"0"}); // [1,0,0] - ASSERT((d128{0xb040000000000000, 0x0000000000000000}) == d128{"-0"}); + EXPECT((d128{0xb040000000000000, 0x0000000000000000}) == d128{"-0"}); // [0,0,1] - ASSERT((d128{0x3040000000000000, 0x0000000000000000}) == d128{"00E0"}); + EXPECT((d128{0x3040000000000000, 0x0000000000000000}) == d128{"00E0"}); // [0,0,-1] - ASSERT((d128{0x303e000000000000, 0x0000000000000000}) == d128{"0.0E0"}); + EXPECT((d128{0x303e000000000000, 0x0000000000000000}) == d128{"0.0E0"}); // [0,123,0] - ASSERT((d128{0x3040000000000000, 0x000000000000007b}) == d128{"123"}); + EXPECT((d128{0x3040000000000000, 0x000000000000007b}) == d128{"123"}); // [1,123,0] - ASSERT((d128{0xb040000000000000, 0x000000000000007b}) == d128{"-123"}); + EXPECT((d128{0xb040000000000000, 0x000000000000007b}) == d128{"-123"}); // [0,123,1] - ASSERT((d128{0x3042000000000000, 0x000000000000007b}) == d128{"1.23E3"}); + EXPECT((d128{0x3042000000000000, 0x000000000000007b}) == d128{"1.23E3"}); // [1,123,1] - ASSERT((d128{0xb042000000000000, 0x000000000000007b}) == d128{"-1.23E3"}); + EXPECT((d128{0xb042000000000000, 0x000000000000007b}) == d128{"-1.23E3"}); // [0,123,-1] - ASSERT((d128{0x303e000000000000, 0x000000000000007b}) == d128{"12.3"}); + EXPECT((d128{0x303e000000000000, 0x000000000000007b}) == d128{"12.3"}); // [1,123,-1] - ASSERT((d128{0xb03e000000000000, 0x000000000000007b}) == d128{"-12.3"}); + EXPECT((d128{0xb03e000000000000, 0x000000000000007b}) == d128{"-12.3"}); // [0,inf] - ASSERT((d128{0x7800000000000000, 0x0000000000000000}) == d128{"Infinity"}); + EXPECT((d128{0x7800000000000000, 0x0000000000000000}) == d128{"Infinity"}); // [1,inf] - ASSERT((d128{0xf800000000000000, 0x0000000000000000}) == d128{"-Infinity"}); + EXPECT((d128{0xf800000000000000, 0x0000000000000000}) == d128{"-Infinity"}); // [0,qNaN] - ASSERT((d128{0x7c00000000000000, 0x0000000000000000}) == d128{"NaN"}); + EXPECT((d128{0x7c00000000000000, 0x0000000000000000}) == d128{"NaN"}); // [1,qNaN]: Negative NaN is string-represented as NaN. - ASSERT((d128{0xfc00000000000000, 0x0000000000000000}).to_string() == "NaN"); + EXPECT((d128{0xfc00000000000000, 0x0000000000000000}).to_string() == "NaN"); // [0,sNaN]: Signaling NaN is string-represented as NaN. - ASSERT((d128{0x7e00000000000000, 0x0000000000000000}).to_string() == "NaN"); + EXPECT((d128{0x7e00000000000000, 0x0000000000000000}).to_string() == "NaN"); // [1,sNaN]: Negative Signaling NaN is string-represented as NaN. - ASSERT((d128{0x7e00000000000000, 0x0000000000000000}).to_string() == "NaN"); + EXPECT((d128{0x7e00000000000000, 0x0000000000000000}).to_string() == "NaN"); } // [Example] diff --git a/examples/api/bsoncxx/examples/oid/basic_usage.cpp b/examples/api/bsoncxx/examples/oid/basic_usage.cpp index 6599104d4c..b58847b891 100644 --- a/examples/api/bsoncxx/examples/oid/basic_usage.cpp +++ b/examples/api/bsoncxx/examples/oid/basic_usage.cpp @@ -29,7 +29,7 @@ void example() { bsoncxx::oid a; bsoncxx::oid b; - ASSERT(a != b); // Random and unique per process. + EXPECT(a != b); // Random and unique per process. } { @@ -40,17 +40,17 @@ void example() { { std::time_t time = oid.get_time_t(); char str[sizeof("YYYY-MM-DD HH:MM:SS")]; - ASSERT(std::strftime(str, sizeof(str), "%F %T", std::gmtime(&time)) == + EXPECT(std::strftime(str, sizeof(str), "%F %T", std::gmtime(&time)) == sizeof(str) - 1u); - ASSERT(std::string(str) == "1970-01-01 00:00:00"); + EXPECT(std::string(str) == "1970-01-01 00:00:00"); } - ASSERT(oid.bytes() != bytes); // Seperate storage. - ASSERT(oid.size() == bsoncxx::oid::k_oid_length); - ASSERT(std::memcmp(bytes, oid.bytes(), oid.size()) == 0); + EXPECT(oid.bytes() != bytes); // Seperate storage. + EXPECT(oid.size() == bsoncxx::oid::k_oid_length); + EXPECT(std::memcmp(bytes, oid.bytes(), oid.size()) == 0); - ASSERT(oid.to_string() == "000000000000000000000000"); - ASSERT(oid == bsoncxx::oid{"000000000000000000000000"}); + EXPECT(oid.to_string() == "000000000000000000000000"); + EXPECT(oid == bsoncxx::oid{"000000000000000000000000"}); } { @@ -62,17 +62,17 @@ void example() { { std::time_t time = oid.get_time_t(); char str[sizeof("YYYY-MM-DD HH:MM:SS")]; - ASSERT(std::strftime(str, sizeof(str), "%F %T", std::gmtime(&time)) == + EXPECT(std::strftime(str, sizeof(str), "%F %T", std::gmtime(&time)) == sizeof(str) - 1u); - ASSERT(std::string(str) == "2000-01-01 23:59:59"); + EXPECT(std::string(str) == "2000-01-01 23:59:59"); } - ASSERT(oid < bsoncxx::oid{"389622001112131415212223"}); // Timestamp: 2000-02-01 00:00:00 - ASSERT(oid > bsoncxx::oid{"386d43801112131415212223"}); // Timestamp: 2000-01-01 00:00:00 - ASSERT(oid < bsoncxx::oid{"386e94ffffffffffff212223"}); // Value: 1099511627775 - ASSERT(oid > bsoncxx::oid{"386e94ff0000000000212223"}); // Value: 0 - ASSERT(oid < bsoncxx::oid{"386e94ff1112131415ffffff"}); // Counter: 16777215 - ASSERT(oid > bsoncxx::oid{"386e94ff1112131415000000"}); // Counter: 0 + EXPECT(oid < bsoncxx::oid{"389622001112131415212223"}); // Timestamp: 2000-02-01 00:00:00 + EXPECT(oid > bsoncxx::oid{"386d43801112131415212223"}); // Timestamp: 2000-01-01 00:00:00 + EXPECT(oid < bsoncxx::oid{"386e94ffffffffffff212223"}); // Value: 1099511627775 + EXPECT(oid > bsoncxx::oid{"386e94ff0000000000212223"}); // Value: 0 + EXPECT(oid < bsoncxx::oid{"386e94ff1112131415ffffff"}); // Counter: 16777215 + EXPECT(oid > bsoncxx::oid{"386e94ff1112131415000000"}); // Counter: 0 } } // [Example] diff --git a/examples/api/bsoncxx/examples/oid/errors.cpp b/examples/api/bsoncxx/examples/oid/errors.cpp index 856adcb182..d653a898ac 100644 --- a/examples/api/bsoncxx/examples/oid/errors.cpp +++ b/examples/api/bsoncxx/examples/oid/errors.cpp @@ -26,9 +26,9 @@ void example() { try { bsoncxx::oid oid{"invalid"}; // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_invalid_oid); + EXPECT(ex.code() == bsoncxx::error_code::k_invalid_oid); } try { @@ -36,9 +36,9 @@ void example() { bsoncxx::oid oid{bytes, sizeof(bytes)}; // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const bsoncxx::exception& ex) { - ASSERT(ex.code() == bsoncxx::error_code::k_invalid_oid); + EXPECT(ex.code() == bsoncxx::error_code::k_invalid_oid); } } // [Example] diff --git a/examples/api/bsoncxx/examples/validation/basic_usage.cpp b/examples/api/bsoncxx/examples/validation/basic_usage.cpp index 41f3198cdd..f24b88db63 100644 --- a/examples/api/bsoncxx/examples/validation/basic_usage.cpp +++ b/examples/api/bsoncxx/examples/validation/basic_usage.cpp @@ -30,14 +30,14 @@ void example() { { std::uint8_t bytes[1]{}; // Invalid. - ASSERT(!bsoncxx::validate(bytes, sizeof(bytes))); + EXPECT(!bsoncxx::validate(bytes, sizeof(bytes))); std::size_t offset; - ASSERT(!bsoncxx::validate(bytes, sizeof(bytes), bsoncxx::validator{}, &offset)); + EXPECT(!bsoncxx::validate(bytes, sizeof(bytes), bsoncxx::validator{}, &offset)); // Set to `0` for an invalid BSON document. - ASSERT(offset == 0u); + EXPECT(offset == 0u); } bsoncxx::document::value owner = bsoncxx::from_json(R"({"x": 1})"); @@ -46,24 +46,24 @@ void example() { { auto doc_opt = bsoncxx::validate(data, length); - ASSERT(doc_opt); + EXPECT(doc_opt); bsoncxx::document::view doc = *doc_opt; - ASSERT(doc.data() == data); - ASSERT(doc.length() == length); - ASSERT(doc == owner.view()); + EXPECT(doc.data() == data); + EXPECT(doc.length() == length); + EXPECT(doc == owner.view()); } { bsoncxx::validator options; std::size_t offset = 123u; - ASSERT(bsoncxx::validate(data, length) == + EXPECT(bsoncxx::validate(data, length) == bsoncxx::validate(data, length, options, &offset)); // Not set when valid. - ASSERT(offset == 123u); + EXPECT(offset == 123u); } } // [Example] diff --git a/examples/api/bsoncxx/examples/validation/validator.cpp b/examples/api/bsoncxx/examples/validation/validator.cpp index 04ea221a0b..b6a40ea43d 100644 --- a/examples/api/bsoncxx/examples/validation/validator.cpp +++ b/examples/api/bsoncxx/examples/validation/validator.cpp @@ -35,7 +35,7 @@ void example(const std::uint8_t* bytes, std::size_t length) { bsoncxx::validator options; std::size_t offset; - ASSERT(bsoncxx::validate(bytes, length, options, &offset)); + EXPECT(bsoncxx::validate(bytes, length, options, &offset)); } // Validate UTF-8 strings. @@ -45,14 +45,14 @@ void example(const std::uint8_t* bytes, std::size_t length) { options.check_utf8(true); - ASSERT(!bsoncxx::validate(bytes, length, options, &offset)); + EXPECT(!bsoncxx::validate(bytes, length, options, &offset)); // Offset of `"x": "\0"` relative to start of the document. - ASSERT(offset == 4u); + EXPECT(offset == 4u); options.check_utf8_allow_null(true); - ASSERT(bsoncxx::validate(bytes, length, options, &offset)); + EXPECT(bsoncxx::validate(bytes, length, options, &offset)); } // Validate dot keys. @@ -62,10 +62,10 @@ void example(const std::uint8_t* bytes, std::size_t length) { options.check_dot_keys(true); - ASSERT(!bsoncxx::validate(bytes, length, options, &offset)); + EXPECT(!bsoncxx::validate(bytes, length, options, &offset)); // Offset of `"a.b": 1` relative to start of the document. - ASSERT(offset == 15u); + EXPECT(offset == 15u); } // Validate dollar keys. @@ -75,10 +75,10 @@ void example(const std::uint8_t* bytes, std::size_t length) { options.check_dollar_keys(true); - ASSERT(!bsoncxx::validate(bytes, length, options, &offset)); + EXPECT(!bsoncxx::validate(bytes, length, options, &offset)); // Offset of `"$numberInt": "123"` relative to start of the sub-document. (CDRIVER-5710) - ASSERT(offset == 4u); + EXPECT(offset == 4u); } } // [Example] diff --git a/examples/api/db_lock.cpp b/examples/api/db_lock.cpp index 26cca50877..dbae316b63 100644 --- a/examples/api/db_lock.cpp +++ b/examples/api/db_lock.cpp @@ -43,7 +43,7 @@ db_lock::db_lock(mongocxx::client& client, std::string name) : _client_ptr(&clie } mongocxx::database db_lock::get() const& { - ASSERT(_client_ptr); + EXPECT(_client_ptr); return _client_ptr->database(_name); } diff --git a/examples/api/mongocxx/examples/clients/create/pool/basic.cpp b/examples/api/mongocxx/examples/clients/create/pool/basic.cpp index abf0c82752..fed36b2f47 100644 --- a/examples/api/mongocxx/examples/clients/create/pool/basic.cpp +++ b/examples/api/mongocxx/examples/clients/create/pool/basic.cpp @@ -28,12 +28,12 @@ void example() { mongocxx::pool::entry entry = pool.acquire(); - ASSERT(entry); + EXPECT(entry); mongocxx::client& client = *entry; - ASSERT(client); - ASSERT(client.uri().to_string() == mongocxx::uri::k_default_uri); + EXPECT(client); + EXPECT(client.uri().to_string() == mongocxx::uri::k_default_uri); } { @@ -42,12 +42,12 @@ void example() { mongocxx::pool::entry entry = pool.acquire(); - ASSERT(entry); + EXPECT(entry); mongocxx::client& client = *entry; - ASSERT(client); - ASSERT(client.uri().to_string() == uri.to_string()); + EXPECT(client); + EXPECT(client.uri().to_string() == uri.to_string()); } } // [Example] diff --git a/examples/api/mongocxx/examples/clients/create/pool/options.cpp b/examples/api/mongocxx/examples/clients/create/pool/options.cpp index 25a1a5606f..a978f226a1 100644 --- a/examples/api/mongocxx/examples/clients/create/pool/options.cpp +++ b/examples/api/mongocxx/examples/clients/create/pool/options.cpp @@ -34,12 +34,12 @@ void example() { mongocxx::pool::entry entry = pool.acquire(); - ASSERT(entry); + EXPECT(entry); mongocxx::client& client = *entry; - ASSERT(client); - ASSERT(client.uri().to_string() == mongocxx::uri::k_default_uri); + EXPECT(client); + EXPECT(client.uri().to_string() == mongocxx::uri::k_default_uri); } // [Example] diff --git a/examples/api/mongocxx/examples/clients/create/pool/try_acquire.cpp b/examples/api/mongocxx/examples/clients/create/pool/try_acquire.cpp index 8fb60fe9f1..1c32f7feb6 100644 --- a/examples/api/mongocxx/examples/clients/create/pool/try_acquire.cpp +++ b/examples/api/mongocxx/examples/clients/create/pool/try_acquire.cpp @@ -28,23 +28,23 @@ void example() { auto entry_opt = pool.try_acquire(); - ASSERT(entry_opt); - ASSERT(*entry_opt); + EXPECT(entry_opt); + EXPECT(*entry_opt); { mongocxx::pool::entry hold = std::move(*entry_opt); - ASSERT(hold); + EXPECT(hold); entry_opt = pool.try_acquire(); - ASSERT(!entry_opt); + EXPECT(!entry_opt); } entry_opt = pool.try_acquire(); - ASSERT(entry_opt); - ASSERT(*entry_opt); + EXPECT(entry_opt); + EXPECT(*entry_opt); } // [Example] diff --git a/examples/api/mongocxx/examples/clients/create/single/basic.cpp b/examples/api/mongocxx/examples/clients/create/single/basic.cpp index 8f4610ab6f..f9dd631946 100644 --- a/examples/api/mongocxx/examples/clients/create/single/basic.cpp +++ b/examples/api/mongocxx/examples/clients/create/single/basic.cpp @@ -25,14 +25,14 @@ void example() { { mongocxx::client client; - ASSERT(!client); + EXPECT(!client); } { mongocxx::client client{mongocxx::uri{}}; - ASSERT(client); - ASSERT(client.uri().to_string() == mongocxx::uri::k_default_uri); + EXPECT(client); + EXPECT(client.uri().to_string() == mongocxx::uri::k_default_uri); } } // [Example] diff --git a/examples/api/mongocxx/examples/clients/create/single/options/apm.cpp b/examples/api/mongocxx/examples/clients/create/single/options/apm.cpp index 9556c1d25c..033b179c55 100644 --- a/examples/api/mongocxx/examples/clients/create/single/options/apm.cpp +++ b/examples/api/mongocxx/examples/clients/create/single/options/apm.cpp @@ -41,7 +41,7 @@ void example() { mongocxx::client client{mongocxx::uri{}, client_opts}; - ASSERT(client); + EXPECT(client); } // [Example] diff --git a/examples/api/mongocxx/examples/clients/create/single/options/auto_encryption.cpp b/examples/api/mongocxx/examples/clients/create/single/options/auto_encryption.cpp index aad7e4480b..016799255a 100644 --- a/examples/api/mongocxx/examples/clients/create/single/options/auto_encryption.cpp +++ b/examples/api/mongocxx/examples/clients/create/single/options/auto_encryption.cpp @@ -45,7 +45,7 @@ void example(bsoncxx::document::view kms_providers) { mongocxx::uri uri; mongocxx::client client{uri, client_opts}; - ASSERT(client); + EXPECT(client); } // [Example] diff --git a/examples/api/mongocxx/examples/clients/create/single/options/stable_api.cpp b/examples/api/mongocxx/examples/clients/create/single/options/stable_api.cpp index 3d01091bfd..17bf380547 100644 --- a/examples/api/mongocxx/examples/clients/create/single/options/stable_api.cpp +++ b/examples/api/mongocxx/examples/clients/create/single/options/stable_api.cpp @@ -39,8 +39,8 @@ mongocxx::client example() { mongocxx::uri uri; mongocxx::client client{uri, client_opts}; - ASSERT(client); - ASSERT(client.uri().to_string() == mongocxx::uri::k_default_uri); + EXPECT(client); + EXPECT(client.uri().to_string() == mongocxx::uri::k_default_uri); return client; } @@ -53,5 +53,5 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { auto reply = client["admin"].run_command(bsoncxx::from_json(R"({"ping": 1})")); - ASSERT(reply["ok"] && reply["ok"].get_double().value == 1.0); + EXPECT(reply["ok"] && reply["ok"].get_double().value == 1.0); } diff --git a/examples/api/mongocxx/examples/clients/create/single/options/tls.cpp b/examples/api/mongocxx/examples/clients/create/single/options/tls.cpp index 5069f11f9b..882bd6cb3c 100644 --- a/examples/api/mongocxx/examples/clients/create/single/options/tls.cpp +++ b/examples/api/mongocxx/examples/clients/create/single/options/tls.cpp @@ -40,8 +40,8 @@ mongocxx::client example() { mongocxx::uri uri{"mongodb://bob:pwd123@localhost:27017/?tls=true"}; mongocxx::client client{uri, client_opts}; - ASSERT(client); - ASSERT(client.uri().to_string() == uri.to_string()); + EXPECT(client); + EXPECT(client.uri().to_string() == uri.to_string()); return client; } @@ -55,7 +55,7 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { auto reply = client["admin"].run_command(bsoncxx::from_json(R"({"ping": 1})")); - ASSERT(reply["ok"] && reply["ok"].get_double().value == 1.0); + EXPECT(reply["ok"] && reply["ok"].get_double().value == 1.0); } catch (const mongocxx::exception& ex) { if (ex.code() == mongocxx::error_code::k_ssl_not_supported) { // Library may not be configured with TLS/SSL support enabled. diff --git a/examples/api/mongocxx/examples/clients/create/single/with_uri.cpp b/examples/api/mongocxx/examples/clients/create/single/with_uri.cpp index 644a4053f8..cc03b746d3 100644 --- a/examples/api/mongocxx/examples/clients/create/single/with_uri.cpp +++ b/examples/api/mongocxx/examples/clients/create/single/with_uri.cpp @@ -25,8 +25,8 @@ void example() { mongocxx::uri uri{"mongodb://localhost:27017/?serverSelectionTryOnce=true"}; mongocxx::client client{uri}; - ASSERT(client); - ASSERT(client.uri().to_string() == uri.to_string()); + EXPECT(client); + EXPECT(client.uri().to_string() == uri.to_string()); } // [Example] diff --git a/examples/api/mongocxx/examples/clients/errors/auto_encryption.cpp b/examples/api/mongocxx/examples/clients/errors/auto_encryption.cpp index 489a0dae8b..cf237289e6 100644 --- a/examples/api/mongocxx/examples/clients/errors/auto_encryption.cpp +++ b/examples/api/mongocxx/examples/clients/errors/auto_encryption.cpp @@ -40,11 +40,11 @@ void example() { mongocxx::options::client{}.auto_encryption_opts( mongocxx::options::auto_encryption{})}; // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const mongocxx::exception& ex) { // CXX-834: libmongoc error code. - ASSERT(ex.code().category() == mongocxx::server_error_category()); - ASSERT(ex.code().value() == 58); // MONGOC_ERROR_CLIENT_INVALID_ENCRYPTION_ARG + EXPECT(ex.code().category() == mongocxx::server_error_category()); + EXPECT(ex.code().value() == 58); // MONGOC_ERROR_CLIENT_INVALID_ENCRYPTION_ARG } // Invalid KMS providers. @@ -56,11 +56,11 @@ void example() { .key_vault_namespace({"keyvault", "datakeys"}) .kms_providers(bsoncxx::from_json(R"({"invalid": 1})")))}; // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const mongocxx::exception& ex) { // CXX-834: libmongocrypt error code. - ASSERT(ex.code().category() == mongocxx::server_error_category()); - ASSERT(ex.code().value() == 1); // MONGOCRYPT_GENERIC_ERROR_CODE + EXPECT(ex.code().category() == mongocxx::server_error_category()); + EXPECT(ex.code().value() == 1); // MONGOCRYPT_GENERIC_ERROR_CODE } // Incompatible options. @@ -71,9 +71,9 @@ void example() { mongocxx::options::auto_encryption{}.key_vault_client(nullptr).key_vault_pool( nullptr))}; // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const mongocxx::exception& ex) { - ASSERT(ex.code() == mongocxx::error_code::k_invalid_parameter); + EXPECT(ex.code() == mongocxx::error_code::k_invalid_parameter); } } // [Example] @@ -88,7 +88,7 @@ RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { mongocxx::uri{}, mongocxx::options::client{}.auto_encryption_opts(mongocxx::options::auto_encryption{})}; - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const mongocxx::exception& ex) { if (std::strstr(ex.what(), "ENABLE_CLIENT_SIDE_ENCRYPTION") != nullptr) { // Library may not be configured with TLS/SSL support enabled. diff --git a/examples/api/mongocxx/examples/clients/errors/invalid_client.cpp b/examples/api/mongocxx/examples/clients/errors/invalid_client.cpp index 8348b471d7..73207997b1 100644 --- a/examples/api/mongocxx/examples/clients/errors/invalid_client.cpp +++ b/examples/api/mongocxx/examples/clients/errors/invalid_client.cpp @@ -26,14 +26,14 @@ namespace { void example() { mongocxx::client client; - ASSERT(!client); + EXPECT(!client); try { mongocxx::uri uri = client.uri(); // DO NOT DO THIS. Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const mongocxx::exception& ex) { - ASSERT(ex.code() == mongocxx::error_code::k_invalid_client_object); + EXPECT(ex.code() == mongocxx::error_code::k_invalid_client_object); } } // [Example] diff --git a/examples/api/mongocxx/examples/clients/errors/stable_api.cpp b/examples/api/mongocxx/examples/clients/errors/stable_api.cpp index 8a1e0173d3..c32deff377 100644 --- a/examples/api/mongocxx/examples/clients/errors/stable_api.cpp +++ b/examples/api/mongocxx/examples/clients/errors/stable_api.cpp @@ -27,18 +27,18 @@ void example() { mongocxx::options::server_api::version version = mongocxx::options::server_api::version_from_string("0"); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const mongocxx::exception& ex) { - ASSERT(ex.code() == mongocxx::error_code::k_invalid_parameter); + EXPECT(ex.code() == mongocxx::error_code::k_invalid_parameter); } try { std::string version = mongocxx::options::server_api::version_to_string( static_cast(1)); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const mongocxx::exception& ex) { - ASSERT(ex.code() == mongocxx::error_code::k_invalid_parameter); + EXPECT(ex.code() == mongocxx::error_code::k_invalid_parameter); } } // [Example] diff --git a/examples/api/mongocxx/examples/clients/errors/tls.cpp b/examples/api/mongocxx/examples/clients/errors/tls.cpp index 7ec5cc8a9c..3518975f8a 100644 --- a/examples/api/mongocxx/examples/clients/errors/tls.cpp +++ b/examples/api/mongocxx/examples/clients/errors/tls.cpp @@ -31,9 +31,9 @@ void example() { mongocxx::uri{}, mongocxx::options::client{}.tls_opts(mongocxx::options::tls{})}; // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const mongocxx::exception& ex) { - ASSERT( + EXPECT( // When TLS/SSL is enabled for both mongocxx and libmongoc. ex.code() == mongocxx::error_code::k_invalid_parameter || diff --git a/examples/api/mongocxx/examples/clients/errors/wait_queue_timeout.cpp b/examples/api/mongocxx/examples/clients/errors/wait_queue_timeout.cpp index c144327dcb..c2bcab3f9a 100644 --- a/examples/api/mongocxx/examples/clients/errors/wait_queue_timeout.cpp +++ b/examples/api/mongocxx/examples/clients/errors/wait_queue_timeout.cpp @@ -32,13 +32,13 @@ void example() { mongocxx::pool::entry hold = pool.acquire(); - ASSERT(hold); + EXPECT(hold); mongocxx::pool::entry entry = pool.acquire(); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const mongocxx::exception& ex) { - ASSERT(ex.code() == mongocxx::error_code::k_pool_wait_queue_timeout); + EXPECT(ex.code() == mongocxx::error_code::k_pool_wait_queue_timeout); } } // [Example] diff --git a/examples/api/mongocxx/examples/clients/use/list_database_names.cpp b/examples/api/mongocxx/examples/clients/use/list_database_names.cpp index 26e0b14041..b78f55c4cb 100644 --- a/examples/api/mongocxx/examples/clients/use/list_database_names.cpp +++ b/examples/api/mongocxx/examples/clients/use/list_database_names.cpp @@ -33,7 +33,7 @@ namespace { void example(mongocxx::client client) { std::vector names = client.list_database_names(); - ASSERT(std::count(names.begin(), names.end(), "admin") == 1); + EXPECT(std::count(names.begin(), names.end(), "admin") == 1); } // [Example] diff --git a/examples/api/mongocxx/examples/clients/use/list_database_names_with_options.cpp b/examples/api/mongocxx/examples/clients/use/list_database_names_with_options.cpp index 9d3aa7efb8..c95c76b61c 100644 --- a/examples/api/mongocxx/examples/clients/use/list_database_names_with_options.cpp +++ b/examples/api/mongocxx/examples/clients/use/list_database_names_with_options.cpp @@ -34,7 +34,7 @@ void example(mongocxx::client client) { std::vector names = client.list_database_names(opts.view()); - ASSERT(std::count(names.begin(), names.end(), "admin") == 0); + EXPECT(std::count(names.begin(), names.end(), "admin") == 0); } // [Example] diff --git a/examples/api/mongocxx/examples/clients/use/list_databases.cpp b/examples/api/mongocxx/examples/clients/use/list_databases.cpp index 20b6252516..f714eec97c 100644 --- a/examples/api/mongocxx/examples/clients/use/list_databases.cpp +++ b/examples/api/mongocxx/examples/clients/use/list_databases.cpp @@ -30,16 +30,16 @@ void example(mongocxx::client client) { int count = 0; for (bsoncxx::document::view doc : databases) { - ASSERT(doc["name"]); - ASSERT(doc["sizeOnDisk"]); - ASSERT(doc["empty"]); + EXPECT(doc["name"]); + EXPECT(doc["sizeOnDisk"]); + EXPECT(doc["empty"]); if (doc["name"].get_string().value.compare("admin") == 0) { ++count; } } - ASSERT(count == 1); + EXPECT(count == 1); } // [Example] diff --git a/examples/api/mongocxx/examples/clients/use/list_databases_with_options.cpp b/examples/api/mongocxx/examples/clients/use/list_databases_with_options.cpp index b1b2b34b25..032e90d2af 100644 --- a/examples/api/mongocxx/examples/clients/use/list_databases_with_options.cpp +++ b/examples/api/mongocxx/examples/clients/use/list_databases_with_options.cpp @@ -35,16 +35,16 @@ void example(mongocxx::client client) { int count = 0; for (bsoncxx::document::view doc : databases) { - ASSERT(doc["name"]); - ASSERT(!doc["sizeOnDisk"]); - ASSERT(!doc["empty"]); + EXPECT(doc["name"]); + EXPECT(!doc["sizeOnDisk"]); + EXPECT(!doc["empty"]); if (doc["name"].get_string().value.compare("admin") == 0) { ++count; } } - ASSERT(count == 1); + EXPECT(count == 1); } // [Example] diff --git a/examples/api/mongocxx/examples/collections/aggregate.cpp b/examples/api/mongocxx/examples/collections/aggregate.cpp index f71b51ba51..a18aa39395 100644 --- a/examples/api/mongocxx/examples/collections/aggregate.cpp +++ b/examples/api/mongocxx/examples/collections/aggregate.cpp @@ -54,7 +54,7 @@ void example(mongocxx::collection coll) { return doc.find("x") != doc.end(); }; - ASSERT(std::count_if(cursor.begin(), cursor.end(), has_field_x) == 1); + EXPECT(std::count_if(cursor.begin(), cursor.end(), has_field_x) == 1); } // With options. @@ -65,7 +65,7 @@ void example(mongocxx::collection coll) { mongocxx::cursor cursor = coll.aggregate(mongocxx::pipeline{}, opts); - ASSERT(std::distance(cursor.begin(), cursor.end()) == 2); + EXPECT(std::distance(cursor.begin(), cursor.end()) == 2); } } // [Example] @@ -82,7 +82,7 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { using insert = mongocxx::model::insert_one; - ASSERT(coll.create_bulk_write() + EXPECT(coll.create_bulk_write() .append(insert{bsoncxx::from_json(R"({"x": 1})")}) .append(insert{bsoncxx::from_json(R"({"x": 2})")}) .execute()); diff --git a/examples/api/mongocxx/examples/collections/bulk_write.cpp b/examples/api/mongocxx/examples/collections/bulk_write.cpp index fd91014069..7baee8cfeb 100644 --- a/examples/api/mongocxx/examples/collections/bulk_write.cpp +++ b/examples/api/mongocxx/examples/collections/bulk_write.cpp @@ -32,7 +32,7 @@ namespace { // [Example] void example(mongocxx::collection coll) { - ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$exists": 1}})")) == 0); + EXPECT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$exists": 1}})")) == 0); mongocxx::model::insert_one insert{bsoncxx::from_json(R"({"x": 10})")}; mongocxx::model::update_one update{bsoncxx::from_json(R"({"x": {"$exists": 1}})"), @@ -46,14 +46,14 @@ void example(mongocxx::collection coll) { auto result_opt = bulk_write.execute(); - ASSERT(result_opt); + EXPECT(result_opt); mongocxx::result::bulk_write& result = *result_opt; - ASSERT(result.inserted_count() == 1); - ASSERT(result.modified_count() == 1); + EXPECT(result.inserted_count() == 1); + EXPECT(result.modified_count() == 1); - ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": 20})")) == 1); + EXPECT(coll.count_documents(bsoncxx::from_json(R"({"x": 20})")) == 1); } // [Example] diff --git a/examples/api/mongocxx/examples/collections/bulk_write_with_options.cpp b/examples/api/mongocxx/examples/collections/bulk_write_with_options.cpp index fb24f51a6a..f68bfd19ee 100644 --- a/examples/api/mongocxx/examples/collections/bulk_write_with_options.cpp +++ b/examples/api/mongocxx/examples/collections/bulk_write_with_options.cpp @@ -36,7 +36,7 @@ namespace { // [Example] void example(mongocxx::collection coll) { - ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$exists": 1}})")) == 0); + EXPECT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$exists": 1}})")) == 0); mongocxx::options::bulk_write opts; @@ -50,14 +50,14 @@ void example(mongocxx::collection coll) { bsoncxx::from_json(R"({"$set": {"x": 20}})")}) .execute(); - ASSERT(result_opt); + EXPECT(result_opt); mongocxx::result::bulk_write& result = *result_opt; - ASSERT(result.inserted_count() == 1); - ASSERT(result.modified_count() == 1); + EXPECT(result.inserted_count() == 1); + EXPECT(result.modified_count() == 1); - ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": 20})")) == 1); + EXPECT(coll.count_documents(bsoncxx::from_json(R"({"x": 20})")) == 1); } // [Example] diff --git a/examples/api/mongocxx/examples/collections/count.cpp b/examples/api/mongocxx/examples/collections/count.cpp index 9818b1c1a0..83e45f00be 100644 --- a/examples/api/mongocxx/examples/collections/count.cpp +++ b/examples/api/mongocxx/examples/collections/count.cpp @@ -36,9 +36,9 @@ namespace { void example(mongocxx::collection coll) { // Basic usage. { - ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$exists": 1}})")) == 3); - ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$gt": 1}})")) == 2); - ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$gt": 2}})")) == 1); + EXPECT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$exists": 1}})")) == 3); + EXPECT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$gt": 1}})")) == 2); + EXPECT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$gt": 2}})")) == 1); } // With options. @@ -48,7 +48,7 @@ void example(mongocxx::collection coll) { opts.limit(1); // ... other count options. - ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$exists": 1}})"), opts) == 1); + EXPECT(coll.count_documents(bsoncxx::from_json(R"({"x": {"$exists": 1}})"), opts) == 1); } } // [Example] @@ -67,7 +67,7 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { using insert = mongocxx::model::insert_one; - ASSERT(coll.create_bulk_write() + EXPECT(coll.create_bulk_write() .append(insert{bsoncxx::from_json(R"({"x": 1})")}) .append(insert{bsoncxx::from_json(R"({"x": 2})")}) .append(insert{bsoncxx::from_json(R"({"x": 3})")}) diff --git a/examples/api/mongocxx/examples/collections/create_index.cpp b/examples/api/mongocxx/examples/collections/create_index.cpp index 33edd52067..a25c221e21 100644 --- a/examples/api/mongocxx/examples/collections/create_index.cpp +++ b/examples/api/mongocxx/examples/collections/create_index.cpp @@ -34,8 +34,8 @@ namespace { void example(mongocxx::collection coll) { bsoncxx::document::value result = coll.create_index(bsoncxx::from_json(R"({"key": 1})")); - ASSERT(result["name"]); - ASSERT(result["name"].get_string().value.compare("key_1") == 0); + EXPECT(result["name"]); + EXPECT(result["name"].get_string().value.compare("key_1") == 0); } // [Example] @@ -55,10 +55,10 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { return std::distance(cursor.begin(), cursor.end()); }; - ASSERT(count_indexes() == 1); // _id + EXPECT(count_indexes() == 1); // _id example(coll); - ASSERT(count_indexes() == 2); // _id, key_1 + EXPECT(count_indexes() == 2); // _id, key_1 } } diff --git a/examples/api/mongocxx/examples/collections/create_index_with_options.cpp b/examples/api/mongocxx/examples/collections/create_index_with_options.cpp index 943f7240f5..a782b09dba 100644 --- a/examples/api/mongocxx/examples/collections/create_index_with_options.cpp +++ b/examples/api/mongocxx/examples/collections/create_index_with_options.cpp @@ -43,8 +43,8 @@ void example(mongocxx::collection coll) { bsoncxx::document::value result = coll.create_index(bsoncxx::from_json(R"({"key_a": 1})"), opts.view()); - ASSERT(result["name"]); - ASSERT(result["name"].get_string().value.compare("custom_name") == 0); + EXPECT(result["name"]); + EXPECT(result["name"].get_string().value.compare("custom_name") == 0); } // Create index options. @@ -58,8 +58,8 @@ void example(mongocxx::collection coll) { bsoncxx::document::value result = coll.create_index(bsoncxx::from_json(R"({"key_b": 1})"), opts.view()); - ASSERT(result["name"]); - ASSERT(result["name"].get_string().value.compare("key_b_1") == 0); + EXPECT(result["name"]); + EXPECT(result["name"].get_string().value.compare("key_b_1") == 0); } } // [Example] @@ -77,11 +77,11 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { example(db.create_collection("coll")); for (auto doc : db["coll"].list_indexes()) { - ASSERT(doc["name"]); + EXPECT(doc["name"]); if (doc["name"].get_string().value.compare("custom_name") == 0) { - ASSERT(doc["unique"]); - ASSERT(doc["unique"].get_bool().value == true); + EXPECT(doc["unique"]); + EXPECT(doc["unique"].get_bool().value == true); } } } diff --git a/examples/api/mongocxx/examples/collections/delete_many.cpp b/examples/api/mongocxx/examples/collections/delete_many.cpp index 298365077c..7c594580d4 100644 --- a/examples/api/mongocxx/examples/collections/delete_many.cpp +++ b/examples/api/mongocxx/examples/collections/delete_many.cpp @@ -40,35 +40,35 @@ void example(mongocxx::collection coll) { // Basic usage. { - ASSERT(coll.count_documents(x1.view()) == 1); - ASSERT(coll.count_documents(x2.view()) == 1); - ASSERT(coll.count_documents(x3.view()) == 1); + EXPECT(coll.count_documents(x1.view()) == 1); + EXPECT(coll.count_documents(x2.view()) == 1); + EXPECT(coll.count_documents(x3.view()) == 1); auto result_opt = coll.delete_many(bsoncxx::from_json(R"({"x": {"$gt": 1}})")); - ASSERT(result_opt); + EXPECT(result_opt); mongocxx::result::delete_result& result = *result_opt; - ASSERT(result.deleted_count() == 2); - ASSERT(result.result().deleted_count() == 2); + EXPECT(result.deleted_count() == 2); + EXPECT(result.result().deleted_count() == 2); - ASSERT(coll.count_documents(x1.view()) == 1); - ASSERT(coll.count_documents(x2.view()) == 0); - ASSERT(coll.count_documents(x3.view()) == 0); + EXPECT(coll.count_documents(x1.view()) == 1); + EXPECT(coll.count_documents(x2.view()) == 0); + EXPECT(coll.count_documents(x3.view()) == 0); } // With options. { - ASSERT(coll.count_documents(x1.view()) == 1); + EXPECT(coll.count_documents(x1.view()) == 1); mongocxx::options::delete_options opts; // ... set delete options. - ASSERT(coll.delete_many(bsoncxx::from_json(R"({"x": {"$exists": 1}})"), opts)); + EXPECT(coll.delete_many(bsoncxx::from_json(R"({"x": {"$exists": 1}})"), opts)); - ASSERT(coll.count_documents(x1.view()) == 0); + EXPECT(coll.count_documents(x1.view()) == 0); } } // [Example] @@ -87,7 +87,7 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { using insert = mongocxx::model::insert_one; - ASSERT(coll.create_bulk_write() + EXPECT(coll.create_bulk_write() .append(insert{bsoncxx::from_json(R"({"x": 1})")}) .append(insert{bsoncxx::from_json(R"({"x": 2})")}) .append(insert{bsoncxx::from_json(R"({"x": 3})")}) diff --git a/examples/api/mongocxx/examples/collections/delete_one.cpp b/examples/api/mongocxx/examples/collections/delete_one.cpp index 08adcd6443..f89039a003 100644 --- a/examples/api/mongocxx/examples/collections/delete_one.cpp +++ b/examples/api/mongocxx/examples/collections/delete_one.cpp @@ -40,34 +40,34 @@ void example(mongocxx::collection coll) { // Basic usage. { - ASSERT(coll.count_documents(x1.view()) == 1); + EXPECT(coll.count_documents(x1.view()) == 1); auto result_opt = coll.delete_one(x1.view()); - ASSERT(result_opt); + EXPECT(result_opt); mongocxx::result::delete_result& result = *result_opt; - ASSERT(result.deleted_count() == 1); - ASSERT(result.result().deleted_count() == 1); + EXPECT(result.deleted_count() == 1); + EXPECT(result.result().deleted_count() == 1); - ASSERT(coll.count_documents(x1.view()) == 0); + EXPECT(coll.count_documents(x1.view()) == 0); } // With options. { - ASSERT(coll.count_documents(x2.view()) == 1); + EXPECT(coll.count_documents(x2.view()) == 1); mongocxx::options::delete_options opts; // ... set delete options. - ASSERT(coll.delete_one(x2.view(), opts)); + EXPECT(coll.delete_one(x2.view(), opts)); - ASSERT(coll.count_documents(x2.view()) == 0); + EXPECT(coll.count_documents(x2.view()) == 0); } - ASSERT(coll.count_documents(x3.view()) == 1); + EXPECT(coll.count_documents(x3.view()) == 1); } // [Example] @@ -85,7 +85,7 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { using insert = mongocxx::model::insert_one; - ASSERT(coll.create_bulk_write() + EXPECT(coll.create_bulk_write() .append(insert{bsoncxx::from_json(R"({"x": 1})")}) .append(insert{bsoncxx::from_json(R"({"x": 2})")}) .append(insert{bsoncxx::from_json(R"({"x": 3})")}) diff --git a/examples/api/mongocxx/examples/collections/distinct.cpp b/examples/api/mongocxx/examples/collections/distinct.cpp index fdc8112499..b6ef69e445 100644 --- a/examples/api/mongocxx/examples/collections/distinct.cpp +++ b/examples/api/mongocxx/examples/collections/distinct.cpp @@ -47,19 +47,19 @@ void example(mongocxx::collection coll) { std::vector docs{cursor.begin(), cursor.end()}; - ASSERT(docs.size() == 1u); + EXPECT(docs.size() == 1u); auto doc = docs[0].view(); - ASSERT(doc["values"]); - ASSERT(doc["values"].type() == bsoncxx::type::k_array); + EXPECT(doc["values"]); + EXPECT(doc["values"].type() == bsoncxx::type::k_array); auto arr = doc["values"].get_array().value; - ASSERT(std::distance(arr.begin(), arr.end()) == 2); + EXPECT(std::distance(arr.begin(), arr.end()) == 2); - ASSERT(arr[0].get_int32().value == 1); - ASSERT(arr[1].get_int32().value == 2); + EXPECT(arr[0].get_int32().value == 1); + EXPECT(arr[1].get_int32().value == 2); } // With options. @@ -70,7 +70,7 @@ void example(mongocxx::collection coll) { mongocxx::cursor cursor = coll.distinct("x", bsoncxx::from_json(R"({"flag": 1})"), opts); - ASSERT(std::distance(cursor.begin(), cursor.end()) == 1); + EXPECT(std::distance(cursor.begin(), cursor.end()) == 1); } } // [Example] @@ -87,7 +87,7 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { using insert = mongocxx::model::insert_one; - ASSERT(coll.create_bulk_write() + EXPECT(coll.create_bulk_write() .append(insert{bsoncxx::from_json(R"({"x": 1, "flag": 1})")}) .append(insert{bsoncxx::from_json(R"({"x": 2, "flag": 1})")}) .append(insert{bsoncxx::from_json(R"({"x": 3, "flag": 0})")}) diff --git a/examples/api/mongocxx/examples/collections/drop.cpp b/examples/api/mongocxx/examples/collections/drop.cpp index fb33605c84..1ea5a24fe0 100644 --- a/examples/api/mongocxx/examples/collections/drop.cpp +++ b/examples/api/mongocxx/examples/collections/drop.cpp @@ -28,11 +28,11 @@ namespace { void example(mongocxx::database db) { mongocxx::collection coll = db["coll"]; - ASSERT(db.has_collection("coll")); + EXPECT(db.has_collection("coll")); coll.drop(); - ASSERT(!db.has_collection("coll")); + EXPECT(!db.has_collection("coll")); } // [Example] diff --git a/examples/api/mongocxx/examples/collections/estimate_count.cpp b/examples/api/mongocxx/examples/collections/estimate_count.cpp index 07be9b090d..a514bae753 100644 --- a/examples/api/mongocxx/examples/collections/estimate_count.cpp +++ b/examples/api/mongocxx/examples/collections/estimate_count.cpp @@ -37,7 +37,7 @@ namespace { // ] void example(mongocxx::collection coll) { // Basic usage. - ASSERT(coll.estimated_document_count() == 3); + EXPECT(coll.estimated_document_count() == 3); // With options. { @@ -45,7 +45,7 @@ void example(mongocxx::collection coll) { // ... set estimated scount options. - ASSERT(coll.estimated_document_count(opts) == 3); + EXPECT(coll.estimated_document_count(opts) == 3); } } // [Example] @@ -62,7 +62,7 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { using insert = mongocxx::model::insert_one; - ASSERT(coll.create_bulk_write() + EXPECT(coll.create_bulk_write() .append(insert{bsoncxx::from_json(R"({"x": 1})")}) .append(insert{bsoncxx::from_json(R"({"x": 2})")}) .append(insert{bsoncxx::from_json(R"({"x": 3})")}) diff --git a/examples/api/mongocxx/examples/collections/find.cpp b/examples/api/mongocxx/examples/collections/find.cpp index dfec92a460..a9f69b48b4 100644 --- a/examples/api/mongocxx/examples/collections/find.cpp +++ b/examples/api/mongocxx/examples/collections/find.cpp @@ -47,15 +47,15 @@ void example(mongocxx::collection coll) { int count = 0; for (bsoncxx::document::view doc : cursor) { - ASSERT(doc["_id"]); + EXPECT(doc["_id"]); - ASSERT(doc["x"]); - ASSERT(doc["x"].get_int32().value != 2); + EXPECT(doc["x"]); + EXPECT(doc["x"].get_int32().value != 2); ++count; } - ASSERT(count == 2); + EXPECT(count == 2); } // With options. @@ -70,11 +70,11 @@ void example(mongocxx::collection coll) { std::vector docs{cursor.begin(), cursor.end()}; - ASSERT(docs.size() == 3u); + EXPECT(docs.size() == 3u); - ASSERT(docs[0] == bsoncxx::from_json(R"({"x": 1})")); - ASSERT(docs[1] == bsoncxx::from_json(R"({"x": 2})")); - ASSERT(docs[2] == bsoncxx::from_json(R"({"x": 3})")); + EXPECT(docs[0] == bsoncxx::from_json(R"({"x": 1})")); + EXPECT(docs[1] == bsoncxx::from_json(R"({"x": 2})")); + EXPECT(docs[2] == bsoncxx::from_json(R"({"x": 3})")); } } // [Example] @@ -93,7 +93,7 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { using insert = mongocxx::model::insert_one; - ASSERT(coll.create_bulk_write() + EXPECT(coll.create_bulk_write() .append(insert{bsoncxx::from_json(R"({"x": 1, "found": true})")}) .append(insert{bsoncxx::from_json(R"({"x": 2, "found": false})")}) .append(insert{bsoncxx::from_json(R"({"x": 3, "found": true})")}) diff --git a/examples/api/mongocxx/examples/collections/find_one.cpp b/examples/api/mongocxx/examples/collections/find_one.cpp index 418dafd99e..136b3803e1 100644 --- a/examples/api/mongocxx/examples/collections/find_one.cpp +++ b/examples/api/mongocxx/examples/collections/find_one.cpp @@ -40,14 +40,14 @@ void example(mongocxx::collection coll) { auto result_opt = coll.find_one(filter.view()); - ASSERT(result_opt); + EXPECT(result_opt); bsoncxx::document::view doc = result_opt->view(); - ASSERT(doc["_id"]); + EXPECT(doc["_id"]); - ASSERT(doc["x"]); - ASSERT(doc["x"].get_int32().value == 2); + EXPECT(doc["x"]); + EXPECT(doc["x"].get_int32().value == 2); } // With options. @@ -60,8 +60,8 @@ void example(mongocxx::collection coll) { auto result_opt = coll.find_one(bsoncxx::builder::basic::make_document(), opts); - ASSERT(result_opt); - ASSERT(*result_opt == bsoncxx::from_json(R"({"x": 1})")); + EXPECT(result_opt); + EXPECT(*result_opt == bsoncxx::from_json(R"({"x": 1})")); } } // [Example] @@ -80,7 +80,7 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { using insert = mongocxx::model::insert_one; - ASSERT(coll.create_bulk_write() + EXPECT(coll.create_bulk_write() .append(insert{bsoncxx::from_json(R"({"x": 1, "found": false})")}) .append(insert{bsoncxx::from_json(R"({"x": 2, "found": true})")}) .append(insert{bsoncxx::from_json(R"({"x": 3, "found": false})")}) diff --git a/examples/api/mongocxx/examples/collections/find_one_and_delete.cpp b/examples/api/mongocxx/examples/collections/find_one_and_delete.cpp index a29e0f1a15..ac7701396a 100644 --- a/examples/api/mongocxx/examples/collections/find_one_and_delete.cpp +++ b/examples/api/mongocxx/examples/collections/find_one_and_delete.cpp @@ -42,25 +42,25 @@ void example(mongocxx::collection coll) { // Basic usage. { - ASSERT(coll.count_documents(has_field_x.view()) == 3); + EXPECT(coll.count_documents(has_field_x.view()) == 3); auto result_opt = coll.find_one_and_delete(x2.view()); - ASSERT(result_opt); + EXPECT(result_opt); bsoncxx::document::view doc = result_opt->view(); - ASSERT(doc["_id"]); + EXPECT(doc["_id"]); - ASSERT(doc["x"]); - ASSERT(doc["x"].get_int32().value == 2); + EXPECT(doc["x"]); + EXPECT(doc["x"].get_int32().value == 2); - ASSERT(coll.count_documents(has_field_x.view()) == 2); + EXPECT(coll.count_documents(has_field_x.view()) == 2); } // With options. { - ASSERT(coll.count_documents(has_field_x.view()) == 2); + EXPECT(coll.count_documents(has_field_x.view()) == 2); mongocxx::options::find_one_and_delete opts; @@ -69,13 +69,13 @@ void example(mongocxx::collection coll) { auto result_opt = coll.find_one_and_delete(x3.view(), opts); - ASSERT(result_opt); - ASSERT(*result_opt == x3); + EXPECT(result_opt); + EXPECT(*result_opt == x3); - ASSERT(coll.count_documents(has_field_x.view()) == 1); + EXPECT(coll.count_documents(has_field_x.view()) == 1); } - ASSERT(coll.count_documents(x1.view()) == 1); + EXPECT(coll.count_documents(x1.view()) == 1); } // [Example] @@ -93,7 +93,7 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { using insert = mongocxx::model::insert_one; - ASSERT(coll.create_bulk_write() + EXPECT(coll.create_bulk_write() .append(insert{bsoncxx::from_json(R"({"x": 1})")}) .append(insert{bsoncxx::from_json(R"({"x": 2})")}) .append(insert{bsoncxx::from_json(R"({"x": 3})")}) diff --git a/examples/api/mongocxx/examples/collections/find_one_and_replace.cpp b/examples/api/mongocxx/examples/collections/find_one_and_replace.cpp index d9c96748ae..ec7b861db5 100644 --- a/examples/api/mongocxx/examples/collections/find_one_and_replace.cpp +++ b/examples/api/mongocxx/examples/collections/find_one_and_replace.cpp @@ -41,25 +41,25 @@ void example(mongocxx::collection coll) { // Basic usage. { - ASSERT(coll.count_documents(x0.view()) == 0); + EXPECT(coll.count_documents(x0.view()) == 0); auto result_opt = coll.find_one_and_replace(x2.view(), x0.view()); - ASSERT(result_opt); + EXPECT(result_opt); bsoncxx::document::view doc = result_opt->view(); - ASSERT(doc["_id"]); + EXPECT(doc["_id"]); - ASSERT(doc["x"]); - ASSERT(doc["x"].get_int32().value == 2); + EXPECT(doc["x"]); + EXPECT(doc["x"].get_int32().value == 2); - ASSERT(coll.count_documents(x0.view()) == 1); + EXPECT(coll.count_documents(x0.view()) == 1); } // With options. { - ASSERT(coll.count_documents(x0.view()) == 1); + EXPECT(coll.count_documents(x0.view()) == 1); mongocxx::options::find_one_and_replace opts; @@ -68,13 +68,13 @@ void example(mongocxx::collection coll) { auto result_opt = coll.find_one_and_replace(x3.view(), x0.view(), opts); - ASSERT(result_opt); - ASSERT(*result_opt == x3); + EXPECT(result_opt); + EXPECT(*result_opt == x3); - ASSERT(coll.count_documents(x0.view()) == 2); + EXPECT(coll.count_documents(x0.view()) == 2); } - ASSERT(coll.count_documents(x1.view()) == 1); + EXPECT(coll.count_documents(x1.view()) == 1); } // [Example] @@ -92,7 +92,7 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { using insert = mongocxx::model::insert_one; - ASSERT(coll.create_bulk_write() + EXPECT(coll.create_bulk_write() .append(insert{bsoncxx::from_json(R"({"x": 1})")}) .append(insert{bsoncxx::from_json(R"({"x": 2})")}) .append(insert{bsoncxx::from_json(R"({"x": 3})")}) diff --git a/examples/api/mongocxx/examples/collections/find_one_and_update.cpp b/examples/api/mongocxx/examples/collections/find_one_and_update.cpp index 36734776cf..2593257c5e 100644 --- a/examples/api/mongocxx/examples/collections/find_one_and_update.cpp +++ b/examples/api/mongocxx/examples/collections/find_one_and_update.cpp @@ -44,28 +44,28 @@ void example(mongocxx::collection coll) { // Basic usage. { - ASSERT(coll.count_documents(x2.view()) == 1); - ASSERT(coll.count_documents(updated.view()) == 0); + EXPECT(coll.count_documents(x2.view()) == 1); + EXPECT(coll.count_documents(updated.view()) == 0); auto result_opt = coll.find_one_and_update(x2.view(), update.view()); - ASSERT(result_opt); + EXPECT(result_opt); bsoncxx::document::view doc = result_opt->view(); - ASSERT(doc["_id"]); + EXPECT(doc["_id"]); - ASSERT(doc["x"]); - ASSERT(doc["x"].get_int32().value == 2); + EXPECT(doc["x"]); + EXPECT(doc["x"].get_int32().value == 2); - ASSERT(coll.count_documents(x2.view()) == 1); - ASSERT(coll.count_documents(updated.view()) == 1); + EXPECT(coll.count_documents(x2.view()) == 1); + EXPECT(coll.count_documents(updated.view()) == 1); } // With options. { - ASSERT(coll.count_documents(x3.view()) == 1); - ASSERT(coll.count_documents(updated.view()) == 1); + EXPECT(coll.count_documents(x3.view()) == 1); + EXPECT(coll.count_documents(updated.view()) == 1); mongocxx::options::find_one_and_update opts; @@ -74,14 +74,14 @@ void example(mongocxx::collection coll) { auto result_opt = coll.find_one_and_update(x3.view(), update.view(), opts); - ASSERT(result_opt); - ASSERT(*result_opt == x3); + EXPECT(result_opt); + EXPECT(*result_opt == x3); - ASSERT(coll.count_documents(x3.view()) == 1); - ASSERT(coll.count_documents(updated.view()) == 2); + EXPECT(coll.count_documents(x3.view()) == 1); + EXPECT(coll.count_documents(updated.view()) == 2); } - ASSERT(coll.count_documents(x1.view()) == 1); + EXPECT(coll.count_documents(x1.view()) == 1); } // [Example] @@ -99,7 +99,7 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { using insert = mongocxx::model::insert_one; - ASSERT(coll.create_bulk_write() + EXPECT(coll.create_bulk_write() .append(insert{bsoncxx::from_json(R"({"x": 1, "updated": false})")}) .append(insert{bsoncxx::from_json(R"({"x": 2, "updated": false})")}) .append(insert{bsoncxx::from_json(R"({"x": 3, "updated": false})")}) diff --git a/examples/api/mongocxx/examples/collections/insert_many.cpp b/examples/api/mongocxx/examples/collections/insert_many.cpp index db22fc413f..265039d671 100644 --- a/examples/api/mongocxx/examples/collections/insert_many.cpp +++ b/examples/api/mongocxx/examples/collections/insert_many.cpp @@ -45,12 +45,12 @@ void example(mongocxx::collection coll) { auto result_opt = coll.insert_many(docs); - ASSERT(result_opt); + EXPECT(result_opt); mongocxx::result::insert_many& result = *result_opt; - ASSERT(result.inserted_count() == 2); - ASSERT(result.result().inserted_count() == 2); + EXPECT(result.inserted_count() == 2); + EXPECT(result.result().inserted_count() == 2); } // Iterators. @@ -59,12 +59,12 @@ void example(mongocxx::collection coll) { auto result_opt = coll.insert_many(std::begin(docs), std::end(docs)); - ASSERT(result_opt); + EXPECT(result_opt); mongocxx::result::insert_many& result = *result_opt; - ASSERT(result.inserted_count() == 2); - ASSERT(result.result().inserted_count() == 2); + EXPECT(result.inserted_count() == 2); + EXPECT(result.result().inserted_count() == 2); } // With options. @@ -75,7 +75,7 @@ void example(mongocxx::collection coll) { // ... set insert options. - ASSERT(coll.insert_many(docs, opts)); + EXPECT(coll.insert_many(docs, opts)); } } // [Example] diff --git a/examples/api/mongocxx/examples/collections/insert_one.cpp b/examples/api/mongocxx/examples/collections/insert_one.cpp index 4a1f520908..1e05b3afde 100644 --- a/examples/api/mongocxx/examples/collections/insert_one.cpp +++ b/examples/api/mongocxx/examples/collections/insert_one.cpp @@ -33,30 +33,30 @@ void example(mongocxx::collection coll) { // Basic usage. { - ASSERT(coll.count_documents(x1.view()) == 0); + EXPECT(coll.count_documents(x1.view()) == 0); auto result_opt = coll.insert_one(x1.view()); - ASSERT(result_opt); + EXPECT(result_opt); mongocxx::result::insert_one& result = *result_opt; - ASSERT(result.result().inserted_count() == 1); + EXPECT(result.result().inserted_count() == 1); - ASSERT(coll.count_documents(x1.view()) == 1); + EXPECT(coll.count_documents(x1.view()) == 1); } // With options. { - ASSERT(coll.count_documents(x2.view()) == 0); + EXPECT(coll.count_documents(x2.view()) == 0); mongocxx::options::insert opts; // ... set insert options. - ASSERT(coll.insert_one(x2.view(), opts)); + EXPECT(coll.insert_one(x2.view(), opts)); - ASSERT(coll.count_documents(x2.view()) == 1); + EXPECT(coll.count_documents(x2.view()) == 1); } } // [Example] diff --git a/examples/api/mongocxx/examples/collections/list_indexes.cpp b/examples/api/mongocxx/examples/collections/list_indexes.cpp index 0f24fd35d6..e0c16c23b0 100644 --- a/examples/api/mongocxx/examples/collections/list_indexes.cpp +++ b/examples/api/mongocxx/examples/collections/list_indexes.cpp @@ -32,11 +32,11 @@ namespace { // [Example] void example(mongocxx::collection coll) { for (bsoncxx::document::view doc : coll.list_indexes()) { - ASSERT(doc["name"]); - ASSERT(doc["name"].type() == bsoncxx::type::k_string); + EXPECT(doc["name"]); + EXPECT(doc["name"].type() == bsoncxx::type::k_string); - ASSERT(doc["key"]); - ASSERT(doc["key"].type() == bsoncxx::type::k_document); + EXPECT(doc["key"]); + EXPECT(doc["key"].type() == bsoncxx::type::k_document); } } // [Example] diff --git a/examples/api/mongocxx/examples/collections/obtain.cpp b/examples/api/mongocxx/examples/collections/obtain.cpp index 55031f23c6..c489e50a30 100644 --- a/examples/api/mongocxx/examples/collections/obtain.cpp +++ b/examples/api/mongocxx/examples/collections/obtain.cpp @@ -26,10 +26,10 @@ namespace { void example(mongocxx::database db) { mongocxx::collection coll = db["coll"]; - ASSERT(coll); - ASSERT(coll.name().compare("coll") == 0); + EXPECT(coll); + EXPECT(coll.name().compare("coll") == 0); - ASSERT(db.collection("coll").name().compare("coll") == 0); + EXPECT(db.collection("coll").name().compare("coll") == 0); } // [Example] diff --git a/examples/api/mongocxx/examples/collections/rc.cpp b/examples/api/mongocxx/examples/collections/rc.cpp index c6c94af1d2..b366cc8921 100644 --- a/examples/api/mongocxx/examples/collections/rc.cpp +++ b/examples/api/mongocxx/examples/collections/rc.cpp @@ -30,7 +30,7 @@ void example(mongocxx::collection coll) { { mongocxx::read_concern rc = coll.read_concern(); - ASSERT(rc.acknowledge_level() == rc_level::k_server_default); + EXPECT(rc.acknowledge_level() == rc_level::k_server_default); } // Explicit. @@ -42,8 +42,8 @@ void example(mongocxx::collection coll) { coll.read_concern(rc); - ASSERT(coll.read_concern() == rc); - ASSERT(coll.read_concern().acknowledge_level() == rc_level::k_majority); + EXPECT(coll.read_concern() == rc); + EXPECT(coll.read_concern().acknowledge_level() == rc_level::k_majority); } } // [Example] diff --git a/examples/api/mongocxx/examples/collections/rename.cpp b/examples/api/mongocxx/examples/collections/rename.cpp index 9945681391..2d44ba4ac1 100644 --- a/examples/api/mongocxx/examples/collections/rename.cpp +++ b/examples/api/mongocxx/examples/collections/rename.cpp @@ -28,17 +28,17 @@ namespace { void example(mongocxx::database db) { mongocxx::collection coll = db["old"]; - ASSERT(db.has_collection("old")); - ASSERT(!db.has_collection("new")); + EXPECT(db.has_collection("old")); + EXPECT(!db.has_collection("new")); - ASSERT(coll.name().compare("old") == 0); + EXPECT(coll.name().compare("old") == 0); coll.rename("new"); - ASSERT(coll.name().compare("new") == 0); + EXPECT(coll.name().compare("new") == 0); - ASSERT(!db.has_collection("old")); - ASSERT(db.has_collection("new")); + EXPECT(!db.has_collection("old")); + EXPECT(db.has_collection("new")); } // [Example] diff --git a/examples/api/mongocxx/examples/collections/replace_one.cpp b/examples/api/mongocxx/examples/collections/replace_one.cpp index 13ac2f5dee..ede04211f9 100644 --- a/examples/api/mongocxx/examples/collections/replace_one.cpp +++ b/examples/api/mongocxx/examples/collections/replace_one.cpp @@ -40,28 +40,28 @@ void example(mongocxx::collection coll) { // Basic usage. { - ASSERT(coll.count_documents(x0.view()) == 0); + EXPECT(coll.count_documents(x0.view()) == 0); auto filter = bsoncxx::from_json(R"({"x": 2})"); auto result_opt = coll.replace_one(filter.view(), x0.view()); - ASSERT(result_opt); + EXPECT(result_opt); mongocxx::result::replace_one& result = *result_opt; - ASSERT(result.matched_count() == 1); - ASSERT(result.modified_count() == 1); - ASSERT(result.result().matched_count() == 1); - ASSERT(result.result().modified_count() == 1); + EXPECT(result.matched_count() == 1); + EXPECT(result.modified_count() == 1); + EXPECT(result.result().matched_count() == 1); + EXPECT(result.result().modified_count() == 1); - ASSERT(coll.count_documents(x0.view()) == 1); + EXPECT(coll.count_documents(x0.view()) == 1); } // With options. { - ASSERT(coll.count_documents(is_original.view()) == 2); - ASSERT(coll.count_documents(is_replaced.view()) == 1); + EXPECT(coll.count_documents(is_original.view()) == 2); + EXPECT(coll.count_documents(is_replaced.view()) == 1); mongocxx::options::replace opts; @@ -70,17 +70,17 @@ void example(mongocxx::collection coll) { auto filter = bsoncxx::from_json(R"({"replaced": false})"); - ASSERT(coll.replace_one(filter.view(), x0.view(), opts)); - ASSERT(coll.count_documents(is_original.view()) == 1); - ASSERT(coll.count_documents(is_replaced.view()) == 2); + EXPECT(coll.replace_one(filter.view(), x0.view(), opts)); + EXPECT(coll.count_documents(is_original.view()) == 1); + EXPECT(coll.count_documents(is_replaced.view()) == 2); - ASSERT(coll.replace_one(filter.view(), x0.view(), opts)); - ASSERT(coll.count_documents(is_original.view()) == 0); - ASSERT(coll.count_documents(is_replaced.view()) == 3); + EXPECT(coll.replace_one(filter.view(), x0.view(), opts)); + EXPECT(coll.count_documents(is_original.view()) == 0); + EXPECT(coll.count_documents(is_replaced.view()) == 3); - ASSERT(coll.replace_one(filter.view(), x0.view(), opts)); - ASSERT(coll.count_documents(is_original.view()) == 0); - ASSERT(coll.count_documents(is_replaced.view()) == 4); + EXPECT(coll.replace_one(filter.view(), x0.view(), opts)); + EXPECT(coll.count_documents(is_original.view()) == 0); + EXPECT(coll.count_documents(is_replaced.view()) == 4); } } // [Example] @@ -99,7 +99,7 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { using insert = mongocxx::model::insert_one; - ASSERT(coll.create_bulk_write() + EXPECT(coll.create_bulk_write() .append(insert(bsoncxx::from_json(R"({"x": 1, "replaced": false})"))) .append(insert(bsoncxx::from_json(R"({"x": 2, "replaced": false})"))) .append(insert(bsoncxx::from_json(R"({"x": 3, "replaced": false})"))) diff --git a/examples/api/mongocxx/examples/collections/rp.cpp b/examples/api/mongocxx/examples/collections/rp.cpp index 9911b4f9fc..ba3fe47349 100644 --- a/examples/api/mongocxx/examples/collections/rp.cpp +++ b/examples/api/mongocxx/examples/collections/rp.cpp @@ -28,8 +28,8 @@ void example(mongocxx::collection coll) { { mongocxx::read_preference rp; - ASSERT(coll.read_preference() == rp); - ASSERT(coll.read_preference().mode() == mongocxx::read_preference::read_mode::k_primary); + EXPECT(coll.read_preference() == rp); + EXPECT(coll.read_preference().mode() == mongocxx::read_preference::read_mode::k_primary); } // Explicit. @@ -41,7 +41,7 @@ void example(mongocxx::collection coll) { coll.read_preference(rp); - ASSERT(coll.read_preference() == rp); + EXPECT(coll.read_preference() == rp); } } // [Example] diff --git a/examples/api/mongocxx/examples/collections/update_many.cpp b/examples/api/mongocxx/examples/collections/update_many.cpp index 4c8dadcaae..4533d4cf67 100644 --- a/examples/api/mongocxx/examples/collections/update_many.cpp +++ b/examples/api/mongocxx/examples/collections/update_many.cpp @@ -42,44 +42,44 @@ void example(mongocxx::collection coll) { // Basic usage. { - ASSERT(coll.count_documents(updated.view()) == 0); + EXPECT(coll.count_documents(updated.view()) == 0); auto filter = bsoncxx::from_json(R"({"x": {"$gt": 1}})"); auto result_opt = coll.update_many(filter.view(), update.view()); - ASSERT(result_opt); + EXPECT(result_opt); mongocxx::result::update& result = *result_opt; - ASSERT(result.matched_count() == 2); - ASSERT(result.modified_count() == 2); - ASSERT(result.result().matched_count() == 2); - ASSERT(result.result().modified_count() == 2); + EXPECT(result.matched_count() == 2); + EXPECT(result.modified_count() == 2); + EXPECT(result.result().matched_count() == 2); + EXPECT(result.result().modified_count() == 2); - ASSERT(coll.count_documents(updated.view()) == 2); - ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": 1, "updated": false})")) == 1); + EXPECT(coll.count_documents(updated.view()) == 2); + EXPECT(coll.count_documents(bsoncxx::from_json(R"({"x": 1, "updated": false})")) == 1); } // With options. { - ASSERT(coll.count_documents(original.view()) == 1); - ASSERT(coll.count_documents(updated.view()) == 2); + EXPECT(coll.count_documents(original.view()) == 1); + EXPECT(coll.count_documents(updated.view()) == 2); mongocxx::options::update opts; opts.upsert(true); // ... other update options. - ASSERT(coll.update_many(original.view(), update.view(), opts)); + EXPECT(coll.update_many(original.view(), update.view(), opts)); - ASSERT(coll.count_documents(original.view()) == 0); - ASSERT(coll.count_documents(updated.view()) == 3); + EXPECT(coll.count_documents(original.view()) == 0); + EXPECT(coll.count_documents(updated.view()) == 3); - ASSERT(coll.update_many(original.view(), update.view(), opts)); + EXPECT(coll.update_many(original.view(), update.view(), opts)); - ASSERT(coll.count_documents(original.view()) == 0); - ASSERT(coll.count_documents(updated.view()) == 4); + EXPECT(coll.count_documents(original.view()) == 0); + EXPECT(coll.count_documents(updated.view()) == 4); } } // [Example] @@ -98,7 +98,7 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { using insert = mongocxx::model::insert_one; - ASSERT(coll.create_bulk_write() + EXPECT(coll.create_bulk_write() .append(insert{bsoncxx::from_json(R"({"x": 1, "updated": false})")}) .append(insert{bsoncxx::from_json(R"({"x": 2, "updated": false})")}) .append(insert{bsoncxx::from_json(R"({"x": 3, "updated": false})")}) diff --git a/examples/api/mongocxx/examples/collections/update_one.cpp b/examples/api/mongocxx/examples/collections/update_one.cpp index f541d56f49..47be0524c9 100644 --- a/examples/api/mongocxx/examples/collections/update_one.cpp +++ b/examples/api/mongocxx/examples/collections/update_one.cpp @@ -39,45 +39,45 @@ void example(mongocxx::collection coll) { // Basic usage. { - ASSERT(coll.count_documents(updated.view()) == 0); + EXPECT(coll.count_documents(updated.view()) == 0); auto filter = bsoncxx::from_json(R"({"x": 2})"); auto update = bsoncxx::from_json(R"({"$set": {"updated": true}})"); auto result_opt = coll.update_one(filter.view(), update.view()); - ASSERT(result_opt); + EXPECT(result_opt); mongocxx::result::update& result = *result_opt; - ASSERT(result.matched_count() == 1); - ASSERT(result.modified_count() == 1); - ASSERT(result.result().matched_count() == 1); - ASSERT(result.result().modified_count() == 1); + EXPECT(result.matched_count() == 1); + EXPECT(result.modified_count() == 1); + EXPECT(result.result().matched_count() == 1); + EXPECT(result.result().modified_count() == 1); - ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": 2, "updated": true})")) == 1); - ASSERT(coll.count_documents(updated.view()) == 1); + EXPECT(coll.count_documents(bsoncxx::from_json(R"({"x": 2, "updated": true})")) == 1); + EXPECT(coll.count_documents(updated.view()) == 1); } // With options. { - ASSERT(coll.count_documents(updated.view()) == 1); + EXPECT(coll.count_documents(updated.view()) == 1); auto x4 = bsoncxx::from_json(R"({"x": 4, "updated": true})"); - ASSERT(coll.count_documents(x4.view()) == 0); + EXPECT(coll.count_documents(x4.view()) == 0); mongocxx::options::update opts; opts.upsert(true); // ... other update options. - ASSERT(coll.update_one(bsoncxx::from_json(R"({"x": 4})"), + EXPECT(coll.update_one(bsoncxx::from_json(R"({"x": 4})"), bsoncxx::from_json(R"({"$set": {"updated": true}})"), opts)); - ASSERT(coll.count_documents(x4.view()) == 1); - ASSERT(coll.count_documents(updated.view()) == 2); + EXPECT(coll.count_documents(x4.view()) == 1); + EXPECT(coll.count_documents(updated.view()) == 2); } } // [Example] @@ -96,7 +96,7 @@ RUNNER_REGISTER_COMPONENT_FOR_SINGLE() { using insert = mongocxx::model::insert_one; - ASSERT(coll.create_bulk_write() + EXPECT(coll.create_bulk_write() .append(insert{bsoncxx::from_json(R"({"x": 1, "updated": false})")}) .append(insert{bsoncxx::from_json(R"({"x": 2, "updated": false})")}) .append(insert{bsoncxx::from_json(R"({"x": 3, "updated": false})")}) diff --git a/examples/api/mongocxx/examples/collections/wc.cpp b/examples/api/mongocxx/examples/collections/wc.cpp index f3c9c2534c..c346a004e4 100644 --- a/examples/api/mongocxx/examples/collections/wc.cpp +++ b/examples/api/mongocxx/examples/collections/wc.cpp @@ -30,8 +30,8 @@ void example(mongocxx::collection coll) { { mongocxx::write_concern wc = coll.write_concern(); - ASSERT(wc.acknowledge_level() == wc_level::k_default); - ASSERT(wc.timeout() == std::chrono::milliseconds(0)); + EXPECT(wc.acknowledge_level() == wc_level::k_default); + EXPECT(wc.timeout() == std::chrono::milliseconds(0)); } // Explicit. @@ -43,9 +43,9 @@ void example(mongocxx::collection coll) { coll.write_concern(wc); - ASSERT(coll.write_concern() == wc); - ASSERT(coll.write_concern().acknowledge_level() == wc_level::k_majority); - ASSERT(coll.write_concern().timeout() == std::chrono::seconds(5)); + EXPECT(coll.write_concern() == wc); + EXPECT(coll.write_concern().acknowledge_level() == wc_level::k_majority); + EXPECT(coll.write_concern().timeout() == std::chrono::seconds(5)); } } // [Example] diff --git a/examples/api/mongocxx/examples/collections/write.cpp b/examples/api/mongocxx/examples/collections/write.cpp index 71bb51cd7e..57449cc994 100644 --- a/examples/api/mongocxx/examples/collections/write.cpp +++ b/examples/api/mongocxx/examples/collections/write.cpp @@ -35,29 +35,29 @@ namespace { // [Example] void example(mongocxx::collection coll) { { - ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": 1})")) == 0); + EXPECT(coll.count_documents(bsoncxx::from_json(R"({"x": 1})")) == 0); mongocxx::model::insert_one insert{bsoncxx::from_json(R"({"x": 1})")}; auto result_opt = coll.write(insert); - ASSERT(result_opt); - ASSERT(result_opt->inserted_count() == 1); + EXPECT(result_opt); + EXPECT(result_opt->inserted_count() == 1); - ASSERT(coll.count_documents(bsoncxx::from_json(R"({"x": 1})")) == 1); + EXPECT(coll.count_documents(bsoncxx::from_json(R"({"x": 1})")) == 1); } { - ASSERT(coll.count_documents(bsoncxx::from_json(R"({"y": 1})")) == 0); + EXPECT(coll.count_documents(bsoncxx::from_json(R"({"y": 1})")) == 0); mongocxx::options::bulk_write opts; opts.ordered(true); // ... other bulk write options. - ASSERT(coll.write(mongocxx::model::insert_one{bsoncxx::from_json(R"({"y": 1})")}, opts)); + EXPECT(coll.write(mongocxx::model::insert_one{bsoncxx::from_json(R"({"y": 1})")}, opts)); - ASSERT(coll.count_documents(bsoncxx::from_json(R"({"y": 1})")) == 1); + EXPECT(coll.count_documents(bsoncxx::from_json(R"({"y": 1})")) == 1); } } // [Example] diff --git a/examples/api/mongocxx/examples/databases/create_collection.cpp b/examples/api/mongocxx/examples/databases/create_collection.cpp index 2857cb14ff..99cb6afb80 100644 --- a/examples/api/mongocxx/examples/databases/create_collection.cpp +++ b/examples/api/mongocxx/examples/databases/create_collection.cpp @@ -26,13 +26,13 @@ namespace { // [Example] void example(mongocxx::database db) { - ASSERT(!db.has_collection("coll")); + EXPECT(!db.has_collection("coll")); mongocxx::collection coll = db.create_collection("coll"); - ASSERT(coll); + EXPECT(coll); - ASSERT(db.has_collection("coll")); + EXPECT(db.has_collection("coll")); } // [Example] diff --git a/examples/api/mongocxx/examples/databases/create_collection_with_options.cpp b/examples/api/mongocxx/examples/databases/create_collection_with_options.cpp index c9f2b99530..9f6da98199 100644 --- a/examples/api/mongocxx/examples/databases/create_collection_with_options.cpp +++ b/examples/api/mongocxx/examples/databases/create_collection_with_options.cpp @@ -27,15 +27,15 @@ namespace { // [Example] void example(mongocxx::database db) { - ASSERT(!db.has_collection("coll")); + EXPECT(!db.has_collection("coll")); auto opts = bsoncxx::from_json(R"({"validationLevel": "strict", "validationAction": "error"})"); // ... other create options. mongocxx::collection coll = db.create_collection("coll", opts.view()); - ASSERT(coll); - ASSERT(db.has_collection("coll")); + EXPECT(coll); + EXPECT(db.has_collection("coll")); } // [Example] diff --git a/examples/api/mongocxx/examples/databases/drop.cpp b/examples/api/mongocxx/examples/databases/drop.cpp index 18da841590..f0538d2cc4 100644 --- a/examples/api/mongocxx/examples/databases/drop.cpp +++ b/examples/api/mongocxx/examples/databases/drop.cpp @@ -33,7 +33,7 @@ void example(mongocxx::client& client) { { std::vector names = client.list_database_names(); - ASSERT(std::count(names.begin(), names.end(), "db") == 1); + EXPECT(std::count(names.begin(), names.end(), "db") == 1); } db.drop(); @@ -41,7 +41,7 @@ void example(mongocxx::client& client) { { std::vector names = client.list_database_names(); - ASSERT(std::count(names.begin(), names.end(), "db") == 0); + EXPECT(std::count(names.begin(), names.end(), "db") == 0); } } // [Example] diff --git a/examples/api/mongocxx/examples/databases/has_collection.cpp b/examples/api/mongocxx/examples/databases/has_collection.cpp index 4f1e09c2dc..cdec15e0be 100644 --- a/examples/api/mongocxx/examples/databases/has_collection.cpp +++ b/examples/api/mongocxx/examples/databases/has_collection.cpp @@ -24,8 +24,8 @@ namespace { // [Example] void example(mongocxx::database db) { - ASSERT(db.has_collection("present")); - ASSERT(!db.has_collection("missing")); + EXPECT(db.has_collection("present")); + EXPECT(!db.has_collection("missing")); } // [Example] diff --git a/examples/api/mongocxx/examples/databases/list_collection_names.cpp b/examples/api/mongocxx/examples/databases/list_collection_names.cpp index 1dd03ab23f..5ae39b26f8 100644 --- a/examples/api/mongocxx/examples/databases/list_collection_names.cpp +++ b/examples/api/mongocxx/examples/databases/list_collection_names.cpp @@ -36,9 +36,9 @@ void example(mongocxx::database db) { { std::vector names = db.list_collection_names(); - ASSERT(std::count(names.begin(), names.end(), "a") == 1); // Present. - ASSERT(std::count(names.begin(), names.end(), "b") == 1); // Present. - ASSERT(std::count(names.begin(), names.end(), "c") == 0); // Missing. + EXPECT(std::count(names.begin(), names.end(), "a") == 1); // Present. + EXPECT(std::count(names.begin(), names.end(), "b") == 1); // Present. + EXPECT(std::count(names.begin(), names.end(), "c") == 0); // Missing. } // With a filter. @@ -47,9 +47,9 @@ void example(mongocxx::database db) { std::vector names = db.list_collection_names(filter.view()); - ASSERT(std::count(names.begin(), names.end(), "a") == 1); // Present. - ASSERT(std::count(names.begin(), names.end(), "b") == 0); // Filtered. - ASSERT(std::count(names.begin(), names.end(), "c") == 0); // Missing. + EXPECT(std::count(names.begin(), names.end(), "a") == 1); // Present. + EXPECT(std::count(names.begin(), names.end(), "b") == 0); // Filtered. + EXPECT(std::count(names.begin(), names.end(), "c") == 0); // Missing. } } // [Example] diff --git a/examples/api/mongocxx/examples/databases/list_collections.cpp b/examples/api/mongocxx/examples/databases/list_collections.cpp index f462047aa3..d37e32634f 100644 --- a/examples/api/mongocxx/examples/databases/list_collections.cpp +++ b/examples/api/mongocxx/examples/databases/list_collections.cpp @@ -40,7 +40,7 @@ void example(mongocxx::database db) { c = 0; for (bsoncxx::document::view doc : cursor) { - ASSERT(doc["name"]); + EXPECT(doc["name"]); auto name = bsoncxx::string::to_string(doc["name"].get_string().value); @@ -58,9 +58,9 @@ void example(mongocxx::database db) { { count_fields(db.list_collections()); - ASSERT(a == 1); // Present. - ASSERT(b == 1); // Present. - ASSERT(c == 0); // Missing. + EXPECT(a == 1); // Present. + EXPECT(b == 1); // Present. + EXPECT(c == 0); // Missing. } // With a filter. @@ -69,9 +69,9 @@ void example(mongocxx::database db) { count_fields(db.list_collections(filter.view())); - ASSERT(a == 1); // Present. - ASSERT(b == 0); // Filtered. - ASSERT(c == 0); // Missing. + EXPECT(a == 1); // Present. + EXPECT(b == 0); // Filtered. + EXPECT(c == 0); // Missing. } } // [Example] diff --git a/examples/api/mongocxx/examples/databases/obtain.cpp b/examples/api/mongocxx/examples/databases/obtain.cpp index 1be8db63b6..7815ed93fb 100644 --- a/examples/api/mongocxx/examples/databases/obtain.cpp +++ b/examples/api/mongocxx/examples/databases/obtain.cpp @@ -25,10 +25,10 @@ namespace { void example(mongocxx::client client) { mongocxx::database db = client["db"]; - ASSERT(db); - ASSERT(db.name().compare("db") == 0); + EXPECT(db); + EXPECT(db.name().compare("db") == 0); - ASSERT(client.database("db").name().compare("db") == 0); + EXPECT(client.database("db").name().compare("db") == 0); } // [Example] diff --git a/examples/api/mongocxx/examples/databases/rc.cpp b/examples/api/mongocxx/examples/databases/rc.cpp index 6023c032ac..f58f8acb50 100644 --- a/examples/api/mongocxx/examples/databases/rc.cpp +++ b/examples/api/mongocxx/examples/databases/rc.cpp @@ -31,7 +31,7 @@ void example(mongocxx::database db) { { mongocxx::read_concern rc = db.read_concern(); - ASSERT(rc.acknowledge_level() == rc_level::k_server_default); + EXPECT(rc.acknowledge_level() == rc_level::k_server_default); } // Explicit. @@ -43,8 +43,8 @@ void example(mongocxx::database db) { db.read_concern(rc); - ASSERT(db.read_concern() == rc); - ASSERT(db.read_concern().acknowledge_level() == rc_level::k_majority); + EXPECT(db.read_concern() == rc); + EXPECT(db.read_concern().acknowledge_level() == rc_level::k_majority); } } // [Example] @@ -54,7 +54,7 @@ void example(mongocxx::database db) { RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { mongocxx::client client{mongocxx::uri{}}; - ASSERT(client.read_concern() == mongocxx::read_concern{}); + EXPECT(client.read_concern() == mongocxx::read_concern{}); example(client["db"]); } diff --git a/examples/api/mongocxx/examples/databases/rp.cpp b/examples/api/mongocxx/examples/databases/rp.cpp index 6556c0caf9..3af6449d2b 100644 --- a/examples/api/mongocxx/examples/databases/rp.cpp +++ b/examples/api/mongocxx/examples/databases/rp.cpp @@ -28,8 +28,8 @@ void example(mongocxx::database db) { { mongocxx::read_preference rp; - ASSERT(db.read_preference() == rp); - ASSERT(db.read_preference().mode() == mongocxx::read_preference::read_mode::k_primary); + EXPECT(db.read_preference() == rp); + EXPECT(db.read_preference().mode() == mongocxx::read_preference::read_mode::k_primary); } // Explicit. @@ -41,7 +41,7 @@ void example(mongocxx::database db) { db.read_preference(rp); - ASSERT(db.read_preference() == rp); + EXPECT(db.read_preference() == rp); } } // [Example] @@ -51,7 +51,7 @@ void example(mongocxx::database db) { RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { mongocxx::client client{mongocxx::uri{}}; - ASSERT(client.read_preference() == mongocxx::read_preference{}); + EXPECT(client.read_preference() == mongocxx::read_preference{}); example(client["db"]); } diff --git a/examples/api/mongocxx/examples/databases/run_command.cpp b/examples/api/mongocxx/examples/databases/run_command.cpp index 175164b960..2308eae49d 100644 --- a/examples/api/mongocxx/examples/databases/run_command.cpp +++ b/examples/api/mongocxx/examples/databases/run_command.cpp @@ -30,8 +30,8 @@ void example(mongocxx::database db) { bsoncxx::document::value reply = db.run_command(cmd.view()); - ASSERT(reply["ok"]); - ASSERT(reply["ok"].get_double().value == 1.0); + EXPECT(reply["ok"]); + EXPECT(reply["ok"].get_double().value == 1.0); } // [Example] diff --git a/examples/api/mongocxx/examples/databases/wc.cpp b/examples/api/mongocxx/examples/databases/wc.cpp index fade393213..8aa1810524 100644 --- a/examples/api/mongocxx/examples/databases/wc.cpp +++ b/examples/api/mongocxx/examples/databases/wc.cpp @@ -32,8 +32,8 @@ void example(mongocxx::database db) { { mongocxx::write_concern wc = db.write_concern(); - ASSERT(wc.acknowledge_level() == wc_level::k_default); - ASSERT(wc.timeout() == std::chrono::milliseconds(0)); + EXPECT(wc.acknowledge_level() == wc_level::k_default); + EXPECT(wc.timeout() == std::chrono::milliseconds(0)); } // Explicit. @@ -45,9 +45,9 @@ void example(mongocxx::database db) { db.write_concern(wc); - ASSERT(db.write_concern() == wc); - ASSERT(db.write_concern().acknowledge_level() == wc_level::k_majority); - ASSERT(db.write_concern().timeout() == std::chrono::seconds(5)); + EXPECT(db.write_concern() == wc); + EXPECT(db.write_concern().acknowledge_level() == wc_level::k_majority); + EXPECT(db.write_concern().timeout() == std::chrono::seconds(5)); } } // [Example] @@ -57,7 +57,7 @@ void example(mongocxx::database db) { RUNNER_REGISTER_COMPONENT_WITH_INSTANCE() { mongocxx::client client{mongocxx::uri{}}; - ASSERT(client.write_concern() == mongocxx::write_concern{}); + EXPECT(client.write_concern() == mongocxx::write_concern{}); example(client["db"]); } diff --git a/examples/api/mongocxx/examples/instance/basic_usage.cpp b/examples/api/mongocxx/examples/instance/basic_usage.cpp index d63e65c3de..2f20a9a4bc 100644 --- a/examples/api/mongocxx/examples/instance/basic_usage.cpp +++ b/examples/api/mongocxx/examples/instance/basic_usage.cpp @@ -32,7 +32,7 @@ void example() { // Initialize the MongoDB C++ Driver. mongocxx::instance instance; - ASSERT(&mongocxx::instance::current() == &instance); + EXPECT(&mongocxx::instance::current() == &instance); // Use mongocxx library interfaces at this point. use(mongocxx::client{}); diff --git a/examples/api/mongocxx/examples/instance/current.cpp b/examples/api/mongocxx/examples/instance/current.cpp index dfdfc78958..238e22e7c7 100644 --- a/examples/api/mongocxx/examples/instance/current.cpp +++ b/examples/api/mongocxx/examples/instance/current.cpp @@ -32,7 +32,7 @@ void example() { // Initialize the MongoDB C++ Driver. auto& instance = mongocxx::instance::current(); - ASSERT(&mongocxx::instance::current() == &instance); + EXPECT(&mongocxx::instance::current() == &instance); // Use mongocxx library interfaces at this point. use(mongocxx::client{}); diff --git a/examples/api/mongocxx/examples/instance/destroyed.cpp b/examples/api/mongocxx/examples/instance/destroyed.cpp index 0d3e8a25f0..c93e55f531 100644 --- a/examples/api/mongocxx/examples/instance/destroyed.cpp +++ b/examples/api/mongocxx/examples/instance/destroyed.cpp @@ -28,17 +28,17 @@ void example() { try { mongocxx::instance instance; // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const mongocxx::exception& ex) { - ASSERT(ex.code() == mongocxx::error_code::k_cannot_recreate_instance); + EXPECT(ex.code() == mongocxx::error_code::k_cannot_recreate_instance); } try { auto& instance = mongocxx::instance::current(); // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const mongocxx::exception& ex) { - ASSERT(ex.code() == mongocxx::error_code::k_instance_destroyed); + EXPECT(ex.code() == mongocxx::error_code::k_instance_destroyed); } } // [Example] diff --git a/examples/api/mongocxx/examples/instance/recreation.cpp b/examples/api/mongocxx/examples/instance/recreation.cpp index 4663f25b3a..0a2e353332 100644 --- a/examples/api/mongocxx/examples/instance/recreation.cpp +++ b/examples/api/mongocxx/examples/instance/recreation.cpp @@ -26,17 +26,17 @@ void example() { { mongocxx::instance instance; - ASSERT(&mongocxx::instance::current() == &instance); + EXPECT(&mongocxx::instance::current() == &instance); try { mongocxx::instance another_instance; // Throws. - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const mongocxx::exception& ex) { - ASSERT(ex.code() == mongocxx::error_code::k_cannot_recreate_instance); + EXPECT(ex.code() == mongocxx::error_code::k_cannot_recreate_instance); } - ASSERT(&mongocxx::instance::current() == &instance); + EXPECT(&mongocxx::instance::current() == &instance); } } // [Example] diff --git a/examples/api/mongocxx/examples/logger/basic_usage.cpp b/examples/api/mongocxx/examples/logger/basic_usage.cpp index 76364fc9ee..ec2f27c314 100644 --- a/examples/api/mongocxx/examples/logger/basic_usage.cpp +++ b/examples/api/mongocxx/examples/logger/basic_usage.cpp @@ -35,9 +35,9 @@ class example_logger : public mongocxx::logger { void operator()(mongocxx::log_level level, bsoncxx::stdx::string_view domain, bsoncxx::stdx::string_view message) noexcept override { - ASSERT(level == mongocxx::log_level::k_info); - ASSERT(domain.compare("mongocxx") == 0); - ASSERT(message.compare("libmongoc logging callback enabled") == 0); + EXPECT(level == mongocxx::log_level::k_info); + EXPECT(domain.compare("mongocxx") == 0); + EXPECT(message.compare("libmongoc logging callback enabled") == 0); *counter_ptr += 1; } @@ -52,7 +52,7 @@ void example() { // Emit the informational mongocxx log message: "libmongoc logging callback enabled". mongocxx::instance instance{std::move(logger)}; - ASSERT(counter == 1); + EXPECT(counter == 1); } // [Example] diff --git a/examples/api/mongocxx/examples/logger/to_string.cpp b/examples/api/mongocxx/examples/logger/to_string.cpp index 43274e6077..9d649f2cc1 100644 --- a/examples/api/mongocxx/examples/logger/to_string.cpp +++ b/examples/api/mongocxx/examples/logger/to_string.cpp @@ -34,13 +34,13 @@ void example() { bsoncxx::stdx::string_view debug = mongocxx::to_string(mongocxx::log_level::k_debug); bsoncxx::stdx::string_view trace = mongocxx::to_string(mongocxx::log_level::k_trace); - ASSERT(error.compare("error") == 0); - ASSERT(critical.compare("critical") == 0); - ASSERT(warning.compare("warning") == 0); - ASSERT(message.compare("message") == 0); - ASSERT(info.compare("info") == 0); - ASSERT(debug.compare("debug") == 0); - ASSERT(trace.compare("trace") == 0); + EXPECT(error.compare("error") == 0); + EXPECT(critical.compare("critical") == 0); + EXPECT(warning.compare("warning") == 0); + EXPECT(message.compare("message") == 0); + EXPECT(info.compare("info") == 0); + EXPECT(debug.compare("debug") == 0); + EXPECT(trace.compare("trace") == 0); } // [Example] diff --git a/examples/api/mongocxx/examples/operation_exceptions/operation.cpp b/examples/api/mongocxx/examples/operation_exceptions/operation.cpp index c293bcf466..96afd7ec73 100644 --- a/examples/api/mongocxx/examples/operation_exceptions/operation.cpp +++ b/examples/api/mongocxx/examples/operation_exceptions/operation.cpp @@ -31,7 +31,7 @@ namespace { // [Example] void example(mongocxx::database db) { - ASSERT(db.name().compare("db") == 0); + EXPECT(db.name().compare("db") == 0); // The `getParameter` command can only be run in the `admin` database. auto cmd = bsoncxx::from_json(R"({"getParameter": "*"})"); @@ -41,23 +41,23 @@ void example(mongocxx::database db) { try { auto reply = db.run_command(cmd.view()); - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const mongocxx::operation_exception& ex) { - ASSERT(ex.code().category() == mongocxx::server_error_category()); - ASSERT(ex.code().value() == 13); // Unauthorized - ASSERT(std::strstr(ex.what(), "admin") != nullptr); + EXPECT(ex.code().category() == mongocxx::server_error_category()); + EXPECT(ex.code().value() == 13); // Unauthorized + EXPECT(std::strstr(ex.what(), "admin") != nullptr); if (auto server_error_opt = ex.raw_server_error()) { bsoncxx::document::view server_error = server_error_opt->view(); - ASSERT(server_error["ok"]); - ASSERT(server_error["ok"].get_double().value == 0.0); + EXPECT(server_error["ok"]); + EXPECT(server_error["ok"].get_double().value == 0.0); - ASSERT(server_error["code"]); - ASSERT(server_error["code"].get_int32().value == ex.code().value()); + EXPECT(server_error["code"]); + EXPECT(server_error["code"].get_int32().value == ex.code().value()); - ASSERT(server_error["codeName"]); - ASSERT(server_error["errmsg"]); + EXPECT(server_error["codeName"]); + EXPECT(server_error["errmsg"]); // ... other error fields. } } diff --git a/examples/api/mongocxx/examples/operation_exceptions/regular.cpp b/examples/api/mongocxx/examples/operation_exceptions/regular.cpp index 0b3fb0ecf0..30dc962e28 100644 --- a/examples/api/mongocxx/examples/operation_exceptions/regular.cpp +++ b/examples/api/mongocxx/examples/operation_exceptions/regular.cpp @@ -29,7 +29,7 @@ namespace { // [Example] void example(mongocxx::database db) { - ASSERT(db.name().compare("db") == 0); + EXPECT(db.name().compare("db") == 0); // The `getParameter` command can only be run in the `admin` database. auto cmd = bsoncxx::from_json(R"({"getParameter": "*"})"); @@ -39,11 +39,11 @@ void example(mongocxx::database db) { try { auto reply = db.run_command(cmd.view()); - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const mongocxx::exception& ex) { - ASSERT(ex.code().category() == mongocxx::server_error_category()); - ASSERT(ex.code().value() == 13); // Unauthorized - ASSERT(std::strstr(ex.what(), "admin") != nullptr); + EXPECT(ex.code().category() == mongocxx::server_error_category()); + EXPECT(ex.code().value() == 13); // Unauthorized + EXPECT(std::strstr(ex.what(), "admin") != nullptr); } } // [Example] diff --git a/examples/api/mongocxx/examples/uri/all_options.cpp b/examples/api/mongocxx/examples/uri/all_options.cpp index a474dfda52..dc4a151615 100644 --- a/examples/api/mongocxx/examples/uri/all_options.cpp +++ b/examples/api/mongocxx/examples/uri/all_options.cpp @@ -31,14 +31,14 @@ void example() { bsoncxx::document::view options = uri.options(); - ASSERT(options["appname"]); - ASSERT(options["appname"].get_string().value.compare("example") == 0); + EXPECT(options["appname"]); + EXPECT(options["appname"].get_string().value.compare("example") == 0); - ASSERT(options["tls"]); - ASSERT(options["tls"].get_bool().value == true); + EXPECT(options["tls"]); + EXPECT(options["tls"].get_bool().value == true); - ASSERT(options["maxpoolsize"]); - ASSERT(options["maxpoolsize"].get_int32().value == 10); + EXPECT(options["maxpoolsize"]); + EXPECT(options["maxpoolsize"].get_int32().value == 10); } // [Example] diff --git a/examples/api/mongocxx/examples/uri/basic_usage.cpp b/examples/api/mongocxx/examples/uri/basic_usage.cpp index cb844ec350..5922b32cea 100644 --- a/examples/api/mongocxx/examples/uri/basic_usage.cpp +++ b/examples/api/mongocxx/examples/uri/basic_usage.cpp @@ -25,11 +25,11 @@ void example() { mongocxx::uri uri{uri_str}; - ASSERT(uri.to_string() == uri_str); + EXPECT(uri.to_string() == uri_str); - ASSERT(uri.username().compare("bob") == 0); - ASSERT(uri.password() == "pwd123"); - ASSERT(uri.tls() == true); + EXPECT(uri.username().compare("bob") == 0); + EXPECT(uri.password() == "pwd123"); + EXPECT(uri.tls() == true); } // [Example] diff --git a/examples/api/mongocxx/examples/uri/default_value.cpp b/examples/api/mongocxx/examples/uri/default_value.cpp index 2227fc6dec..6a6d46d5e4 100644 --- a/examples/api/mongocxx/examples/uri/default_value.cpp +++ b/examples/api/mongocxx/examples/uri/default_value.cpp @@ -27,9 +27,9 @@ void example() { mongocxx::uri b{uri_str}; mongocxx::uri c{mongocxx::uri::k_default_uri}; - ASSERT(a.to_string() == uri_str); - ASSERT(b.to_string() == uri_str); - ASSERT(c.to_string() == uri_str); + EXPECT(a.to_string() == uri_str); + EXPECT(b.to_string() == uri_str); + EXPECT(c.to_string() == uri_str); } // [Example] diff --git a/examples/api/mongocxx/examples/uri/hosts.cpp b/examples/api/mongocxx/examples/uri/hosts.cpp index c587c26261..0b0dc03986 100644 --- a/examples/api/mongocxx/examples/uri/hosts.cpp +++ b/examples/api/mongocxx/examples/uri/hosts.cpp @@ -27,23 +27,23 @@ void example() { std::vector hosts = uri.hosts(); - ASSERT(hosts.size() == 3u); + EXPECT(hosts.size() == 3u); const mongocxx::uri::host& first = hosts[0]; const mongocxx::uri::host& second = hosts[1]; const mongocxx::uri::host& third = hosts[2]; - ASSERT(first.name == "127.0.0.1"); - ASSERT(first.port == 27017u); - ASSERT(first.family == 0); // AF_UNSPEC (AP_INET). + EXPECT(first.name == "127.0.0.1"); + EXPECT(first.port == 27017u); + EXPECT(first.family == 0); // AF_UNSPEC (AP_INET). - ASSERT(second.name == "::1"); - ASSERT(second.port == 27018u); - ASSERT(second.family != 0); // AF_INET6. + EXPECT(second.name == "::1"); + EXPECT(second.port == 27018u); + EXPECT(second.family != 0); // AF_INET6. - ASSERT(third.name == "/path/to.socket"); - ASSERT(third.port == 27019u); - ASSERT(third.family != 0); // AF_UNIX. + EXPECT(third.name == "/path/to.socket"); + EXPECT(third.port == 27019u); + EXPECT(third.family != 0); // AF_UNIX. } // [Example] diff --git a/examples/api/mongocxx/examples/uri/invalid.cpp b/examples/api/mongocxx/examples/uri/invalid.cpp index ca33d36a03..aab9a60759 100644 --- a/examples/api/mongocxx/examples/uri/invalid.cpp +++ b/examples/api/mongocxx/examples/uri/invalid.cpp @@ -27,27 +27,27 @@ void example() { // Missing `mongodb://`. mongocxx::uri invalid_uri{"invalid"}; - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const mongocxx::exception& ex) { - ASSERT(ex.code() == mongocxx::error_code::k_invalid_uri); + EXPECT(ex.code() == mongocxx::error_code::k_invalid_uri); } try { // Missing `=`. mongocxx::uri invalid_uri{"mongodb://localhost:27017/?tls"}; - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const mongocxx::exception& ex) { - ASSERT(ex.code() == mongocxx::error_code::k_invalid_uri); + EXPECT(ex.code() == mongocxx::error_code::k_invalid_uri); } try { // Missing user credentials when authMechanism is provided. mongocxx::uri invalid_uri{"mongodb://localhost:27017/?authMechanism=SCRAM-SHA-256"}; - ASSERT(false && "should not reach this point"); + EXPECT(false && "should not reach this point"); } catch (const mongocxx::exception& ex) { - ASSERT(ex.code() == mongocxx::error_code::k_invalid_uri); + EXPECT(ex.code() == mongocxx::error_code::k_invalid_uri); } } // [Example] diff --git a/examples/api/mongocxx/examples/uri/optional.cpp b/examples/api/mongocxx/examples/uri/optional.cpp index cc95d2d0c3..ca8485ab89 100644 --- a/examples/api/mongocxx/examples/uri/optional.cpp +++ b/examples/api/mongocxx/examples/uri/optional.cpp @@ -30,13 +30,13 @@ void example() { mongocxx::uri uri{"mongodb://localhost:27017/"}; std::string database = uri.database(); - ASSERT(database.empty()); + EXPECT(database.empty()); auto try_once_opt = uri.server_selection_try_once(); - ASSERT(!try_once_opt); + EXPECT(!try_once_opt); auto appname_opt = uri.appname(); - ASSERT(!appname_opt); + EXPECT(!appname_opt); } { @@ -44,15 +44,15 @@ void example() { "mongodb://localhost:27017/dbName?appName=example&serverSelectionTryOnce=false"}; auto database = uri.database(); - ASSERT(database.compare("dbName") == 0); + EXPECT(database.compare("dbName") == 0); auto try_once_opt = uri.server_selection_try_once(); - ASSERT(try_once_opt); - ASSERT(*try_once_opt == false); + EXPECT(try_once_opt); + EXPECT(*try_once_opt == false); auto appname_opt = uri.appname(); - ASSERT(appname_opt); - ASSERT(appname_opt->compare("example") == 0); + EXPECT(appname_opt); + EXPECT(appname_opt->compare("example") == 0); } } // [Example] diff --git a/examples/api/mongocxx/examples/uri/userpass.cpp b/examples/api/mongocxx/examples/uri/userpass.cpp index 77be7aa6d8..8d64b1712e 100644 --- a/examples/api/mongocxx/examples/uri/userpass.cpp +++ b/examples/api/mongocxx/examples/uri/userpass.cpp @@ -26,19 +26,19 @@ void example() { { mongocxx::uri uri{"mongodb://localhost:27017/"}; - ASSERT(uri.username().empty()); - ASSERT(uri.password().empty()); + EXPECT(uri.username().empty()); + EXPECT(uri.password().empty()); - ASSERT(uri.tls() == false); + EXPECT(uri.tls() == false); } { mongocxx::uri uri{"mongodb://bob:pwd123@localhost:27017/?tls=true"}; - ASSERT(uri.username().compare("bob") == 0); - ASSERT(uri.password() == "pwd123"); + EXPECT(uri.username().compare("bob") == 0); + EXPECT(uri.password() == "pwd123"); - ASSERT(uri.tls() == true); + EXPECT(uri.tls() == true); } } // [Example] diff --git a/examples/api/runner.cpp b/examples/api/runner.cpp index 4ad52c5f7f..bcafa0e21f 100644 --- a/examples/api/runner.cpp +++ b/examples/api/runner.cpp @@ -156,7 +156,7 @@ class runner_type { // We don't expect any other failure condition. else { - ASSERT(ret != -1); + EXPECT(ret != -1); } } } @@ -238,7 +238,7 @@ class runner_type { } int run() { - ASSERT(jobs > 0u); + EXPECT(jobs > 0u); std::cout << "seed: " << seed << std::endl; diff --git a/examples/macros.hh b/examples/macros.hh index 86dfaede8e..ac879df96e 100644 --- a/examples/macros.hh +++ b/examples/macros.hh @@ -31,12 +31,12 @@ #define EXAMPLES_STR(e) #e -// Unconditionally define `assert()` for examples. -#define ASSERT(...) \ - if (!static_cast(__VA_ARGS__)) { \ - std::printf( \ - "%s:%d: %s: assertion failed: %s\n", __FILE__, __LINE__, __func__, #__VA_ARGS__); \ - std::fflush(stdout); \ - std::abort(); \ - } else \ +// Unconditionally `assert()` expectations in examples. +#define EXPECT(...) \ + if (!static_cast(__VA_ARGS__)) { \ + std::printf( \ + "%s:%d: %s: expectation failed: %s\n", __FILE__, __LINE__, __func__, #__VA_ARGS__); \ + std::fflush(stdout); \ + std::abort(); \ + } else \ ((void)0) From a2e12d4eeb4d5fd09d5712479e487ad0df390daa Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Mon, 7 Oct 2024 15:14:34 -0500 Subject: [PATCH 19/25] Revert assertions in API runner to `assert()` --- examples/api/runner.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/api/runner.cpp b/examples/api/runner.cpp index bcafa0e21f..7360f1cd3c 100644 --- a/examples/api/runner.cpp +++ b/examples/api/runner.cpp @@ -20,6 +20,7 @@ // #include +#include #include #include #include @@ -156,7 +157,7 @@ class runner_type { // We don't expect any other failure condition. else { - EXPECT(ret != -1); + assert(ret != -1); } } } @@ -238,7 +239,7 @@ class runner_type { } int run() { - EXPECT(jobs > 0u); + assert(jobs > 0u); std::cout << "seed: " << seed << std::endl; From da66059bfc6bb3ed718fd4e9dd437ea4ffb9a3c2 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 9 Oct 2024 12:39:44 -0500 Subject: [PATCH 20/25] Use k_majority instead of milliseconds(0) --- examples/api/concern.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/api/concern.cpp b/examples/api/concern.cpp index 00af68194b..451004ea91 100644 --- a/examples/api/concern.cpp +++ b/examples/api/concern.cpp @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include - #include #include #include @@ -43,6 +41,6 @@ mongocxx::read_concern rc_majority() { mongocxx::write_concern wc_majority() { mongocxx::write_concern wc; - wc.majority(std::chrono::milliseconds(0)); + wc.acknowledge_level(mongocxx::write_concern::level::k_majority); return wc; } From bc9431cc3fb23873017f0d906baa95f3398b27b7 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 9 Oct 2024 12:39:44 -0500 Subject: [PATCH 21/25] Avoid using serverSelectionTryOnce in examples --- .../api/mongocxx/examples/clients/create/pool/basic.cpp | 2 +- .../mongocxx/examples/clients/create/single/with_uri.cpp | 2 +- examples/api/mongocxx/examples/uri/optional.cpp | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/api/mongocxx/examples/clients/create/pool/basic.cpp b/examples/api/mongocxx/examples/clients/create/pool/basic.cpp index fed36b2f47..a43d256171 100644 --- a/examples/api/mongocxx/examples/clients/create/pool/basic.cpp +++ b/examples/api/mongocxx/examples/clients/create/pool/basic.cpp @@ -37,7 +37,7 @@ void example() { } { - mongocxx::uri uri{"mongodb://localhost:27017/?serverSelectionTryOnce=true"}; + mongocxx::uri uri{"mongodb://localhost:27017/?appName=example"}; mongocxx::pool pool{uri}; mongocxx::pool::entry entry = pool.acquire(); diff --git a/examples/api/mongocxx/examples/clients/create/single/with_uri.cpp b/examples/api/mongocxx/examples/clients/create/single/with_uri.cpp index cc03b746d3..363397004f 100644 --- a/examples/api/mongocxx/examples/clients/create/single/with_uri.cpp +++ b/examples/api/mongocxx/examples/clients/create/single/with_uri.cpp @@ -22,7 +22,7 @@ namespace { // [Example] void example() { - mongocxx::uri uri{"mongodb://localhost:27017/?serverSelectionTryOnce=true"}; + mongocxx::uri uri{"mongodb://localhost:27017/?appName=example"}; mongocxx::client client{uri}; EXPECT(client); diff --git a/examples/api/mongocxx/examples/uri/optional.cpp b/examples/api/mongocxx/examples/uri/optional.cpp index ca8485ab89..b24eb87fad 100644 --- a/examples/api/mongocxx/examples/uri/optional.cpp +++ b/examples/api/mongocxx/examples/uri/optional.cpp @@ -41,14 +41,14 @@ void example() { { mongocxx::uri uri{ - "mongodb://localhost:27017/dbName?appName=example&serverSelectionTryOnce=false"}; + "mongodb://localhost:27017/dbName?appName=example&retryReads=true"}; auto database = uri.database(); EXPECT(database.compare("dbName") == 0); - auto try_once_opt = uri.server_selection_try_once(); - EXPECT(try_once_opt); - EXPECT(*try_once_opt == false); + auto retry_reads_opt = uri.retry_reads(); + EXPECT(retry_reads_opt); + EXPECT(*retry_reads_opt == true); auto appname_opt = uri.appname(); EXPECT(appname_opt); From f5dcde64171b1fb687809f3d443684b22f275242 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 9 Oct 2024 12:39:44 -0500 Subject: [PATCH 22/25] Fix typo --- examples/api/mongocxx/examples/collections/estimate_count.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/api/mongocxx/examples/collections/estimate_count.cpp b/examples/api/mongocxx/examples/collections/estimate_count.cpp index a514bae753..43bc493280 100644 --- a/examples/api/mongocxx/examples/collections/estimate_count.cpp +++ b/examples/api/mongocxx/examples/collections/estimate_count.cpp @@ -43,7 +43,7 @@ void example(mongocxx::collection coll) { { mongocxx::options::estimated_document_count opts; - // ... set estimated scount options. + // ... set estimated count options. EXPECT(coll.estimated_document_count(opts) == 3); } From c83d735943bb8fee5b4248f43c00651be635e7bb Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Wed, 9 Oct 2024 15:52:43 -0500 Subject: [PATCH 23/25] Remove stray mongocryptd.pid --- mongocryptd.pid | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 mongocryptd.pid diff --git a/mongocryptd.pid b/mongocryptd.pid deleted file mode 100644 index e69de29bb2..0000000000 From 1371cb09bb33ea5be4643acf1f2a4b3c2b511630 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 10 Oct 2024 09:53:18 -0500 Subject: [PATCH 24/25] Clarify init/cleanup is handled by instance ctor/dtor --- examples/api/mongocxx/examples/instance/basic_usage.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/api/mongocxx/examples/instance/basic_usage.cpp b/examples/api/mongocxx/examples/instance/basic_usage.cpp index 2f20a9a4bc..addfd04c4d 100644 --- a/examples/api/mongocxx/examples/instance/basic_usage.cpp +++ b/examples/api/mongocxx/examples/instance/basic_usage.cpp @@ -29,7 +29,7 @@ void example() { // Do not use mongocxx library interfaces at this point! { - // Initialize the MongoDB C++ Driver. + // Initialize the MongoDB C++ Driver by constructing the instance object. mongocxx::instance instance; EXPECT(&mongocxx::instance::current() == &instance); @@ -37,7 +37,7 @@ void example() { // Use mongocxx library interfaces at this point. use(mongocxx::client{}); - // Cleanup the MongoDB C++ Driver. + // Cleanup the MongoDB C++ Driver by destroying the instance object. } // Do not use mongocxx library interfaces at this point! From 5e3db05e1582b605838fd62fe1f7d647d5666fa2 Mon Sep 17 00:00:00 2001 From: Ezra Chung Date: Thu, 10 Oct 2024 13:49:06 -0500 Subject: [PATCH 25/25] Formatting --- examples/api/mongocxx/examples/uri/optional.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/api/mongocxx/examples/uri/optional.cpp b/examples/api/mongocxx/examples/uri/optional.cpp index b24eb87fad..2f4050cf89 100644 --- a/examples/api/mongocxx/examples/uri/optional.cpp +++ b/examples/api/mongocxx/examples/uri/optional.cpp @@ -40,8 +40,7 @@ void example() { } { - mongocxx::uri uri{ - "mongodb://localhost:27017/dbName?appName=example&retryReads=true"}; + mongocxx::uri uri{"mongodb://localhost:27017/dbName?appName=example&retryReads=true"}; auto database = uri.database(); EXPECT(database.compare("dbName") == 0);