UNPKG

8.13 kBJavaScriptView Raw
1// Copyright IBM Corp. 2017,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'use strict';
7const jdb = require('../');
8const DataSource = jdb.DataSource;
9const path = require('path');
10const fs = require('fs');
11const assert = require('assert');
12const async = require('async');
13const should = require('./init.js');
14const Memory = require('../lib/connectors/memory').Memory;
15
16describe('normalizeUndefinedInQuery', function() {
17 describe('with setting "throw"', function() {
18 const ds = new DataSource({
19 connector: 'memory',
20 normalizeUndefinedInQuery: 'throw',
21 });
22
23 const User = ds.define('User', {
24 seq: {type: Number, index: true},
25 name: {type: String, index: true, sort: true},
26 email: {type: String, index: true},
27 birthday: {type: Date, index: true},
28 role: {type: String, index: true},
29 order: {type: Number, index: true, sort: true},
30 vip: {type: Boolean},
31 address: {
32 street: String,
33 city: String,
34 state: String,
35 zipCode: String,
36 tags: [
37 {
38 tag: String,
39 },
40 ],
41 },
42 friends: [
43 {
44 name: String,
45 },
46 ],
47 });
48
49 before(function(done) {
50 seed(User, done);
51 });
52
53 it('should throw if find where contains undefined', function(done) {
54 User.find({where: {name: undefined}}, function(err, users) {
55 should.exist(err);
56 done();
57 });
58 });
59
60 it('should throw if destroyAll where contains undefined', function(done) {
61 User.destroyAll({name: undefined}, function(err, count) {
62 should.exist(err);
63 done();
64 });
65 });
66
67 it('should throw if updateAll where contains undefined', function(done) {
68 User.updateAll({name: undefined}, {vip: false}, function(err, count) {
69 should.exist(err);
70 done();
71 });
72 });
73
74 it('should throw if upsertWithWhere where contains undefined', function(done) {
75 User.upsertWithWhere({name: undefined}, {vip: false}, function(err, count) {
76 should.exist(err);
77 done();
78 });
79 });
80
81 it('should throw if count where contains undefined', function(done) {
82 User.count({name: undefined}, function(err, count) {
83 should.exist(err);
84 done();
85 });
86 });
87 });
88
89 describe('with setting "nullify"', function() {
90 const ds = new DataSource({
91 connector: 'memory',
92 });
93
94 const User = ds.define('User', {
95 seq: {type: Number, index: true},
96 name: {type: String, index: true, sort: true},
97 email: {type: String, index: true},
98 birthday: {type: Date, index: true},
99 role: {type: String, index: true},
100 order: {type: Number, index: true, sort: true},
101 vip: {type: Boolean},
102 address: {
103 street: String,
104 city: String,
105 state: String,
106 zipCode: String,
107 tags: [
108 {
109 tag: String,
110 },
111 ],
112 },
113 friends: [
114 {
115 name: String,
116 },
117 ],
118 }, {
119 normalizeUndefinedInQuery: 'nullify',
120 });
121
122 before(function(done) {
123 seed(User, done);
124 });
125
126 it('should nullify if find where contains undefined', function(done) {
127 User.find({where: {role: undefined}}, function(err, users) {
128 should.not.exist(err);
129 users.length.should.eql(4);
130 done();
131 });
132 });
133
134 it('should nullify if updateAll where contains undefined', function(done) {
135 User.updateAll({role: undefined}, {vip: false}, function(err, count) {
136 should.not.exist(err);
137 count.count.should.eql(4);
138 done();
139 });
140 });
141
142 it('should nullify if upsertWithWhere where contains undefined', function(done) {
143 User.upsertWithWhere({role: undefined, order: 6}, {vip: false}, function(err, user) {
144 should.not.exist(err);
145 user.order.should.eql(6);
146 done();
147 });
148 });
149
150 it('should nullify if count where contains undefined', function(done) {
151 User.count({role: undefined}, function(err, count) {
152 should.not.exist(err);
153 count.should.eql(4);
154 done();
155 });
156 });
157
158 it('should nullify if destroyAll where contains undefined', function(done) {
159 User.destroyAll({role: undefined}, function(err, count) {
160 should.not.exist(err);
161 count.count.should.eql(4);
162 done();
163 });
164 });
165 });
166
167 describe('with setting "ignore"', function() {
168 const ds = new DataSource({
169 connector: 'memory',
170 });
171
172 const User = ds.define('User', {
173 seq: {type: Number, index: true},
174 name: {type: String, index: true, sort: true},
175 email: {type: String, index: true},
176 birthday: {type: Date, index: true},
177 role: {type: String, index: true},
178 order: {type: Number, index: true, sort: true},
179 vip: {type: Boolean},
180 address: {
181 street: String,
182 city: String,
183 state: String,
184 zipCode: String,
185 tags: [
186 {
187 tag: String,
188 },
189 ],
190 },
191 friends: [
192 {
193 name: String,
194 },
195 ],
196 }, {
197 normalizeUndefinedInQuery: 'ignore',
198 });
199
200 before(function(done) {
201 seed(User, done);
202 });
203
204 it('should ignore if find where contains undefined', function(done) {
205 User.find({where: {role: undefined}}, function(err, users) {
206 should.not.exist(err);
207 users.length.should.eql(6);
208 done();
209 });
210 });
211
212 it('should ignore if updateAll where contains undefined', function(done) {
213 User.updateAll({role: undefined}, {vip: false}, function(err, count) {
214 should.not.exist(err);
215 count.count.should.eql(6);
216 done();
217 });
218 });
219
220 it('should ignore if upsertWithWhere where contains undefined', function(done) {
221 User.upsertWithWhere({role: undefined, order: 6}, {vip: false}, function(err, user) {
222 should.not.exist(err);
223 user.order.should.eql(6);
224 done();
225 });
226 });
227
228 it('should ignore if count where contains undefined', function(done) {
229 User.count({role: undefined}, function(err, count) {
230 should.not.exist(err);
231 count.should.eql(6);
232 done();
233 });
234 });
235
236 it('should ignore if destroyAll where contains undefined', function(done) {
237 User.destroyAll({role: undefined}, function(err, count) {
238 should.not.exist(err);
239 count.count.should.eql(6);
240 done();
241 });
242 });
243 });
244});
245
246function seed(User, done) {
247 const beatles = [
248 {
249 seq: 0,
250 name: 'John Lennon',
251 email: 'john@b3atl3s.co.uk',
252 role: 'lead',
253 birthday: new Date('1980-12-08'),
254 vip: true,
255 address: {
256 street: '123 A St',
257 city: 'San Jose',
258 state: 'CA',
259 zipCode: '95131',
260 tags: [{tag: 'business'}, {tag: 'rent'}],
261 },
262 friends: [{name: 'Paul McCartney'}, {name: 'George Harrison'}, {name: 'Ringo Starr'}],
263 children: ['Sean', 'Julian'],
264 },
265 {
266 seq: 1,
267 name: 'Paul McCartney',
268 email: 'paul@b3atl3s.co.uk',
269 role: 'lead',
270 birthday: new Date('1942-06-18'),
271 order: 1,
272 vip: true,
273 address: {
274 street: '456 B St',
275 city: 'San Mateo',
276 state: 'CA',
277 zipCode: '94065',
278 },
279 friends: [{name: 'John Lennon'}, {name: 'George Harrison'}, {name: 'Ringo Starr'}],
280 children: ['Stella', 'Mary', 'Heather', 'Beatrice', 'James'],
281 },
282 {seq: 2, name: 'George Harrison', role: null, order: 5, vip: false, children: ['Dhani']},
283 {seq: 3, name: 'Ringo Starr', role: null, order: 6, vip: false},
284 {seq: 4, name: 'Pete Best', role: null, order: 4, children: []},
285 {seq: 5, name: 'Stuart Sutcliffe', role: null, order: 3, vip: true},
286 ];
287
288 async.series(
289 [
290 User.destroyAll.bind(User),
291 function(cb) {
292 async.each(beatles, User.create.bind(User), cb);
293 },
294 ],
295 done,
296 );
297}