diff --git a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java index 6aec26e9a..35a19f5df 100644 --- a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java +++ b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/LocalFirestoreHelper.java @@ -341,24 +341,24 @@ public static Answer queryResponseWithDone( } } - public static Answer aggregationQueryResponse() { - return aggregationQueryResponse(42); + public static Answer countQueryResponse() { + return countQueryResponse(42); } - public static Answer aggregationQueryResponse(int count) { - return aggregationQueryResponse(count, null); + public static Answer countQueryResponse(int count) { + return countQueryResponse(count, null); } - public static Answer aggregationQueryResponse( + public static Answer countQueryResponse( int count, @Nullable Timestamp readTime) { return streamingResponse( new RunAggregationQueryResponse[] { - createRunAggregationQueryResponse(count, readTime), + createCountQueryResponse(count, readTime), }, /*throwable=*/ null); } - public static Answer aggregationQueryResponse(Throwable throwable) { + public static Answer countQueryResponse(Throwable throwable) { return streamingResponse(new RunAggregationQueryResponse[] {}, throwable); } @@ -366,8 +366,7 @@ public static Answer aggregationQueryResponses( int count1, int count2) { return streamingResponse( new RunAggregationQueryResponse[] { - createRunAggregationQueryResponse(count1, null), - createRunAggregationQueryResponse(count2, null), + createCountQueryResponse(count1, null), createCountQueryResponse(count2, null), }, /*throwable=*/ null); } @@ -376,17 +375,17 @@ public static Answer aggregationQueryResponses( int count1, Throwable throwable) { return streamingResponse( new RunAggregationQueryResponse[] { - createRunAggregationQueryResponse(count1, null), + createCountQueryResponse(count1, null), }, throwable); } - private static RunAggregationQueryResponse createRunAggregationQueryResponse( + private static RunAggregationQueryResponse createCountQueryResponse( int count, @Nullable Timestamp timestamp) { RunAggregationQueryResponse.Builder builder = RunAggregationQueryResponse.newBuilder(); builder.setResult( AggregationResult.newBuilder() - .putAggregateFields("count", Value.newBuilder().setIntegerValue(count).build()) + .putAggregateFields("aggregate_0", Value.newBuilder().setIntegerValue(count).build()) .build()); if (timestamp != null) { builder.setReadTime(timestamp.toProto()); @@ -751,11 +750,11 @@ public static RunQueryRequest query( return request.build(); } - public static RunAggregationQueryRequest aggregationQuery() { - return aggregationQuery((String) null); + public static RunAggregationQueryRequest countQuery() { + return countQuery((String) null); } - public static RunAggregationQueryRequest aggregationQuery(@Nullable String transactionId) { + public static RunAggregationQueryRequest countQuery(@Nullable String transactionId) { RunQueryRequest runQueryRequest = query(TRANSACTION_ID, false); RunAggregationQueryRequest.Builder request = @@ -766,7 +765,7 @@ public static RunAggregationQueryRequest aggregationQuery(@Nullable String trans .setStructuredQuery(runQueryRequest.getStructuredQuery()) .addAggregations( Aggregation.newBuilder() - .setAlias("count") + .setAlias("aggregate_0") .setCount(Aggregation.Count.getDefaultInstance()))); if (transactionId != null) { @@ -776,7 +775,7 @@ public static RunAggregationQueryRequest aggregationQuery(@Nullable String trans return request.build(); } - public static RunAggregationQueryRequest aggregationQuery(RunQueryRequest runQueryRequest) { + public static RunAggregationQueryRequest countQuery(RunQueryRequest runQueryRequest) { return RunAggregationQueryRequest.newBuilder() .setParent(runQueryRequest.getParent()) .setStructuredAggregationQuery( @@ -784,7 +783,7 @@ public static RunAggregationQueryRequest aggregationQuery(RunQueryRequest runQue .setStructuredQuery(runQueryRequest.getStructuredQuery()) .addAggregations( Aggregation.newBuilder() - .setAlias("count") + .setAlias("aggregate_0") .setCount(Aggregation.Count.getDefaultInstance()))) .build(); } diff --git a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/QueryCountTest.java b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/QueryCountTest.java index ea9dad975..00dafb778 100644 --- a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/QueryCountTest.java +++ b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/QueryCountTest.java @@ -17,9 +17,8 @@ package com.google.cloud.firestore; import static com.google.cloud.firestore.LocalFirestoreHelper.COLLECTION_ID; -import static com.google.cloud.firestore.LocalFirestoreHelper.aggregationQuery; -import static com.google.cloud.firestore.LocalFirestoreHelper.aggregationQueryResponse; import static com.google.cloud.firestore.LocalFirestoreHelper.aggregationQueryResponses; +import static com.google.cloud.firestore.LocalFirestoreHelper.countQueryResponse; import static com.google.cloud.firestore.LocalFirestoreHelper.endAt; import static com.google.cloud.firestore.LocalFirestoreHelper.limit; import static com.google.cloud.firestore.LocalFirestoreHelper.order; @@ -78,7 +77,7 @@ public void before() { @Test public void countShouldBeZeroForEmptyCollection() throws Exception { - doAnswer(aggregationQueryResponse(0)) + doAnswer(LocalFirestoreHelper.countQueryResponse(0)) .when(firestoreMock) .streamRequest( runAggregationQuery.capture(), @@ -92,7 +91,7 @@ public void countShouldBeZeroForEmptyCollection() throws Exception { @Test public void countShouldBe99ForCollectionWith99Documents() throws Exception { - doAnswer(aggregationQueryResponse(99)) + doAnswer(LocalFirestoreHelper.countQueryResponse(99)) .when(firestoreMock) .streamRequest( runAggregationQuery.capture(), @@ -106,7 +105,7 @@ public void countShouldBe99ForCollectionWith99Documents() throws Exception { @Test public void countShouldMakeCorrectRequestForACollection() throws Exception { - doAnswer(aggregationQueryResponse(0)) + doAnswer(LocalFirestoreHelper.countQueryResponse(0)) .when(firestoreMock) .streamRequest( runAggregationQuery.capture(), @@ -116,12 +115,12 @@ public void countShouldMakeCorrectRequestForACollection() throws Exception { CollectionReference collection = firestoreMock.collection(COLLECTION_ID); collection.count().get(); - assertThat(runAggregationQuery.getValue()).isEqualTo(aggregationQuery()); + assertThat(runAggregationQuery.getValue()).isEqualTo(LocalFirestoreHelper.countQuery()); } @Test public void countShouldMakeCorrectRequestForAComplexQuery() throws Exception { - doAnswer(aggregationQueryResponse(0)) + doAnswer(LocalFirestoreHelper.countQueryResponse(0)) .when(firestoreMock) .streamRequest( runAggregationQuery.capture(), @@ -132,7 +131,7 @@ public void countShouldMakeCorrectRequestForAComplexQuery() throws Exception { assertThat(runAggregationQuery.getValue()) .isEqualTo( - aggregationQuery( + LocalFirestoreHelper.countQuery( query( limit(42), order("foo", StructuredQuery.Direction.DESCENDING), @@ -142,7 +141,7 @@ public void countShouldMakeCorrectRequestForAComplexQuery() throws Exception { @Test public void shouldReturnReadTimeFromResponse() throws Exception { - doAnswer(aggregationQueryResponse(99, Timestamp.ofTimeSecondsAndNanos(123, 456))) + doAnswer(LocalFirestoreHelper.countQueryResponse(99, Timestamp.ofTimeSecondsAndNanos(123, 456))) .when(firestoreMock) .streamRequest( runAggregationQuery.capture(), @@ -185,7 +184,7 @@ public void shouldIgnoreExtraErrors() throws Exception { @Test public void shouldPropagateErrors() throws Exception { Exception exception = new Exception(); - doAnswer(aggregationQueryResponse(exception)) + doAnswer(countQueryResponse(exception)) .when(firestoreMock) .streamRequest( runAggregationQuery.capture(), @@ -205,7 +204,7 @@ public void aggregateQueryGetQueryShouldReturnCorrectValue() throws Exception { @Test public void aggregateQuerySnapshotGetQueryShouldReturnCorrectValue() throws Exception { - doAnswer(aggregationQueryResponse()) + doAnswer(LocalFirestoreHelper.countQueryResponse()) .when(firestoreMock) .streamRequest( runAggregationQuery.capture(), @@ -220,8 +219,8 @@ public void aggregateQuerySnapshotGetQueryShouldReturnCorrectValue() throws Exce @Test public void shouldNotRetryIfExceptionIsNotFirestoreException() { - doAnswer(aggregationQueryResponse(new NotFirestoreException())) - .doAnswer(aggregationQueryResponse()) + doAnswer(countQueryResponse(new NotFirestoreException())) + .doAnswer(LocalFirestoreHelper.countQueryResponse()) .when(firestoreMock) .streamRequest( runAggregationQuery.capture(), @@ -235,8 +234,8 @@ public void shouldNotRetryIfExceptionIsNotFirestoreException() { @Test public void shouldRetryIfExceptionIsFirestoreExceptionWithRetryableStatus() throws Exception { - doAnswer(aggregationQueryResponse(new FirestoreException("reason", Status.INTERNAL))) - .doAnswer(aggregationQueryResponse(42)) + doAnswer(countQueryResponse(new FirestoreException("reason", Status.INTERNAL))) + .doAnswer(LocalFirestoreHelper.countQueryResponse(42)) .when(firestoreMock) .streamRequest( runAggregationQuery.capture(), @@ -252,8 +251,8 @@ public void shouldRetryIfExceptionIsFirestoreExceptionWithRetryableStatus() thro @Test public void shouldNotRetryIfExceptionIsFirestoreExceptionWithNonRetryableStatus() { doReturn(Duration.ZERO).when(firestoreMock).getTotalRequestTimeout(); - doAnswer(aggregationQueryResponse(new FirestoreException("reason", Status.INVALID_ARGUMENT))) - .doAnswer(aggregationQueryResponse()) + doAnswer(countQueryResponse(new FirestoreException("reason", Status.INVALID_ARGUMENT))) + .doAnswer(LocalFirestoreHelper.countQueryResponse()) .when(firestoreMock) .streamRequest( runAggregationQuery.capture(), @@ -270,8 +269,8 @@ public void shouldNotRetryIfExceptionIsFirestoreExceptionWithNonRetryableStatus( shouldRetryIfExceptionIsFirestoreExceptionWithRetryableStatusWithInfiniteTimeoutWindow() throws Exception { doReturn(Duration.ZERO).when(firestoreMock).getTotalRequestTimeout(); - doAnswer(aggregationQueryResponse(new FirestoreException("reason", Status.INTERNAL))) - .doAnswer(aggregationQueryResponse(42)) + doAnswer(countQueryResponse(new FirestoreException("reason", Status.INTERNAL))) + .doAnswer(LocalFirestoreHelper.countQueryResponse(42)) .when(firestoreMock) .streamRequest( runAggregationQuery.capture(), @@ -288,8 +287,8 @@ public void shouldNotRetryIfExceptionIsFirestoreExceptionWithNonRetryableStatus( public void shouldRetryIfExceptionIsFirestoreExceptionWithRetryableStatusWithinTimeoutWindow() throws Exception { doReturn(Duration.ofDays(999)).when(firestoreMock).getTotalRequestTimeout(); - doAnswer(aggregationQueryResponse(new FirestoreException("reason", Status.INTERNAL))) - .doAnswer(aggregationQueryResponse(42)) + doAnswer(countQueryResponse(new FirestoreException("reason", Status.INTERNAL))) + .doAnswer(LocalFirestoreHelper.countQueryResponse(42)) .when(firestoreMock) .streamRequest( runAggregationQuery.capture(), @@ -313,8 +312,8 @@ public void shouldRetryIfExceptionIsFirestoreExceptionWithRetryableStatusWithinT .when(clockMock) .nanoTime(); doReturn(Duration.ofSeconds(5)).when(firestoreMock).getTotalRequestTimeout(); - doAnswer(aggregationQueryResponse(new FirestoreException("reason", Status.INTERNAL))) - .doAnswer(aggregationQueryResponse(42)) + doAnswer(countQueryResponse(new FirestoreException("reason", Status.INTERNAL))) + .doAnswer(LocalFirestoreHelper.countQueryResponse(42)) .when(firestoreMock) .streamRequest( runAggregationQuery.capture(), diff --git a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/TransactionTest.java b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/TransactionTest.java index 7f9200222..dd791cef5 100644 --- a/google-cloud-firestore/src/test/java/com/google/cloud/firestore/TransactionTest.java +++ b/google-cloud-firestore/src/test/java/com/google/cloud/firestore/TransactionTest.java @@ -19,12 +19,11 @@ import static com.google.cloud.firestore.LocalFirestoreHelper.IMMEDIATE_RETRY_SETTINGS; import static com.google.cloud.firestore.LocalFirestoreHelper.SINGLE_FIELD_PROTO; import static com.google.cloud.firestore.LocalFirestoreHelper.TRANSACTION_ID; -import static com.google.cloud.firestore.LocalFirestoreHelper.aggregationQuery; -import static com.google.cloud.firestore.LocalFirestoreHelper.aggregationQueryResponse; import static com.google.cloud.firestore.LocalFirestoreHelper.begin; import static com.google.cloud.firestore.LocalFirestoreHelper.beginResponse; import static com.google.cloud.firestore.LocalFirestoreHelper.commit; import static com.google.cloud.firestore.LocalFirestoreHelper.commitResponse; +import static com.google.cloud.firestore.LocalFirestoreHelper.countQuery; import static com.google.cloud.firestore.LocalFirestoreHelper.create; import static com.google.cloud.firestore.LocalFirestoreHelper.delete; import static com.google.cloud.firestore.LocalFirestoreHelper.get; @@ -678,7 +677,7 @@ public void getAggregateQuery() throws Exception { .when(firestoreMock) .sendRequest(requestCapture.capture(), Matchers.>any()); - doAnswer(aggregationQueryResponse(42)) + doAnswer(LocalFirestoreHelper.countQueryResponse(42)) .when(firestoreMock) .streamRequest(requestCapture.capture(), streamObserverCapture.capture(), Matchers.any()); @@ -691,7 +690,7 @@ public void getAggregateQuery() throws Exception { assertEquals(3, requests.size()); assertEquals(begin(), requests.get(0)); - assertEquals(aggregationQuery(TRANSACTION_ID), requests.get(1)); + assertEquals(countQuery(TRANSACTION_ID), requests.get(1)); assertEquals(commit(TRANSACTION_ID), requests.get(2)); }