-
Notifications
You must be signed in to change notification settings - Fork 698
Expand file tree
/
Copy pathaggregateQuery.ts
More file actions
219 lines (197 loc) · 6.43 KB
/
Copy pathaggregateQuery.ts
File metadata and controls
219 lines (197 loc) · 6.43 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
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
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {afterEach, beforeEach, it} from 'mocha';
import {
ApiOverride,
createInstance,
stream,
verifyInstance,
} from './util/helpers';
import {Firestore, Query, Timestamp} from '../src';
import {expect, use} from 'chai';
import {google} from '../protos/firestore_v1_proto_api';
import api = google.firestore.v1;
import * as chaiAsPromised from 'chai-as-promised';
import {setTimeoutHandler} from '../src/backoff';
import * as extend from 'extend';
use(chaiAsPromised);
describe('aggregate query interface', () => {
let firestore: Firestore;
beforeEach(() => {
setTimeoutHandler(setImmediate);
return createInstance().then(firestoreInstance => {
firestore = firestoreInstance;
});
});
afterEach(async () => {
await verifyInstance(firestore);
setTimeoutHandler(setTimeout);
});
it('has isEqual() method', () => {
const queryA = firestore.collection('collectionId');
const queryB = firestore.collection('collectionId');
const queryEquals = (equals: Query[], notEquals: Query[]) => {
for (const equal1 of equals) {
const equal1count = equal1.count();
for (const equal2 of equals) {
const equal2count = equal2.count();
expect(equal1count.isEqual(equal2count)).to.be.true;
expect(equal2count.isEqual(equal1count)).to.be.true;
}
for (const notEqual of notEquals) {
const notEqual2count = notEqual.count();
expect(equal1count.isEqual(notEqual2count)).to.be.false;
expect(notEqual2count.isEqual(equal1count)).to.be.false;
}
}
};
queryEquals(
[
queryA.orderBy('foo').endBefore('a'),
queryB.orderBy('foo').endBefore('a'),
],
[
queryA.orderBy('foo').endBefore('b'),
queryB.orderBy('bar').endBefore('a'),
],
);
});
it('returns results', async () => {
// Here we are mocking the response from the server. The client uses
// `aggregate_$i` aliases in requests and will receive these in responses.
const result: api.IRunAggregationQueryResponse = {
result: {
aggregateFields: {
aggregate_0: {integerValue: '99'},
},
},
readTime: {seconds: 5, nanos: 6},
};
const overrides: ApiOverride = {
runAggregationQuery: () => stream(result),
};
firestore = await createInstance(overrides);
const query = firestore.collection('collectionId').count();
return query.get().then(results => {
expect(results.data().count).to.be.equal(99);
expect(results.readTime.isEqual(new Timestamp(5, 6))).to.be.true;
expect(results.query).to.be.equal(query);
});
});
it('supports alwaysUseImplicitOrderBy', async () => {
const result: api.IRunAggregationQueryResponse = {
result: {
aggregateFields: {
aggregate_0: {integerValue: '99'},
},
},
readTime: {seconds: 5, nanos: 6},
};
const overrides: ApiOverride = {
runAggregationQuery: request => {
let actualStructuredQuery =
request!.structuredAggregationQuery?.structuredQuery;
actualStructuredQuery = extend(true, {}, actualStructuredQuery);
expect(actualStructuredQuery).to.deep.equal({
from: [{collectionId: 'collectionId'}],
where: {
fieldFilter: {
field: {fieldPath: 'foo'},
op: 'GREATER_THAN' as api.StructuredQuery.FieldFilter.Operator,
value: {stringValue: 'bar'},
},
},
orderBy: [
{
direction: 'ASCENDING' as api.StructuredQuery.Direction,
field: {fieldPath: 'foo'},
},
{
direction: 'ASCENDING' as api.StructuredQuery.Direction,
field: {fieldPath: '__name__'},
},
],
});
return stream(result);
},
};
firestore = await createInstance(overrides, {
alwaysUseImplicitOrderBy: true,
});
const query = firestore
.collection('collectionId')
.where('foo', '>', 'bar')
.count();
return query.get().then(results => {
expect(results.data().count).to.be.equal(99);
});
});
it('handles stream exception at initialization', async () => {
let attempts = 0;
const query = firestore.collection('collectionId').count();
query._stream = () => {
++attempts;
throw new Error('Expected error');
};
await query
.get()
.then(() => {
throw new Error('Unexpected success in Promise');
})
.catch(err => {
expect(err.message).to.equal('Expected error');
expect(attempts).to.equal(1);
});
});
it('handles stream exception during initialization', async () => {
let attempts = 0;
const overrides: ApiOverride = {
runAggregationQuery: () => {
++attempts;
return stream(new Error('Expected error'));
},
};
firestore = await createInstance(overrides);
const query = firestore.collection('collectionId').count();
await query
.get()
.then(() => {
throw new Error('Unexpected success in Promise');
})
.catch(err => {
expect(err.message).to.equal('Expected error');
expect(attempts).to.equal(5);
});
});
it('handles message without result during initialization', async () => {
let attempts = 0;
const overrides: ApiOverride = {
runAggregationQuery: () => {
++attempts;
return stream({readTime: {seconds: 5, nanos: 6}});
},
};
firestore = await createInstance(overrides);
const query = firestore.collection('collectionId').count();
await query
.get()
.then(() => {
throw new Error('Unexpected success in Promise');
})
.catch(err => {
expect(err.message).to.equal('No AggregateQuery results');
expect(attempts).to.equal(1);
});
});
});