TEST(TestCommand, testInsertCommand) { cout << "testInsertCommand" << endl; FileOutputStream* fos = new FileOutputStream("test.dat", "wb"); CommandWriter* commandWriter = new CommandWriter(fos); InsertCommand cmd; cmd.setDB("testdb"); cmd.setNameSpace("test.namespace.db"); BSONObj* obj = new BSONObj(); obj->add("name", "Cross"); obj->add("age", 18); cmd.setBSON(obj); commandWriter->writeCommand(&cmd); fos->close(); delete fos; delete commandWriter; FileInputStream* fis = new FileInputStream("test.dat", "rb"); CommandReader* reader = new CommandReader(fis); InsertCommand* rdCmd = (InsertCommand*) reader->readCommand(); EXPECT_TRUE(rdCmd != NULL); EXPECT_TRUE(rdCmd->nameSpace()->compare("test.namespace.db") == 0); EXPECT_TRUE(rdCmd->DB()->compare("testdb") == 0); const BSONObj* objResult = rdCmd->bson(); EXPECT_TRUE(objResult != NULL); EXPECT_TRUE(objResult->has("name")); EXPECT_TRUE(objResult->getString("name").compare("Cross") == 0); }
TEST(TestCommand, testOptionsCommand) { cout << "testOptionsCommand" << endl; FileOutputStream* fos = new FileOutputStream("test.dat", "wb"); CommandWriter* commandWriter = new CommandWriter(fos); BSONObj* options = new BSONObj(); std::string* txId = uuid(); options->add("_transactionId", const_cast<char*>(txId->c_str())); delete txId; InsertCommand cmd; cmd.setDB("testdb"); cmd.setNameSpace("test.namespace.db"); cmd.setOptions(options); BSONObj* obj = new BSONObj(); obj->add("name", "Cross"); obj->add("age", 18); cmd.setBSON(obj); commandWriter->writeCommand(&cmd); fos->close(); delete fos; delete commandWriter; FileInputStream* fis = new FileInputStream("test.dat", "rb"); CommandReader* reader = new CommandReader(fis); InsertCommand* rdCmd = (InsertCommand*) reader->readCommand(); EXPECT_TRUE(rdCmd != NULL); EXPECT_TRUE(rdCmd->nameSpace()->compare("test.namespace.db") == 0); EXPECT_TRUE(rdCmd->DB()->compare("testdb") == 0); const BSONObj* objResult = rdCmd->bson(); EXPECT_TRUE(objResult != NULL); EXPECT_TRUE(objResult->has("name")); EXPECT_TRUE(objResult->getString("name").compare("Cross") == 0); EXPECT_TRUE(rdCmd->options() != NULL); if (rdCmd->options() != NULL) { const BSONObj* options = rdCmd->options(); EXPECT_TRUE(options->has("_transactionId")); } }