Skip to content

Commit 13d27bc

Browse files
test: add tests for behavior when overwriteImmutable: false
1 parent 27cbe27 commit 13d27bc

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

test/model.updateOne.test.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2723,7 +2723,7 @@ describe('model: updateOne: ', function() {
27232723
assert.strictEqual(updatedUser.createdAt.valueOf(), customCreatedAt.valueOf());
27242724
});
27252725

2726-
for (const timestamps of [true, false, null, undefined])
2726+
for (const timestamps of [true, false, null, undefined]) {
27272727
it(`overwriting immutable createdAt with bulkWrite (gh-15781) when \`timestamps\` is \`${timestamps}\``, async function() {
27282728
// Arrange
27292729
const schema = Schema({ name: String }, { timestamps: true });
@@ -2761,6 +2761,46 @@ describe('model: updateOne: ', function() {
27612761
assert.equal(updatesDocs[0].createdAt.valueOf(), createdAt.valueOf());
27622762
assert.equal(updatesDocs[1].createdAt.valueOf(), createdAt.valueOf());
27632763
});
2764+
}
2765+
2766+
it('can not update immutable fields without overwriteImmutable: true', async function() {
2767+
// Arrange
2768+
const { User } = createTestContext();
2769+
const users = await User.create([
2770+
{ name: 'Bob', ssn: '222-22-2222' },
2771+
{ name: 'Eve', ssn: '333-33-3333' }
2772+
]);
2773+
const newCreatedAt = new Date('2020-01-01');
2774+
2775+
// Act
2776+
await User.bulkWrite([
2777+
{
2778+
updateOne: {
2779+
filter: { _id: users[0]._id },
2780+
update: { ssn: '888-88-8888', createdAt: newCreatedAt }
2781+
}
2782+
2783+
},
2784+
{
2785+
updateMany: {
2786+
filter: { _id: users[1]._id },
2787+
update: { ssn: '777-77-7777', createdAt: newCreatedAt }
2788+
}
2789+
}
2790+
]);
2791+
2792+
2793+
// Assert
2794+
const [updatedUser1, updatedUser2] = await Promise.all([
2795+
User.findById(users[0]._id),
2796+
User.findById(users[1]._id)
2797+
]);
2798+
assert.strictEqual(updatedUser1.ssn, '222-22-2222');
2799+
assert.notStrictEqual(updatedUser1.createdAt.valueOf(), newCreatedAt.valueOf());
2800+
2801+
assert.strictEqual(updatedUser2.ssn, '333-33-3333');
2802+
assert.notStrictEqual(updatedUser2.createdAt.valueOf(), newCreatedAt.valueOf());
2803+
});
27642804

27652805
function createTestContext() {
27662806
const userSchema = new Schema({

0 commit comments

Comments
 (0)