|
|
|
@ -6,7 +6,6 @@ |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
var assert = chai.assert; |
|
|
|
|
chai.should(); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Given a docs array and an id, return the document whose id matches, or null if none is found |
|
|
|
@ -21,8 +20,8 @@ describe('Basic CRUD functionality', function () { |
|
|
|
|
it('Able to create a database object in the browser', function () { |
|
|
|
|
var db = new Nedb(); |
|
|
|
|
|
|
|
|
|
db.inMemoryOnly.should.equal(true); |
|
|
|
|
db.persistence.inMemoryOnly.should.equal(true); |
|
|
|
|
assert.equal(db.inMemoryOnly, true); |
|
|
|
|
assert.equal(db.persistence.inMemoryOnly, true); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it('Insertion and querying', function (done) { |
|
|
|
@ -41,14 +40,14 @@ describe('Basic CRUD functionality', function () { |
|
|
|
|
; |
|
|
|
|
|
|
|
|
|
assert.isNull(err); |
|
|
|
|
docs.length.should.equal(2); |
|
|
|
|
doc2.a.should.equal(40); |
|
|
|
|
doc3.a.should.equal(400); |
|
|
|
|
assert.equal(docs.length, 2); |
|
|
|
|
assert.equal(doc2.a, 40); |
|
|
|
|
assert.equal(doc3.a, 400); |
|
|
|
|
|
|
|
|
|
db.find({ a: { $lt: 36 } }, function (err, docs) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
docs.length.should.equal(1); |
|
|
|
|
docs[0].a.should.equal(4); |
|
|
|
|
assert.equal(docs.length, 1); |
|
|
|
|
assert.equal(docs[0].a, 4); |
|
|
|
|
done(); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
@ -73,17 +72,17 @@ describe('Basic CRUD functionality', function () { |
|
|
|
|
|
|
|
|
|
db.find({ planet: /ar/ }, function (err, docs) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
docs.length.should.equal(4); |
|
|
|
|
_.find(docs, function (doc) { return doc._id === newDoc1._id; }).planet.should.equal('Earth'); |
|
|
|
|
_.find(docs, function (doc) { return doc._id === newDoc2._id; }).planet.should.equal('Mars'); |
|
|
|
|
_.find(docs, function (doc) { return doc._id === newDoc4._id; }).planet.should.equal('Eaaaaaarth'); |
|
|
|
|
_.find(docs, function (doc) { return doc._id === newDoc5._id; }).planet.should.equal('Maaaars'); |
|
|
|
|
assert.equal(docs.length, 4); |
|
|
|
|
assert.equal(_.find(docs, function (doc) { return doc._id === newDoc1._id; }).planet, 'Earth'); |
|
|
|
|
assert.equal(_.find(docs, function (doc) { return doc._id === newDoc2._id; }).planet, 'Mars'); |
|
|
|
|
assert.equal(_.find(docs, function (doc) { return doc._id === newDoc4._id; }).planet, 'Eaaaaaarth'); |
|
|
|
|
assert.equal(_.find(docs, function (doc) { return doc._id === newDoc5._id; }).planet, 'Maaaars'); |
|
|
|
|
|
|
|
|
|
db.find({ planet: /aa+r/ }, function (err, docs) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
docs.length.should.equal(2); |
|
|
|
|
_.find(docs, function (doc) { return doc._id === newDoc4._id; }).planet.should.equal('Eaaaaaarth'); |
|
|
|
|
_.find(docs, function (doc) { return doc._id === newDoc5._id; }).planet.should.equal('Maaaars'); |
|
|
|
|
assert.equal(docs.length, 2); |
|
|
|
|
assert.equal(_.find(docs, function (doc) { return doc._id === newDoc4._id; }).planet, 'Eaaaaaarth'); |
|
|
|
|
assert.equal(_.find(docs, function (doc) { return doc._id === newDoc5._id; }).planet, 'Maaaars'); |
|
|
|
|
|
|
|
|
|
done(); |
|
|
|
|
}); |
|
|
|
@ -103,44 +102,44 @@ describe('Basic CRUD functionality', function () { |
|
|
|
|
// Simple update
|
|
|
|
|
db.update({ _id: newDoc2._id }, { $set: { planet: 'Saturn' } }, {}, function (err, nr) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
nr.should.equal(1); |
|
|
|
|
assert.equal(nr, 1); |
|
|
|
|
|
|
|
|
|
db.find({}, function (err, docs) { |
|
|
|
|
docs.length.should.equal(2); |
|
|
|
|
findById(docs, newDoc1._id).planet.should.equal('Eaaaaarth'); |
|
|
|
|
findById(docs, newDoc2._id).planet.should.equal('Saturn'); |
|
|
|
|
assert.equal(docs.length, 2); |
|
|
|
|
assert.equal(findById(docs, newDoc1._id).planet, 'Eaaaaarth'); |
|
|
|
|
assert.equal(findById(docs, newDoc2._id).planet, 'Saturn'); |
|
|
|
|
|
|
|
|
|
// Failing update
|
|
|
|
|
db.update({ _id: 'unknown' }, { $inc: { count: 1 } }, {}, function (err, nr) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
nr.should.equal(0); |
|
|
|
|
assert.equal(nr, 0); |
|
|
|
|
|
|
|
|
|
db.find({}, function (err, docs) { |
|
|
|
|
docs.length.should.equal(2); |
|
|
|
|
findById(docs, newDoc1._id).planet.should.equal('Eaaaaarth'); |
|
|
|
|
findById(docs, newDoc2._id).planet.should.equal('Saturn'); |
|
|
|
|
assert.equal(docs.length, 2); |
|
|
|
|
assert.equal(findById(docs, newDoc1._id).planet, 'Eaaaaarth'); |
|
|
|
|
assert.equal(findById(docs, newDoc2._id).planet, 'Saturn'); |
|
|
|
|
|
|
|
|
|
// Document replacement
|
|
|
|
|
db.update({ planet: 'Eaaaaarth' }, { planet: 'Uranus' }, { multi: false }, function (err, nr) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
nr.should.equal(1); |
|
|
|
|
assert.equal(nr, 1); |
|
|
|
|
|
|
|
|
|
db.find({}, function (err, docs) { |
|
|
|
|
docs.length.should.equal(2); |
|
|
|
|
findById(docs, newDoc1._id).planet.should.equal('Uranus'); |
|
|
|
|
findById(docs, newDoc2._id).planet.should.equal('Saturn'); |
|
|
|
|
assert.equal(docs.length, 2); |
|
|
|
|
assert.equal(findById(docs, newDoc1._id).planet, 'Uranus'); |
|
|
|
|
assert.equal(findById(docs, newDoc2._id).planet, 'Saturn'); |
|
|
|
|
|
|
|
|
|
// Multi update
|
|
|
|
|
db.update({}, { $inc: { count: 3 } }, { multi: true }, function (err, nr) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
nr.should.equal(2); |
|
|
|
|
assert.equal(nr, 2); |
|
|
|
|
|
|
|
|
|
db.find({}, function (err, docs) { |
|
|
|
|
docs.length.should.equal(2); |
|
|
|
|
findById(docs, newDoc1._id).planet.should.equal('Uranus'); |
|
|
|
|
findById(docs, newDoc1._id).count.should.equal(3); |
|
|
|
|
findById(docs, newDoc2._id).planet.should.equal('Saturn'); |
|
|
|
|
findById(docs, newDoc2._id).count.should.equal(3); |
|
|
|
|
assert.equal(docs.length, 2); |
|
|
|
|
assert.equal(findById(docs, newDoc1._id).planet, 'Uranus'); |
|
|
|
|
assert.equal(findById(docs, newDoc1._id).count, 3); |
|
|
|
|
assert.equal(findById(docs, newDoc2._id).planet, 'Saturn'); |
|
|
|
|
assert.equal(findById(docs, newDoc2._id).count, 3); |
|
|
|
|
|
|
|
|
|
done(); |
|
|
|
|
}); |
|
|
|
@ -162,14 +161,14 @@ describe('Basic CRUD functionality', function () { |
|
|
|
|
// Pushing to an array
|
|
|
|
|
db.update({}, { $push: { satellites: 'Phobos' } }, {}, function (err, nr) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
nr.should.equal(1); |
|
|
|
|
assert.equal(nr, 1); |
|
|
|
|
|
|
|
|
|
db.findOne({}, function (err, doc) { |
|
|
|
|
assert.deepEqual(doc, { planet: 'Earth', _id: newDoc1._id, satellites: ['Phobos'] }); |
|
|
|
|
|
|
|
|
|
db.update({}, { $push: { satellites: 'Deimos' } }, {}, function (err, nr) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
nr.should.equal(1); |
|
|
|
|
assert.equal(nr, 1); |
|
|
|
|
|
|
|
|
|
db.findOne({}, function (err, doc) { |
|
|
|
|
assert.deepEqual(doc, { planet: 'Earth', _id: newDoc1._id, satellites: ['Phobos', 'Deimos'] }); |
|
|
|
@ -188,14 +187,14 @@ describe('Basic CRUD functionality', function () { |
|
|
|
|
db.update({ a: 4 }, { $inc: { b: 1 } }, { upsert: true }, function (err, nr, upsert) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
// Return upserted document
|
|
|
|
|
upsert.a.should.equal(4); |
|
|
|
|
upsert.b.should.equal(1); |
|
|
|
|
nr.should.equal(1); |
|
|
|
|
assert.equal(upsert.a, 4); |
|
|
|
|
assert.equal(upsert.b, 1); |
|
|
|
|
assert.equal(nr, 1); |
|
|
|
|
|
|
|
|
|
db.find({}, function (err, docs) { |
|
|
|
|
docs.length.should.equal(1); |
|
|
|
|
docs[0].a.should.equal(4); |
|
|
|
|
docs[0].b.should.equal(1); |
|
|
|
|
assert.equal(docs.length, 1); |
|
|
|
|
assert.equal(docs[0].a, 4); |
|
|
|
|
assert.equal(docs[0].b, 1); |
|
|
|
|
|
|
|
|
|
done(); |
|
|
|
|
}); |
|
|
|
@ -212,28 +211,28 @@ describe('Basic CRUD functionality', function () { |
|
|
|
|
// Multi remove
|
|
|
|
|
db.remove({ a: { $in: [ 5, 7 ] } }, { multi: true }, function (err, nr) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
nr.should.equal(2); |
|
|
|
|
assert.equal(nr, 2); |
|
|
|
|
|
|
|
|
|
db.find({}, function (err, docs) { |
|
|
|
|
docs.length.should.equal(1); |
|
|
|
|
docs[0].a.should.equal(2); |
|
|
|
|
assert.equal(docs.length, 1); |
|
|
|
|
assert.equal(docs[0].a, 2); |
|
|
|
|
|
|
|
|
|
// Remove with no match
|
|
|
|
|
db.remove({ b: { $exists: true } }, { multi: true }, function (err, nr) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
nr.should.equal(0); |
|
|
|
|
assert.equal(nr, 0); |
|
|
|
|
|
|
|
|
|
db.find({}, function (err, docs) { |
|
|
|
|
docs.length.should.equal(1); |
|
|
|
|
docs[0].a.should.equal(2); |
|
|
|
|
assert.equal(docs.length, 1); |
|
|
|
|
assert.equal(docs[0].a, 2); |
|
|
|
|
|
|
|
|
|
// Simple remove
|
|
|
|
|
db.remove({ a: { $exists: true } }, { multi: true }, function (err, nr) { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
nr.should.equal(1); |
|
|
|
|
assert.equal(nr, 1); |
|
|
|
|
|
|
|
|
|
db.find({}, function (err, docs) { |
|
|
|
|
docs.length.should.equal(0); |
|
|
|
|
assert.equal(docs.length, 0); |
|
|
|
|
|
|
|
|
|
done(); |
|
|
|
|
}); |
|
|
|
@ -256,7 +255,7 @@ describe('Indexing', function () { |
|
|
|
|
db.insert({ a: 6 }, function () { |
|
|
|
|
db.insert({ a: 7 }, function () { |
|
|
|
|
var candidates = db.getCandidates({ a: 6 }) |
|
|
|
|
candidates.length.should.equal(3); |
|
|
|
|
assert.equal(candidates.length, 3); |
|
|
|
|
assert.isDefined(_.find(candidates, function (doc) { return doc.a === 4; })); |
|
|
|
|
assert.isDefined(_.find(candidates, function (doc) { return doc.a === 6; })); |
|
|
|
|
assert.isDefined(_.find(candidates, function (doc) { return doc.a === 7; })); |
|
|
|
@ -264,7 +263,7 @@ describe('Indexing', function () { |
|
|
|
|
db.ensureIndex({ fieldName: 'a' }); |
|
|
|
|
|
|
|
|
|
candidates = db.getCandidates({ a: 6 }) |
|
|
|
|
candidates.length.should.equal(1); |
|
|
|
|
assert.equal(candidates.length, 1); |
|
|
|
|
assert.isDefined(_.find(candidates, function (doc) { return doc.a === 6; })); |
|
|
|
|
|
|
|
|
|
done(); |
|
|
|
@ -285,7 +284,7 @@ describe('Indexing', function () { |
|
|
|
|
assert.isNull(err); |
|
|
|
|
|
|
|
|
|
db.insert({ u : 5 }, function (err) { |
|
|
|
|
err.errorType.should.equal('uniqueViolated'); |
|
|
|
|
assert.equal(err.errorType, 'uniqueViolated'); |
|
|
|
|
|
|
|
|
|
done(); |
|
|
|
|
}); |
|
|
|
|