Sparse indexes work

pull/2/head
Louis Chatriot 12 years ago
parent 30eec9ff43
commit fa2ffeb8de
  1. 27
      test/db.test.js
  2. 2
      test/indexes.test.js

@ -1466,7 +1466,7 @@ describe('Database', function () {
}); });
}); });
it('If the index has a unique constraint, an error is thrown if it is violated and the data didnt change', function (done) { it('If the index has a unique constraint, an error is thrown if it is violated and the data is not modified', function (done) {
d.ensureIndex({ fieldName: 'z', unique: true }); d.ensureIndex({ fieldName: 'z', unique: true });
d.indexes.z.tree.getNumberOfKeys().should.equal(0); d.indexes.z.tree.getNumberOfKeys().should.equal(0);
@ -1522,6 +1522,31 @@ describe('Database', function () {
}); });
}); });
it('Unique indexes prevent you from inserting two docs where the field is undefined except if theyre sparse', function (done) {
d.ensureIndex({ fieldName: 'zzz', unique: true });
d.indexes.zzz.tree.getNumberOfKeys().should.equal(0);
d.insert({ a: 2, z: 'yes' }, function (err, newDoc) {
d.indexes.zzz.tree.getNumberOfKeys().should.equal(1);
assert.deepEqual(d.indexes.zzz.getMatching(undefined), [newDoc]);
d.insert({ a: 5, z: 'other' }, function (err) {
err.errorType.should.equal('uniqueViolated');
assert.isUndefined(err.key);
d.ensureIndex({ fieldName: 'yyy', unique: true, sparse: true });
d.insert({ a: 5, z: 'other', zzz: 'set' }, function (err) {
assert.isNull(err);
d.indexes.yyy.getAll().length.should.equal(0); // Nothing indexed
d.indexes.zzz.getAll().length.should.equal(2);
done();
});
});
});
});
it('Insertion still works as before with indexing', function (done) { it('Insertion still works as before with indexing', function (done) {
d.ensureIndex({ fieldName: 'a' }); d.ensureIndex({ fieldName: 'a' });
d.ensureIndex({ fieldName: 'b' }); d.ensureIndex({ fieldName: 'b' });

@ -7,7 +7,7 @@ var Index = require('../lib/indexes')
, model = require('../lib/model') , model = require('../lib/model')
; ;
describe.only('Indexes', function () { describe('Indexes', function () {
describe('Insertion', function () { describe('Insertion', function () {

Loading…
Cancel
Save