-
Notifications
You must be signed in to change notification settings - Fork 595
Expand file tree
/
Copy pathpopulateArray.js
More file actions
187 lines (163 loc) · 4.4 KB
/
Copy pathpopulateArray.js
File metadata and controls
187 lines (163 loc) · 4.4 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
var assert = require('assert');
var Waterline = require('../../../../lib/waterline');
describe('Collection Query ::', function() {
describe('specific populated associations ::', function() {
var Car;
before(function(done) {
var waterline = new Waterline();
var collections = {};
collections.user = Waterline.Model.extend({
identity: 'user',
datastore: 'foo',
primaryKey: 'id',
attributes: {
id: {
type: 'number'
},
car: {
model: 'car'
},
name: {
columnName: 'my_name',
type: 'string'
}
}
});
collections.ticket = Waterline.Model.extend({
identity: 'ticket',
datastore: 'foo',
primaryKey: 'id',
attributes: {
id: {
type: 'number'
},
reason: {
columnName: 'reason',
type: 'string'
},
car: {
model: 'car'
}
}
});
collections.car = Waterline.Model.extend({
identity: 'car',
datastore: 'foo',
primaryKey: 'id',
attributes: {
id: {
type: 'number'
},
driver: {
model: 'user',
columnName: 'foobar'
},
tickets: {
collection: 'ticket',
via: 'car'
}
}
});
waterline.registerModel(collections.user);
waterline.registerModel(collections.car);
waterline.registerModel(collections.ticket);
// Fixture Adapter Def
var adapterDef = {
identity: 'foo',
find: function(con, query, cb) {
if(query.using === 'user') {
return cb(null, [{ id: 1, car: 1, name: 'John Doe' }]);
}
if(query.using === 'car') {
return cb(null, [{ id: 1, foobar: 1, tickets: [1, 2]}]);
}
if(query.using === 'ticket') {
return cb(null, [{ id: 1, reason: 'red light', car:1}, { id: 2, reason: 'Parking in a disabled space', car: 1 }]);
}
return cb();
}
};
var connections = {
'foo': {
adapter: 'foobar'
}
};
waterline.initialize({ adapters: { foobar: adapterDef }, datastores: connections }, function(err, orm) {
if (err) {
return done(err);
}
Car = orm.collections.car;
return done();
});
});
it('should populate all related collections', function(done) {
Car.find()
.populate('driver')
.populate('tickets')
.exec(function(err, car) {
if (err) {
return done(err);
}
assert(car[0].driver);
assert(car[0].driver.name);
assert(car[0].tickets);
assert(car[0].tickets[0].car);
assert(car[0].tickets[1].car);
return done();
});
});
it('should allow populate with select subcriteria for singular associations', function(done) {
Car.find()
.populate('driver', {
select: ['name']
})
.exec(function(err, car) {
if (err) {
return done(err);
}
assert(car[0].driver);
assert(car[0].driver.name);
return done();
});
});
it('should allow populate with omit subcriteria for singular associations', function(done) {
Car.find()
.populate('driver', {
omit: ['car']
})
.exec(function(err, car) {
if (err) {
return done(err);
}
assert(car[0].driver);
assert(car[0].driver.name);
return done();
});
});
it('should allow populate with empty object (converts to true)', function(done) {
Car.find()
.populate('driver', {})
.exec(function(err, car) {
if (err) {
return done(err);
}
assert(car[0].driver);
assert(car[0].driver.name);
return done();
});
});
it('should throw error when both select and omit are provided together', function(done) {
Car.find()
.populate('driver', {
select: ['name'],
omit: ['id']
})
.exec(function(err, car) {
assert(err);
assert.equal(err.code, 'E_INVALID_POPULATES');
assert(err.message.indexOf('providing both `select` AND `omit`') > -1);
return done();
});
});
});
});