-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathpostgresql.filterUndefined.test.js
More file actions
190 lines (176 loc) · 5.47 KB
/
postgresql.filterUndefined.test.js
File metadata and controls
190 lines (176 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright IBM Corp. 2015,2019. All Rights Reserved.
// Node module: loopback-connector-postgresql
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
const should = require('should'),
assert = require('assert');
let Post, db, Charge, Currency;
describe('filter undefined fields', function() {
before(function() {
db = global.getDataSource();
Post = db.define('FilterUndefined', {
defaultInt: {
type: 'Number',
postgresql: {
dbDefault: '5',
},
},
first: {
type: 'String',
},
second: {
type: 'Number',
},
third: {
type: 'Number',
},
});
});
it('should run migration', function(done) {
db.automigrate('FilterUndefined', function() {
done();
});
});
it('should insert only default value', function(done) {
const dflPost = new Post();
dflPost.save(function(err, p) {
should.not.exist(err);
Post.findOne({where: {id: p.id}}, function(err, p) {
should.not.exist(err);
p.defaultInt.should.be.equal(5);
should.not.exist(p.first);
should.not.exist(p.second);
should.not.exist(p.third);
});
done();
});
});
it('should insert default value and \'third\' field', function(done) {
const dflPost = new Post();
dflPost.third = 3;
dflPost.save(function(err, p) {
should.not.exist(err);
Post.findOne({where: {id: p.id}}, function(err, p) {
should.not.exist(err);
p.defaultInt.should.be.equal(5);
should.not.exist(p.first);
should.not.exist(p.second);
should.exist(p.third);
p.third.should.be.equal(3);
});
done();
});
});
it('should update \'first\' and \'third\' fields of record with id==2 to predefined values', function(done) {
Post.findOne({where: {id: 2}}, function(err, p) {
should.not.exist(err);
should.exist(p);
p.id.should.be.equal(2);
p.updateAttributes({first: 'one', third: 4}, function(err, p) {
Post.findOne({where: {id: 2}}, function(err, p) {
should.not.exist(err);
p.defaultInt.should.be.equal(5);
p.first.should.be.equal('one');
should.not.exist(p.second);
p.third.should.be.equal(4);
done();
});
});
});
});
it('should update \'third\' field of record with id==2 to null value', function(done) {
Post.findOne({where: {id: 2}}, function(err, p) {
should.not.exist(err);
should.exist(p);
p.id.should.be.equal(2);
p.updateAttributes({first: 'null in third', third: null}, function(err, p) {
Post.findOne({where: {id: 2}}, function(err, p) {
should.not.exist(err);
p.defaultInt.should.be.equal(5);
p.first.should.be.equal('null in third');
should.not.exist(p.second);
should.not.exist(p.third);
done();
});
});
});
});
it('should insert a value into \'defaultInt\' and \'second\'', function(done) {
const dflPost = new Post();
dflPost.second = 2;
dflPost.defaultInt = 11;
dflPost.save(function(err, p) {
should.not.exist(err);
Post.findOne({where: {id: p.id}}, function(err, p) {
should.not.exist(err);
p.defaultInt.should.be.equal(11);
should.not.exist(p.first);
should.not.exist(p.third);
// should.exist(p.third);
p.second.should.be.equal(2);
done();
});
});
});
it('should create an object with a null value in \'first\'', function(done) {
Post.create({first: null}, function(err, p) {
should.not.exist(err);
Post.findOne({where: {id: p.id}}, function(err, p) {
should.not.exist(err);
p.defaultInt.should.equal(5);
should.not.exist(p.first);
should.not.exist(p.second);
should.not.exist(p.third);
done();
});
});
});
describe('able to handle null foreign keys', function() {
before(function(done) {
Charge = db.define('Charge', {amount: Number});
Currency = db.define('Currency', {code: String, country: String});
Charge.belongsTo('Currency', {as: 'currency'});
db.automigrate(['Charge', 'Currency'], done);
});
it('able to create entity with null foreign key', function(done) {
Currency.create({code: 'USD', country: 'USA'}, function(err, currency) {
if (err) return done(err);
Charge.create({amount: 10, currencyId: currency.id}, function(err) {
if (err) return done(err);
Charge.create({amount: 10, currencyId: null}, function(err, charge) {
if (err) return done(err);
should.not.exist(charge.currencyId);
done();
});
});
});
});
it('able to query entities with null foreign key', function(done) {
Charge.find({
where: {currency: {
inq: [null, 1],
}},
include: {relation: 'currency'},
}, function(err, charges) {
if (err) return done(err);
charges.should.have.lengthOf(2);
done();
});
});
it('able to filter entities with null id condition', function(done) {
Currency.find({
where: {
id: {
inq: [null, 1],
},
},
},
function(err, currencies) {
if (err) return done(err);
currencies.should.have.lengthOf(1);
done();
});
});
});
});