Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add sum and average aggregates
- Loading branch information
Showing
14 changed files
with
901 additions
and
99 deletions.
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
apis/Google.Cloud.Firestore/Google.Cloud.Firestore.IntegrationTests/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,147 @@ | ||
// Copyright 2023 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.Firestore.IntegrationTests.Models; | ||
using Google.Cloud.Firestore.V1; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
using static Google.Cloud.Firestore.AggregateField; | ||
|
||
namespace Google.Cloud.Firestore.IntegrationTests; | ||
|
||
[Collection(nameof(FirestoreFixture))] | ||
public class AggregateQueryTest | ||
{ | ||
private readonly FirestoreFixture _fixture; | ||
|
||
public AggregateQueryTest(FirestoreFixture fixture) => _fixture = fixture; | ||
|
||
[Fact] | ||
public async Task Count_WithoutLimit() | ||
{ | ||
CollectionReference collection = _fixture.HighScoreCollection; | ||
var snapshot = await collection.Count().GetSnapshotAsync(); | ||
Assert.Equal(HighScore.Data.Length, snapshot.Count); | ||
} | ||
|
||
[Fact] | ||
public async Task Count_WithLimit() | ||
{ | ||
CollectionReference collection = _fixture.HighScoreCollection; | ||
var snapshotWithoutLimit = await collection.Count().GetSnapshotAsync(); | ||
var snapshotWithLimit = await collection.Limit(2).Count().GetSnapshotAsync(); | ||
Assert.Equal(HighScore.Data.Length, snapshotWithoutLimit.Count); | ||
Assert.Equal(2, snapshotWithLimit.Count); | ||
} | ||
|
||
[Fact] | ||
public async Task Sum_ResultTypes() | ||
{ | ||
var db = _fixture.FirestoreDb; | ||
var collection = _fixture.CreateUniqueCollection(); | ||
var batch = db.StartBatch(); | ||
batch.Set(collection.Document("a"), new { x = 1, y = 1L << 63, z = 1.0 }); | ||
batch.Set(collection.Document("b"), new { x = 2, y = 1L << 63, z = 2.0 }); | ||
await batch.CommitAsync(); | ||
|
||
var snapshot = await collection | ||
.Aggregate(AggregateField.Sum("x"), AggregateField.Sum("y"), AggregateField.Sum("z")) | ||
.GetSnapshotAsync(); | ||
|
||
var sumX = snapshot.GetValue<object>("Sum_x"); | ||
var sumY = snapshot.GetValue<object>("Sum_y"); | ||
var sumZ = snapshot.GetValue<object>("Sum_z"); | ||
|
||
// These assertions check the value *and* the type together. | ||
Assert.Equal(3L, sumX); | ||
// The result is a double, because 2^64 can't be represented as a signed 64-bit integer. | ||
Assert.Equal((1L << 63) * 2.0, sumY); | ||
Assert.Equal(3.0, sumZ); | ||
} | ||
|
||
[Fact] | ||
public async Task Sum() | ||
{ | ||
CollectionReference collection = _fixture.StudentCollection; | ||
var snapshot = await collection.Aggregate(AggregateField.Sum("Level", "Sum_Of_Levels"), AggregateField.Sum("MathScore"), AggregateField.Sum("EnglishScore"), AggregateField.Sum("Name")).GetSnapshotAsync(); | ||
Assert.Equal(Student.Data.Sum(c => c.Level), snapshot.GetValue<long>("Sum_Of_Levels")); // Long value, Alias check | ||
Assert.Equal(Student.Data.Sum(c => c.MathScore), snapshot.GetValue<double>("Sum_MathScore")); // Double value check | ||
Assert.Equal(double.NaN, snapshot.GetValue<double>("Sum_EnglishScore")); // NaN value check | ||
} | ||
|
||
[Fact] | ||
public async Task Avg() | ||
{ | ||
CollectionReference collection = _fixture.StudentCollection; | ||
var snapshot = await collection.Aggregate(Average("MathScore"), Average("Level", "Avg_Of_Level"), Average("EnglishScore"), Average("Name")).GetSnapshotAsync(); | ||
Assert.Equal(Student.Data.Average(c => c.MathScore), snapshot.GetValue<double>("Avg_MathScore")); // Double value check | ||
Assert.Equal(Student.Data.Average(c => c.Level), snapshot.GetValue<double>("Avg_Of_Level")); // Alias check | ||
Assert.Equal(double.NaN, snapshot.GetValue<double>("Avg_EnglishScore")); // NaN value check | ||
} | ||
|
||
[Fact] | ||
public async Task SumWithLimit() | ||
{ | ||
CollectionReference collection = _fixture.HighScoreCollection; | ||
var snapshot = await collection.Limit(2).Aggregate(AggregateField.Sum("Score")).GetSnapshotAsync(); | ||
Assert.Equal(HighScore.Data.OrderBy(c => c.Score).Take(2).Sum(c => c.Score), snapshot.GetValue<long>("Sum_Score")); | ||
} | ||
|
||
[Fact] | ||
public async Task AvgWithLimit() | ||
{ | ||
CollectionReference collection = _fixture.HighScoreCollection; | ||
var snapshot = await collection.Limit(2).Aggregate(Average("Level")).GetSnapshotAsync(); | ||
Assert.Equal(HighScore.Data.OrderBy(c => c.Level).Take(2).Average(c => c.Level), snapshot.GetValue<double>("Avg_Level")); | ||
} | ||
|
||
[Fact] | ||
public async Task SumWithFilter() | ||
{ | ||
CollectionReference collection = _fixture.HighScoreCollection; | ||
var snapshot = await collection.WhereGreaterThan("Score", 100).Aggregate(AggregateField.Sum("Score")).GetSnapshotAsync(); | ||
Assert.Equal(HighScore.Data.Where(x => x.Score > 100).Sum(c => c.Score), snapshot.GetValue<long>("Sum_Score")); | ||
} | ||
|
||
[Fact] | ||
public async Task AvgWithFilter() | ||
{ | ||
CollectionReference collection = _fixture.HighScoreCollection; | ||
var snapshot = await collection.WhereGreaterThan("Level", 20).Aggregate(Average("Level")).GetSnapshotAsync(); | ||
Assert.Equal(HighScore.Data.Where(x => x.Level > 20).Average(c => c.Level), snapshot.GetValue<double>("Avg_Level")); | ||
} | ||
|
||
[Fact] | ||
public async Task MultipleAggregations() | ||
{ | ||
CollectionReference collection = _fixture.StudentCollection; | ||
var snapshot = await collection.Aggregate(AggregateField.Sum("MathScore"), Average("MathScore", "avg_score"), Count()).GetSnapshotAsync(); | ||
Assert.Equal(Student.Data.Length, snapshot.GetValue<Value>("Count").IntegerValue); | ||
Assert.Equal(Student.Data.Length, snapshot.Count.Value); | ||
Assert.Equal(Student.Data.Sum(c => c.MathScore), snapshot.GetValue<double>("Sum_MathScore")); | ||
Assert.Equal(Student.Data.Average(c => c.MathScore), snapshot.GetValue<double>("avg_score")); | ||
} | ||
|
||
[Fact] | ||
public async Task NestedAggregations() | ||
{ | ||
CollectionReference collection = _fixture.StudentCollection; | ||
var snapshot = await collection.Aggregate(AggregateField.Sum("Nested.Math"), Average("Nested.Math", "avg_score"), Count()).GetSnapshotAsync(); | ||
Assert.Equal(Student.Data.Length, snapshot.GetValue<Value>("Count").IntegerValue); | ||
Assert.Equal(Student.Data.Length, snapshot.Count.Value); | ||
Assert.Equal(Student.Data.Sum(c => c.Nested.Math), snapshot.GetValue<double>("Sum_Nested.Math")); | ||
Assert.Equal(Student.Data.Average(c => c.Nested.Math), snapshot.GetValue<double>("avg_score")); | ||
} | ||
} |
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
108 changes: 108 additions & 0 deletions
108
apis/Google.Cloud.Firestore/Google.Cloud.Firestore.IntegrationTests/Models/Student.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,108 @@ | ||
// Copyright 2023, Google Inc. All rights reserved. | ||
// | ||
// 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 | ||
// | ||
// http://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; | ||
using System; | ||
|
||
namespace Google.Cloud.Firestore.IntegrationTests.Models; | ||
|
||
[FirestoreData] | ||
public class Student : IEquatable<Student> | ||
{ | ||
// Note: Keep ordered by name, so we don't need to do that explicitly in tests. | ||
// All names have distinct values. | ||
// "mathScore" has decimal values, "englishScore" has double.NaN value. | ||
public static Student[] Data = new[] | ||
{ | ||
new Student("Andy", 10, 87.2, double.NaN), | ||
new Student("Bharati", 20, 98.5, 90), | ||
new Student("Colin", 15, 99, 95), | ||
new Student("Deborah", 25, 88, 100), | ||
new Student("Edward", 30, 100, 80), | ||
new Student("Faisal", 30, 80.4, 85) | ||
}; | ||
|
||
/// <summary> | ||
/// Needed for deserialization. | ||
/// </summary> | ||
public Student() | ||
{ | ||
} | ||
|
||
public Student(string name, int level, double mathScore, double englishScore) | ||
{ | ||
Name = name; | ||
Level = level; | ||
MathScore = mathScore; | ||
EnglishScore = englishScore; | ||
Nested = new NestedScores { Math = mathScore, English = englishScore }; | ||
} | ||
|
||
[FirestoreProperty] | ||
public string Name { get; set; } | ||
|
||
[FirestoreProperty] | ||
public int Level { get; set; } | ||
|
||
[FirestoreProperty] | ||
public double MathScore { get; set; } | ||
|
||
[FirestoreProperty] | ||
public double EnglishScore { get; set; } | ||
|
||
/// <summary> | ||
/// A nested representation of the scores, used to test aggregation with dotted field | ||
/// paths. The values in here are constructed to be the same as MathScore and EnglishScore. | ||
/// </summary> | ||
[FirestoreProperty] | ||
public NestedScores Nested { get; set; } | ||
|
||
public override int GetHashCode() => | ||
GaxEqualityHelpers.CombineHashCodes( | ||
Name?.GetHashCode() ?? 0, | ||
Level.GetHashCode(), | ||
MathScore.GetHashCode(), | ||
EnglishScore.GetHashCode(), | ||
Nested?.GetHashCode() ?? 0); | ||
|
||
public override bool Equals(object obj) => Equals(obj as Student); | ||
|
||
public bool Equals(Student other) => | ||
other is not null && | ||
Name == other.Name && | ||
Level == other.Level && | ||
MathScore == other.MathScore && | ||
EnglishScore == other.EnglishScore && | ||
Equals(Nested, other.Nested); | ||
} | ||
|
||
[FirestoreData] | ||
public class NestedScores : IEquatable<NestedScores> | ||
{ | ||
[FirestoreProperty] | ||
public double English { get; set; } | ||
|
||
[FirestoreProperty] | ||
public double Math { get; set; } | ||
|
||
public override int GetHashCode() => | ||
GaxEqualityHelpers.CombineHashCodes(English.GetHashCode(), Math.GetHashCode()); | ||
|
||
public override bool Equals(object obj) => Equals(obj as NestedScores); | ||
|
||
public bool Equals(NestedScores other) => | ||
other is not null && | ||
Math == other.Math && | ||
English == other.English; | ||
} |
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.