-
-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathParseQueryAggregateTest.js
123 lines (109 loc) · 3.66 KB
/
ParseQueryAggregateTest.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
'use strict';
const assert = require('assert');
const Parse = require('../../node');
describe('Parse Aggregate Query', () => {
beforeEach(async () => {
const obj1 = new TestObject({ score: 10, name: 'foo' });
const obj2 = new TestObject({ score: 10, name: 'foo' });
const obj3 = new TestObject({ score: 10, name: 'bar' });
const obj4 = new TestObject({ score: 20, name: 'dpl' });
await Parse.Object.saveAll([obj1, obj2, obj3, obj4]);
});
it('aggregate pipeline object query', done => {
const pipeline = {
$group: { _id: '$name' },
};
const query = new Parse.Query(TestObject);
query.aggregate(pipeline).then(results => {
assert.equal(results.length, 3);
done();
});
});
it('aggregate pipeline array query', done => {
const pipeline = [{ $group: { _id: '$name' } }];
const query = new Parse.Query(TestObject);
query.aggregate(pipeline).then(results => {
assert.equal(results.length, 3);
done();
});
});
it('aggregate pipeline invalid query', done => {
const pipeline = 1234;
const query = new Parse.Query(TestObject);
try {
query.aggregate(pipeline).then(() => {});
} catch (_) {
done();
}
});
it('aggregate allow multiple of same stage', async () => {
const pointer1 = new TestObject({ value: 1 });
const pointer2 = new TestObject({ value: 2 });
const pointer3 = new TestObject({ value: 3 });
const obj1 = new TestObject({ pointer: pointer1, name: 'Hello' });
const obj2 = new TestObject({ pointer: pointer2, name: 'Hello' });
const obj3 = new TestObject({ pointer: pointer3, name: 'World' });
const pipeline = [
{
$match: { name: 'Hello' },
},
{
// Transform className$objectId to objectId and store in new field tempPointer
$project: {
tempPointer: { $substr: ['$_p_pointer', 11, -1] }, // Remove TestObject$
},
},
{
// Left Join, replace objectId stored in tempPointer with an actual object
$lookup: {
from: 'TestObject',
localField: 'tempPointer',
foreignField: '_id',
as: 'tempPointer',
},
},
{
// lookup returns an array, Deconstructs an array field to objects
$unwind: {
path: '$tempPointer',
},
},
{
$match: { 'tempPointer.value': 2 },
},
];
await Parse.Object.saveAll([pointer1, pointer2, pointer3, obj1, obj2, obj3]);
const query = new Parse.Query(TestObject);
const results = await query.aggregate(pipeline);
expect(results.length).toEqual(1);
expect(results[0].tempPointer.value).toEqual(2);
});
it('aggregate pipeline on top of a simple query', async () => {
const pipeline = {
$group: { _id: '$name' },
};
let results = await new Parse.Query(TestObject).equalTo('name', 'foo').aggregate(pipeline);
expect(results.length).toBe(1);
results = await new Parse.Query(TestObject).equalTo('score', 20).aggregate(pipeline);
expect(results.length).toBe(1);
});
it('distinct query', () => {
const query = new Parse.Query(TestObject);
return query.distinct('score').then(results => {
assert.equal(results.length, 2);
// Order the results in case
const orderedResults = results.sort((a, b) => a - b);
assert.equal(orderedResults[0], 10);
assert.equal(orderedResults[1], 20);
});
});
it('distinct equalTo query', done => {
const query = new Parse.Query(TestObject);
query.equalTo('name', 'foo');
query.distinct('score').then(results => {
assert.equal(results.length, 1);
assert.equal(results[0], 10);
done();
});
});
});