add test suit for scope - dynamic function
This commit is contained in:
parent
d8ecea6111
commit
61dc239bd8
|
@ -32,7 +32,7 @@ describe('scope', function () {
|
|||
Station.destroyAll(done);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should define scope using options.scopes', function () {
|
||||
Railway.scopes.should.have.property('highSpeed');
|
||||
Railway.highSpeed.should.be.function;
|
||||
|
@ -75,7 +75,7 @@ describe('scope', function () {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
describe('scope - order', function () {
|
||||
|
@ -92,19 +92,19 @@ describe('scope - order', function () {
|
|||
beforeEach(function (done) {
|
||||
Station.destroyAll(done);
|
||||
});
|
||||
|
||||
|
||||
beforeEach(function (done) {
|
||||
Station.create({ name: 'a', order: 1 }, done);
|
||||
});
|
||||
|
||||
|
||||
beforeEach(function (done) {
|
||||
Station.create({ name: 'b', order: 2 }, done);
|
||||
});
|
||||
|
||||
|
||||
beforeEach(function (done) {
|
||||
Station.create({ name: 'c', order: 3 }, done);
|
||||
});
|
||||
|
||||
|
||||
it('should define scope with default order', function (done) {
|
||||
Station.reverse(function(err, stations) {
|
||||
stations[0].name.should.equal('c');
|
||||
|
@ -116,7 +116,7 @@ describe('scope - order', function () {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should override default scope order', function (done) {
|
||||
Station.reverse({order: 'order ASC'}, function(err, stations) {
|
||||
stations[0].name.should.equal('a');
|
||||
|
@ -128,7 +128,7 @@ describe('scope - order', function () {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
describe('scope - filtered count, updateAll and destroyAll', function () {
|
||||
|
@ -152,26 +152,26 @@ describe('scope - filtered count, updateAll and destroyAll', function () {
|
|||
beforeEach(function (done) {
|
||||
Station.destroyAll(done);
|
||||
});
|
||||
|
||||
|
||||
beforeEach(function (done) {
|
||||
Station.create({ name: 'b', order: 2, active: false }, done);
|
||||
});
|
||||
|
||||
|
||||
beforeEach(function (done) {
|
||||
Station.create({ name: 'a', order: 1 }, function(err, inst) {
|
||||
stationA = inst;
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
beforeEach(function (done) {
|
||||
Station.create({ name: 'd', order: 4, active: false }, done);
|
||||
});
|
||||
|
||||
|
||||
beforeEach(function (done) {
|
||||
Station.create({ name: 'c', order: 3 }, done);
|
||||
});
|
||||
|
||||
|
||||
it('should find all - verify', function(done) {
|
||||
Station.ordered(function(err, stations) {
|
||||
should.not.exist(err);
|
||||
|
@ -183,7 +183,7 @@ describe('scope - filtered count, updateAll and destroyAll', function () {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should find one', function(done) {
|
||||
Station.active.findOne(function(err, station) {
|
||||
should.not.exist(err);
|
||||
|
@ -191,7 +191,7 @@ describe('scope - filtered count, updateAll and destroyAll', function () {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should find one - with filter', function(done) {
|
||||
Station.active.findOne({ where: { name: 'c' } }, function(err, station) {
|
||||
should.not.exist(err);
|
||||
|
@ -199,7 +199,7 @@ describe('scope - filtered count, updateAll and destroyAll', function () {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should find by id - match', function(done) {
|
||||
Station.active.findById(stationA.id, function(err, station) {
|
||||
should.not.exist(err);
|
||||
|
@ -207,7 +207,7 @@ describe('scope - filtered count, updateAll and destroyAll', function () {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should find by id - no match', function(done) {
|
||||
Station.inactive.findById(stationA.id, function(err, station) {
|
||||
should.not.exist(err);
|
||||
|
@ -215,7 +215,7 @@ describe('scope - filtered count, updateAll and destroyAll', function () {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should count all in scope - active', function(done) {
|
||||
Station.active.count(function(err, count) {
|
||||
should.not.exist(err);
|
||||
|
@ -223,7 +223,7 @@ describe('scope - filtered count, updateAll and destroyAll', function () {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should count all in scope - inactive', function(done) {
|
||||
Station.inactive.count(function(err, count) {
|
||||
should.not.exist(err);
|
||||
|
@ -231,7 +231,7 @@ describe('scope - filtered count, updateAll and destroyAll', function () {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should count filtered - active', function(done) {
|
||||
Station.active.count({ order: { gt: 1 } }, function(err, count) {
|
||||
should.not.exist(err);
|
||||
|
@ -285,7 +285,7 @@ describe('scope - filtered count, updateAll and destroyAll', function () {
|
|||
should.not.exist(err);
|
||||
verify();
|
||||
});
|
||||
|
||||
|
||||
var verify = function() {
|
||||
Station.ordered.count(function(err, count) {
|
||||
should.not.exist(err);
|
||||
|
@ -298,19 +298,19 @@ describe('scope - filtered count, updateAll and destroyAll', function () {
|
|||
});
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
describe('scope - dynamic target class', function () {
|
||||
|
||||
|
||||
var Collection, Media, Image, Video;
|
||||
|
||||
|
||||
|
||||
|
||||
before(function () {
|
||||
db = getSchema();
|
||||
Image = db.define('Image', {name: String});
|
||||
Video = db.define('Video', {name: String});
|
||||
|
||||
|
||||
Collection = db.define('Collection', {name: String, modelName: String});
|
||||
Collection.scope('items', function() {
|
||||
return {}; // could return a scope based on `this` (receiver)
|
||||
|
@ -318,7 +318,7 @@ describe('scope - dynamic target class', function () {
|
|||
return db.models[receiver.modelName];
|
||||
} });
|
||||
});
|
||||
|
||||
|
||||
beforeEach(function (done) {
|
||||
Collection.destroyAll(function() {
|
||||
Image.destroyAll(function() {
|
||||
|
@ -326,27 +326,27 @@ describe('scope - dynamic target class', function () {
|
|||
})
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
beforeEach(function (done) {
|
||||
Collection.create({ name: 'Images', modelName: 'Image' }, done);
|
||||
});
|
||||
|
||||
|
||||
beforeEach(function (done) {
|
||||
Collection.create({ name: 'Videos', modelName: 'Video' }, done);
|
||||
});
|
||||
|
||||
|
||||
beforeEach(function (done) {
|
||||
Collection.create({ name: 'Things', modelName: 'Unknown' }, done);
|
||||
});
|
||||
|
||||
|
||||
beforeEach(function (done) {
|
||||
Image.create({ name: 'Image A' }, done);
|
||||
});
|
||||
|
||||
|
||||
beforeEach(function (done) {
|
||||
Video.create({ name: 'Video A' }, done);
|
||||
});
|
||||
|
||||
|
||||
it('should deduce modelTo at runtime - Image', function(done) {
|
||||
Collection.findOne({ where: { modelName: 'Image' } }, function(err, coll) {
|
||||
should.not.exist(err);
|
||||
|
@ -374,7 +374,7 @@ describe('scope - dynamic target class', function () {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should throw if modelTo is invalid', function(done) {
|
||||
Collection.findOne({ where: { name: 'Things' } }, function(err, coll) {
|
||||
should.not.exist(err);
|
||||
|
@ -385,5 +385,37 @@ describe('scope - dynamic target class', function () {
|
|||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
describe('scope - dynamic function', function () {
|
||||
|
||||
var Item,seed=0;
|
||||
|
||||
before(function () {
|
||||
db = getSchema();
|
||||
Item = db.define('Item', {title: Number,creator:Number});
|
||||
Item.scope('dynamicQuery', function () {
|
||||
seed++;
|
||||
return {where:{creator:seed}};
|
||||
})
|
||||
});
|
||||
|
||||
beforeEach(function (done) {
|
||||
Item.create({ title:1,creator:1 }, function () {
|
||||
Item.create({ title:2,creator:2 },done)
|
||||
})
|
||||
});
|
||||
|
||||
it('should deduce item by runtime creator', function (done) {
|
||||
Item.dynamicQuery.findOne(function (err,firstQuery) {
|
||||
should.not.exist(err);
|
||||
firstQuery.title.should.equal(1);
|
||||
Item.dynamicQuery.findOne(function (err,secondQuery) {
|
||||
should.not.exist(err);
|
||||
secondQuery.title.should.equal(2);
|
||||
done();
|
||||
})
|
||||
})
|
||||
})
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue