forked from parse-community/Parse-SDK-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseQueryAggregateTest.js
78 lines (70 loc) · 2.24 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
'use strict';
const assert = require('assert');
const clear = require('./clear');
const Parse = require('../../node');
const TestObject = Parse.Object.extend('TestObject');
describe('Parse Aggregate Query', () => {
beforeEach((done) => {
Parse.initialize('integration', null, 'notsosecret');
Parse.CoreManager.set('SERVER_URL', 'http://localhost:1337/parse');
Parse.Storage._clear();
clear().then(() => {
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'});
return Parse.Object.saveAll([obj1, obj2, obj3, obj4]);
}).then(() => {
return Parse.User.logOut();
})
.then(() => { done() }, () => { done() });
});
it('aggregate pipeline object query', (done) => {
const pipeline = {
group: { objectId: '$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: { objectId: '$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 (e) {
done();
}
});
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();
});
});
});