Skip to content

Commit

Permalink
test(model): syncIndxes() should only create indexes when they do not…
Browse files Browse the repository at this point in the history
… exist on mongodb server re #12250
  • Loading branch information
AbdelrahmanHafez committed Aug 15, 2022
1 parent d11f809 commit d9246a3
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion test/model.test.js
Expand Up @@ -3,7 +3,7 @@
/**
* Test dependencies.
*/

const sinon = require('sinon');
const start = require('./common');

const assert = require('assert');
Expand Down Expand Up @@ -6756,6 +6756,29 @@ describe('Model', function() {
);

});
it('creates indexes only when they do not exist on the mongodb server (gh-12250)', async() => {
const userSchema = new Schema({
name: { type: String }
}, { autoIndex: false });

userSchema.index({ name: 1 });

const User = db.model('User', userSchema);

const createIndexSpy = sinon.spy(User.collection, 'createIndex');
const listIndexesSpy = sinon.spy(User.collection, 'listIndexes');

// Act
await User.syncIndexes();
assert.equal(createIndexSpy.callCount, 1);
assert.equal(listIndexesSpy.callCount, 1);

await User.syncIndexes();

// Assert
assert.equal(listIndexesSpy.callCount, 2);
assert.equal(createIndexSpy.callCount, 1);
});
});

it('using `new db.model()()` (gh-6698)', function() {
Expand Down

0 comments on commit d9246a3

Please sign in to comment.