diff options
author | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2017-12-29 12:15:35 -0500 |
---|---|---|
committer | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2017-12-29 12:15:35 -0500 |
commit | 449905c81af3803ebb16eb8f7ca779ae783517d9 (patch) | |
tree | 4336c1e1efcd1646b7e2dc5b9968cd0d2c8f11e8 /src/mongo/idl | |
parent | 9fc948694d27b5ed78f713b026cb6e5661c95551 (diff) | |
download | mongo-449905c81af3803ebb16eb8f7ca779ae783517d9.tar.gz |
SERVER-32386 IDL should allow custom types for command's first element
Diffstat (limited to 'src/mongo/idl')
-rw-r--r-- | src/mongo/idl/idl_test.cpp | 215 | ||||
-rw-r--r-- | src/mongo/idl/unittest.idl | 30 |
2 files changed, 245 insertions, 0 deletions
diff --git a/src/mongo/idl/idl_test.cpp b/src/mongo/idl/idl_test.cpp index 4c871112b1b..ab2e9328c3a 100644 --- a/src/mongo/idl/idl_test.cpp +++ b/src/mongo/idl/idl_test.cpp @@ -2329,5 +2329,220 @@ TEST(IDLChainedStruct, TestInline) { } } +// Positive: verify a command a string arg +TEST(IDLTypeCommand, TestString) { + IDLParserErrorContext ctxt("root"); + + auto testDoc = BSON(CommandTypeStringCommand::kCommandName << "foo" + << "field1" + << 3 + << "$db" + << "db"); + + auto testStruct = CommandTypeStringCommand::parse(ctxt, makeOMR(testDoc)); + ASSERT_EQUALS(testStruct.getField1(), 3); + ASSERT_EQUALS(testStruct.getCommandParameter(), "foo"); + + assert_same_types<decltype(testStruct.getCommandParameter()), const StringData>(); + + // Positive: Test we can roundtrip from the just parsed document + { + BSONObjBuilder builder; + OpMsgRequest reply = testStruct.serialize(BSONObj()); + + ASSERT_BSONOBJ_EQ(testDoc, reply.body); + } + + // Positive: Test we can serialize from nothing the same document except for $db + { + auto testDocWithoutDb = BSON(CommandTypeStringCommand::kCommandName << "foo" + << "field1" + << 3); + + BSONObjBuilder builder; + CommandTypeStringCommand one_new("foo"); + one_new.setField1(3); + one_new.setDbName("db"); + one_new.serialize(BSONObj(), &builder); + + auto serializedDoc = builder.obj(); + ASSERT_BSONOBJ_EQ(testDocWithoutDb, serializedDoc); + } + + // Positive: Test we can serialize from nothing the same document + { + BSONObjBuilder builder; + CommandTypeStringCommand one_new("foo"); + one_new.setField1(3); + one_new.setDbName("db"); + OpMsgRequest reply = one_new.serialize(BSONObj()); + + ASSERT_BSONOBJ_EQ(testDoc, reply.body); + } +} + +// Positive: verify a command can take an array of object +TEST(IDLTypeCommand, TestArrayObject) { + IDLParserErrorContext ctxt("root"); + + auto testDoc = BSON(CommandTypeArrayObjectCommand::kCommandName << BSON_ARRAY(BSON("sample" + << "doc")) + << "$db" + << "db"); + + auto testStruct = CommandTypeArrayObjectCommand::parse(ctxt, makeOMR(testDoc)); + ASSERT_EQUALS(testStruct.getCommandParameter().size(), 1UL); + + assert_same_types<decltype(testStruct.getCommandParameter()), + const std::vector<mongo::BSONObj>&>(); + + // Positive: Test we can roundtrip from the just parsed document + { + BSONObjBuilder builder; + OpMsgRequest reply = testStruct.serialize(BSONObj()); + + ASSERT_BSONOBJ_EQ(testDoc, reply.body); + } + + // Positive: Test we can serialize from nothing the same document + { + BSONObjBuilder builder; + std::vector<BSONObj> vec; + vec.emplace_back(BSON("sample" + << "doc")); + CommandTypeArrayObjectCommand one_new(vec); + one_new.setDbName("db"); + OpMsgRequest reply = one_new.serialize(BSONObj()); + + ASSERT_BSONOBJ_EQ(testDoc, reply.body); + } +} + +// Positive: verify a command can take a struct +TEST(IDLTypeCommand, TestStruct) { + IDLParserErrorContext ctxt("root"); + + auto testDoc = BSON(CommandTypeStructCommand::kCommandName << BSON("value" + << "sample") + << "$db" + << "db"); + + auto testStruct = CommandTypeStructCommand::parse(ctxt, makeOMR(testDoc)); + ASSERT_EQUALS(testStruct.getCommandParameter().getValue(), "sample"); + + assert_same_types<decltype(testStruct.getCommandParameter()), + mongo::idl::import::One_string&>(); + + // Positive: Test we can roundtrip from the just parsed document + { + BSONObjBuilder builder; + OpMsgRequest reply = testStruct.serialize(BSONObj()); + + ASSERT_BSONOBJ_EQ(testDoc, reply.body); + } + + // Positive: Test we can serialize from nothing the same document + { + BSONObjBuilder builder; + One_string os; + os.setValue("sample"); + CommandTypeStructCommand one_new(os); + one_new.setDbName("db"); + OpMsgRequest reply = one_new.serialize(BSONObj()); + + ASSERT_BSONOBJ_EQ(testDoc, reply.body); + } +} + +// Positive: verify a command can take an array of structs +TEST(IDLTypeCommand, TestStructArray) { + IDLParserErrorContext ctxt("root"); + + auto testDoc = BSON(CommandTypeArrayStructCommand::kCommandName << BSON_ARRAY(BSON("value" + << "sample")) + << "$db" + << "db"); + + auto testStruct = CommandTypeArrayStructCommand::parse(ctxt, makeOMR(testDoc)); + ASSERT_EQUALS(testStruct.getCommandParameter().size(), 1UL); + + assert_same_types<decltype(testStruct.getCommandParameter()), + const std::vector<mongo::idl::import::One_string>&>(); + + // Positive: Test we can roundtrip from the just parsed document + { + BSONObjBuilder builder; + OpMsgRequest reply = testStruct.serialize(BSONObj()); + + ASSERT_BSONOBJ_EQ(testDoc, reply.body); + } + + // Positive: Test we can serialize from nothing the same document + { + BSONObjBuilder builder; + std::vector<One_string> vec; + One_string os; + os.setValue("sample"); + vec.push_back(os); + CommandTypeArrayStructCommand one_new(vec); + one_new.setDbName("db"); + OpMsgRequest reply = one_new.serialize(BSONObj()); + + ASSERT_BSONOBJ_EQ(testDoc, reply.body); + } +} + +// Positive: verify a command a string arg and alternate C++ name +TEST(IDLTypeCommand, TestUnderscoreCommand) { + IDLParserErrorContext ctxt("root"); + + auto testDoc = BSON(WellNamedCommand::kCommandName << "foo" + << "field1" + << 3 + << "$db" + << "db"); + + auto testStruct = WellNamedCommand::parse(ctxt, makeOMR(testDoc)); + ASSERT_EQUALS(testStruct.getField1(), 3); + ASSERT_EQUALS(testStruct.getCommandParameter(), "foo"); + + assert_same_types<decltype(testStruct.getCommandParameter()), const StringData>(); + + // Positive: Test we can roundtrip from the just parsed document + { + BSONObjBuilder builder; + OpMsgRequest reply = testStruct.serialize(BSONObj()); + + ASSERT_BSONOBJ_EQ(testDoc, reply.body); + } + + // Positive: Test we can serialize from nothing the same document except for $db + { + auto testDocWithoutDb = BSON(WellNamedCommand::kCommandName << "foo" + << "field1" + << 3); + + BSONObjBuilder builder; + WellNamedCommand one_new("foo"); + one_new.setField1(3); + one_new.setDbName("db"); + one_new.serialize(BSONObj(), &builder); + + auto serializedDoc = builder.obj(); + ASSERT_BSONOBJ_EQ(testDocWithoutDb, serializedDoc); + } + + // Positive: Test we can serialize from nothing the same document + { + BSONObjBuilder builder; + WellNamedCommand one_new("foo"); + one_new.setField1(3); + one_new.setDbName("db"); + OpMsgRequest reply = one_new.serialize(BSONObj()); + + ASSERT_BSONOBJ_EQ(testDoc, reply.body); + } +} + } // namespace } // namespace mongo diff --git a/src/mongo/idl/unittest.idl b/src/mongo/idl/unittest.idl index 8c621daa1d0..01475aeccdc 100644 --- a/src/mongo/idl/unittest.idl +++ b/src/mongo/idl/unittest.idl @@ -586,3 +586,33 @@ commands: fields: field3: type: int + + CommandTypeStringCommand: + description: Command with custom type string + namespace: type + type: string + fields: + field1: int + + CommandTypeArrayObjectCommand: + description: Command with just an array of object parameter + namespace: type + type: array<object> + + CommandTypeStructCommand: + description: Command with just a struct parameter + namespace: type + type: one_string + + CommandTypeArrayStructCommand: + description: Command with just an array of struct parameter + namespace: type + type: array<one_string> + + _underscore_command: + description: Command with custom type string + namespace: type + type: string + cpp_name: WellNamedCommand + fields: + field1: int
\ No newline at end of file |