Skip to content

Commit 475b718

Browse files
committed
Public Count
1 parent ed10eb5 commit 475b718

File tree

5 files changed

+21
-37
lines changed

5 files changed

+21
-37
lines changed

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/CountTest.java

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,12 @@
3030
import androidx.test.ext.junit.runners.AndroidJUnit4;
3131
import com.google.firebase.firestore.testutil.IntegrationTestUtil;
3232
import org.junit.After;
33-
import org.junit.Before;
3433
import org.junit.Test;
3534
import org.junit.runner.RunWith;
3635

3736
@RunWith(AndroidJUnit4.class)
3837
public class CountTest {
3938

40-
@Before
41-
public void setUp() {
42-
// TODO(b/243368243): Remove this once backend is ready to support count.
43-
org.junit.Assume.assumeTrue(BuildConfig.USE_EMULATOR_FOR_TESTS);
44-
}
45-
4639
@After
4740
public void tearDown() {
4841
IntegrationTestUtil.tearDown();
@@ -88,8 +81,7 @@ public void testCanRunCount() {
8881
"b", map("k", "b"),
8982
"c", map("k", "c")));
9083

91-
AggregateQuerySnapshot snapshot =
92-
waitFor(collection.count().get(AggregateSource.SERVER_DIRECT));
84+
AggregateQuerySnapshot snapshot = waitFor(collection.count().get(AggregateSource.SERVER));
9385
assertEquals(Long.valueOf(3), snapshot.getCount());
9486
}
9587

@@ -103,7 +95,7 @@ public void testCanRunCountWithFilters() {
10395
"c", map("k", "c")));
10496

10597
AggregateQuerySnapshot snapshot =
106-
waitFor(collection.whereEqualTo("k", "b").count().get(AggregateSource.SERVER_DIRECT));
98+
waitFor(collection.whereEqualTo("k", "b").count().get(AggregateSource.SERVER));
10799
assertEquals(Long.valueOf(1), snapshot.getCount());
108100
}
109101

@@ -118,7 +110,7 @@ public void testCanRunCountWithOrderBy() {
118110
"d", map("absent", "d")));
119111

120112
AggregateQuerySnapshot snapshot =
121-
waitFor(collection.orderBy("k").count().get(AggregateSource.SERVER_DIRECT));
113+
waitFor(collection.orderBy("k").count().get(AggregateSource.SERVER));
122114
// "d" is filtered out because it is ordered by "k".
123115
assertEquals(Long.valueOf(3), snapshot.getCount());
124116
}
@@ -132,7 +124,7 @@ public void testTerminateDoesNotCrashWithFlyingCountQuery() {
132124
"b", map("k", "b"),
133125
"c", map("k", "c")));
134126

135-
collection.orderBy("k").count().get(AggregateSource.SERVER_DIRECT);
127+
collection.orderBy("k").count().get(AggregateSource.SERVER);
136128
waitFor(collection.firestore.terminate());
137129
}
138130

@@ -146,15 +138,15 @@ public void testSnapshotEquals() {
146138
"c", map("k", "c")));
147139

148140
AggregateQuerySnapshot snapshot1 =
149-
waitFor(collection.whereEqualTo("k", "b").count().get(AggregateSource.SERVER_DIRECT));
141+
waitFor(collection.whereEqualTo("k", "b").count().get(AggregateSource.SERVER));
150142
AggregateQuerySnapshot snapshot1_same =
151-
waitFor(collection.whereEqualTo("k", "b").count().get(AggregateSource.SERVER_DIRECT));
143+
waitFor(collection.whereEqualTo("k", "b").count().get(AggregateSource.SERVER));
152144

153145
AggregateQuerySnapshot snapshot2 =
154-
waitFor(collection.whereEqualTo("k", "a").count().get(AggregateSource.SERVER_DIRECT));
146+
waitFor(collection.whereEqualTo("k", "a").count().get(AggregateSource.SERVER));
155147
waitFor(collection.document("d").set(map("k", "a")));
156148
AggregateQuerySnapshot snapshot2_different =
157-
waitFor(collection.whereEqualTo("k", "a").count().get(AggregateSource.SERVER_DIRECT));
149+
waitFor(collection.whereEqualTo("k", "a").count().get(AggregateSource.SERVER));
158150

159151
assertTrue(snapshot1.equals(snapshot1_same));
160152
assertEquals(snapshot1.hashCode(), snapshot1_same.hashCode());
@@ -196,7 +188,7 @@ public void testCanRunCollectionGroupQuery() {
196188
waitFor(batch.commit());
197189

198190
AggregateQuerySnapshot snapshot =
199-
waitFor(db.collectionGroup(collectionGroup).count().get(AggregateSource.SERVER_DIRECT));
191+
waitFor(db.collectionGroup(collectionGroup).count().get(AggregateSource.SERVER));
200192
assertEquals(
201193
Long.valueOf(5), // "cg-doc1", "cg-doc2", "cg-doc3", "cg-doc4", "cg-doc5",
202194
snapshot.getCount());
@@ -213,17 +205,12 @@ public void testCanRunCountWithFiltersAndLimits() {
213205
"d", map("k", "d")));
214206

215207
AggregateQuerySnapshot snapshot =
216-
waitFor(
217-
collection.whereEqualTo("k", "a").limit(2).count().get(AggregateSource.SERVER_DIRECT));
208+
waitFor(collection.whereEqualTo("k", "a").limit(2).count().get(AggregateSource.SERVER));
218209
assertEquals(Long.valueOf(2), snapshot.getCount());
219210

220211
snapshot =
221212
waitFor(
222-
collection
223-
.whereEqualTo("k", "a")
224-
.limitToLast(2)
225-
.count()
226-
.get(AggregateSource.SERVER_DIRECT));
213+
collection.whereEqualTo("k", "a").limitToLast(2).count().get(AggregateSource.SERVER));
227214
assertEquals(Long.valueOf(2), snapshot.getCount());
228215

229216
snapshot =
@@ -232,20 +219,18 @@ public void testCanRunCountWithFiltersAndLimits() {
232219
.whereEqualTo("k", "d")
233220
.limitToLast(1000)
234221
.count()
235-
.get(AggregateSource.SERVER_DIRECT));
222+
.get(AggregateSource.SERVER));
236223
assertEquals(Long.valueOf(1), snapshot.getCount());
237224
}
238225

239226
@Test
240227
public void testCanRunCountOnNonExistentCollection() {
241228
CollectionReference collection = testFirestore().collection("random-coll");
242229

243-
AggregateQuerySnapshot snapshot =
244-
waitFor(collection.count().get(AggregateSource.SERVER_DIRECT));
230+
AggregateQuerySnapshot snapshot = waitFor(collection.count().get(AggregateSource.SERVER));
245231
assertEquals(Long.valueOf(0), snapshot.getCount());
246232

247-
snapshot =
248-
waitFor(collection.whereEqualTo("k", 100).count().get(AggregateSource.SERVER_DIRECT));
233+
snapshot = waitFor(collection.whereEqualTo("k", 100).count().get(AggregateSource.SERVER));
249234
assertEquals(Long.valueOf(0), snapshot.getCount());
250235
}
251236

@@ -259,14 +244,13 @@ public void testFailWithoutNetwork() {
259244
"c", map("k", "c")));
260245
waitFor(collection.getFirestore().disableNetwork());
261246

262-
Exception e = waitForException(collection.count().get(AggregateSource.SERVER_DIRECT));
247+
Exception e = waitForException(collection.count().get(AggregateSource.SERVER));
263248
assertThat(e, instanceOf(FirebaseFirestoreException.class));
264249
assertEquals(
265250
FirebaseFirestoreException.Code.UNAVAILABLE, ((FirebaseFirestoreException) e).getCode());
266251

267252
waitFor(collection.getFirestore().enableNetwork());
268-
AggregateQuerySnapshot snapshot =
269-
waitFor(collection.count().get(AggregateSource.SERVER_DIRECT));
253+
AggregateQuerySnapshot snapshot = waitFor(collection.count().get(AggregateSource.SERVER));
270254
assertEquals(Long.valueOf(3), snapshot.getCount());
271255
}
272256
}

firebase-firestore/src/main/java/com/google/firebase/firestore/AggregateQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* in test mocks. Subclassing is not supported in production code and new SDK releases may break
2929
* code that does so.
3030
*/
31-
class AggregateQuery {
31+
public class AggregateQuery {
3232
// The base query.
3333
private final Query query;
3434

firebase-firestore/src/main/java/com/google/firebase/firestore/AggregateQuerySnapshot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* in test mocks. Subclassing is not supported in production code and new SDK releases may break
2828
* code that does so.
2929
*/
30-
class AggregateQuerySnapshot {
30+
public class AggregateQuerySnapshot {
3131

3232
private final long count;
3333
private final AggregateQuery query;

firebase-firestore/src/main/java/com/google/firebase/firestore/AggregateSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
package com.google.firebase.firestore;
1616

1717
/** Configures the behavior of {@link AggregateQuery#get}. */
18-
enum AggregateSource {
18+
public enum AggregateSource {
1919
/**
2020
* Reach to the Firestore backend and surface the result verbatim, that is no local documents or
2121
* mutations in the SDK cache will be included in the surfaced result.
2222
*
2323
* <p>Requires client to be online.
2424
*/
25-
SERVER_DIRECT,
25+
SERVER,
2626
}

firebase-firestore/src/main/java/com/google/firebase/firestore/Query.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,7 @@ private void validateHasExplicitOrderByForLimitToLast() {
12301230
* the result set of this query.
12311231
*/
12321232
@NonNull
1233-
AggregateQuery count() {
1233+
public AggregateQuery count() {
12341234
return new AggregateQuery(this);
12351235
}
12361236

0 commit comments

Comments
 (0)