diff --git a/lib/scope.js b/lib/scope.js index 8b90432c..c51f00fd 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -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); diff --git a/test/scope.test.js b/test/scope.test.js new file mode 100644 index 00000000..352133b1 --- /dev/null +++ b/test/scope.test.js @@ -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(); + }); + }); + }); + }); +});