Skip to content

Commit

Permalink
fix: Using namespace from DatastoreOptions if aggregation query is no…
Browse files Browse the repository at this point in the history
…t configured with one. (#1055)

fix #1054
  • Loading branch information
jainsahab committed Apr 27, 2023
1 parent 207a04e commit ac21ef6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.cloud.datastore.ReadOptionProtoPreparer;
import com.google.cloud.datastore.StructuredQueryProtoPreparer;
import com.google.cloud.datastore.aggregation.Aggregation;
import com.google.common.base.MoreObjects;
import com.google.datastore.v1.GqlQuery;
import com.google.datastore.v1.PartitionId;
import com.google.datastore.v1.Query;
Expand Down Expand Up @@ -92,9 +93,9 @@ private com.google.datastore.v1.AggregationQuery getAggregationQuery(
private PartitionId getPartitionId(AggregationQuery aggregationQuery) {
PartitionId.Builder builder =
PartitionId.newBuilder().setProjectId(datastoreOptions.getProjectId());
if (aggregationQuery.getNamespace() != null) {
builder.setNamespaceId(aggregationQuery.getNamespace());
}
String namespace =
MoreObjects.firstNonNull(aggregationQuery.getNamespace(), datastoreOptions.getNamespace());
builder.setNamespaceId(namespace);
return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void shouldPrepareReadOptionsWithGivenGqlQuery() {
}

@Test
public void shouldPrepareAggregationQueryWithoutNamespace() {
public void shouldPrepareAggregationQueryWithNamespaceFromDatastoreOptions() {
AggregationQuery structuredQueryWithoutNamespace =
Query.newAggregationQueryBuilder()
.addAggregation(count().as("total"))
Expand All @@ -169,8 +169,9 @@ public void shouldPrepareAggregationQueryWithoutNamespace() {
protoPreparer.prepare(QueryAndReadOptions.create(gqlQueryWithoutNamespace));

assertThat(runAggregationQueryFromStructuredQuery.getPartitionId().getNamespaceId())
.isEqualTo("");
assertThat(runAggregationQueryFromGqlQuery.getPartitionId().getNamespaceId()).isEqualTo("");
.isEqualTo(NAMESPACE);
assertThat(runAggregationQueryFromGqlQuery.getPartitionId().getNamespaceId())
.isEqualTo(NAMESPACE);
}

private RunAggregationQueryRequest prepareQuery(AggregationQuery query, ReadOption readOption) {
Expand Down

0 comments on commit ac21ef6

Please sign in to comment.