Skip to content

Commit 98189d5

Browse files
authored
Mila/count non lite api tests (#6581)
1 parent 507e984 commit 98189d5

File tree

3 files changed

+157
-57
lines changed

3 files changed

+157
-57
lines changed

packages/firestore/test/integration/api/aggregation.test.ts

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,112 @@
1818
import { expect } from 'chai';
1919

2020
import {
21+
collection,
22+
collectionGroup,
2123
countQuery,
24+
doc,
2225
disableNetwork,
2326
getAggregateFromServerDirect,
24-
query
27+
query,
28+
terminate,
29+
where,
30+
writeBatch
2531
} from '../util/firebase_export';
26-
import { apiDescribe, withTestCollection } from '../util/helpers';
32+
import {
33+
apiDescribe,
34+
postConverter,
35+
withEmptyTestCollection,
36+
withTestCollection,
37+
withTestDb
38+
} from '../util/helpers';
2739

2840
apiDescribe('Aggregation query', (persistence: boolean) => {
2941
it('can run count query getAggregateFromServerDirect', () => {
3042
const testDocs = {
31-
a: { k: 'a', sort: 1 },
32-
b: { k: 'b', sort: 2 },
33-
c: { k: 'c', sort: 2 }
43+
a: { author: 'authorA', title: 'titleA' },
44+
b: { author: 'authorB', title: 'titleB' }
3445
};
3546
return withTestCollection(persistence, testDocs, async coll => {
47+
const countQuery_ = countQuery(coll);
48+
const snapshot = await getAggregateFromServerDirect(countQuery_);
49+
expect(snapshot.getCount()).to.equal(2);
50+
});
51+
});
52+
53+
it('aggregateQuery.query equals to original query', () => {
54+
return withEmptyTestCollection(persistence, async coll => {
3655
const query_ = query(coll);
56+
const aggregateQuery_ = countQuery(query_);
57+
expect(aggregateQuery_.query).to.be.equal(query_);
58+
});
59+
});
60+
61+
it('aggregateQuerySnapshot.query equals to aggregateQuery', () => {
62+
return withEmptyTestCollection(persistence, async coll => {
63+
const aggregateQuery_ = countQuery(coll);
64+
const snapshot = await getAggregateFromServerDirect(aggregateQuery_);
65+
expect(snapshot.query).to.be.equal(aggregateQuery_);
66+
});
67+
});
68+
69+
it('aggregate query supports withConverter', () => {
70+
const testDocs = {
71+
a: { author: 'authorA', title: 'titleA' },
72+
b: { author: 'authorB', title: 'titleB' }
73+
};
74+
return withTestCollection(persistence, testDocs, async coll => {
75+
const query_ = query(
76+
coll,
77+
where('author', '==', 'authorA')
78+
).withConverter(postConverter);
3779
const countQuery_ = countQuery(query_);
3880
const snapshot = await getAggregateFromServerDirect(countQuery_);
39-
expect(snapshot.getCount()).to.equal(3);
81+
expect(snapshot.getCount()).to.equal(1);
82+
});
83+
});
84+
85+
it('aggregate query supports collection groups', () => {
86+
return withTestDb(persistence, async db => {
87+
const collectionGroupId = doc(collection(db, 'aggregateQueryTest')).id;
88+
const docPaths = [
89+
`${collectionGroupId}/cg-doc1`,
90+
`abc/123/${collectionGroupId}/cg-doc2`,
91+
`zzz${collectionGroupId}/cg-doc3`,
92+
`abc/123/zzz${collectionGroupId}/cg-doc4`,
93+
`abc/123/zzz/${collectionGroupId}`
94+
];
95+
const batch = writeBatch(db);
96+
for (const docPath of docPaths) {
97+
batch.set(doc(db, docPath), { x: 1 });
98+
}
99+
await batch.commit();
100+
const countQuery_ = countQuery(collectionGroup(db, collectionGroupId));
101+
const snapshot = await getAggregateFromServerDirect(countQuery_);
102+
expect(snapshot.getCount()).to.equal(2);
103+
});
104+
});
105+
106+
it('aggregate query fails if firestore is terminated', () => {
107+
return withEmptyTestCollection(persistence, async (coll, firestore) => {
108+
await terminate(firestore);
109+
const countQuery_ = countQuery(coll);
110+
expect(() => getAggregateFromServerDirect(countQuery_)).to.throw(
111+
'The client has already been terminated.'
112+
);
113+
});
114+
});
115+
116+
it("terminate doesn't crash when there is aggregate query in flight", () => {
117+
return withEmptyTestCollection(persistence, async (coll, firestore) => {
118+
const countQuery_ = countQuery(coll);
119+
void getAggregateFromServerDirect(countQuery_);
120+
await terminate(firestore);
40121
});
41122
});
42123

43124
it('getAggregateFromServerDirect fails if user is offline', () => {
44-
const testDocs = {};
45-
return withTestCollection(
125+
return withEmptyTestCollection(
46126
persistence,
47-
testDocs,
48127
async (collection, firestore) => {
49128
await disableNetwork(firestore);
50129
const countQuery_ = countQuery(collection);

packages/firestore/test/integration/util/helpers.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ import {
3131
setDoc,
3232
PrivateSettings,
3333
SnapshotListenOptions,
34-
newTestFirestore
34+
newTestFirestore,
35+
QueryDocumentSnapshot
3536
} from './firebase_export';
3637
import {
3738
ALT_PROJECT_ID,
@@ -254,6 +255,13 @@ export function withTestCollection(
254255
return withTestCollectionSettings(persistence, DEFAULT_SETTINGS, docs, fn);
255256
}
256257

258+
export function withEmptyTestCollection(
259+
persistence: boolean,
260+
fn: (collection: CollectionReference, db: Firestore) => Promise<void>
261+
): Promise<void> {
262+
return withTestCollection(persistence, {}, fn);
263+
}
264+
257265
// TODO(mikelehen): Once we wipe the database between tests, we can probably
258266
// return the same collection every time.
259267
export function withTestCollectionSettings(
@@ -280,3 +288,24 @@ export function withTestCollectionSettings(
280288
}
281289
);
282290
}
291+
292+
export class Post {
293+
constructor(
294+
readonly title: string,
295+
readonly author: string,
296+
readonly ref: DocumentReference | null = null
297+
) {}
298+
byline(): string {
299+
return this.title + ', by ' + this.author;
300+
}
301+
}
302+
303+
export const postConverter = {
304+
toFirestore(post: Post): DocumentData {
305+
return { title: post.title, author: post.author };
306+
},
307+
fromFirestore(snapshot: QueryDocumentSnapshot): Post {
308+
const data = snapshot.data();
309+
return new Post(data.title, data.author, snapshot.ref);
310+
}
311+
};

packages/firestore/test/lite/integration.test.ts

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,7 +2059,7 @@ describe('countQuery()', () => {
20592059

20602060
it('empty test collection count', () => {
20612061
return withTestCollection(async coll => {
2062-
const countQuery_ = countQuery(query(coll));
2062+
const countQuery_ = countQuery(coll);
20632063
const snapshot = await getAggregate(countQuery_);
20642064
expect(snapshot.getCount()).to.equal(0);
20652065
});
@@ -2071,8 +2071,8 @@ describe('countQuery()', () => {
20712071
{ author: 'authorA', title: 'titleB' },
20722072
{ author: 'authorB', title: 'titleC' }
20732073
];
2074-
return withTestCollectionAndInitialData(testDocs, async collection => {
2075-
const countQuery_ = countQuery(query(collection));
2074+
return withTestCollectionAndInitialData(testDocs, async coll => {
2075+
const countQuery_ = countQuery(coll);
20762076
const snapshot = await getAggregate(countQuery_);
20772077
expect(snapshot.getCount()).to.equal(3);
20782078
});
@@ -2084,8 +2084,8 @@ describe('countQuery()', () => {
20842084
{ author: 'authorA', title: 'titleB' },
20852085
{ author: 'authorB', title: 'titleC' }
20862086
];
2087-
return withTestCollectionAndInitialData(testDocs, async collection => {
2088-
const query_ = query(collection, where('author', '==', 'authorA'));
2087+
return withTestCollectionAndInitialData(testDocs, async coll => {
2088+
const query_ = query(coll, where('author', '==', 'authorA'));
20892089
const countQuery_ = countQuery(query_);
20902090
const snapshot = await getAggregate(countQuery_);
20912091
expect(snapshot.getCount()).to.equal(2);
@@ -2098,12 +2098,8 @@ describe('countQuery()', () => {
20982098
{ author: 'authorA', title: 'titleB' },
20992099
{ author: 'authorB', title: 'titleC' }
21002100
];
2101-
return withTestCollectionAndInitialData(testDocs, async collection => {
2102-
const query_ = query(
2103-
collection,
2104-
where('author', '==', 'authorA'),
2105-
limit(1)
2106-
);
2101+
return withTestCollectionAndInitialData(testDocs, async coll => {
2102+
const query_ = query(coll, where('author', '==', 'authorA'), limit(1));
21072103
const countQuery_ = countQuery(query_);
21082104
const snapshot = await getAggregate(countQuery_);
21092105
expect(snapshot.getCount()).to.equal(1);
@@ -2116,12 +2112,8 @@ describe('countQuery()', () => {
21162112
{ author: 'authorA', title: 'titleB' },
21172113
{ author: 'authorB', title: 'titleC' }
21182114
];
2119-
return withTestCollectionAndInitialData(testDocs, async collection => {
2120-
const query_ = query(
2121-
collection,
2122-
where('author', '==', 'authorA'),
2123-
limit(3)
2124-
);
2115+
return withTestCollectionAndInitialData(testDocs, async coll => {
2116+
const query_ = query(coll, where('author', '==', 'authorA'), limit(3));
21252117
const countQuery_ = countQuery(query_);
21262118
const snapshot = await getAggregate(countQuery_);
21272119
expect(snapshot.getCount()).to.equal(2);
@@ -2135,8 +2127,8 @@ describe('countQuery()', () => {
21352127
{ author: 'authorB', title: null },
21362128
{ author: 'authorB' }
21372129
];
2138-
return withTestCollectionAndInitialData(testDocs, async collection => {
2139-
const query_ = query(collection, orderBy('title'));
2130+
return withTestCollectionAndInitialData(testDocs, async coll => {
2131+
const query_ = query(coll, orderBy('title'));
21402132
const countQuery_ = countQuery(query_);
21412133
const snapshot = await getAggregate(countQuery_);
21422134
expect(snapshot.getCount()).to.equal(3);
@@ -2150,8 +2142,8 @@ describe('countQuery()', () => {
21502142
{ id: 2, author: 'authorB', title: 'titleC' },
21512143
{ id: null, author: 'authorB', title: 'titleD' }
21522144
];
2153-
return withTestCollectionAndInitialData(testDocs, async collection => {
2154-
const query_ = query(collection, orderBy('id'), startAt(2));
2145+
return withTestCollectionAndInitialData(testDocs, async coll => {
2146+
const query_ = query(coll, orderBy('id'), startAt(2));
21552147
const countQuery_ = countQuery(query_);
21562148
const snapshot = await getAggregate(countQuery_);
21572149
expect(snapshot.getCount()).to.equal(2);
@@ -2165,8 +2157,8 @@ describe('countQuery()', () => {
21652157
{ id: 2, author: 'authorB', title: 'titleC' },
21662158
{ id: null, author: 'authorB', title: 'titleD' }
21672159
];
2168-
return withTestCollectionAndInitialData(testDocs, async collection => {
2169-
const query_ = query(collection, orderBy('id'), startAfter(2));
2160+
return withTestCollectionAndInitialData(testDocs, async coll => {
2161+
const query_ = query(coll, orderBy('id'), startAfter(2));
21702162
const countQuery_ = countQuery(query_);
21712163
const snapshot = await getAggregate(countQuery_);
21722164
expect(snapshot.getCount()).to.equal(1);
@@ -2180,8 +2172,8 @@ describe('countQuery()', () => {
21802172
{ id: 2, author: 'authorB', title: 'titleC' },
21812173
{ id: null, author: 'authorB', title: 'titleD' }
21822174
];
2183-
return withTestCollectionAndInitialData(testDocs, async collection => {
2184-
const query_ = query(collection, orderBy('id'), startAt(1), endAt(2));
2175+
return withTestCollectionAndInitialData(testDocs, async coll => {
2176+
const query_ = query(coll, orderBy('id'), startAt(1), endAt(2));
21852177
const countQuery_ = countQuery(query_);
21862178
const snapshot = await getAggregate(countQuery_);
21872179
expect(snapshot.getCount()).to.equal(2);
@@ -2195,8 +2187,8 @@ describe('countQuery()', () => {
21952187
{ id: 2, author: 'authorB', title: 'titleC' },
21962188
{ id: null, author: 'authorB', title: 'titleD' }
21972189
];
2198-
return withTestCollectionAndInitialData(testDocs, async collection => {
2199-
const query_ = query(collection, orderBy('id'), startAt(1), endBefore(2));
2190+
return withTestCollectionAndInitialData(testDocs, async coll => {
2191+
const query_ = query(coll, orderBy('id'), startAt(1), endBefore(2));
22002192
const countQuery_ = countQuery(query_);
22012193
const snapshot = await getAggregate(countQuery_);
22022194
expect(snapshot.getCount()).to.equal(1);
@@ -2209,9 +2201,9 @@ describe('countQuery()', () => {
22092201
{ author: 'authorA', title: 'titleB' },
22102202
{ author: 'authorB', title: 'titleC' }
22112203
];
2212-
return withTestCollectionAndInitialData(testDocs, async collection => {
2204+
return withTestCollectionAndInitialData(testDocs, async coll => {
22132205
const query_ = query(
2214-
collection,
2206+
coll,
22152207
where('author', '==', 'authorA')
22162208
).withConverter(postConverter);
22172209
const countQuery_ = countQuery(query_);
@@ -2247,9 +2239,9 @@ describe('countQuery()', () => {
22472239
{ author: 'authorA', title: 'titleB' },
22482240
{ author: 'authorB', title: 'titleC' }
22492241
];
2250-
return withTestCollectionAndInitialData(testDocs, async collection => {
2251-
const query1 = query(collection, where('author', '==', 'authorA'));
2252-
const query2 = query(collection, where('author', '==', 'authorA'));
2242+
return withTestCollectionAndInitialData(testDocs, async coll => {
2243+
const query1 = query(coll, where('author', '==', 'authorA'));
2244+
const query2 = query(coll, where('author', '==', 'authorA'));
22532245
const countQuery1 = countQuery(query1);
22542246
const countQuery2 = countQuery(query2);
22552247
expect(aggregateQueryEqual(countQuery1, countQuery2)).to.be.true;
@@ -2262,9 +2254,9 @@ describe('countQuery()', () => {
22622254
{ author: 'authorA', title: 'titleB' },
22632255
{ author: 'authorB', title: 'titleC' }
22642256
];
2265-
return withTestCollectionAndInitialData(testDocs, async collection => {
2266-
const query1 = query(collection, where('author', '==', 'authorA'));
2267-
const query2 = query(collection, where('author', '==', 'authorB'));
2257+
return withTestCollectionAndInitialData(testDocs, async coll => {
2258+
const query1 = query(coll, where('author', '==', 'authorA'));
2259+
const query2 = query(coll, where('author', '==', 'authorB'));
22682260
const countQuery1 = countQuery(query1);
22692261
const countQuery2 = countQuery(query2);
22702262
expect(aggregateQueryEqual(countQuery1, countQuery2)).to.be.false;
@@ -2277,9 +2269,9 @@ describe('countQuery()', () => {
22772269
{ author: 'authorA', title: 'titleB' },
22782270
{ author: 'authorB', title: 'titleC' }
22792271
];
2280-
return withTestCollectionAndInitialData(testDocs, async collection => {
2281-
const query1 = query(collection, where('author', '==', 'authorA'));
2282-
const query2 = query(collection, where('author', '==', 'authorA'));
2272+
return withTestCollectionAndInitialData(testDocs, async coll => {
2273+
const query1 = query(coll, where('author', '==', 'authorA'));
2274+
const query2 = query(coll, where('author', '==', 'authorA'));
22832275
const countQuery1A = countQuery(query1);
22842276
const countQuery1B = countQuery(query1);
22852277
const countQuery2 = countQuery(query2);
@@ -2297,9 +2289,9 @@ describe('countQuery()', () => {
22972289
{ author: 'authorA', title: 'titleB' },
22982290
{ author: 'authorB', title: 'titleC' }
22992291
];
2300-
return withTestCollectionAndInitialData(testDocs, async collection => {
2301-
const query1 = query(collection, where('author', '==', 'authorA'));
2302-
const query2 = query(collection, where('author', '==', 'authorB'));
2292+
return withTestCollectionAndInitialData(testDocs, async coll => {
2293+
const query1 = query(coll, where('author', '==', 'authorA'));
2294+
const query2 = query(coll, where('author', '==', 'authorB'));
23032295
const countQuery1 = countQuery(query1);
23042296
const countQuery2 = countQuery(query2);
23052297
const snapshot1 = await getAggregate(countQuery1);
@@ -2309,9 +2301,9 @@ describe('countQuery()', () => {
23092301
});
23102302

23112303
it('count query on a terminated Firestore', () => {
2312-
return withTestCollection(async collection => {
2313-
await terminate(collection.firestore);
2314-
const countQuery_ = countQuery(query(collection));
2304+
return withTestCollection(async coll => {
2305+
await terminate(coll.firestore);
2306+
const countQuery_ = countQuery(coll);
23152307
expect(() => getAggregate(countQuery_)).to.throw(
23162308
'The client has already been terminated.'
23172309
);
@@ -2324,10 +2316,10 @@ describe('countQuery()', () => {
23242316
{ author: 'authorA', title: 'titleB' },
23252317
{ author: 'authorB', title: 'titleC' }
23262318
];
2327-
return withTestCollectionAndInitialData(testDocs, async collection => {
2328-
const countQuery_ = countQuery(query(collection));
2319+
return withTestCollectionAndInitialData(testDocs, async coll => {
2320+
const countQuery_ = countQuery(coll);
23292321
const promise = getAggregate(countQuery_);
2330-
await terminate(collection.firestore);
2322+
await terminate(coll.firestore);
23312323
const snapshot = await promise;
23322324
expect(snapshot.getCount()).to.equal(3);
23332325
});

0 commit comments

Comments
 (0)