UNPKG

6.03 kBJavaScriptView Raw
1// Copyright IBM Corp. 2013,2019. All Rights Reserved.
2// Node module: loopback-datasource-juggler
3// This file is licensed under the MIT License.
4// License text available at https://opensource.org/licenses/MIT
5
6// This test written in mocha+should.js
7'use strict';
8
9/* global getSchema:false */
10const should = require('./init.js');
11
12const db = getSchema();
13
14describe('defaults', function() {
15 let Server;
16
17 before(function() {
18 Server = db.define('Server', {
19 host: String,
20 port: {type: Number, default: 80},
21 createdAt: {type: Date, default: '$now'},
22 });
23 });
24
25 it('should apply defaults on new', function() {
26 const s = new Server;
27 s.port.should.equal(80);
28 });
29
30 it('should apply defaults on create', function(done) {
31 Server.create(function(err, s) {
32 s.port.should.equal(80);
33 done();
34 });
35 });
36
37 it('should NOT apply defaults on read', function(done) {
38 db.defineProperty('Server', 'host', {
39 type: String,
40 default: 'localhost',
41 });
42 Server.all(function(err, servers) {
43 should(servers[0].host).be.undefined();
44 done();
45 });
46 });
47
48 it('should ignore defaults with limited fields', function(done) {
49 Server.create({host: 'localhost', port: 8080}, function(err, s) {
50 should.not.exist(err);
51 s.port.should.equal(8080);
52 Server.findById(s.id, {fields: ['host']}, function(err, server) {
53 server.should.have.property('host', 'localhost');
54 server.should.have.property('port', undefined);
55 done();
56 });
57 });
58 });
59
60 it('should apply defaults in upsert create', function(done) {
61 Server.upsert({port: 8181}, function(err, server) {
62 should.not.exist(err);
63 should.exist(server.createdAt);
64 done();
65 });
66 });
67
68 it('should preserve defaults in upsert update', function(done) {
69 Server.findOne({}, function(err, server) {
70 Server.upsert({id: server.id, port: 1337}, function(err, s) {
71 should.not.exist(err);
72 (Number(1337)).should.equal(s.port);
73 server.createdAt.should.eql(s.createdAt);
74 done();
75 });
76 });
77 });
78
79 context('applyDefaultOnWrites', function() {
80 it('does not affect default behavior when not set', async () => {
81 const Apple = db.define('Apple', {
82 color: {type: String, default: 'red'},
83 taste: {type: String, default: 'sweet'},
84 }, {applyDefaultsOnReads: false});
85
86 const apple = await Apple.create();
87 apple.color.should.equal('red');
88 apple.taste.should.equal('sweet');
89 });
90
91 it('removes the property when set to `false`', async () => {
92 const Apple = db.define('Apple', {
93 color: {type: String, default: 'red', applyDefaultOnWrites: false},
94 taste: {type: String, default: 'sweet'},
95 }, {applyDefaultsOnReads: false});
96
97 const apple = await Apple.create({color: 'red', taste: 'sweet'});
98 const found = await Apple.findById(apple.id);
99 should(found.color).be.undefined();
100 found.taste.should.equal('sweet');
101 });
102
103 it('removes nested property in an object when set to `false`', async () => {
104 const Apple = db.define('Apple', {
105 name: {type: String},
106 qualities: {
107 color: {type: String, default: 'red', applyDefaultOnWrites: false},
108 taste: {type: String, default: 'sweet'},
109 },
110 }, {applyDefaultsOnReads: false});
111
112 const apple = await Apple.create({name: 'Honeycrisp', qualities: {taste: 'sweet'}});
113 const found = await Apple.findById(apple.id);
114 should(found.qualities.color).be.undefined();
115 found.qualities.taste.should.equal('sweet');
116 });
117
118 it('removes nested property in an array when set to `false', async () => {
119 const Apple = db.define('Apple', {
120 name: {type: String},
121 qualities: [
122 {color: {type: String, default: 'red', applyDefaultOnWrites: false}},
123 {taste: {type: String, default: 'sweet'}},
124 ],
125 }, {applyDefaultsOnReads: false});
126
127 const apple = await Apple.create({name: 'Honeycrisp', qualities: [{taste: 'sweet'}]});
128 const found = await Apple.findById(apple.id);
129 should(found.qualities[0].color).be.undefined();
130 found.qualities.length.should.equal(1);
131 });
132 });
133
134 context('persistDefaultValues', function() {
135 it('removes property if value matches default', async () => {
136 const Apple = db.define('Apple', {
137 color: {type: String, default: 'red', persistDefaultValues: false},
138 taste: {type: String, default: 'sweet'},
139 }, {applyDefaultsOnReads: false});
140
141 const apple = await Apple.create({color: 'red', taste: 'sweet'});
142 const found = await Apple.findById(apple.id);
143 should(found.color).be.undefined();
144 found.taste.should.equal('sweet');
145 });
146
147 it('removes property if value matches default in an object', async () => {
148 const Apple = db.define('Apple', {
149 name: {type: String},
150 qualities: {
151 color: {type: String, default: 'red', persistDefaultValues: false},
152 taste: {type: String, default: 'sweet'},
153 },
154 }, {applyDefaultsOnReads: false});
155
156 const apple = await Apple.create({name: 'Honeycrisp', qualities: {taste: 'sweet'}});
157 const found = await Apple.findById(apple.id);
158 should(found.qualities.color).be.undefined();
159 found.qualities.taste.should.equal('sweet');
160 });
161
162 it('removes property if value matches default in an array', async () => {
163 const Apple = db.define('Apple', {
164 name: {type: String},
165 qualities: [
166 {color: {type: String, default: 'red', persistDefaultValues: false}},
167 {taste: {type: String, default: 'sweet'}},
168 ],
169 }, {applyDefaultsOnReads: false});
170
171 const apple = await Apple.create({name: 'Honeycrisp', qualities: [{taste: 'sweet'}]});
172 const found = await Apple.findById(apple.id);
173 should(found.qualities[0].color).be.undefined();
174 found.qualities.length.should.equal(1);
175 });
176 });
177});