Skip to content
Browse files

Some tests for scope

  • Loading branch information...
1 parent 709d246 commit f4bf02a29c730b1f2786cb29ec795568211de07a @1602 committed Apr 12, 2013
Showing with 68 additions and 0 deletions.
  1. +3 −0 lib/scope.js
  2. +65 −0 test/scope.test.js
View
3 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);
View
65 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();
+ });
+ });
+ });
+ });
+});

0 comments on commit f4bf02a

Please sign in to comment.
Something went wrong with that request. Please try again.