Пример #1
0
std::string to_json(document::element element) {
    std::stringstream ss;

    json_visitor v(ss, false, 0);

    switch ((int)element.type()) {
#define BSONCXX_ENUM(name, val)              \
    case val:                                \
        v.visit_key(element.key());          \
        v.visit_value(element.get_##name()); \
        break;
#include <bsoncxx/enums/type.hpp>
#undef BSONCXX_ENUM
    }

    return ss.str();
}
        validation.rule(rule.view());
        validation.level(validation_criteria::validation_level::k_strict);

        cc.validation_criteria(validation);
        cc.capped(true);
        cc.size(256);
        cc.max(100);
        cc.no_padding(true);

        auto doc = cc.to_document();
        document::view doc_view{doc.view()};

        // capped field is set to true
        document::element capped{doc_view["capped"]};
        REQUIRE(capped);
        REQUIRE(capped.type() == type::k_bool);
        REQUIRE(capped.get_bool() == true);

        // autoIndexId should not be set
        document::element autoIndex{doc_view["autoIndexId"]};
        REQUIRE(!autoIndex);

        // size should be set
        document::element size{doc_view["size"]};
        REQUIRE(size);
        REQUIRE(size.type() == type::k_int32);
        REQUIRE(size.get_int32() == 256);

        // max should be set
        document::element max{doc_view["max"]};
        REQUIRE(max);
Пример #3
0
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);
        }

        SECTION("Does not equal index document") {
            auto index_doc = builder::stream::document{} << "a" << 1 << builder::stream::finalize;
        validation.rule(rule.view());
        validation.level(validation_criteria::validation_level::k_strict);

        auto index = builder::stream::document{} << "a" << 1 << finalize;

        cm.index(index.view(), std::chrono::seconds(10));
        cm.validation_criteria(validation);
        cm.no_padding(false);

        auto doc = cm.to_document();
        document::view doc_view{doc.view()};

        // noPadding should be set to false
        document::element padding{doc_view["noPadding"]};
        REQUIRE(padding);
        REQUIRE(padding.type() == type::k_bool);
        REQUIRE(padding.get_bool() == false);

        // validator and validationLevel should be set, but not validationAction
        document::element validator{doc_view["validator"]};
        REQUIRE(validator);
        REQUIRE(validator.type() == type::k_document);
        REQUIRE(validator.get_document().value == rule);

        document::element validationLevel{doc_view["validationLevel"]};
        REQUIRE(validationLevel);
        REQUIRE(validationLevel.type() == type::k_utf8);
        REQUIRE(validationLevel.get_utf8().value.to_string() == "strict");

        document::element validationAction{doc_view["validationAction"]};
        REQUIRE(!validationAction);