Full test CRUD suite for default scope

This commit is contained in:
Fabien Franzen 2014-09-06 19:24:30 +02:00
parent ad55681d69
commit b136a8fce7
3 changed files with 501 additions and 92 deletions

View File

@ -529,13 +529,12 @@ Memory.prototype.destroyAll = function destroyAll(model, where, callback) {
var filter = null;
if (where) {
filter = applyFilter({where: where});
}
Object.keys(cache).forEach(function (id) {
if (!filter || filter(this.fromDb(model, cache[id]))) {
delete cache[id];
}
}.bind(this));
if (!where) {
Object.keys(cache).forEach(function (id) {
if (!filter || filter(this.fromDb(model, cache[id]))) {
delete cache[id];
}
}.bind(this));
} else {
this.collection(model, {});
}
this.saveToFile(null, callback);

View File

@ -54,6 +54,14 @@ function setIdValue(m, data, value) {
}
}
function byIdQuery(m, id) {
var pk = idName(m);
var query = { where: {} };
query.where[pk] = id;
m.applyScope(query);
return query;
}
DataAccessObject._forDB = function (data) {
if (!(this.getDataSource().isRelational && this.getDataSource().isRelational())) {
return data;
@ -78,10 +86,10 @@ DataAccessObject.applyProperties = function(data) {
}
};
DataAccessObject.applyScope = function(cond) {
DataAccessObject.applyScope = function(query) {
var scope = this.definition.settings.scope;
if (typeof scope === 'object') {
mergeQuery(cond, scope || {}, this.definition.settings.scoping);
mergeQuery(query, scope || {}, this.definition.settings.scoping);
}
};
@ -312,7 +320,9 @@ DataAccessObject.exists = function exists(id, cb) {
if (stillConnecting(this.getDataSource(), this, arguments)) return;
if (id !== undefined && id !== null && id !== '') {
this.dataSource.connector.exists(this.modelName, id, cb);
this.count(byIdQuery(this, id).where, function(err, count) {
cb(err, err ? false : count === 1);
});
} else {
cb(new Error('Model::exists requires the id argument'));
}
@ -716,6 +726,7 @@ DataAccessObject.find = function find(query, cb) {
} else if (query.where) {
// do in memory query
// using all documents
// TODO [fabien] use default scope here?
this.getDataSource().connector.all(this.modelName, {}, function (err, data) {
var memory = new Memory();
var modelName = self.modelName;
@ -831,34 +842,39 @@ DataAccessObject.findOne = function findOne(query, cb) {
* @param {Function} [cb] Callback called with (err)
*/
DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyAll = function destroyAll(where, cb) {
if (stillConnecting(this.getDataSource(), this, arguments)) return;
var Model = this;
if (stillConnecting(this.getDataSource(), this, arguments)) return;
var Model = this;
if (!cb && 'function' === typeof where) {
cb = where;
where = undefined;
}
if (!where) {
this.getDataSource().connector.destroyAll(this.modelName, function (err, data) {
cb && cb(err, data);
if(!err) Model.emit('deletedAll');
}.bind(this));
} else {
try {
// Support an optional where object
where = removeUndefined(where);
where = this._coerce(where);
} catch (err) {
return process.nextTick(function() {
cb && cb(err);
});
}
this.getDataSource().connector.destroyAll(this.modelName, where, function (err, data) {
cb && cb(err, data);
if(!err) Model.emit('deletedAll', where);
}.bind(this));
}
};
if (!cb && 'function' === typeof where) {
cb = where;
where = undefined;
}
var query = { where: where };
this.applyScope(query);
where = query.where;
if (!where || (typeof where === 'object' && Object.keys(where).length === 0)) {
this.getDataSource().connector.destroyAll(this.modelName, function (err, data) {
cb && cb(err, data);
if(!err) Model.emit('deletedAll');
}.bind(this));
} else {
try {
// Support an optional where object
where = removeUndefined(where);
where = this._coerce(where);
} catch (err) {
return process.nextTick(function() {
cb && cb(err);
});
}
this.getDataSource().connector.destroyAll(this.modelName, where, function (err, data) {
cb && cb(err, data);
if(!err) Model.emit('deletedAll', where);
}.bind(this));
}
};
/**
* Delete the record with the specified ID.
@ -871,16 +887,16 @@ DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyA
// 'deleteById' will be used as the name for strong-remoting to keep it backward
// compatible for angular SDK
DataAccessObject.removeById = DataAccessObject.destroyById = DataAccessObject.deleteById = function deleteById(id, cb) {
if (stillConnecting(this.getDataSource(), this, arguments)) return;
var Model = this;
if (stillConnecting(this.getDataSource(), this, arguments)) return;
var Model = this;
this.getDataSource().connector.destroy(this.modelName, id, function (err) {
if ('function' === typeof cb) {
cb(err);
}
if(!err) Model.emit('deleted', id);
}.bind(this));
};
this.remove(byIdQuery(this, id).where, function(err) {
if ('function' === typeof cb) {
cb(err);
}
if(!err) Model.emit('deleted', id);
});
};
/**
* Return count of matched records. Optional query parameter allows you to count filtered set of model instances.
@ -902,6 +918,11 @@ DataAccessObject.count = function (where, cb) {
cb = where;
where = null;
}
var query = { where: where };
this.applyScope(query);
where = query.where;
try {
where = removeUndefined(where);
where = this._coerce(where);
@ -910,6 +931,7 @@ DataAccessObject.count = function (where, cb) {
cb && cb(err);
});
}
this.getDataSource().connector.count(this.modelName, cb, where);
};
@ -1035,6 +1057,12 @@ DataAccessObject.updateAll = function (where, data, cb) {
assert(typeof data === 'object', 'The data argument should be an object');
assert(cb === null || typeof cb === 'function', 'The cb argument should be a function');
var query = { where: where };
this.applyScope(query);
this.applyProperties(data);
where = query.where;
try {
where = removeUndefined(where);
where = this._coerce(where);
@ -1044,8 +1072,6 @@ DataAccessObject.updateAll = function (where, data, cb) {
});
}
this.applyProperties(data);
var connector = this.getDataSource().connector;
connector.update(this.modelName, where, data, cb);
};

View File

@ -7,6 +7,43 @@ var db, Product, Tool, Widget;
// This test requires a connector that can
// handle a custom collection or table name
// TODO [fabien] add table for pgsql/mysql
var setupProducts = function(ids, done) {
async.series([
function(next) {
Tool.create({name: 'Tool Z'}, function(err, inst) {
ids.toolZ = inst.id;
next();
});
},
function(next) {
Widget.create({name: 'Widget Z'}, function(err, inst) {
ids.widgetZ = inst.id;
next();
});
},
function(next) {
Tool.create({name: 'Tool A', active: false}, function(err, inst) {
ids.toolA = inst.id;
next();
});
},
function(next) {
Widget.create({name: 'Widget A'}, function(err, inst) {
ids.widgetA = inst.id;
next();
});
},
function(next) {
Widget.create({name: 'Widget B', active: false}, function(err, inst) {
ids.widgetB = inst.id;
next();
});
}
], done);
};
describe('default scope', function () {
before(function (done) {
@ -15,18 +52,22 @@ describe('default scope', function () {
Product = db.define('Product', {
name: String,
kind: String,
description: String
description: String,
active: { type: Boolean, default: true }
}, {
scope: { order: 'name' },
scopes: { active: { where: { active: true } } }
});
Tool = db.define('Tool', {
name: String,
kind: String,
description: String
description: String,
active: { type: Boolean, default: true }
}, {
base: 'Product',
scope: { where: { kind: 'tool' }, order: 'name' },
scopes: { active: { where: { active: true } } },
mongodb: { collection: 'Product' },
memory: { collection: 'Product' }
});
@ -34,10 +75,12 @@ describe('default scope', function () {
Widget = db.define('Widget', {
name: String,
kind: String,
description: String
description: String,
active: { type: Boolean, default: true }
}, {
base: 'Product',
scope: { where: { kind: 'widget' }, order: 'name' },
scopes: { active: { where: { active: true } } },
mongodb: { collection: 'Product' },
memory: { collection: 'Product' }
});
@ -126,49 +169,57 @@ describe('default scope', function () {
});
describe('queries', function() {
describe('findById', function() {
var ids = {};
before(function (done) {
db.automigrate(function(err) {
async.series([
function(next) {
Tool.create({name: 'Tool Z'}, function(err, inst) {
ids.toolZ = inst.id;
next();
});
},
function(next) {
Widget.create({name: 'Widget Z'}, function(err, inst) {
ids.widgetZ = inst.id;
next();
});
},
function(next) {
Tool.create({name: 'Tool A'}, function(err, inst) {
ids.toolA = inst.id;
next();
});
},
function(next) {
Widget.create({name: 'Widget A'}, function(err, inst) {
ids.widgetA = inst.id;
next();
});
}
], done);
db.automigrate(setupProducts.bind(null, ids, done));
});
it('should apply default scope', function(done) {
Product.findById(ids.toolA, function(err, inst) {
should.not.exist(err);
inst.name.should.equal('Tool A');
done();
});
});
it('should apply default scope - tool', function(done) {
Tool.findById(ids.toolA, function(err, inst) {
should.not.exist(err);
inst.name.should.equal('Tool A');
done();
});
});
it('should apply default scope (no match)', function(done) {
Widget.findById(ids.toolA, function(err, inst) {
should.not.exist(err);
should.not.exist(inst);
done();
});
});
});
describe('find', function() {
var ids = {};
before(function (done) {
db.automigrate(setupProducts.bind(null, ids, done));
});
it('should apply default scope - order', function(done) {
Product.find(function(err, products) {
should.not.exist(err);
products.should.have.length(4);
products.should.have.length(5);
products[0].name.should.equal('Tool A');
products[1].name.should.equal('Tool Z');
products[2].name.should.equal('Widget A');
products[3].name.should.equal('Widget Z');
products[3].name.should.equal('Widget B');
products[4].name.should.equal('Widget Z');
done();
});
});
@ -176,16 +227,17 @@ describe('default scope', function () {
it('should apply default scope - order override', function(done) {
Product.find({ order: 'name DESC' }, function(err, products) {
should.not.exist(err);
products.should.have.length(4);
products.should.have.length(5);
products[0].name.should.equal('Widget Z');
products[1].name.should.equal('Widget A');
products[2].name.should.equal('Tool Z');
products[3].name.should.equal('Tool A');
products[1].name.should.equal('Widget B');
products[2].name.should.equal('Widget A');
products[3].name.should.equal('Tool Z');
products[4].name.should.equal('Tool A');
done();
});
});
it('should apply default scope - where + order (tool)', function(done) {
it('should apply default scope - tool', function(done) {
Tool.find(function(err, products) {
should.not.exist(err);
products.should.have.length(2);
@ -195,12 +247,344 @@ describe('default scope', function () {
});
});
it('should apply default scope - where + order (widget)', function(done) {
Widget.find({ order: 'name DESC' }, function(err, products) {
it('should apply default scope - where (widget)', function(done) {
Widget.find({ where: { active: true } }, function(err, products) {
should.not.exist(err);
products.should.have.length(2);
products[0].name.should.equal('Widget A');
products[1].name.should.equal('Widget Z');
done();
});
});
it('should apply default scope - order (widget)', function(done) {
Widget.find({ order: 'name DESC' }, function(err, products) {
should.not.exist(err);
products.should.have.length(3);
products[0].name.should.equal('Widget Z');
products[1].name.should.equal('Widget B');
products[2].name.should.equal('Widget A');
done();
});
});
});
describe('exists', function() {
var ids = {};
before(function (done) {
db.automigrate(setupProducts.bind(null, ids, done));
});
it('should apply default scope', function(done) {
Product.exists(ids.widgetA, function(err, exists) {
should.not.exist(err);
exists.should.be.true;
done();
});
});
it('should apply default scope - tool', function(done) {
Tool.exists(ids.toolZ, function(err, exists) {
should.not.exist(err);
exists.should.be.true;
done();
});
});
it('should apply default scope - widget', function(done) {
Widget.exists(ids.widgetA, function(err, exists) {
should.not.exist(err);
exists.should.be.true;
done();
});
});
it('should apply default scope - tool (no match)', function(done) {
Tool.exists(ids.widgetA, function(err, exists) {
should.not.exist(err);
exists.should.be.false;
done();
});
});
it('should apply default scope - widget (no match)', function(done) {
Widget.exists(ids.toolZ, function(err, exists) {
should.not.exist(err);
exists.should.be.false;
done();
});
});
});
describe('count', function() {
var ids = {};
before(function (done) {
db.automigrate(setupProducts.bind(null, ids, done));
});
it('should apply default scope - order', function(done) {
Product.count(function(err, count) {
should.not.exist(err);
count.should.equal(5);
done();
});
});
it('should apply default scope - tool', function(done) {
Tool.count(function(err, count) {
should.not.exist(err);
count.should.equal(2);
done();
});
});
it('should apply default scope - widget', function(done) {
Widget.count(function(err, count) {
should.not.exist(err);
count.should.equal(3);
done();
});
});
it('should apply default scope - where', function(done) {
Widget.count({name: 'Widget Z'}, function(err, count) {
should.not.exist(err);
count.should.equal(1);
done();
});
});
it('should apply default scope - no match', function(done) {
Tool.count({name: 'Widget Z'}, function(err, count) {
should.not.exist(err);
count.should.equal(0);
done();
});
});
});
describe('removeById', function() {
var ids = {};
function isDeleted(id, done) {
Product.exists(id, function(err, exists) {
should.not.exist(err);
exists.should.be.false;
done();
});
};
before(function (done) {
db.automigrate(setupProducts.bind(null, ids, done));
});
it('should apply default scope', function(done) {
Product.removeById(ids.widgetZ, function(err) {
should.not.exist(err);
isDeleted(ids.widgetZ, done);
});
});
it('should apply default scope - tool', function(done) {
Tool.removeById(ids.toolA, function(err) {
should.not.exist(err);
isDeleted(ids.toolA, done);
});
});
it('should apply default scope - no match', function(done) {
Tool.removeById(ids.widgetA, function(err) {
should.not.exist(err);
Product.exists(ids.widgetA, function(err, exists) {
should.not.exist(err);
exists.should.be.true;
done();
});
});
});
it('should apply default scope - widget', function(done) {
Widget.removeById(ids.widgetA, function(err) {
should.not.exist(err);
isDeleted(ids.widgetA, done);
});
});
it('should apply default scope - verify', function(done) {
Product.find(function(err, products) {
should.not.exist(err);
products.should.have.length(2);
products[0].name.should.equal('Tool Z');
products[1].name.should.equal('Widget B');
done();
});
});
});
describe('update', function() {
var ids = {};
before(function (done) {
db.automigrate(setupProducts.bind(null, ids, done));
});
it('should apply default scope', function(done) {
Widget.update({active: false},{active: true, kind: 'ignored'}, function(err) {
should.not.exist(err);
Widget.find({where: { active: true }}, function(err, products) {
should.not.exist(err);
products.should.have.length(3);
products[0].name.should.equal('Widget A');
products[1].name.should.equal('Widget B');
products[2].name.should.equal('Widget Z');
done();
});
});
});
it('should apply default scope - no match', function(done) {
Tool.update({name: 'Widget A'},{name: 'Ignored'}, function(err) {
should.not.exist(err);
Product.findById(ids.widgetA, function(err, product) {
should.not.exist(err);
product.name.should.equal('Widget A');
done();
});
});
});
it('should have updated within scope', function(done) {
Product.find({where: {active: true}}, function(err, products) {
should.not.exist(err);
products.should.have.length(4);
products[0].name.should.equal('Tool Z');
products[1].name.should.equal('Widget A');
products[2].name.should.equal('Widget B');
products[3].name.should.equal('Widget Z');
done();
});
});
});
describe('remove', function() {
var ids = {};
before(function (done) {
db.automigrate(setupProducts.bind(null, ids, done));
});
it('should apply default scope - custom where', function(done) {
Widget.remove({name: 'Widget A'}, function(err) {
should.not.exist(err);
Product.find(function(err, products) {
products.should.have.length(4);
products[0].name.should.equal('Tool A');
products[1].name.should.equal('Tool Z');
products[2].name.should.equal('Widget B');
products[3].name.should.equal('Widget Z');
done();
});
});
});
it('should apply default scope - custom where (no match)', function(done) {
Tool.remove({name: 'Widget Z'}, function(err) {
should.not.exist(err);
Product.find(function(err, products) {
products.should.have.length(4);
products[0].name.should.equal('Tool A');
products[1].name.should.equal('Tool Z');
products[2].name.should.equal('Widget B');
products[3].name.should.equal('Widget Z');
done();
});
});
});
it('should apply default scope - deleteAll', function(done) {
Tool.deleteAll(function(err) {
should.not.exist(err);
Product.find(function(err, products) {
products.should.have.length(2);
products[0].name.should.equal('Widget B');
products[1].name.should.equal('Widget Z');
done();
});
});
});
it('should create a scoped instance - tool', function(done) {
Tool.create({name: 'Tool B'}, function(err, p) {
should.not.exist(err);
Product.find(function(err, products) {
products.should.have.length(3);
products[0].name.should.equal('Tool B');
products[1].name.should.equal('Widget B');
products[2].name.should.equal('Widget Z');
done();
});
});
});
it('should apply default scope - destroyAll', function(done) {
Widget.destroyAll(function(err) {
should.not.exist(err);
Product.find(function(err, products) {
products.should.have.length(1);
products[0].name.should.equal('Tool B');
done();
});
});
});
});
describe('scopes', function() {
var ids = {};
before(function (done) {
db.automigrate(setupProducts.bind(null, ids, done));
});
it('should merge with default scope', function(done) {
Product.active(function(err, products) {
should.not.exist(err);
products.should.have.length(3);
products[0].name.should.equal('Tool Z');
products[1].name.should.equal('Widget A');
products[2].name.should.equal('Widget Z');
done();
});
});
it('should merge with default scope - tool', function(done) {
Tool.active(function(err, products) {
should.not.exist(err);
products.should.have.length(1);
products[0].name.should.equal('Tool Z');
done();
});
});
it('should merge with default scope - widget', function(done) {
Widget.active(function(err, products) {
should.not.exist(err);
products.should.have.length(2);
products[0].name.should.equal('Widget A');
products[1].name.should.equal('Widget Z');
done();
});
});