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; var filter = null;
if (where) { if (where) {
filter = applyFilter({where: where}); filter = applyFilter({where: where});
}
Object.keys(cache).forEach(function (id) { Object.keys(cache).forEach(function (id) {
if (!filter || filter(this.fromDb(model, cache[id]))) { if (!filter || filter(this.fromDb(model, cache[id]))) {
delete cache[id]; delete cache[id];
} }
}.bind(this)); }.bind(this));
if (!where) { } else {
this.collection(model, {}); this.collection(model, {});
} }
this.saveToFile(null, callback); 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) { DataAccessObject._forDB = function (data) {
if (!(this.getDataSource().isRelational && this.getDataSource().isRelational())) { if (!(this.getDataSource().isRelational && this.getDataSource().isRelational())) {
return data; return data;
@ -78,10 +86,10 @@ DataAccessObject.applyProperties = function(data) {
} }
}; };
DataAccessObject.applyScope = function(cond) { DataAccessObject.applyScope = function(query) {
var scope = this.definition.settings.scope; var scope = this.definition.settings.scope;
if (typeof scope === 'object') { 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 (stillConnecting(this.getDataSource(), this, arguments)) return;
if (id !== undefined && id !== null && id !== '') { 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 { } else {
cb(new Error('Model::exists requires the id argument')); cb(new Error('Model::exists requires the id argument'));
} }
@ -716,6 +726,7 @@ DataAccessObject.find = function find(query, cb) {
} else if (query.where) { } else if (query.where) {
// do in memory query // do in memory query
// using all documents // using all documents
// TODO [fabien] use default scope here?
this.getDataSource().connector.all(this.modelName, {}, function (err, data) { this.getDataSource().connector.all(this.modelName, {}, function (err, data) {
var memory = new Memory(); var memory = new Memory();
var modelName = self.modelName; var modelName = self.modelName;
@ -838,7 +849,12 @@ DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyA
cb = where; cb = where;
where = undefined; where = undefined;
} }
if (!where) {
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) { this.getDataSource().connector.destroyAll(this.modelName, function (err, data) {
cb && cb(err, data); cb && cb(err, data);
if(!err) Model.emit('deletedAll'); if(!err) Model.emit('deletedAll');
@ -858,7 +874,7 @@ DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyA
if(!err) Model.emit('deletedAll', where); if(!err) Model.emit('deletedAll', where);
}.bind(this)); }.bind(this));
} }
}; };
/** /**
* Delete the record with the specified ID. * Delete the record with the specified ID.
@ -874,13 +890,13 @@ DataAccessObject.removeById = DataAccessObject.destroyById = DataAccessObject.de
if (stillConnecting(this.getDataSource(), this, arguments)) return; if (stillConnecting(this.getDataSource(), this, arguments)) return;
var Model = this; var Model = this;
this.getDataSource().connector.destroy(this.modelName, id, function (err) { this.remove(byIdQuery(this, id).where, function(err) {
if ('function' === typeof cb) { if ('function' === typeof cb) {
cb(err); cb(err);
} }
if(!err) Model.emit('deleted', id); if(!err) Model.emit('deleted', id);
}.bind(this)); });
}; };
/** /**
* Return count of matched records. Optional query parameter allows you to count filtered set of model instances. * 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; cb = where;
where = null; where = null;
} }
var query = { where: where };
this.applyScope(query);
where = query.where;
try { try {
where = removeUndefined(where); where = removeUndefined(where);
where = this._coerce(where); where = this._coerce(where);
@ -910,6 +931,7 @@ DataAccessObject.count = function (where, cb) {
cb && cb(err); cb && cb(err);
}); });
} }
this.getDataSource().connector.count(this.modelName, cb, where); 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(typeof data === 'object', 'The data argument should be an object');
assert(cb === null || typeof cb === 'function', 'The cb argument should be a function'); 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 { try {
where = removeUndefined(where); where = removeUndefined(where);
where = this._coerce(where); where = this._coerce(where);
@ -1044,8 +1072,6 @@ DataAccessObject.updateAll = function (where, data, cb) {
}); });
} }
this.applyProperties(data);
var connector = this.getDataSource().connector; var connector = this.getDataSource().connector;
connector.update(this.modelName, where, data, cb); 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 // This test requires a connector that can
// handle a custom collection or table name // 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 () { describe('default scope', function () {
before(function (done) { before(function (done) {
@ -15,18 +52,22 @@ describe('default scope', function () {
Product = db.define('Product', { Product = db.define('Product', {
name: String, name: String,
kind: String, kind: String,
description: String description: String,
active: { type: Boolean, default: true }
}, { }, {
scope: { order: 'name' }, scope: { order: 'name' },
scopes: { active: { where: { active: true } } }
}); });
Tool = db.define('Tool', { Tool = db.define('Tool', {
name: String, name: String,
kind: String, kind: String,
description: String description: String,
active: { type: Boolean, default: true }
}, { }, {
base: 'Product', base: 'Product',
scope: { where: { kind: 'tool' }, order: 'name' }, scope: { where: { kind: 'tool' }, order: 'name' },
scopes: { active: { where: { active: true } } },
mongodb: { collection: 'Product' }, mongodb: { collection: 'Product' },
memory: { collection: 'Product' } memory: { collection: 'Product' }
}); });
@ -34,10 +75,12 @@ describe('default scope', function () {
Widget = db.define('Widget', { Widget = db.define('Widget', {
name: String, name: String,
kind: String, kind: String,
description: String description: String,
active: { type: Boolean, default: true }
}, { }, {
base: 'Product', base: 'Product',
scope: { where: { kind: 'widget' }, order: 'name' }, scope: { where: { kind: 'widget' }, order: 'name' },
scopes: { active: { where: { active: true } } },
mongodb: { collection: 'Product' }, mongodb: { collection: 'Product' },
memory: { collection: 'Product' } memory: { collection: 'Product' }
}); });
@ -126,49 +169,57 @@ describe('default scope', function () {
}); });
describe('queries', function() { describe('findById', function() {
var ids = {}; var ids = {};
before(function (done) { before(function (done) {
db.automigrate(function(err) { db.automigrate(setupProducts.bind(null, ids, done));
async.series([
function(next) {
Tool.create({name: 'Tool Z'}, function(err, inst) {
ids.toolZ = inst.id;
next();
}); });
},
function(next) { it('should apply default scope', function(done) {
Widget.create({name: 'Widget Z'}, function(err, inst) { Product.findById(ids.toolA, function(err, inst) {
ids.widgetZ = inst.id; should.not.exist(err);
next(); inst.name.should.equal('Tool A');
done();
}); });
},
function(next) {
Tool.create({name: 'Tool A'}, function(err, inst) {
ids.toolA = inst.id;
next();
}); });
},
function(next) { it('should apply default scope - tool', function(done) {
Widget.create({name: 'Widget A'}, function(err, inst) { Tool.findById(ids.toolA, function(err, inst) {
ids.widgetA = inst.id; should.not.exist(err);
next(); inst.name.should.equal('Tool A');
done();
}); });
}
], 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) { it('should apply default scope - order', function(done) {
Product.find(function(err, products) { Product.find(function(err, products) {
should.not.exist(err); should.not.exist(err);
products.should.have.length(4); products.should.have.length(5);
products[0].name.should.equal('Tool A'); products[0].name.should.equal('Tool A');
products[1].name.should.equal('Tool Z'); products[1].name.should.equal('Tool Z');
products[2].name.should.equal('Widget A'); 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(); done();
}); });
}); });
@ -176,16 +227,17 @@ describe('default scope', function () {
it('should apply default scope - order override', function(done) { it('should apply default scope - order override', function(done) {
Product.find({ order: 'name DESC' }, function(err, products) { Product.find({ order: 'name DESC' }, function(err, products) {
should.not.exist(err); should.not.exist(err);
products.should.have.length(4); products.should.have.length(5);
products[0].name.should.equal('Widget Z'); products[0].name.should.equal('Widget Z');
products[1].name.should.equal('Widget A'); products[1].name.should.equal('Widget B');
products[2].name.should.equal('Tool Z'); products[2].name.should.equal('Widget A');
products[3].name.should.equal('Tool A'); products[3].name.should.equal('Tool Z');
products[4].name.should.equal('Tool A');
done(); done();
}); });
}); });
it('should apply default scope - where + order (tool)', function(done) { it('should apply default scope - tool', function(done) {
Tool.find(function(err, products) { Tool.find(function(err, products) {
should.not.exist(err); should.not.exist(err);
products.should.have.length(2); products.should.have.length(2);
@ -195,12 +247,344 @@ describe('default scope', function () {
}); });
}); });
it('should apply default scope - where + order (widget)', function(done) { it('should apply default scope - where (widget)', function(done) {
Widget.find({ order: 'name DESC' }, function(err, products) { Widget.find({ where: { active: true } }, function(err, products) {
should.not.exist(err); should.not.exist(err);
products.should.have.length(2); 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[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[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(); done();
}); });
}); });