Tested that we can insert document in an index automatically

pull/2/head
Louis Chatriot 12 years ago
parent 8db119479d
commit ddb7ffc74e
  1. 4
      lib/indexes.js
  2. 42
      test/db.test.js

@ -19,6 +19,10 @@ function checkValueEquality (a, b) {
* @param {Datastore} options.datastore Datastore on which the index is created
* @param {Boolean} options.unique Optional, enforce a unique constraint (default: false)
* @param {Boolean} options.sparse Optional, allow a sparse index (we can have documents for which fieldName is undefined) (default: false)
* TODO: for now the sparse option doesn't work fully
* don't use it. I will implement it in the future
* in the meantime you can use non-unique, non-sparse indexes
* for approx. the same behaviour
*/
function Index (options) {
this.fieldName = options.fieldName;

@ -1109,7 +1109,7 @@ describe('Database', function () {
}); // ==== End of 'Remove' ==== //
describe('Using indexes', function () {
describe.only('Using indexes', function () {
describe('ensureIndex', function () {
@ -1220,7 +1220,45 @@ describe('Database', function () {
});
});
});
}); // ==== End of 'ensureIndex' ==== //
describe('Indexing newly inserted documents', function () {
it('Newly inserted documents are indexed', function (done) {
d.ensureIndex({ fieldName: 'z' });
d.indexes.z.tree.getNumberOfKeys().should.equal(0);
d.insert({ a: 2, z: 'yes' }, function (err, newDoc) {
d.indexes.z.tree.getNumberOfKeys().should.equal(1);
assert.deepEqual(d.indexes.z.getMatching('yes'), [newDoc]);
d.insert({ a: 5, z: 'nope' }, function (err, newDoc) {
d.indexes.z.tree.getNumberOfKeys().should.equal(2);
assert.deepEqual(d.indexes.z.getMatching('nope'), [newDoc]);
done();
});
});
});
it('Can insert two docs at the same key for a non unique index', function (done) {
d.ensureIndex({ fieldName: 'z' });
d.indexes.z.tree.getNumberOfKeys().should.equal(0);
d.insert({ a: 2, z: 'yes' }, function (err, newDoc) {
d.indexes.z.tree.getNumberOfKeys().should.equal(1);
assert.deepEqual(d.indexes.z.getMatching('yes'), [newDoc]);
d.insert({ a: 5, z: 'yes' }, function (err, newDoc2) {
d.indexes.z.tree.getNumberOfKeys().should.equal(1);
assert.deepEqual(d.indexes.z.getMatching('yes'), [newDoc, newDoc2]);
done();
});
});
});
}); // ==== End of '' ==== //

Loading…
Cancel
Save