Some tests for scope

This commit is contained in:
Anatoliy Chakkaev 2013-04-12 03:56:51 +04:00 committed by Raymond Feng
parent 136ea91fbf
commit 9facf369b1
2 changed files with 68 additions and 0 deletions

View File

@ -59,6 +59,9 @@ function defineScope(cls, targetClass, name, params, methods) {
var self = this;
return targetClass.all(mergeParams(actualCond, caller._scope), function(err, data) {
if (!err && saveOnCache) {
if (!self.__cachedRelations) {
self.__cachedRelations = {};
}
self.__cachedRelations[name] = data;
}
cb(err, data);

65
test/scope.test.js Normal file
View File

@ -0,0 +1,65 @@
// This test written in mocha+should.js
var should = require('./init.js');
var db, Railway, Station;
describe('sc0pe', function() {
before(function() {
db = getSchema();
Railway = db.define('Railway', {
URID: {type: String, index: true}
});
Station = db.define('Station', {
USID: {type: String, index: true},
capacity: {type: Number, index: true},
thoughput: {type: Number, index: true},
isActive: {type: Boolean, index: true},
isUndeground: {type: Boolean, index: true}
});
});
beforeEach(function(done) {
Railway.destroyAll(function() {
Station.destroyAll(done);
});
});
it('should define scope with query', function(done) {
Station.scope('active', {where: {isActive: true}});
Station.active.create(function(err, station) {
should.not.exist(err);
should.exist(station);
should.exist(station.isActive);
station.isActive.should.be.true;
done();
});
});
it('should allow scope chaining', function(done) {
Station.scope('active', {where: {isActive: true}});
Station.scope('subway', {where: {isUndeground: true}});
Station.active.subway.create(function(err, station) {
should.not.exist(err);
should.exist(station);
station.isActive.should.be.true;
station.isUndeground.should.be.true;
done();
})
});
it('should query all', function(done) {
Station.scope('active', {where: {isActive: true}});
Station.scope('inactive', {where: {isActive: false}});
Station.scope('ground', {where: {isUndeground: true}});
Station.active.ground.create(function() {
Station.inactive.ground.create(function() {
Station.ground.inactive(function(err, ss) {
ss.should.have.lengthOf(1);
done();
});
});
});
});
});