Skip to content

Commit c0151b9

Browse files
committed
#41 sum and average implemented
1 parent e269ebc commit c0151b9

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

SubSonic.Tests/DAL/ExtensionMethod/ExtensionMethodTests.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace SubSonic.Tests.DAL.ExtensionMethod
1212
{
1313
using Extensions.Test;
14+
using SubSonic.Extensions.Test.Models;
1415

1516
[TestFixture]
1617
public class ExtensionMethodTests
@@ -24,10 +25,18 @@ protected override void SetSelectBehaviors()
2425
person_min = @"SELECT MIN([T1].[ID])
2526
FROM [dbo].[Person] AS [T1]",
2627
person_max = @"SELECT MAX([T1].[ID])
27-
FROM [dbo].[Person] AS [T1]";
28+
FROM [dbo].[Person] AS [T1]",
29+
renter_sum = @"SELECT SUM([T1].[Rent])
30+
FROM [dbo].[Renter] AS [T1]
31+
WHERE ([T1].[PersonID] = @personid_1)",
32+
renter_avg = @"SELECT AVG([T1].[Rent])
33+
FROM [dbo].[Renter] AS [T1]
34+
WHERE ([T1].[PersonID] = @personid_1)";
2835

2936
Context.Database.Instance.AddCommandBehavior(person_max, cmd => People.Max(x => x.ID));
3037
Context.Database.Instance.AddCommandBehavior(person_min, cmd => People.Min(x => x.ID));
38+
Context.Database.Instance.AddCommandBehavior(renter_sum, cmd => Renters.Where(x => x.PersonID == cmd.Parameters["@personid_1"].GetValue<int>()).Sum(x => x.Rent));
39+
Context.Database.Instance.AddCommandBehavior(renter_avg, cmd => Renters.Where(x => x.PersonID == cmd.Parameters["@personid_1"].GetValue<int>()).Average(x => x.Rent));
3140
}
3241

3342

@@ -54,5 +63,21 @@ public void TheMaxMethodIsSupported()
5463
{
5564
Context.People.Max(x => x.ID).Should().Be(People.Count);
5665
}
66+
67+
[Test]
68+
public void TheSumMethodIsSupported()
69+
{
70+
Person person = Context.People.Single(x => x.ID == 1);
71+
72+
person.Renters.Sum(x => x.Rent).Should().Be(Renters.Where(x => x.PersonID == person.ID).Sum(x => x.Rent));
73+
}
74+
75+
[Test]
76+
public void TheAverageMethodIsSupported()
77+
{
78+
Person person = Context.People.Single(x => x.ID == 1);
79+
80+
person.Renters.Average(x => x.Rent).Should().Be(Renters.Where(x => x.PersonID == person.ID).Average(x => x.Rent));
81+
}
5782
}
5883
}

SubSonic.Tests/DAL/SUT/BaseTestFixture.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,19 @@ FROM [dbo].[Person] AS [T1]
117117
WHERE ([T1].[ID] > @id_1)",
118118
people_less_than = @"SELECT [T1].[ID], [T1].[FirstName], [T1].[MiddleInitial], [T1].[FamilyName], [T1].[FullName]
119119
FROM [dbo].[Person] AS [T1]
120-
WHERE ([T1].[ID] < @id_1)";
120+
WHERE ([T1].[ID] < @id_1)",
121+
renter_byperson = @"SELECT [T1].[PersonID], [T1].[UnitID], [T1].[Rent], [T1].[StartDate], [T1].[EndDate]
122+
FROM [dbo].[Renter] AS [T1]
123+
WHERE ([T1].[PersonID] == @personid_1)";
121124

122125
Context.Database.Instance.AddCommandBehavior(people_all, cmd => People.ToDataTable());
123126
Context.Database.Instance.AddCommandBehavior(people_all_count, cmd => People.Count());
124127
Context.Database.Instance.AddCommandBehavior(people_all_long_count, cmd => People.LongCount());
125128
Context.Database.Instance.AddCommandBehavior(people_greater_than, cmd => People.Where(x => x.ID > cmd.Parameters["@id_1"].GetValue<int>()).ToDataTable());
126129
Context.Database.Instance.AddCommandBehavior(people_equal, cmd => People.Where(x => x.ID == cmd.Parameters["@id_1"].GetValue<int>()).ToDataTable());
127130
Context.Database.Instance.AddCommandBehavior(people_less_than, cmd => People.Where(x => x.ID < cmd.Parameters["@id_1"].GetValue<int>()).ToDataTable());
131+
132+
Context.Database.Instance.AddCommandBehavior(renter_byperson, cmd => Renters.Where(x => x.PersonID == cmd.Parameters["@personid_1"].GetValue<int>()).ToDataTable());
128133
}
129134

130135
protected virtual void SetInsertBehaviors()

SubSonic/Infrastructure/Builders/DbSqlQueryBuilder/DbSqlQueryBuilderQueryProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public TResult ExecuteMethod<TResult>(MethodCallExpression call)
9191
throw Error.InvalidOperation($"Method {call.Method.Name} expects data.");
9292
}
9393
}
94-
else if (call.Method.Name.In(nameof(Queryable.Count), nameof(Queryable.LongCount), nameof(Queryable.Min), nameof(Queryable.Max)))
94+
else if (call.Method.Name.In(nameof(Queryable.Count), nameof(Queryable.LongCount), nameof(Queryable.Min), nameof(Queryable.Max), nameof(Queryable.Sum), nameof(Queryable.Average)))
9595
{
9696
if (BuildSelect(dbSelect, where) is DbSelectExpression select)
9797
{

0 commit comments

Comments
 (0)