2013-04-11 23:56:51 +00:00
|
|
|
// This test written in mocha+should.js
|
|
|
|
var should = require('./init.js');
|
|
|
|
|
|
|
|
var db, Railway, Station;
|
|
|
|
|
2014-06-19 19:00:49 +00:00
|
|
|
describe('scope', function () {
|
2013-04-12 21:35:06 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
before(function () {
|
|
|
|
db = getSchema();
|
|
|
|
Railway = db.define('Railway', {
|
|
|
|
URID: {type: String, index: true}
|
2014-08-08 16:01:24 +00:00
|
|
|
}, {
|
|
|
|
scopes: {
|
|
|
|
highSpeed: {
|
|
|
|
where: {
|
|
|
|
highSpeed: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-11 23:56:51 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
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}
|
2013-04-11 23:56:51 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-04-11 23:56:51 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
beforeEach(function (done) {
|
|
|
|
Railway.destroyAll(function () {
|
|
|
|
Station.destroyAll(done);
|
2013-04-11 23:56:51 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2014-08-08 16:01:24 +00:00
|
|
|
|
|
|
|
it('should define scope using options.scopes', function () {
|
|
|
|
Railway.scopes.should.have.property('highSpeed');
|
|
|
|
Railway.highSpeed.should.be.function;
|
|
|
|
});
|
2013-04-11 23:56:51 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
it('should define scope with query', function (done) {
|
|
|
|
Station.scope('active', {where: {isActive: true}});
|
2014-08-08 16:01:24 +00:00
|
|
|
Station.scopes.should.have.property('active');
|
2014-01-24 17:09:53 +00:00
|
|
|
Station.active.create(function (err, station) {
|
|
|
|
should.not.exist(err);
|
|
|
|
should.exist(station);
|
|
|
|
should.exist(station.isActive);
|
|
|
|
station.isActive.should.be.true;
|
|
|
|
done();
|
2013-04-11 23:56:51 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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();
|
|
|
|
})
|
|
|
|
});
|
2013-04-11 23:56:51 +00:00
|
|
|
|
2014-01-24 17:09:53 +00:00
|
|
|
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();
|
2013-04-11 23:56:51 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-04-11 23:56:51 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2014-08-26 12:54:19 +00:00
|
|
|
|
2013-04-11 23:56:51 +00:00
|
|
|
});
|
2014-08-13 14:24:11 +00:00
|
|
|
|
|
|
|
describe('scope - order', function () {
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
db = getSchema();
|
|
|
|
Station = db.define('Station', {
|
|
|
|
name: {type: String, index: true},
|
|
|
|
order: {type: Number, index: true}
|
|
|
|
});
|
|
|
|
Station.scope('reverse', {order: 'order DESC'});
|
|
|
|
});
|
|
|
|
|
|
|
|
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');
|
|
|
|
stations[0].order.should.equal(3);
|
|
|
|
stations[1].name.should.equal('b');
|
|
|
|
stations[1].order.should.equal(2);
|
|
|
|
stations[2].name.should.equal('a');
|
|
|
|
stations[2].order.should.equal(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should override default scope order', function (done) {
|
|
|
|
Station.reverse({order: 'order ASC'}, function(err, stations) {
|
|
|
|
stations[0].name.should.equal('a');
|
|
|
|
stations[0].order.should.equal(1);
|
|
|
|
stations[1].name.should.equal('b');
|
|
|
|
stations[1].order.should.equal(2);
|
|
|
|
stations[2].name.should.equal('c');
|
|
|
|
stations[2].order.should.equal(3);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-08-26 12:54:19 +00:00
|
|
|
describe('scope - filtered count and destroyAll', function () {
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
db = getSchema();
|
|
|
|
Station = db.define('Station', {
|
|
|
|
name: {type: String, index: true},
|
|
|
|
order: {type: Number, index: true},
|
|
|
|
active: {type: Boolean, index: true, default: true}
|
|
|
|
});
|
|
|
|
Station.scope('ordered', {order: 'order'});
|
|
|
|
Station.scope('active', {where: { active: true}});
|
|
|
|
Station.scope('inactive', {where: { active: false}});
|
|
|
|
});
|
|
|
|
|
|
|
|
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 }, 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);
|
|
|
|
stations.should.have.length(4);
|
|
|
|
stations[0].name.should.equal('a');
|
|
|
|
stations[1].name.should.equal('b');
|
|
|
|
stations[2].name.should.equal('c');
|
|
|
|
stations[3].name.should.equal('d');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should count all in scope - active', function(done) {
|
|
|
|
Station.active.count(function(err, count) {
|
|
|
|
should.not.exist(err);
|
|
|
|
count.should.equal(2);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should count all in scope - inactive', function(done) {
|
|
|
|
Station.inactive.count(function(err, count) {
|
|
|
|
should.not.exist(err);
|
|
|
|
count.should.equal(2);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should count filtered - active', function(done) {
|
|
|
|
Station.active.count({ order: { gt: 1 } }, function(err, count) {
|
|
|
|
should.not.exist(err);
|
|
|
|
count.should.equal(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should count filtered - inactive', function(done) {
|
|
|
|
Station.inactive.count({ order: 2 }, function(err, count) {
|
|
|
|
should.not.exist(err);
|
|
|
|
count.should.equal(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow filtered destroyAll', function(done) {
|
|
|
|
Station.ordered.destroyAll({ active: false }, function(err) {
|
|
|
|
should.not.exist(err);
|
|
|
|
verify();
|
|
|
|
});
|
|
|
|
|
|
|
|
var verify = function() {
|
|
|
|
Station.ordered.count(function(err, count) {
|
|
|
|
should.not.exist(err);
|
|
|
|
count.should.equal(2);
|
|
|
|
Station.inactive.count(function(err, count) {
|
|
|
|
should.not.exist(err);
|
|
|
|
count.should.equal(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|