@@ -2046,20 +2046,12 @@ describe('withConverter() support', () => {
2046
2046
} ) ;
2047
2047
2048
2048
describe ( '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
-
2057
2049
it ( 'AggregateQuery and AggregateQuerySnapshot inherits the original query' , ( ) => {
2058
2050
return withTestCollection ( async coll => {
2059
2051
const query_ = query ( coll ) ;
2060
2052
const countQuery_ = countQuery ( query_ ) ;
2061
- expect ( countQuery_ . query ) . to . equal ( query_ ) ;
2062
2053
const snapshot = await getAggregateFromServerDirect ( countQuery_ ) ;
2054
+ expect ( countQuery_ . query ) . to . equal ( query_ ) ;
2063
2055
expect ( snapshot . query ) . to . equal ( countQuery_ ) ;
2064
2056
expect ( snapshot . query . query ) . to . equal ( query_ ) ;
2065
2057
} ) ;
@@ -2073,15 +2065,25 @@ describe('countQuery()', () => {
2073
2065
} ) ;
2074
2066
} ) ;
2075
2067
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
+ ] ;
2077
2074
return withTestCollectionAndInitialData ( testDocs , async collection => {
2078
2075
const countQuery_ = countQuery ( query ( collection ) ) ;
2079
2076
const snapshot = await getAggregateFromServerDirect ( countQuery_ ) ;
2080
- expect ( snapshot . getCount ( ) ) . to . equal ( 5 ) ;
2077
+ expect ( snapshot . getCount ( ) ) . to . equal ( 3 ) ;
2081
2078
} ) ;
2082
2079
} ) ;
2083
2080
2084
2081
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
+ ] ;
2085
2087
return withTestCollectionAndInitialData ( testDocs , async collection => {
2086
2088
const query_ = query ( collection , where ( 'author' , '==' , 'authorA' ) ) ;
2087
2089
const countQuery_ = countQuery ( query_ ) ;
@@ -2091,6 +2093,11 @@ describe('countQuery()', () => {
2091
2093
} ) ;
2092
2094
2093
2095
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
+ ] ;
2094
2101
return withTestCollectionAndInitialData ( testDocs , async collection => {
2095
2102
const query_ = query (
2096
2103
collection ,
@@ -2104,6 +2111,11 @@ describe('countQuery()', () => {
2104
2111
} ) ;
2105
2112
2106
2113
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
+ ] ;
2107
2119
return withTestCollectionAndInitialData ( testDocs , async collection => {
2108
2120
const query_ = query (
2109
2121
collection ,
@@ -2116,7 +2128,87 @@ describe('countQuery()', () => {
2116
2128
} ) ;
2117
2129
} ) ;
2118
2130
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
+
2119
2206
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
+ ] ;
2120
2212
return withTestCollectionAndInitialData ( testDocs , async collection => {
2121
2213
const query_ = query (
2122
2214
collection ,
@@ -2128,7 +2220,33 @@ describe('countQuery()', () => {
2128
2220
} ) ;
2129
2221
} ) ;
2130
2222
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
+
2131
2244
it ( 'aggregateQueryEqual on same queries' , ( ) => {
2245
+ const testDocs = [
2246
+ { author : 'authorA' , title : 'titleA' } ,
2247
+ { author : 'authorA' , title : 'titleB' } ,
2248
+ { author : 'authorB' , title : 'titleC' }
2249
+ ] ;
2132
2250
return withTestCollectionAndInitialData ( testDocs , async collection => {
2133
2251
const query1 = query ( collection , where ( 'author' , '==' , 'authorA' ) ) ;
2134
2252
const query2 = query ( collection , where ( 'author' , '==' , 'authorA' ) ) ;
@@ -2139,6 +2257,11 @@ describe('countQuery()', () => {
2139
2257
} ) ;
2140
2258
2141
2259
it ( 'aggregateQueryEqual on different queries' , ( ) => {
2260
+ const testDocs = [
2261
+ { author : 'authorA' , title : 'titleA' } ,
2262
+ { author : 'authorA' , title : 'titleB' } ,
2263
+ { author : 'authorB' , title : 'titleC' }
2264
+ ] ;
2142
2265
return withTestCollectionAndInitialData ( testDocs , async collection => {
2143
2266
const query1 = query ( collection , where ( 'author' , '==' , 'authorA' ) ) ;
2144
2267
const query2 = query ( collection , where ( 'author' , '==' , 'authorB' ) ) ;
@@ -2149,6 +2272,11 @@ describe('countQuery()', () => {
2149
2272
} ) ;
2150
2273
2151
2274
it ( 'aggregateQuerySnapshotEqual on same queries' , ( ) => {
2275
+ const testDocs = [
2276
+ { author : 'authorA' , title : 'titleA' } ,
2277
+ { author : 'authorA' , title : 'titleB' } ,
2278
+ { author : 'authorB' , title : 'titleC' }
2279
+ ] ;
2152
2280
return withTestCollectionAndInitialData ( testDocs , async collection => {
2153
2281
const query1 = query ( collection , where ( 'author' , '==' , 'authorA' ) ) ;
2154
2282
const query2 = query ( collection , where ( 'author' , '==' , 'authorA' ) ) ;
@@ -2164,6 +2292,11 @@ describe('countQuery()', () => {
2164
2292
} ) ;
2165
2293
2166
2294
it ( 'aggregateQuerySnapshotEqual on different queries' , ( ) => {
2295
+ const testDocs = [
2296
+ { author : 'authorA' , title : 'titleA' } ,
2297
+ { author : 'authorA' , title : 'titleB' } ,
2298
+ { author : 'authorB' , title : 'titleC' }
2299
+ ] ;
2167
2300
return withTestCollectionAndInitialData ( testDocs , async collection => {
2168
2301
const query1 = query ( collection , where ( 'author' , '==' , 'authorA' ) ) ;
2169
2302
const query2 = query ( collection , where ( 'author' , '==' , 'authorB' ) ) ;
@@ -2180,8 +2313,23 @@ describe('countQuery()', () => {
2180
2313
await terminate ( collection . firestore ) ;
2181
2314
const countQuery_ = countQuery ( query ( collection ) ) ;
2182
2315
expect ( ( ) => getAggregateFromServerDirect ( countQuery_ ) ) . to . throw (
2183
- 'The client has already been terminated'
2316
+ 'The client has already been terminated. '
2184
2317
) ;
2185
2318
} ) ;
2186
2319
} ) ;
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
+ } ) ;
2187
2335
} ) ;
0 commit comments