// set up test BSON documents and objects
std::string json_str = R"({"a": 1, "b":4, "c": 9})";
auto doc = from_json(json_str);
auto doc_view = doc.view();

std::string json_str_2 = R"({"a": 1, "b":4, "c": 900})";
auto doc_2 = from_json(json_str_2);
auto doc_2_view = doc_2.view();

Foo obj{1, 4, 9};

TEST_CASE("Function to_document can faithfully convert objects to BSON documents.",
          "[mangrove::to_document]") {
    document::value val = to_document(obj);
    auto v = val.view();

    REQUIRE(v["a"].get_int32() == obj.a);
    REQUIRE(v["b"].get_int32() == obj.b);
    REQUIRE(v["c"].get_int32() == obj.c);
}

TEST_CASE("Function to_obj can faithfully convert documents to objects.", "[mangrove::to_obj]") {
    // Test return-by-value
    Foo obj1 = to_obj<Foo>(doc_view);
    // Test fill-by-reference
    Foo obj2;
    to_obj(doc_view, obj2);

    REQUIRE(doc_view["a"].get_int32() == obj1.a);
    REQUIRE(doc_view["b"].get_int32() == obj1.b);
Esempio n. 2
0
#include <mongocxx/hint.hpp>

using namespace mongocxx;
using namespace bsoncxx;

TEST_CASE("Hint", "[hint]") {
    SECTION("Can be constructed with index name") {
        std::string index_name = "a_1";
        hint index_hint{index_name};

        SECTION("Can be applied to a query") {
            document::value filter = builder::stream::document{}
                                     << "a" << 15
                                     << builder::stream::concatenate{index_hint.to_document()}
                                     << builder::stream::finalize;
            document::view view{filter.view()};
            document::element ele{view["hint"]};
            REQUIRE(ele);
            REQUIRE(ele.type() == type::k_utf8);
            REQUIRE(ele.get_utf8().value.to_string() == index_name);
        }

        SECTION("Compares equal to matching index name") {
            REQUIRE(index_hint == index_name);
            REQUIRE(index_name == index_hint);
        }

        SECTION("Does not equal non-matching index name") {
            REQUIRE(index_hint != "sam");
            REQUIRE("sam" != index_hint);
        }