Skip to content

Commit 03b9803

Browse files
authored
Merge branch 'master' into mila/count-export-count-quries
2 parents 597d1fb + e35db6f commit 03b9803

File tree

3 files changed

+130
-3
lines changed

3 files changed

+130
-3
lines changed

.changeset/hot-insects-wink.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
'@firebase/firestore': minor
33
---
44

5-
Release count query for internal use.
5+
Implement count query for internal use.

packages/firestore/src/core/firestore_client.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,10 @@ export function firestoreClientRunCountQuery(
525525
'Failed to get count result because the client is offline.'
526526
)
527527
);
528+
} else {
529+
const result = await getCount(query);
530+
deferred.resolve(result);
528531
}
529-
const result = await getCount(query);
530-
deferred.resolve(result);
531532
} catch (e) {
532533
deferred.reject(e as Error);
533534
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* @license
3+
* Copyright 2022 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/** This file is emporarily staying in api_internal folder till aggregate queries are public. */
19+
import { expect } from 'chai';
20+
21+
import { getCountFromServer } from '../../../src/api/aggregate';
22+
import {
23+
collection,
24+
collectionGroup,
25+
doc,
26+
disableNetwork,
27+
query,
28+
terminate,
29+
where,
30+
writeBatch,
31+
DocumentData,
32+
QueryDocumentSnapshot
33+
} from '../util/firebase_export';
34+
import {
35+
apiDescribe,
36+
withEmptyTestCollection,
37+
withTestCollection,
38+
withTestDb
39+
} from '../util/helpers';
40+
import { USE_EMULATOR } from '../util/settings';
41+
42+
(USE_EMULATOR ? apiDescribe : apiDescribe.skip)(
43+
'Count quries',
44+
(persistence: boolean) => {
45+
it('can run count query getCountFromServer', () => {
46+
const testDocs = {
47+
a: { author: 'authorA', title: 'titleA' },
48+
b: { author: 'authorB', title: 'titleB' }
49+
};
50+
return withTestCollection(persistence, testDocs, async coll => {
51+
const snapshot = await getCountFromServer(coll);
52+
expect(snapshot.data().count).to.equal(2);
53+
});
54+
});
55+
56+
it("count query doesn't use converter", () => {
57+
const testDocs = {
58+
a: { author: 'authorA', title: 'titleA' },
59+
b: { author: 'authorB', title: 'titleB' }
60+
};
61+
const throwingConverter = {
62+
toFirestore(obj: never): DocumentData {
63+
throw new Error('should never be called');
64+
},
65+
fromFirestore(snapshot: QueryDocumentSnapshot): never {
66+
throw new Error('should never be called');
67+
}
68+
};
69+
return withTestCollection(persistence, testDocs, async coll => {
70+
const query_ = query(
71+
coll,
72+
where('author', '==', 'authorA')
73+
).withConverter(throwingConverter);
74+
const snapshot = await getCountFromServer(query_);
75+
expect(snapshot.data().count).to.equal(1);
76+
});
77+
});
78+
79+
it('count query supports collection groups', () => {
80+
return withTestDb(persistence, async db => {
81+
const collectionGroupId = doc(collection(db, 'aggregateQueryTest')).id;
82+
const docPaths = [
83+
`${collectionGroupId}/cg-doc1`,
84+
`abc/123/${collectionGroupId}/cg-doc2`,
85+
`zzz${collectionGroupId}/cg-doc3`,
86+
`abc/123/zzz${collectionGroupId}/cg-doc4`,
87+
`abc/123/zzz/${collectionGroupId}`
88+
];
89+
const batch = writeBatch(db);
90+
for (const docPath of docPaths) {
91+
batch.set(doc(db, docPath), { x: 1 });
92+
}
93+
await batch.commit();
94+
const snapshot = await getCountFromServer(
95+
collectionGroup(db, collectionGroupId)
96+
);
97+
expect(snapshot.data().count).to.equal(2);
98+
});
99+
});
100+
101+
it('getCountFromServer fails if firestore is terminated', () => {
102+
return withEmptyTestCollection(persistence, async (coll, firestore) => {
103+
await terminate(firestore);
104+
expect(() => getCountFromServer(coll)).to.throw(
105+
'The client has already been terminated.'
106+
);
107+
});
108+
});
109+
110+
it("terminate doesn't crash when there is count query in flight", () => {
111+
return withEmptyTestCollection(persistence, async (coll, firestore) => {
112+
void getCountFromServer(coll);
113+
await terminate(firestore);
114+
});
115+
});
116+
117+
it('getCountFromServer fails if user is offline', () => {
118+
return withEmptyTestCollection(persistence, async (coll, firestore) => {
119+
await disableNetwork(firestore);
120+
await expect(getCountFromServer(coll)).to.be.eventually.rejectedWith(
121+
'Failed to get count result because the client is offline'
122+
);
123+
});
124+
});
125+
}
126+
);

0 commit comments

Comments
 (0)