Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/mongo/client/bulk_update_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,31 @@ namespace mongo {
{}

void BulkUpdateBuilder::updateOne(const BSONObj& update) {
uassert(0, "update object must not be empty",
!update.isEmpty());
uassert(0, "update object must consist of $-prefixed modifiers",
update.firstElementFieldName()[0] == '$');

UpdateWriteOperation* update_op = new UpdateWriteOperation(_selector, update, 0);
_builder->enqueue(update_op);
}

void BulkUpdateBuilder::update(const BSONObj& update) {
uassert(0, "update object must not be empty",
!update.isEmpty());
uassert(0, "update object must consist of $-prefixed modifiers",
update.firstElementFieldName()[0] == '$');

UpdateWriteOperation* update_op = new UpdateWriteOperation(
_selector, update, UpdateOption_Multi);
_builder->enqueue(update_op);
}

void BulkUpdateBuilder::replaceOne(const BSONObj& replacement) {
if (!replacement.isEmpty())
uassert(0, "replacement object must not include $ operators",
replacement.firstElementFieldName()[0] != '$');

UpdateWriteOperation* update_op = new UpdateWriteOperation(
_selector, replacement, 0);
_builder->enqueue(update_op);
Expand Down
14 changes: 14 additions & 0 deletions src/mongo/client/bulk_upsert_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,32 @@ namespace mongo {
{}

void BulkUpsertBuilder::updateOne(const BSONObj& update) {
uassert(0, "update object must not be empty",
!update.isEmpty());
uassert(0, "update object must consist of $-prefixed modifiers",
update.firstElementFieldName()[0] == '$');

UpdateWriteOperation* update_op = new UpdateWriteOperation(
_selector, update, UpdateOption_Upsert);
_builder->enqueue(update_op);
}

void BulkUpsertBuilder::update(const BSONObj& update) {
uassert(0, "update object must not be empty",
!update.isEmpty());
uassert(0, "update object must consist of $-prefixed modifiers",
update.firstElementFieldName()[0] == '$');

UpdateWriteOperation* update_op = new UpdateWriteOperation(
_selector, update, UpdateOption_Upsert + UpdateOption_Multi);
_builder->enqueue(update_op);
}

void BulkUpsertBuilder::replaceOne(const BSONObj& replacement) {
if (!replacement.isEmpty())
uassert(0, "replacement object must not include $ operators",
replacement.firstElementFieldName()[0] != '$');

UpdateWriteOperation* update_op = new UpdateWriteOperation(
_selector, replacement, UpdateOption_Upsert);
_builder->enqueue(update_op);
Expand Down
132 changes: 132 additions & 0 deletions src/mongo/unittest/bulk_operation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,138 @@ namespace {
ASSERT_FALSE(result.hasErrors());
}

TYPED_TEST(BulkOperationTest, UpdateEmpty) {
if (!this->testSupported()) return;
BulkOperationBuilder bulk(this->c, TEST_NS, true);

ASSERT_THROWS(
bulk.find(BSON("b" << 1)).update(BSONObj()),
std::exception
);
}

TYPED_TEST(BulkOperationTest, UpdateMissingDollarSign) {
if (!this->testSupported()) return;
BulkOperationBuilder bulk(this->c, TEST_NS, true);

ASSERT_THROWS(
bulk.find(BSON("b" << 1)).update(BSON("a" << 2)),
std::exception
);
}

TYPED_TEST(BulkOperationTest, UpdateOneMissingDollarSign) {
if (!this->testSupported()) return;
BulkOperationBuilder bulk(this->c, TEST_NS, true);

ASSERT_THROWS(
bulk.find(BSON("b" << 1)).updateOne(BSON("a" << 2)),
std::exception
);
}

TYPED_TEST(BulkOperationTest, UpdateMixedDollarSign) {
if (!this->testSupported()) return;
BulkOperationBuilder bulk(this->c, TEST_NS, true);

bulk.find(BSON("b" << 1)).update(
BSON("$set" << BSON("a" << "2") << "a" << 2)
);

WriteResult result;
ASSERT_THROWS(
bulk.execute(&WriteConcern::acknowledged, &result),
OperationException
);
}

TYPED_TEST(BulkOperationTest, ReplaceOneEmpty) {
if (!this->testSupported()) return;
BulkOperationBuilder bulk(this->c, TEST_NS, true);

bulk.insert(BSON("b" << 1));
bulk.find(BSON("b" << 1)).replaceOne(BSONObj());
ASSERT_EQUALS(this->c->count(TEST_NS, BSON("b" << 1)), 0U);
}

TYPED_TEST(BulkOperationTest, ReplaceOneHavingDollarSign) {
if (!this->testSupported()) return;
BulkOperationBuilder bulk(this->c, TEST_NS, true);

ASSERT_THROWS(
bulk.find(BSON("b" << 1)).replaceOne(
BSON("$set" << BSON("a" << 2))
),
std::exception
);
}

TYPED_TEST(BulkOperationTest, UpdateUpsertEmpty) {
if (!this->testSupported()) return;
BulkOperationBuilder bulk(this->c, TEST_NS, true);

ASSERT_THROWS(
bulk.find(BSON("b" << 1)).upsert().update(BSONObj()),
std::exception
);
}

TYPED_TEST(BulkOperationTest, UpdateUpsertMissingDollarSign) {
if (!this->testSupported()) return;
BulkOperationBuilder bulk(this->c, TEST_NS, true);

ASSERT_THROWS(
bulk.find(BSON("b" << 1)).upsert().update(BSON("a" << 2)),
std::exception
);
}

TYPED_TEST(BulkOperationTest, UpdateOneUpsertMissingDollarSign) {
if (!this->testSupported()) return;
BulkOperationBuilder bulk(this->c, TEST_NS, true);

ASSERT_THROWS(
bulk.find(BSON("b" << 1)).upsert().updateOne(BSON("a" << 2)),
std::exception
);
}

TYPED_TEST(BulkOperationTest, UpdateUpsertMixedDollarSign) {
if (!this->testSupported()) return;
BulkOperationBuilder bulk(this->c, TEST_NS, true);

bulk.find(BSON("b" << 1)).upsert().update(
BSON("$set" << BSON("a" << "2") << "a" << 2)
);

WriteResult result;
ASSERT_THROWS(
bulk.execute(&WriteConcern::acknowledged, &result),
OperationException
);
}

TYPED_TEST(BulkOperationTest, ReplaceOneUpsertHavingDollarSign) {
if (!this->testSupported()) return;
BulkOperationBuilder bulk(this->c, TEST_NS, true);

ASSERT_THROWS(
bulk.find(BSON("b" << 1)).upsert().replaceOne(
BSON("$set" << BSON("a" << 2))
),
std::exception
);
}

TYPED_TEST(BulkOperationTest, ReplaceOneUpsertEmpty) {
if (!this->testSupported()) return;
BulkOperationBuilder bulk(this->c, TEST_NS, true);

bulk.insert(BSON("b" << 1));
bulk.find(BSON("b" << 1)).upsert().replaceOne(BSONObj());
ASSERT_EQUALS(this->c->count(TEST_NS, BSON("b" << 1)), 0U);
}

TYPED_TEST(BulkOperationTest, UnorderedBatchWithErrors) {
if (!this->testSupported()) return;

Expand Down