Skip to content

Commit 5ae89eb

Browse files
authored
Merge pull request #28 from SubSonic-Core/dev/27-Queryable-Extensions
Dev/27 queryable extensions
2 parents 285e2a6 + 22bd983 commit 5ae89eb

File tree

55 files changed

+666
-869
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+666
-869
lines changed

SubSonic.Extensions.SqlServer/Infrastructure/DbSubSonicCommandQueryProcedure.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Data;
3+
using System.Linq;
34

45
namespace SubSonic.Infrastructure
56
{

SubSonic.Tests/DAL/Builders/DbWherePredicateBuilderTests.cs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,23 @@ namespace SubSonic.Tests.DAL.Builders
1414
using Linq.Expressions;
1515
using Infrastructure.Builders;
1616
using Models = Extensions.Test.Models;
17-
17+
using System.Reflection;
18+
using SubSonic.Infrastructure;
19+
1820
[TestFixture]
1921
public class DbWherePredicateBuilderTests
2022
: SUT.BaseTestFixture
2123
{
2224
interface IPredicateTestCase
2325
{
24-
LambdaExpression Predicate { get; }
26+
Expression Predicate { get; }
2527
Type Type { get; }
26-
DbTableExpression Table { get; }
28+
Type DbSetType { get; }
29+
Expression Expression { get; }
2730
}
2831
class GetPredicateFor<TEntity>
2932
: IPredicateTestCase
33+
where TEntity : class
3034
{
3135
public GetPredicateFor(Expression<Func<TEntity, bool>> selector)
3236
{
@@ -35,9 +39,21 @@ public GetPredicateFor(Expression<Func<TEntity, bool>> selector)
3539

3640
public Type Type => typeof(TEntity);
3741

38-
public LambdaExpression Predicate { get; }
42+
public Type DbSetType => typeof(DbSetCollection<>).MakeGenericType(Type);
43+
44+
public Expression Predicate { get; }
3945

40-
public DbTableExpression Table => DbContext.DbModel.GetEntityModel<TEntity>().Table;
46+
public Expression Expression
47+
{
48+
get
49+
{
50+
MethodInfo method = typeof(Queryable).GetGenericMethod(nameof(Queryable.Where), new[] { DbSetType, Predicate.GetType() });
51+
52+
return DbExpression.DbWhere(method, new[] {
53+
DbContext.Current.Set<TEntity>()?.Expression,
54+
Predicate });
55+
}
56+
}
4157
}
4258

4359
private static IEnumerable<IPredicateTestCase> Expressions()
@@ -50,14 +66,25 @@ private static IEnumerable<IPredicateTestCase> Expressions()
5066
[Test]
5167
public void ShouldBeAbleToGetWherePredicateBody()
5268
{
53-
//foreach(IPredicateTestCase @case in Expressions())
54-
Parallel.ForEach(Expressions(), @case =>
69+
foreach(IPredicateTestCase @case in Expressions())
70+
//Parallel.ForEach(Expressions(), @case =>
5571
{
56-
var result = DbWherePredicateBuilder.GetWherePredicate(@case.Type, @case.Predicate, DbExpressionType.Where, @case.Table);
72+
var result = DbWherePredicateBuilder.GetWhereTranslation(@case.Expression);
73+
74+
if (result is DbWhereExpression where)
75+
{
76+
where.Parameters.Count.Should().NotBe(0);
77+
78+
where.Type.Should().Be(typeof(bool));
79+
80+
where.GetArgument(0).Type.Should().Be(@case.DbSetType);
81+
82+
where.Expression.Should().NotBeNull();
83+
}
5784

5885
result.Should().NotBeNull();
5986
}
60-
);
87+
//);
6188
}
6289
}
6390
}

SubSonic.Tests/DAL/DbContext/DbContextTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ public void ShouldBeAbleToDeleteRecordsUsingCQRS()
224224
.Where(x => x.ID == 1)
225225
.Single();
226226

227+
property.Should().NotBeNull();
228+
227229
((IEntityProxy)property).IsDeleted.Should().BeFalse();
228230

229231
Context.RealEstateProperties.Delete(property);

SubSonic.Tests/DAL/DbContext/DbDeleteTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using FluentAssertions;
33
using System;
44
using System.Collections.Generic;
5-
using System.Text;
5+
using System.Linq;
66
using System.Data;
77
using System.Data.Common;
88

SubSonic.Tests/DAL/DbContext/DbInsertTests.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ OUTPUT INSERTED.* INTO @output
2424
VALUES
2525
(@FirstName, @MiddleInitial, @FamilyName)";
2626

27-
Models.Person person = new Models.Person(){ FirstName = "First_1", FamilyName = "Last_1", MiddleInitial = "M" };
27+
Models.Person person = GetFakePerson.Generate();
2828

2929
Context.Database.Instance.AddCommandBehavior(expected_cmd, cmd =>
3030
{
@@ -41,7 +41,7 @@ OUTPUT INSERTED.* INTO @output
4141

4242
data.FullName = String.Format("{0}, {1}{2}",
4343
data.FamilyName, data.FirstName,
44-
data.MiddleInitial.IsNotNullOrEmpty() ? $" {data.MiddleInitial}." : "");
44+
string.IsNullOrEmpty(data.MiddleInitial?.Trim()) ? "" : $" {data.MiddleInitial}.");
4545

4646
return new[] { data }.ToDataTable();
4747
});
@@ -53,7 +53,9 @@ OUTPUT INSERTED.* INTO @output
5353
Context.SaveChanges().Should().BeTrue();
5454

5555
person.ID.Should().Be(5);
56-
person.FullName.Should().Be("Last_1, First_1 M.");
56+
person.FullName.Should().Be(String.Format("{0}, {1}{2}",
57+
person.FamilyName, person.FirstName,
58+
string.IsNullOrEmpty(person.MiddleInitial?.Trim()) ? "" : $" {person.MiddleInitial}."));
5759
}
5860

5961
[Test]

SubSonic.Tests/DAL/DbContext/DbUpdateTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using FluentAssertions;
33
using System;
44
using System.Collections.Generic;
5-
using System.Text;
5+
using System.Linq;
66
using System.Data;
77
using System.Data.Common;
88

SubSonic.Tests/DAL/DbSetCollection/DbSetCollectionTests.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
using FluentAssertions;
22
using NUnit.Framework;
3-
using SubSonic.Extensions.Test;
4-
using SubSonic.Extensions.Test.Models;
5-
using SubSonic.Infrastructure;
6-
using SubSonic.Linq;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Linq.Expressions;
76

87
namespace SubSonic.Tests.DAL.DbSetCollection
98
{
10-
using SubSonic.Data.Caching;
11-
using SubSonic.Data.DynamicProxies;
9+
using Data.Caching;
10+
using Data.DynamicProxies;
11+
using Extensions.Test;
12+
using Extensions.Test.Models;
13+
using Infrastructure;
1214
using SUT;
13-
using System.Collections.Generic;
14-
using System.Linq.Expressions;
1515

1616
[TestFixture]
1717
public class DbSetCollectionTests
@@ -22,6 +22,8 @@ public override void SetupTestFixture()
2222
base.SetupTestFixture();
2323

2424
string
25+
properties_all = @"SELECT [T1].[ID], [T1].[StatusID], [T1].[HasParallelPowerGeneration]
26+
FROM [dbo].[RealEstateProperty] AS [T1]",
2527
units =
2628
@"SELECT [{0}].[ID], [{0}].[Bedrooms] AS [NumberOfBedrooms], [{0}].[StatusID], [{0}].[RealEstatePropertyID]
2729
FROM [dbo].[Unit] AS [{0}]
@@ -38,6 +40,7 @@ FROM [dbo].[Status] AS [{0}]
3840
FROM [dbo].[Person] AS [{0}]
3941
WHERE ([{0}].[ID] = @id_1)";
4042

43+
Context.Database.Instance.AddCommandBehavior(properties_all, cmd => RealEstateProperties.ToDataTable());
4144
Context.Database.Instance.AddCommandBehavior(units.Format("T1"), cmd => Units.Where(x => x.RealEstatePropertyID == cmd.Parameters["@realestatepropertyid_1"].GetValue<int>()).ToDataTable());
4245
Context.Database.Instance.AddCommandBehavior(status.Format("T1"), cmd => Statuses.Where(x => x.ID == cmd.Parameters["@id_1"].GetValue<int>()).ToDataTable());
4346
Context.Database.Instance.AddCommandBehavior(statuses.Format("T1"), Statuses);
@@ -58,7 +61,7 @@ public void CanAddNewInstanceToCollection()
5861

5962
Context.RealEstateProperties.Add(property);
6063

61-
IEntityProxy<RealEstateProperty> proxy = (IEntityProxy<RealEstateProperty>)Context.RealEstateProperties.ElementAt(0);
64+
IEntityProxy<RealEstateProperty> proxy = (IEntityProxy<RealEstateProperty>)Context.RealEstateProperties.AsEnumerable().ElementAt(0);
6265

6366
proxy.IsNew.Should().BeTrue();
6467
proxy.Data.HasParallelPowerGeneration.Should().Be(property.HasParallelPowerGeneration);

SubSonic.Tests/DAL/SUT/DbTestCase.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Linq;
45
using System.Linq.Expressions;
5-
using System.Text;
66

77
namespace SubSonic.Tests.DAL
88
{
9-
using Infrastructure.Schema;
109
using Extensions.Test;
1110
using Infrastructure;
12-
using Linq;
11+
using Infrastructure.Schema;
1312
using SysLinq = System.Linq;
1413

1514
public class DbTestCase<TModel>

SubSonic.Tests/DAL/ScalarFunctions/ScalarFunctionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using FluentAssertions;
22
using Microsoft.Extensions.Logging;
33
using NUnit.Framework;
4+
using System.Linq;
45
using System.Linq.Expressions;
56

67
namespace SubSonic.Tests.DAL.ScalarFunctions
78
{
89
using Extensions.Test.Data.Functions;
910
using Infrastructure;
1011
using Infrastructure.Logging;
11-
using Linq;
1212
using Linq.Expressions;
1313
using SubSonic.Extensions.Test.Models;
1414
using SUT;

SubSonic.Tests/DAL/SqlQueryProvider/SqlGeneratorTests.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using FluentAssertions;
22
using Microsoft.Extensions.Logging;
33
using NUnit.Framework;
4-
using SubSonic.Tests.DAL.SUT;
54
using System;
65
using System.Data;
6+
using System.Linq;
77
using System.Linq.Expressions;
88

99
namespace SubSonic.Tests.DAL.SqlQueryProvider
@@ -13,9 +13,9 @@ namespace SubSonic.Tests.DAL.SqlQueryProvider
1313
using Extensions.Test.Models;
1414
using Infrastructure;
1515
using Infrastructure.Logging;
16-
using Infrastructure.Schema;
1716
using Linq;
1817
using Linq.Expressions;
18+
using Tests.DAL.SUT;
1919

2020
[TestFixture]
2121
public partial class SqlQueryProviderTests
@@ -477,11 +477,11 @@ FROM [dbo].[Unit] AS [{1}]
477477
public void CanMergeMultipleWhereStatements()
478478
{
479479
string
480-
units =
480+
units_sql =
481481
@"SELECT [{0}].[ID], [{0}].[Bedrooms] AS [NumberOfBedrooms], [{0}].[StatusID], [{0}].[RealEstatePropertyID]
482482
FROM [dbo].[Unit] AS [{0}]
483483
WHERE ([{0}].[RealEstatePropertyID] = 1)".Format("T1"),
484-
status =
484+
status_sql =
485485
@"SELECT [{0}].[ID], [{0}].[Bedrooms] AS [NumberOfBedrooms], [{0}].[StatusID], [{0}].[RealEstatePropertyID]
486486
FROM [dbo].[Unit] AS [{0}]
487487
WHERE (([{0}].[RealEstatePropertyID] = 1) AND ([{0}].[StatusID] = 1))".Format("T1"),
@@ -496,14 +496,18 @@ FROM [dbo].[Unit] AS [{0}]
496496
instance.StatusID = 1;
497497

498498
Context.Database.Instance.AddCommandBehavior(
499-
units,
499+
units_sql,
500500
Units.Where(x => x.RealEstatePropertyID == 1));
501501

502502
Context.Database.Instance.AddCommandBehavior(
503-
status,
503+
status_sql,
504504
Units.Where(x => x.RealEstatePropertyID == 1 && x.StatusID == 1));
505505

506-
Expression select = ((ISubSonicCollection<Unit>)instance.Units.Where(Unit => Unit.StatusID == 1)).Expression;
506+
IQueryable<Unit> units = instance.Units
507+
.AsQueryable()
508+
.Where(Unit => Unit.StatusID == 1);
509+
510+
Expression select = units.Expression;
507511

508512
select.Should().NotBeNull();
509513

@@ -946,7 +950,7 @@ INNER JOIN page
946950
.Where((property) =>
947951
property.HasParallelPowerGeneration == true)
948952
.OrderBy((property) => property.ID)
949-
.ThenOrderBy((property) => property.StatusID)
953+
.ThenBy((property) => property.StatusID)
950954
.Page(default(int), 5)
951955
.Expression;
952956

@@ -998,7 +1002,7 @@ INNER JOIN page
9981002
.Where((property) =>
9991003
property.HasParallelPowerGeneration == true)
10001004
.OrderByDescending((property) => property.ID)
1001-
.ThenOrderByDescending((property) => property.StatusID)
1005+
.ThenByDescending((property) => property.StatusID)
10021006
.Page(default(int), 5)
10031007
.Expression;
10041008

0 commit comments

Comments
 (0)