Tests actually written

pull/2/head
Louis Chatriot 11 years ago
parent 8852e93974
commit 313098a63c
  1. 55
      lib/datastore.js
  2. 2
      lib/model.js
  3. 50
      test/db.test.js

@ -493,61 +493,6 @@ db.insert({ tag: ['a', 'b', 'c'] }, function () {
});
*/
/*
var db = new Datastore({ inMemoryOnly: true });
db.ensureIndex({ fieldName: 'a', unique: true });
db.insert({ a: 4 }, function () {
db.insert({ a: 5 }, function () {
db.insert({ a: 6 }, function () {
db.find({}, function (err, docs) {
console.log(docs);
db.update({}, { $set: { a: 42 } }, { multi: true }, function (err) {
console.log(err);
db.find({}, function (err, docs) {
// FAIL
// The first document was modified and the modification was not roll back when the
// unique constraint was triggered on the second modif
console.log(docs);
});
});
});
});
});
});
*/
var db = new Datastore({ inMemoryOnly: true });
db.insert({ a: [4] }, function () {
db.insert({ a: [5] }, function () {
db.insert({ a: 6 }, function () {
db.find({}, function (err, docs) {
console.log(docs);
db.update({}, { $push: { a: 42 } }, { multi: true }, function (err) {
console.log(err);
db.find({}, function (err, docs) {
console.log(docs);
});
});
});
});
});
});
module.exports = Datastore;

@ -160,7 +160,7 @@ function compareArrays (a, b) {
* Compare { things U undefined }
* Things are defined as any native types (string, number, boolean, null, date) and objects
* We need to compare with undefined as it will be used in indexes
* In the case of objects and arrays, we compare the serialized versions
* In the case of objects and arrays, we deep-compare
* If two objects dont have the same type, the (arbitrary) type hierarchy is: undefined, null, number, strings, boolean, dates, arrays, objects
* Return -1 if a < b, 1 if a > b and 0 if a = b (note that equality here is NOT the same as defined in areThingsEqual!)
*/

@ -569,7 +569,7 @@ describe('Database', function () {
});
describe.only('Update', function () {
describe('Update', function () {
it("If the query doesn't match anything, database is not modified", function (done) {
async.waterfall([
@ -977,14 +977,56 @@ describe('Database', function () {
});
it('If a multi update fails on one document, previous updates should be rolled back', function (done) {
throw 'TODO';
d.ensureIndex({ fieldName: 'a' });
d.insert({ a: 4 }, function (err, doc1) {
d.insert({ a: 5 }, function (err, doc2) {
d.insert({ a: 'abc' }, function (err, doc3) {
// With this query, candidates are always returned in the order 4, 5, 'abc' so it's always the last one which fails
d.update({ a: { $in: [4, 5, 'abc'] } }, { $inc: { a: 10 } }, { multi: true }, function (err) {
assert.isDefined(err);
d.find({}, function (err, docs) {
var d1 = _.find(docs, function (doc) { return doc._id === doc1._id })
, d2 = _.find(docs, function (doc) { return doc._id === doc2._id })
, d3 = _.find(docs, function (doc) { return doc._id === doc3._id })
;
// All changes rollbacked, including those that didn't trigger an error
d1.a.should.equal(4);
d2.a.should.equal(5);
d3.a.should.equal('abc');
done();
});
});
});
});
});
});
it.only('If an index constraint is violated by an update, all changes should be rolled back', function (done) {
d.ensureIndex({ fieldName: 'a', unique: true });
d.insert({ a: 4 }, function (err, doc1) {
d.insert({ a: 5 }, function (err, doc2) {
// With this query, candidates are always returned in the order 4, 5, 'abc' so it's always the last one which fails
d.update({ a: { $in: [4, 5, 'abc'] } }, { $set: { a: 10 } }, { multi: true }, function (err) {
assert.isDefined(err);
d.find({}, function (err, docs) {
var d1 = _.find(docs, function (doc) { return doc._id === doc1._id })
, d2 = _.find(docs, function (doc) { return doc._id === doc2._id })
;
// All changes rollbacked, including those that didn't trigger an error
d1.a.should.equal(4);
d2.a.should.equal(5);
it('If an index constraint is violated by an update, all changes should be rolled back', function (done) {
throw 'TODO';
done();
});
});
});
});
});
}); // ==== End of 'Update' ==== //

Loading…
Cancel
Save