|
| 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