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

78 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-04-01 22:25:16 +00:00
// Copyright IBM Corp. 2013,2016. All Rights Reserved.
// Node module: loopback-datasource-juggler
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2013-04-06 10:57:12 +00:00
// This test written in mocha+should.js
var should = require('./init.js');
2013-03-26 00:41:00 +00:00
var db = getSchema();
2016-04-01 11:48:17 +00:00
describe('defaults', function() {
2014-01-24 17:09:53 +00:00
var Server;
2016-04-01 11:48:17 +00:00
before(function() {
2014-01-24 17:09:53 +00:00
Server = db.define('Server', {
host: String,
2016-04-01 11:48:17 +00:00
port: { type: Number, default: 80 },
createdAt: { type: Date, default: '$now' },
});
2014-01-24 17:09:53 +00:00
});
2016-04-01 11:48:17 +00:00
it('should apply defaults on new', function() {
2014-01-24 17:09:53 +00:00
var s = new Server;
s.port.should.equal(80);
});
2016-04-01 11:48:17 +00:00
it('should apply defaults on create', function(done) {
Server.create(function(err, s) {
2014-01-24 17:09:53 +00:00
s.port.should.equal(80);
done();
});
2014-01-24 17:09:53 +00:00
});
2016-04-01 11:48:17 +00:00
it('should apply defaults on read', function(done) {
2014-01-24 17:09:53 +00:00
db.defineProperty('Server', 'host', {
type: String,
2016-04-01 11:48:17 +00:00
default: 'localhost',
2014-01-24 17:09:53 +00:00
});
2016-04-01 11:48:17 +00:00
Server.all(function(err, servers) {
2014-01-24 17:09:53 +00:00
(new String('localhost')).should.equal(servers[0].host);
done();
});
2014-01-24 17:09:53 +00:00
});
2016-04-01 11:48:17 +00:00
it('should ignore defaults with limited fields', function(done) {
Server.create({ host: 'localhost', port: 8080 }, function(err, s) {
should.not.exist(err);
s.port.should.equal(8080);
2016-04-01 11:48:17 +00:00
Server.find({ fields: ['host'] }, function(err, servers) {
servers[0].host.should.equal('localhost');
servers[0].should.have.property('host');
servers[0].should.have.property('port', undefined);
done();
});
});
});
2016-04-01 11:48:17 +00:00
it('should apply defaults in upsert create', function(done) {
Server.upsert({ port: 8181 }, function(err, server) {
should.not.exist(err);
should.exist(server.createdAt);
done();
});
});
2016-04-01 11:48:17 +00:00
it('should preserve defaults in upsert update', function(done) {
Server.findOne({}, function(err, server) {
2016-04-01 13:23:42 +00:00
Server.upsert({ id: server.id, port: 1337 }, function(err, s) {
should.not.exist(err);
(Number(1337)).should.equal(s.port);
server.createdAt.should.eql(s.createdAt);
done();
});
});
});
});