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(); }
TEST_CASE("create_collection", "[create_collection]") { instance::current(); options::create_collection cc; SECTION("Can be exported to a document") { auto rule = builder::stream::document{} << "brain" << open_document << "$exists" << true << close_document << finalize; validation_criteria validation; 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"]};
void checkbox_input::toggle(document::element& element) { auto&& checked = element.get_attribute("checked"); checked = std::atoi(checked.c_str()) ? "0" : "1"; element.set_attribute("checked", checked); }
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); }
validation_criteria validation; 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");