1
1
const AggregateRouter = require ( '../lib/Routers/AggregateRouter' ) . AggregateRouter ;
2
2
3
3
describe ( 'AggregateRouter' , ( ) => {
4
- // TODO: update pipeline syntax. See [#7339](https://bit.ly/3incnWx)
5
4
it ( 'get pipeline from Array' , ( ) => {
6
5
const body = [
7
6
{
8
- group : { objectId : { } } ,
7
+ $ group : { _id : { } } ,
9
8
} ,
10
9
] ;
11
10
const expected = [ { $group : { _id : { } } } ] ;
12
11
const result = AggregateRouter . getPipeline ( body ) ;
13
12
expect ( result ) . toEqual ( expected ) ;
14
13
} ) ;
15
14
16
- // TODO: update pipeline syntax. See [#7339](https://bit.ly/3incnWx)
17
15
it ( 'get pipeline from Object' , ( ) => {
18
16
const body = {
19
- group : { objectId : { } } ,
17
+ $ group : { _id : { } } ,
20
18
} ;
21
19
const expected = [ { $group : { _id : { } } } ] ;
22
20
const result = AggregateRouter . getPipeline ( body ) ;
23
21
expect ( result ) . toEqual ( expected ) ;
24
22
} ) ;
25
23
26
- // TODO: update pipeline syntax. See [#7339](https://bit.ly/3incnWx)
27
24
it ( 'get pipeline from Pipeline Operator (Array)' , ( ) => {
28
25
const body = {
29
26
pipeline : [
30
27
{
31
- group : { objectId : { } } ,
28
+ $ group : { _id : { } } ,
32
29
} ,
33
30
] ,
34
31
} ;
@@ -37,55 +34,53 @@ describe('AggregateRouter', () => {
37
34
expect ( result ) . toEqual ( expected ) ;
38
35
} ) ;
39
36
40
- // TODO: update pipeline syntax. See [#7339](https://bit.ly/3incnWx)
41
37
it ( 'get pipeline from Pipeline Operator (Object)' , ( ) => {
42
38
const body = {
43
39
pipeline : {
44
- group : { objectId : { } } ,
40
+ $ group : { _id : { } } ,
45
41
} ,
46
42
} ;
47
43
const expected = [ { $group : { _id : { } } } ] ;
48
44
const result = AggregateRouter . getPipeline ( body ) ;
49
45
expect ( result ) . toEqual ( expected ) ;
50
46
} ) ;
51
47
52
- // TODO: update pipeline syntax. See [#7339](https://bit.ly/3incnWx)
53
48
it ( 'get pipeline fails multiple keys in Array stage ' , ( ) => {
54
49
const body = [
55
50
{
56
- group : { objectId : { } } ,
57
- match : { name : 'Test' } ,
51
+ $ group : { _id : { } } ,
52
+ $ match : { name : 'Test' } ,
58
53
} ,
59
54
] ;
60
- try {
61
- AggregateRouter . getPipeline ( body ) ;
62
- } catch ( e ) {
63
- expect ( e . message ) . toBe ( 'Pipeline stages should only have one key found group, match' ) ;
64
- }
55
+ expect ( ( ) => AggregateRouter . getPipeline ( body ) ) . toThrow (
56
+ new Parse . Error (
57
+ Parse . Error . INVALID_QUERY ,
58
+ 'Pipeline stages should only have one key but found $group, $match.'
59
+ )
60
+ ) ;
65
61
} ) ;
66
62
67
- // TODO: update pipeline syntax. See [#7339](https://bit.ly/3incnWx)
68
63
it ( 'get pipeline fails multiple keys in Pipeline Operator Array stage ' , ( ) => {
69
64
const body = {
70
65
pipeline : [
71
66
{
72
- group : { objectId : { } } ,
73
- match : { name : 'Test' } ,
67
+ $ group : { _id : { } } ,
68
+ $ match : { name : 'Test' } ,
74
69
} ,
75
70
] ,
76
71
} ;
77
- try {
78
- AggregateRouter . getPipeline ( body ) ;
79
- } catch ( e ) {
80
- expect ( e . message ) . toBe ( 'Pipeline stages should only have one key found group, match' ) ;
81
- }
72
+ expect ( ( ) => AggregateRouter . getPipeline ( body ) ) . toThrow (
73
+ new Parse . Error (
74
+ Parse . Error . INVALID_QUERY ,
75
+ 'Pipeline stages should only have one key but found $group, $match.'
76
+ )
77
+ ) ;
82
78
} ) ;
83
79
84
- // TODO: update pipeline syntax. See [#7339](https://bit.ly/3incnWx)
85
80
it ( 'get search pipeline from Pipeline Operator (Array)' , ( ) => {
86
81
const body = {
87
82
pipeline : {
88
- search : { } ,
83
+ $ search : { } ,
89
84
} ,
90
85
} ;
91
86
const expected = [ { $search : { } } ] ;
@@ -105,7 +100,7 @@ describe('AggregateRouter', () => {
105
100
it ( 'support nested stage names starting with `$`' , ( ) => {
106
101
const body = [
107
102
{
108
- lookup : {
103
+ $ lookup : {
109
104
from : 'ACollection' ,
110
105
let : { id : '_id' } ,
111
106
as : 'results' ,
@@ -145,11 +140,11 @@ describe('AggregateRouter', () => {
145
140
146
141
it ( 'support the use of `_id` in stages' , ( ) => {
147
142
const body = [
148
- { match : { _id : 'randomId' } } ,
149
- { sort : { _id : - 1 } } ,
150
- { addFields : { _id : 1 } } ,
151
- { group : { _id : { } } } ,
152
- { project : { _id : 0 } } ,
143
+ { $ match : { _id : 'randomId' } } ,
144
+ { $ sort : { _id : - 1 } } ,
145
+ { $ addFields : { _id : 1 } } ,
146
+ { $ group : { _id : { } } } ,
147
+ { $ project : { _id : 0 } } ,
153
148
] ;
154
149
const expected = [
155
150
{ $match : { _id : 'randomId' } } ,
@@ -161,4 +156,19 @@ describe('AggregateRouter', () => {
161
156
const result = AggregateRouter . getPipeline ( body ) ;
162
157
expect ( result ) . toEqual ( expected ) ;
163
158
} ) ;
159
+
160
+ it ( 'should throw with invalid stage' , ( ) => {
161
+ expect ( ( ) => AggregateRouter . getPipeline ( [ { foo : 'bar' } ] ) ) . toThrow (
162
+ new Parse . Error ( Parse . Error . INVALID_QUERY , `Invalid aggregate stage 'foo'.` )
163
+ ) ;
164
+ } ) ;
165
+
166
+ it ( 'should throw with invalid group' , ( ) => {
167
+ expect ( ( ) => AggregateRouter . getPipeline ( [ { $group : { objectId : 'bar' } } ] ) ) . toThrow (
168
+ new Parse . Error (
169
+ Parse . Error . INVALID_QUERY ,
170
+ `Cannot use 'objectId' in aggregation stage $group.`
171
+ )
172
+ ) ;
173
+ } ) ;
164
174
} ) ;
0 commit comments