-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for count(*) aggregation query firestore
- Loading branch information
Showing
12 changed files
with
503 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
apis/Google.Cloud.Firestore/Google.Cloud.Firestore.Tests/AggregateQuerySnapshotTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Google.Cloud.ClientTesting; | ||
using Xunit; | ||
using static Google.Cloud.Firestore.Tests.ProtoHelpers; | ||
|
||
namespace Google.Cloud.Firestore.Tests; | ||
public class AggregateQuerySnapshotTest | ||
{ | ||
private static readonly FirestoreDb s_db = FirestoreDb.Create("proj", "db", new FakeFirestoreClient()); | ||
|
||
[Fact] | ||
public void VerifyAllMembers() | ||
{ | ||
AggregateQuery aggQuery = s_db.Collection("col").Count(); | ||
var sampleReadTime = Timestamp.FromProto(CreateProtoTimestamp(1, 2)); | ||
int sampleCount = 10; | ||
var aggQuerySnapshot = new AggregateQuerySnapshot(aggQuery, sampleReadTime, sampleCount); | ||
Assert.Equal(sampleCount, aggQuerySnapshot.Count); | ||
Assert.Equal(sampleReadTime, aggQuerySnapshot.ReadTime); | ||
Assert.Equal(aggQuery, aggQuerySnapshot.Query); | ||
} | ||
|
||
[Fact] | ||
public void Equality() | ||
{ | ||
AggregateQuery aggQuery1 = s_db.Collection("col").Count(); | ||
AggregateQuery aggQuery2 = s_db.Collection("col1").Count(); | ||
var sampleReadTime = Timestamp.FromProto(CreateProtoTimestamp(1, 2)); | ||
int sampleCount = 10; | ||
var control = new AggregateQuerySnapshot(aggQuery1, sampleReadTime, sampleCount); | ||
|
||
EqualityTester.AssertEqual(control, | ||
equal: new[] { | ||
// All the members are equal. | ||
new AggregateQuerySnapshot(s_db.Collection("col").Count(), Timestamp.FromProto(CreateProtoTimestamp(1, 2)), 10) | ||
}, | ||
unequal: new[] { | ||
// Unequal aggregate query | ||
new AggregateQuerySnapshot(aggQuery2, sampleReadTime, sampleCount), | ||
// Null count. | ||
new AggregateQuerySnapshot(aggQuery2, sampleReadTime, null), | ||
// Same aggregate query and count but different read time. | ||
new AggregateQuerySnapshot(s_db.Collection("col").Count(), Timestamp.FromProto(CreateProtoTimestamp(3, 4)), sampleCount), | ||
// Same aggregate query but different read time and count. | ||
new AggregateQuerySnapshot(s_db.Collection("col").Count(), Timestamp.FromProto(CreateProtoTimestamp(3, 4)), 20) | ||
}); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
apis/Google.Cloud.Firestore/Google.Cloud.Firestore.Tests/AggregateQueryTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Google.Api.Gax.Grpc; | ||
using Google.Cloud.ClientTesting; | ||
using Google.Cloud.Firestore.V1; | ||
using Moq; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using static Google.Cloud.Firestore.Tests.ProtoHelpers; | ||
using static Google.Cloud.Firestore.V1.StructuredAggregationQuery.Types; | ||
using static Google.Cloud.Firestore.V1.StructuredAggregationQuery.Types.Aggregation.Types; | ||
|
||
namespace Google.Cloud.Firestore.Tests; | ||
public class AggregateQueryTest | ||
{ | ||
private static readonly FirestoreDb s_db = FirestoreDb.Create("proj", "db", new FakeFirestoreClient()); | ||
private static readonly Query s_query = s_db.Collection("col"); | ||
|
||
[Fact] | ||
public void TestToStructuredAggregationQuery() | ||
{ | ||
var expectedStructuredAggregationQuery = new StructuredAggregationQuery | ||
{ | ||
StructuredQuery = s_query.ToStructuredQuery(), | ||
Aggregations = { new Aggregation { Alias = "Count", Count = new Count() } } | ||
}; | ||
Assert.Equal(expectedStructuredAggregationQuery, s_query.Count().ToStructuredAggregationQuery()); | ||
} | ||
|
||
[Fact] | ||
public async Task GetSnapshotAsync_VerifySnapshotMembers() | ||
{ | ||
Mock<FirestoreClient> mock = new() { CallBase = true }; | ||
var db = FirestoreDb.Create("proj", "db", mock.Object); | ||
var query = db.Collection("col").Select("Name"); | ||
var sampleReadTime = CreateProtoTimestamp(1, 3); | ||
var request = new RunAggregationQueryRequest | ||
{ | ||
Parent = query.ParentPath, | ||
StructuredAggregationQuery = new StructuredAggregationQuery | ||
{ | ||
StructuredQuery = query.ToStructuredQuery(), | ||
Aggregations = { new Aggregation { Alias = "Count", Count = new Count() } } | ||
} | ||
}; | ||
var response = new FakeAggregationQueryStream(new[] | ||
{ | ||
new RunAggregationQueryResponse { ReadTime = sampleReadTime, Result = new AggregationResult() } | ||
}); | ||
mock.Setup(c => c.RunAggregationQuery(request, It.IsAny<CallSettings>())).Returns(response); | ||
var aggregateQuery = query.Count(); | ||
var snapshot = await aggregateQuery.GetSnapshotAsync(); | ||
Assert.Equal(aggregateQuery, snapshot.Query); | ||
Assert.Equal(Timestamp.FromProto(sampleReadTime), snapshot.ReadTime); | ||
} | ||
|
||
[Fact] | ||
public void Equality() | ||
{ | ||
var control = s_query.Count(); | ||
EqualityTester.AssertEqual(control, | ||
equal: new[] | ||
{ | ||
new AggregateQuery(s_db.Collection("col")).WithAggregation(Aggregates.CreateCountAggregate()), | ||
// Aggregate query returned by count. | ||
s_db.Collection("col").Count() | ||
}, | ||
unequal: new[] | ||
{ | ||
// same query but without any aggregation. | ||
new AggregateQuery(s_db.Collection("col")), | ||
// un-equal query. | ||
new AggregateQuery(s_db.Collection("col1")) | ||
}); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
apis/Google.Cloud.Firestore/Google.Cloud.Firestore.Tests/FakeAggregationQueryStream.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"). | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Google.Api.Gax.Grpc.Testing; | ||
using Google.Cloud.Firestore.V1; | ||
using Grpc.Core; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using static Google.Cloud.Firestore.V1.FirestoreClient; | ||
|
||
namespace Google.Cloud.Firestore.Tests; | ||
|
||
/// <summary> | ||
/// An aggregation query stream that simply returns the responses provided at construction time. | ||
/// </summary> | ||
internal class FakeAggregationQueryStream : RunAggregationQueryStream | ||
{ | ||
internal FakeAggregationQueryStream(IEnumerable<RunAggregationQueryResponse> responses) | ||
{ | ||
var adapter = new AsyncStreamAdapter<RunAggregationQueryResponse>(responses.ToAsyncEnumerable().GetAsyncEnumerator(default)); | ||
GrpcCall = new AsyncServerStreamingCall<RunAggregationQueryResponse>(adapter, null, null, null, () => { }); | ||
} | ||
|
||
public override AsyncServerStreamingCall<RunAggregationQueryResponse> GrpcCall { get; } | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.