loopback-datasource-juggler/test/scope.test.js

65 lines
1.8 KiB
JavaScript
Raw Normal View History

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 () {
2014-01-24 17:09:53 +00:00
before(function () {
db = getSchema();
Railway = db.define('Railway', {
URID: {type: String, index: 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
});
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}});
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
});
2013-04-11 23:56:51 +00:00
});