forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParseRelation.spec.js
338 lines (311 loc) · 10.8 KB
/
ParseRelation.spec.js
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// This is a port of the test suite:
// hungry/js/test/parse_relation_test.js
var ChildObject = Parse.Object.extend({className: "ChildObject"});
var ParentObject = Parse.Object.extend({className: "ParentObject"});
describe('Parse.Relation testing', () => {
it("simple add and remove relation", (done) => {
var child = new ChildObject();
child.set("x", 2);
var parent = new ParentObject();
parent.set("x", 4);
var relation = parent.relation("child");
child.save().then(() => {
relation.add(child);
return parent.save();
}, (e) => {
fail(e);
}).then(() => {
return relation.query().find();
}).then((list) => {
equal(list.length, 1,
"Should have gotten one element back");
equal(list[0].id, child.id,
"Should have gotten the right value");
ok(!parent.dirty("child"),
"The relation should not be dirty");
relation.remove(child);
return parent.save();
}).then(() => {
return relation.query().find();
}).then((list) => {
equal(list.length, 0,
"Delete should have worked");
ok(!parent.dirty("child"),
"The relation should not be dirty");
done();
});
});
it("query relation without schema", (done) => {
var ChildObject = Parse.Object.extend("ChildObject");
var childObjects = [];
for (var i = 0; i < 10; i++) {
childObjects.push(new ChildObject({x:i}));
};
Parse.Object.saveAll(childObjects, expectSuccess({
success: function(list) {
var ParentObject = Parse.Object.extend("ParentObject");
var parent = new ParentObject();
parent.set("x", 4);
var relation = parent.relation("child");
relation.add(childObjects[0]);
parent.save(null, expectSuccess({
success: function() {
var parentAgain = new ParentObject();
parentAgain.id = parent.id;
var relation = parentAgain.relation("child");
relation.query().find(expectSuccess({
success: function(list) {
equal(list.length, 1,
"Should have gotten one element back");
equal(list[0].id, childObjects[0].id,
"Should have gotten the right value");
done();
}
}));
}
}));
}
}));
});
it("relations are constructed right from query", (done) => {
var ChildObject = Parse.Object.extend("ChildObject");
var childObjects = [];
for (var i = 0; i < 10; i++) {
childObjects.push(new ChildObject({x: i}));
}
Parse.Object.saveAll(childObjects, {
success: function(list) {
var ParentObject = Parse.Object.extend("ParentObject");
var parent = new ParentObject();
parent.set("x", 4);
var relation = parent.relation("child");
relation.add(childObjects[0]);
parent.save(null, {
success: function() {
var query = new Parse.Query(ParentObject);
query.get(parent.id, {
success: function(object) {
var relationAgain = object.relation("child");
relationAgain.query().find({
success: function(list) {
equal(list.length, 1,
"Should have gotten one element back");
equal(list[0].id, childObjects[0].id,
"Should have gotten the right value");
ok(!parent.dirty("child"),
"The relation should not be dirty");
done();
},
error: function(list) {
ok(false, "This shouldn't have failed");
done();
}
});
}
});
}
});
}
});
});
it("compound add and remove relation", (done) => {
var ChildObject = Parse.Object.extend("ChildObject");
var childObjects = [];
for (var i = 0; i < 10; i++) {
childObjects.push(new ChildObject({x: i}));
}
var parent;
var relation;
Parse.Object.saveAll(childObjects).then(function(list) {
var ParentObject = Parse.Object.extend('ParentObject');
parent = new ParentObject();
parent.set('x', 4);
relation = parent.relation('child');
relation.add(childObjects[0]);
relation.add(childObjects[1]);
relation.remove(childObjects[0]);
relation.add(childObjects[2]);
return parent.save();
}).then(function() {
return relation.query().find();
}).then(function(list) {
equal(list.length, 2, 'Should have gotten two elements back');
ok(!parent.dirty('child'), 'The relation should not be dirty');
relation.remove(childObjects[1]);
relation.remove(childObjects[2]);
relation.add(childObjects[1]);
relation.add(childObjects[0]);
return parent.save();
}).then(function() {
return relation.query().find();
}).then(function(list) {
equal(list.length, 2, 'Deletes and then adds should have worked');
ok(!parent.dirty('child'), 'The relation should not be dirty');
done();
}, function(err) {
ok(false, err.message);
done();
});
});
it("queries with relations", (done) => {
var ChildObject = Parse.Object.extend("ChildObject");
var childObjects = [];
for (var i = 0; i < 10; i++) {
childObjects.push(new ChildObject({x: i}));
}
Parse.Object.saveAll(childObjects, {
success: function() {
var ParentObject = Parse.Object.extend("ParentObject");
var parent = new ParentObject();
parent.set("x", 4);
var relation = parent.relation("child");
relation.add(childObjects[0]);
relation.add(childObjects[1]);
relation.add(childObjects[2]);
parent.save(null, {
success: function() {
var query = relation.query();
query.equalTo("x", 2);
query.find({
success: function(list) {
equal(list.length, 1,
"There should only be one element");
ok(list[0] instanceof ChildObject,
"Should be of type ChildObject");
equal(list[0].id, childObjects[2].id,
"We should have gotten back the right result");
done();
}
});
}
});
}
});
});
it("queries on relation fields", (done) => {
var ChildObject = Parse.Object.extend("ChildObject");
var childObjects = [];
for (var i = 0; i < 10; i++) {
childObjects.push(new ChildObject({x: i}));
}
Parse.Object.saveAll(childObjects, {
success: function() {
var ParentObject = Parse.Object.extend("ParentObject");
var parent = new ParentObject();
parent.set("x", 4);
var relation = parent.relation("child");
relation.add(childObjects[0]);
relation.add(childObjects[1]);
relation.add(childObjects[2]);
var parent2 = new ParentObject();
parent2.set("x", 3);
var relation2 = parent2.relation("child");
relation2.add(childObjects[4]);
relation2.add(childObjects[5]);
relation2.add(childObjects[6]);
var parents = [];
parents.push(parent);
parents.push(parent2);
Parse.Object.saveAll(parents, {
success: function() {
var query = new Parse.Query(ParentObject);
var objects = [];
objects.push(childObjects[4]);
objects.push(childObjects[9]);
query.containedIn("child", objects);
query.find({
success: function(list) {
equal(list.length, 1, "There should be only one result");
equal(list[0].id, parent2.id,
"Should have gotten back the right result");
done();
}
});
}
});
}
});
});
it("Get query on relation using un-fetched parent object", (done) => {
// Setup data model
var Wheel = Parse.Object.extend('Wheel');
var Car = Parse.Object.extend('Car');
var origWheel = new Wheel();
origWheel.save().then(function() {
var car = new Car();
var relation = car.relation('wheels');
relation.add(origWheel);
return car.save();
}).then(function(car) {
// Test starts here.
// Create an un-fetched shell car object
var unfetchedCar = new Car();
unfetchedCar.id = car.id;
var relation = unfetchedCar.relation('wheels');
var query = relation.query();
// Parent object is un-fetched, so this will call /1/classes/Car instead
// of /1/classes/Wheel and pass { "redirectClassNameForKey":"wheels" }.
return query.get(origWheel.id);
}).then(function(wheel) {
// Make sure this is Wheel and not Car.
strictEqual(wheel.className, 'Wheel');
strictEqual(wheel.id, origWheel.id);
}).then(function() {
done();
},function(err) {
ok(false, 'unexpected error: ' + JSON.stringify(err));
done();
});
});
it("Find query on relation using un-fetched parent object", (done) => {
// Setup data model
var Wheel = Parse.Object.extend('Wheel');
var Car = Parse.Object.extend('Car');
var origWheel = new Wheel();
origWheel.save().then(function() {
var car = new Car();
var relation = car.relation('wheels');
relation.add(origWheel);
return car.save();
}).then(function(car) {
// Test starts here.
// Create an un-fetched shell car object
var unfetchedCar = new Car();
unfetchedCar.id = car.id;
var relation = unfetchedCar.relation('wheels');
var query = relation.query();
// Parent object is un-fetched, so this will call /1/classes/Car instead
// of /1/classes/Wheel and pass { "redirectClassNameForKey":"wheels" }.
return query.find(origWheel.id);
}).then(function(results) {
// Make sure this is Wheel and not Car.
var wheel = results[0];
strictEqual(wheel.className, 'Wheel');
strictEqual(wheel.id, origWheel.id);
}).then(function() {
done();
},function(err) {
ok(false, 'unexpected error: ' + JSON.stringify(err));
done();
});
});
it('Find objects with a related object using equalTo', (done) => {
// Setup the objects
var Card = Parse.Object.extend('Card');
var House = Parse.Object.extend('House');
var card = new Card();
card.save().then(() => {
var house = new House();
var relation = house.relation('cards');
relation.add(card);
return house.save();
}).then(() => {
var query = new Parse.Query('House');
query.equalTo('cards', card);
return query.find();
}).then((results) => {
expect(results.length).toEqual(1);
done();
});
});
});