@@ -2046,20 +2046,12 @@ describe('withConverter() support', () => {
20462046} ) ;
20472047
20482048describe ( 'countQuery()' , ( ) => {
2049- const testDocs = [
2050- { author : 'authorA' , title : 'titleA' } ,
2051- { author : 'authorA' , title : 'titleB' } ,
2052- { author : 'authorB' , title : 'titleC' } ,
2053- { author : 'authorB' , title : 'titleD' } ,
2054- { author : 'authorB' , title : 'titleE' }
2055- ] ;
2056-
20572049 it ( 'AggregateQuery and AggregateQuerySnapshot inherits the original query' , ( ) => {
20582050 return withTestCollection ( async coll => {
20592051 const query_ = query ( coll ) ;
20602052 const countQuery_ = countQuery ( query_ ) ;
2061- expect ( countQuery_ . query ) . to . equal ( query_ ) ;
20622053 const snapshot = await getAggregateFromServerDirect ( countQuery_ ) ;
2054+ expect ( countQuery_ . query ) . to . equal ( query_ ) ;
20632055 expect ( snapshot . query ) . to . equal ( countQuery_ ) ;
20642056 expect ( snapshot . query . query ) . to . equal ( query_ ) ;
20652057 } ) ;
@@ -2073,15 +2065,25 @@ describe('countQuery()', () => {
20732065 } ) ;
20742066 } ) ;
20752067
2076- it ( 'test collection count with 5 docs' , ( ) => {
2068+ it ( 'test collection count with 3 docs' , ( ) => {
2069+ const testDocs = [
2070+ { author : 'authorA' , title : 'titleA' } ,
2071+ { author : 'authorA' , title : 'titleB' } ,
2072+ { author : 'authorB' , title : 'titleC' }
2073+ ] ;
20772074 return withTestCollectionAndInitialData ( testDocs , async collection => {
20782075 const countQuery_ = countQuery ( query ( collection ) ) ;
20792076 const snapshot = await getAggregateFromServerDirect ( countQuery_ ) ;
2080- expect ( snapshot . getCount ( ) ) . to . equal ( 5 ) ;
2077+ expect ( snapshot . getCount ( ) ) . to . equal ( 3 ) ;
20812078 } ) ;
20822079 } ) ;
20832080
20842081 it ( 'test collection count with filter' , ( ) => {
2082+ const testDocs = [
2083+ { author : 'authorA' , title : 'titleA' } ,
2084+ { author : 'authorA' , title : 'titleB' } ,
2085+ { author : 'authorB' , title : 'titleC' }
2086+ ] ;
20852087 return withTestCollectionAndInitialData ( testDocs , async collection => {
20862088 const query_ = query ( collection , where ( 'author' , '==' , 'authorA' ) ) ;
20872089 const countQuery_ = countQuery ( query_ ) ;
@@ -2091,6 +2093,11 @@ describe('countQuery()', () => {
20912093 } ) ;
20922094
20932095 it ( 'test collection count with filter and a small limit size' , ( ) => {
2096+ const testDocs = [
2097+ { author : 'authorA' , title : 'titleA' } ,
2098+ { author : 'authorA' , title : 'titleB' } ,
2099+ { author : 'authorB' , title : 'titleC' }
2100+ ] ;
20942101 return withTestCollectionAndInitialData ( testDocs , async collection => {
20952102 const query_ = query (
20962103 collection ,
@@ -2104,6 +2111,11 @@ describe('countQuery()', () => {
21042111 } ) ;
21052112
21062113 it ( 'test collection count with filter and a large limit size' , ( ) => {
2114+ const testDocs = [
2115+ { author : 'authorA' , title : 'titleA' } ,
2116+ { author : 'authorA' , title : 'titleB' } ,
2117+ { author : 'authorB' , title : 'titleC' }
2118+ ] ;
21072119 return withTestCollectionAndInitialData ( testDocs , async collection => {
21082120 const query_ = query (
21092121 collection ,
@@ -2116,7 +2128,87 @@ describe('countQuery()', () => {
21162128 } ) ;
21172129 } ) ;
21182130
2131+ it ( 'count with order by' , ( ) => {
2132+ const testDocs = [
2133+ { author : 'authorA' , title : 'titleA' } ,
2134+ { author : 'authorA' , title : 'titleB' } ,
2135+ { author : 'authorB' , title : null } ,
2136+ { author : 'authorB' }
2137+ ] ;
2138+ return withTestCollectionAndInitialData ( testDocs , async collection => {
2139+ const query_ = query ( collection , orderBy ( 'title' ) ) ;
2140+ const countQuery_ = countQuery ( query_ ) ;
2141+ const snapshot = await getAggregateFromServerDirect ( countQuery_ ) ;
2142+ expect ( snapshot . getCount ( ) ) . to . equal ( 3 ) ;
2143+ } ) ;
2144+ } ) ;
2145+
2146+ it ( 'count with order by and startAt' , ( ) => {
2147+ const testDocs = [
2148+ { id : 3 , author : 'authorA' , title : 'titleA' } ,
2149+ { id : 1 , author : 'authorA' , title : 'titleB' } ,
2150+ { id : 2 , author : 'authorB' , title : 'titleC' } ,
2151+ { id : null , author : 'authorB' , title : 'titleD' }
2152+ ] ;
2153+ return withTestCollectionAndInitialData ( testDocs , async collection => {
2154+ const query_ = query ( collection , orderBy ( 'id' ) , startAt ( 2 ) ) ;
2155+ const countQuery_ = countQuery ( query_ ) ;
2156+ const snapshot = await getAggregateFromServerDirect ( countQuery_ ) ;
2157+ expect ( snapshot . getCount ( ) ) . to . equal ( 2 ) ;
2158+ } ) ;
2159+ } ) ;
2160+
2161+ it ( 'count with order by and startAfter' , ( ) => {
2162+ const testDocs = [
2163+ { id : 3 , author : 'authorA' , title : 'titleA' } ,
2164+ { id : 1 , author : 'authorA' , title : 'titleB' } ,
2165+ { id : 2 , author : 'authorB' , title : 'titleC' } ,
2166+ { id : null , author : 'authorB' , title : 'titleD' }
2167+ ] ;
2168+ return withTestCollectionAndInitialData ( testDocs , async collection => {
2169+ const query_ = query ( collection , orderBy ( 'id' ) , startAfter ( 2 ) ) ;
2170+ const countQuery_ = countQuery ( query_ ) ;
2171+ const snapshot = await getAggregateFromServerDirect ( countQuery_ ) ;
2172+ expect ( snapshot . getCount ( ) ) . to . equal ( 1 ) ;
2173+ } ) ;
2174+ } ) ;
2175+
2176+ it ( 'count with order by and endAt' , ( ) => {
2177+ const testDocs = [
2178+ { id : 3 , author : 'authorA' , title : 'titleA' } ,
2179+ { id : 1 , author : 'authorA' , title : 'titleB' } ,
2180+ { id : 2 , author : 'authorB' , title : 'titleC' } ,
2181+ { id : null , author : 'authorB' , title : 'titleD' }
2182+ ] ;
2183+ return withTestCollectionAndInitialData ( testDocs , async collection => {
2184+ const query_ = query ( collection , orderBy ( 'id' ) , startAt ( 1 ) , endAt ( 2 ) ) ;
2185+ const countQuery_ = countQuery ( query_ ) ;
2186+ const snapshot = await getAggregateFromServerDirect ( countQuery_ ) ;
2187+ expect ( snapshot . getCount ( ) ) . to . equal ( 2 ) ;
2188+ } ) ;
2189+ } ) ;
2190+
2191+ it ( 'count with order by and endBefore' , ( ) => {
2192+ const testDocs = [
2193+ { id : 3 , author : 'authorA' , title : 'titleA' } ,
2194+ { id : 1 , author : 'authorA' , title : 'titleB' } ,
2195+ { id : 2 , author : 'authorB' , title : 'titleC' } ,
2196+ { id : null , author : 'authorB' , title : 'titleD' }
2197+ ] ;
2198+ return withTestCollectionAndInitialData ( testDocs , async collection => {
2199+ const query_ = query ( collection , orderBy ( 'id' ) , startAt ( 1 ) , endBefore ( 2 ) ) ;
2200+ const countQuery_ = countQuery ( query_ ) ;
2201+ const snapshot = await getAggregateFromServerDirect ( countQuery_ ) ;
2202+ expect ( snapshot . getCount ( ) ) . to . equal ( 1 ) ;
2203+ } ) ;
2204+ } ) ;
2205+
21192206 it ( 'test collection count with converter on query' , ( ) => {
2207+ const testDocs = [
2208+ { author : 'authorA' , title : 'titleA' } ,
2209+ { author : 'authorA' , title : 'titleB' } ,
2210+ { author : 'authorB' , title : 'titleC' }
2211+ ] ;
21202212 return withTestCollectionAndInitialData ( testDocs , async collection => {
21212213 const query_ = query (
21222214 collection ,
@@ -2128,7 +2220,33 @@ describe('countQuery()', () => {
21282220 } ) ;
21292221 } ) ;
21302222
2223+ it ( 'count query with collection groups' , ( ) => {
2224+ return withTestDb ( async db => {
2225+ const collectionGroupId = doc ( collection ( db , 'countTest' ) ) . id ;
2226+ const docPaths = [
2227+ `${ collectionGroupId } /cg-doc1` ,
2228+ `abc/123/${ collectionGroupId } /cg-doc2` ,
2229+ `zzz${ collectionGroupId } /cg-doc3` ,
2230+ `abc/123/zzz${ collectionGroupId } /cg-doc4` ,
2231+ `abc/123/zzz/${ collectionGroupId } `
2232+ ] ;
2233+ const batch = writeBatch ( db ) ;
2234+ for ( const docPath of docPaths ) {
2235+ batch . set ( doc ( db , docPath ) , { x : 1 } ) ;
2236+ }
2237+ await batch . commit ( ) ;
2238+ const countQuery_ = countQuery ( collectionGroup ( db , collectionGroupId ) ) ;
2239+ const snapshot = await getAggregateFromServerDirect ( countQuery_ ) ;
2240+ expect ( snapshot . getCount ( ) ) . to . equal ( 2 ) ;
2241+ } ) ;
2242+ } ) ;
2243+
21312244 it ( 'aggregateQueryEqual on same queries' , ( ) => {
2245+ const testDocs = [
2246+ { author : 'authorA' , title : 'titleA' } ,
2247+ { author : 'authorA' , title : 'titleB' } ,
2248+ { author : 'authorB' , title : 'titleC' }
2249+ ] ;
21322250 return withTestCollectionAndInitialData ( testDocs , async collection => {
21332251 const query1 = query ( collection , where ( 'author' , '==' , 'authorA' ) ) ;
21342252 const query2 = query ( collection , where ( 'author' , '==' , 'authorA' ) ) ;
@@ -2139,6 +2257,11 @@ describe('countQuery()', () => {
21392257 } ) ;
21402258
21412259 it ( 'aggregateQueryEqual on different queries' , ( ) => {
2260+ const testDocs = [
2261+ { author : 'authorA' , title : 'titleA' } ,
2262+ { author : 'authorA' , title : 'titleB' } ,
2263+ { author : 'authorB' , title : 'titleC' }
2264+ ] ;
21422265 return withTestCollectionAndInitialData ( testDocs , async collection => {
21432266 const query1 = query ( collection , where ( 'author' , '==' , 'authorA' ) ) ;
21442267 const query2 = query ( collection , where ( 'author' , '==' , 'authorB' ) ) ;
@@ -2149,6 +2272,11 @@ describe('countQuery()', () => {
21492272 } ) ;
21502273
21512274 it ( 'aggregateQuerySnapshotEqual on same queries' , ( ) => {
2275+ const testDocs = [
2276+ { author : 'authorA' , title : 'titleA' } ,
2277+ { author : 'authorA' , title : 'titleB' } ,
2278+ { author : 'authorB' , title : 'titleC' }
2279+ ] ;
21522280 return withTestCollectionAndInitialData ( testDocs , async collection => {
21532281 const query1 = query ( collection , where ( 'author' , '==' , 'authorA' ) ) ;
21542282 const query2 = query ( collection , where ( 'author' , '==' , 'authorA' ) ) ;
@@ -2164,6 +2292,11 @@ describe('countQuery()', () => {
21642292 } ) ;
21652293
21662294 it ( 'aggregateQuerySnapshotEqual on different queries' , ( ) => {
2295+ const testDocs = [
2296+ { author : 'authorA' , title : 'titleA' } ,
2297+ { author : 'authorA' , title : 'titleB' } ,
2298+ { author : 'authorB' , title : 'titleC' }
2299+ ] ;
21672300 return withTestCollectionAndInitialData ( testDocs , async collection => {
21682301 const query1 = query ( collection , where ( 'author' , '==' , 'authorA' ) ) ;
21692302 const query2 = query ( collection , where ( 'author' , '==' , 'authorB' ) ) ;
@@ -2180,8 +2313,23 @@ describe('countQuery()', () => {
21802313 await terminate ( collection . firestore ) ;
21812314 const countQuery_ = countQuery ( query ( collection ) ) ;
21822315 expect ( ( ) => getAggregateFromServerDirect ( countQuery_ ) ) . to . throw (
2183- 'The client has already been terminated'
2316+ 'The client has already been terminated. '
21842317 ) ;
21852318 } ) ;
21862319 } ) ;
2320+
2321+ it ( 'terminate Firestore while calling count query' , ( ) => {
2322+ const testDocs = [
2323+ { author : 'authorA' , title : 'titleA' } ,
2324+ { author : 'authorA' , title : 'titleB' } ,
2325+ { author : 'authorB' , title : 'titleC' }
2326+ ] ;
2327+ return withTestCollectionAndInitialData ( testDocs , async collection => {
2328+ const countQuery_ = countQuery ( query ( collection ) ) ;
2329+ const promise = getAggregateFromServerDirect ( countQuery_ ) ;
2330+ await terminate ( collection . firestore ) ;
2331+ const snapshot = await promise ;
2332+ expect ( snapshot . getCount ( ) ) . to . equal ( 3 ) ;
2333+ } ) ;
2334+ } ) ;
21872335} ) ;
0 commit comments