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
|
2016-08-22 19:55:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-12-05 14:14:09 +00:00
|
|
|
/* global getSchema:false */
|
2018-12-07 14:54:29 +00:00
|
|
|
const should = require('./init.js');
|
2013-04-06 10:57:12 +00:00
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const db = getSchema();
|
2013-03-24 13:37:13 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
describe('defaults', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
let Server;
|
2013-03-24 13:37:13 +00:00
|
|
|
|
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-08-19 17:46:59 +00:00
|
|
|
port: {type: Number, default: 80},
|
|
|
|
createdAt: {type: Date, default: '$now'},
|
2013-03-24 13:37:13 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-24 13:37:13 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should apply defaults on new', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const s = new Server;
|
2014-01-24 17:09:53 +00:00
|
|
|
s.port.should.equal(80);
|
|
|
|
});
|
2013-03-24 13:37:13 +00:00
|
|
|
|
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();
|
2013-03-24 13:37:13 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2013-03-24 13:37:13 +00:00
|
|
|
|
2019-04-01 11:18:05 +00:00
|
|
|
it('should NOT 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) {
|
2019-04-01 11:18:05 +00:00
|
|
|
should(servers[0].host).be.undefined();
|
2014-01-24 17:09:53 +00:00
|
|
|
done();
|
2013-03-24 13:37:13 +00:00
|
|
|
});
|
2014-01-24 17:09:53 +00:00
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should ignore defaults with limited fields', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Server.create({host: 'localhost', port: 8080}, function(err, s) {
|
2014-10-19 16:47:27 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
s.port.should.equal(8080);
|
2019-04-01 11:18:05 +00:00
|
|
|
Server.findById(s.id, {fields: ['host']}, function(err, server) {
|
|
|
|
server.should.have.property('host', 'localhost');
|
|
|
|
server.should.have.property('port', undefined);
|
2014-10-19 16:47:27 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-02-03 10:37:43 +00:00
|
|
|
|
2016-04-01 11:48:17 +00:00
|
|
|
it('should apply defaults in upsert create', function(done) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Server.upsert({port: 8181}, function(err, server) {
|
2015-06-18 22:19:45 +00:00
|
|
|
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) {
|
2015-06-18 22:19:45 +00:00
|
|
|
Server.findOne({}, function(err, server) {
|
2016-08-19 17:46:59 +00:00
|
|
|
Server.upsert({id: server.id, port: 1337}, function(err, s) {
|
2015-06-18 22:19:45 +00:00
|
|
|
should.not.exist(err);
|
|
|
|
(Number(1337)).should.equal(s.port);
|
|
|
|
server.createdAt.should.eql(s.createdAt);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-03-24 13:37:13 +00:00
|
|
|
});
|