Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions SubSonic.Tests/DAL/ExtensionMethod/ExtensionMethodTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,36 @@

namespace SubSonic.Tests.DAL.ExtensionMethod
{
using Extensions.Test;
using SubSonic.Extensions.Test.Models;

[TestFixture]
public class ExtensionMethodTests
: BaseTestFixture
{
protected override void SetSelectBehaviors()
{
base.SetSelectBehaviors();

string
person_min = @"SELECT MIN([T1].[ID])
FROM [dbo].[Person] AS [T1]",
person_max = @"SELECT MAX([T1].[ID])
FROM [dbo].[Person] AS [T1]",
renter_sum = @"SELECT SUM([T1].[Rent])
FROM [dbo].[Renter] AS [T1]
WHERE ([T1].[PersonID] = @personid_1)",
renter_avg = @"SELECT AVG([T1].[Rent])
FROM [dbo].[Renter] AS [T1]
WHERE ([T1].[PersonID] = @personid_1)";

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


[Test]
public void TheCountMethodIsSupported()
{
Expand All @@ -25,5 +51,33 @@ public void TheLongCountMethodIsSupported()
{
Context.People.LongCount().Should().BeGreaterThan(0).And.IsOfType<long>();
}

[Test]
public void TheMinMethodIsSupported()
{
Context.People.Min(x => x.ID).Should().Be(1);
}

[Test]
public void TheMaxMethodIsSupported()
{
Context.People.Max(x => x.ID).Should().Be(People.Count);
}

[Test]
public void TheSumMethodIsSupported()
{
Person person = Context.People.Single(x => x.ID == 1);

person.Renters.Sum(x => x.Rent).Should().Be(Renters.Where(x => x.PersonID == person.ID).Sum(x => x.Rent));
}

[Test]
public void TheAverageMethodIsSupported()
{
Person person = Context.People.Single(x => x.ID == 1);

person.Renters.Average(x => x.Rent).Should().Be(Renters.Where(x => x.PersonID == person.ID).Average(x => x.Rent));
}
}
}
17 changes: 11 additions & 6 deletions SubSonic.Tests/DAL/SUT/BaseTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ public virtual void SetupTestFixture()

Renters = new List<Renter>()
{
new Renter() { PersonID = 1, UnitID = 1, StartDate = new DateTime(1980, 01, 01), EndDate = new DateTime(1990, 02, 28) },
new Renter() { PersonID = 2, UnitID = 1, StartDate = new DateTime(1990, 03, 01) },
new Renter() { PersonID = 3, UnitID = 2, StartDate = new DateTime(1980, 03, 01), EndDate = new DateTime(2000, 01, 01) },
new Renter() { PersonID = 1, UnitID = 3, StartDate = new DateTime(1990, 03, 01) },
new Renter() { PersonID = 4, UnitID = 4, StartDate = new DateTime(2000, 01, 01) }
new Renter() { PersonID = 1, UnitID = 1, Rent = 100M, StartDate = new DateTime(1980, 01, 01), EndDate = new DateTime(1990, 02, 28) },
new Renter() { PersonID = 2, UnitID = 1, Rent = 150M, StartDate = new DateTime(1990, 03, 01) },
new Renter() { PersonID = 3, UnitID = 2, Rent = 200M, StartDate = new DateTime(1980, 03, 01), EndDate = new DateTime(2000, 01, 01) },
new Renter() { PersonID = 1, UnitID = 3, Rent = 250M, StartDate = new DateTime(1990, 03, 01) },
new Renter() { PersonID = 4, UnitID = 4, Rent = 300M, StartDate = new DateTime(2000, 01, 01) }
};

RealEstateProperties = new List<RealEstateProperty>()
Expand Down Expand Up @@ -117,14 +117,19 @@ FROM [dbo].[Person] AS [T1]
WHERE ([T1].[ID] > @id_1)",
people_less_than = @"SELECT [T1].[ID], [T1].[FirstName], [T1].[MiddleInitial], [T1].[FamilyName], [T1].[FullName]
FROM [dbo].[Person] AS [T1]
WHERE ([T1].[ID] < @id_1)";
WHERE ([T1].[ID] < @id_1)",
renter_byperson = @"SELECT [T1].[PersonID], [T1].[UnitID], [T1].[Rent], [T1].[StartDate], [T1].[EndDate]
FROM [dbo].[Renter] AS [T1]
WHERE ([T1].[PersonID] == @personid_1)";

Context.Database.Instance.AddCommandBehavior(people_all, cmd => People.ToDataTable());
Context.Database.Instance.AddCommandBehavior(people_all_count, cmd => People.Count());
Context.Database.Instance.AddCommandBehavior(people_all_long_count, cmd => People.LongCount());
Context.Database.Instance.AddCommandBehavior(people_greater_than, cmd => People.Where(x => x.ID > cmd.Parameters["@id_1"].GetValue<int>()).ToDataTable());
Context.Database.Instance.AddCommandBehavior(people_equal, cmd => People.Where(x => x.ID == cmd.Parameters["@id_1"].GetValue<int>()).ToDataTable());
Context.Database.Instance.AddCommandBehavior(people_less_than, cmd => People.Where(x => x.ID < cmd.Parameters["@id_1"].GetValue<int>()).ToDataTable());

Context.Database.Instance.AddCommandBehavior(renter_byperson, cmd => Renters.Where(x => x.PersonID == cmd.Parameters["@personid_1"].GetValue<int>()).ToDataTable());
}

protected virtual void SetInsertBehaviors()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,15 @@ public TResult ExecuteMethod<TResult>(MethodCallExpression call)
{
if (unary.Operand is LambdaExpression lambda)
{
where = BuildWhere(dbSelect, lambda);
switch (lambda.Body.NodeType)
{
case ExpressionType.MemberAccess:
dbSelect = (DbSelectExpression)BuildSelect(dbSelect, dbSelect.Columns.Where(x => x.PropertyName.Equals(lambda.GetProperty().Name, StringComparison.Ordinal)));
break;
default:
where = BuildWhere(dbSelect, lambda);
break;
}
}
}
}
Expand Down Expand Up @@ -83,7 +91,7 @@ public TResult ExecuteMethod<TResult>(MethodCallExpression call)
throw Error.InvalidOperation($"Method {call.Method.Name} expects data.");
}
}
else if (call.Method.Name.In(nameof(Queryable.Count), nameof(Queryable.LongCount)))
else if (call.Method.Name.In(nameof(Queryable.Count), nameof(Queryable.LongCount), nameof(Queryable.Min), nameof(Queryable.Max), nameof(Queryable.Sum), nameof(Queryable.Average)))
{
if (BuildSelect(dbSelect, where) is DbSelectExpression select)
{
Expand All @@ -92,9 +100,20 @@ public TResult ExecuteMethod<TResult>(MethodCallExpression call)
throw Error.NotSupported(SubSonicErrorMessages.MethodNotSupported.Format(call.Method.Name));
}

Expression argument = null;

if (select.Columns.Count > 1)
{
argument = select.Columns.First(x => x.Property.IsPrimaryKey).Expression;
}
else
{
argument = select.Columns.Single().Expression;
}

TResult result = Execute<TResult>(DbExpression.DbSelectAggregate(select, new[]
{
DbExpression.DbAggregate(typeof(TResult), aggregateType, select.Columns.First(x => x.Property.IsPrimaryKey).Expression)
DbExpression.DbAggregate(typeof(TResult), aggregateType, argument)
}));

if (select.Take is ConstantExpression take)
Expand Down