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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace SubSonic.Infrastructure
{
Expand Down
45 changes: 36 additions & 9 deletions SubSonic.Tests/DAL/Builders/DbWherePredicateBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ namespace SubSonic.Tests.DAL.Builders
using Linq.Expressions;
using Infrastructure.Builders;
using Models = Extensions.Test.Models;

using System.Reflection;
using SubSonic.Infrastructure;

[TestFixture]
public class DbWherePredicateBuilderTests
: SUT.BaseTestFixture
{
interface IPredicateTestCase
{
LambdaExpression Predicate { get; }
Expression Predicate { get; }
Type Type { get; }
DbTableExpression Table { get; }
Type DbSetType { get; }
Expression Expression { get; }
}
class GetPredicateFor<TEntity>
: IPredicateTestCase
where TEntity : class
{
public GetPredicateFor(Expression<Func<TEntity, bool>> selector)
{
Expand All @@ -35,9 +39,21 @@ public GetPredicateFor(Expression<Func<TEntity, bool>> selector)

public Type Type => typeof(TEntity);

public LambdaExpression Predicate { get; }
public Type DbSetType => typeof(DbSetCollection<>).MakeGenericType(Type);

public Expression Predicate { get; }

public DbTableExpression Table => DbContext.DbModel.GetEntityModel<TEntity>().Table;
public Expression Expression
{
get
{
MethodInfo method = typeof(Queryable).GetGenericMethod(nameof(Queryable.Where), new[] { DbSetType, Predicate.GetType() });

return DbExpression.DbWhere(method, new[] {
DbContext.Current.Set<TEntity>()?.Expression,
Predicate });
}
}
}

private static IEnumerable<IPredicateTestCase> Expressions()
Expand All @@ -50,14 +66,25 @@ private static IEnumerable<IPredicateTestCase> Expressions()
[Test]
public void ShouldBeAbleToGetWherePredicateBody()
{
//foreach(IPredicateTestCase @case in Expressions())
Parallel.ForEach(Expressions(), @case =>
foreach(IPredicateTestCase @case in Expressions())
//Parallel.ForEach(Expressions(), @case =>
{
var result = DbWherePredicateBuilder.GetWherePredicate(@case.Type, @case.Predicate, DbExpressionType.Where, @case.Table);
var result = DbWherePredicateBuilder.GetWhereTranslation(@case.Expression);

if (result is DbWhereExpression where)
{
where.Parameters.Count.Should().NotBe(0);

where.Type.Should().Be(typeof(bool));

where.GetArgument(0).Type.Should().Be(@case.DbSetType);

where.Expression.Should().NotBeNull();
}

result.Should().NotBeNull();
}
);
//);
}
}
}
2 changes: 2 additions & 0 deletions SubSonic.Tests/DAL/DbContext/DbContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ public void ShouldBeAbleToDeleteRecordsUsingCQRS()
.Where(x => x.ID == 1)
.Single();

property.Should().NotBeNull();

((IEntityProxy)property).IsDeleted.Should().BeFalse();

Context.RealEstateProperties.Delete(property);
Expand Down
2 changes: 1 addition & 1 deletion SubSonic.Tests/DAL/DbContext/DbDeleteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data;
using System.Data.Common;

Expand Down
8 changes: 5 additions & 3 deletions SubSonic.Tests/DAL/DbContext/DbInsertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ OUTPUT INSERTED.* INTO @output
VALUES
(@FirstName, @MiddleInitial, @FamilyName)";

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

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

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

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

person.ID.Should().Be(5);
person.FullName.Should().Be("Last_1, First_1 M.");
person.FullName.Should().Be(String.Format("{0}, {1}{2}",
person.FamilyName, person.FirstName,
string.IsNullOrEmpty(person.MiddleInitial?.Trim()) ? "" : $" {person.MiddleInitial}."));
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion SubSonic.Tests/DAL/DbContext/DbUpdateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data;
using System.Data.Common;

Expand Down
21 changes: 12 additions & 9 deletions SubSonic.Tests/DAL/DbSetCollection/DbSetCollectionTests.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using FluentAssertions;
using NUnit.Framework;
using SubSonic.Extensions.Test;
using SubSonic.Extensions.Test.Models;
using SubSonic.Infrastructure;
using SubSonic.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace SubSonic.Tests.DAL.DbSetCollection
{
using SubSonic.Data.Caching;
using SubSonic.Data.DynamicProxies;
using Data.Caching;
using Data.DynamicProxies;
using Extensions.Test;
using Extensions.Test.Models;
using Infrastructure;
using SUT;
using System.Collections.Generic;
using System.Linq.Expressions;

[TestFixture]
public class DbSetCollectionTests
Expand All @@ -22,6 +22,8 @@ public override void SetupTestFixture()
base.SetupTestFixture();

string
properties_all = @"SELECT [T1].[ID], [T1].[StatusID], [T1].[HasParallelPowerGeneration]
FROM [dbo].[RealEstateProperty] AS [T1]",
units =
@"SELECT [{0}].[ID], [{0}].[Bedrooms] AS [NumberOfBedrooms], [{0}].[StatusID], [{0}].[RealEstatePropertyID]
FROM [dbo].[Unit] AS [{0}]
Expand All @@ -38,6 +40,7 @@ FROM [dbo].[Status] AS [{0}]
FROM [dbo].[Person] AS [{0}]
WHERE ([{0}].[ID] = @id_1)";

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

Context.RealEstateProperties.Add(property);

IEntityProxy<RealEstateProperty> proxy = (IEntityProxy<RealEstateProperty>)Context.RealEstateProperties.ElementAt(0);
IEntityProxy<RealEstateProperty> proxy = (IEntityProxy<RealEstateProperty>)Context.RealEstateProperties.AsEnumerable().ElementAt(0);

proxy.IsNew.Should().BeTrue();
proxy.Data.HasParallelPowerGeneration.Should().Be(property.HasParallelPowerGeneration);
Expand Down
5 changes: 2 additions & 3 deletions SubSonic.Tests/DAL/SUT/DbTestCase.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace SubSonic.Tests.DAL
{
using Infrastructure.Schema;
using Extensions.Test;
using Infrastructure;
using Linq;
using Infrastructure.Schema;
using SysLinq = System.Linq;

public class DbTestCase<TModel>
Expand Down
2 changes: 1 addition & 1 deletion SubSonic.Tests/DAL/ScalarFunctions/ScalarFunctionTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using FluentAssertions;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
using System.Linq;
using System.Linq.Expressions;

namespace SubSonic.Tests.DAL.ScalarFunctions
{
using Extensions.Test.Data.Functions;
using Infrastructure;
using Infrastructure.Logging;
using Linq;
using Linq.Expressions;
using SubSonic.Extensions.Test.Models;
using SUT;
Expand Down
22 changes: 13 additions & 9 deletions SubSonic.Tests/DAL/SqlQueryProvider/SqlGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using FluentAssertions;
using Microsoft.Extensions.Logging;
using NUnit.Framework;
using SubSonic.Tests.DAL.SUT;
using System;
using System.Data;
using System.Linq;
using System.Linq.Expressions;

namespace SubSonic.Tests.DAL.SqlQueryProvider
Expand All @@ -13,9 +13,9 @@ namespace SubSonic.Tests.DAL.SqlQueryProvider
using Extensions.Test.Models;
using Infrastructure;
using Infrastructure.Logging;
using Infrastructure.Schema;
using Linq;
using Linq.Expressions;
using Tests.DAL.SUT;

[TestFixture]
public partial class SqlQueryProviderTests
Expand Down Expand Up @@ -477,11 +477,11 @@ FROM [dbo].[Unit] AS [{1}]
public void CanMergeMultipleWhereStatements()
{
string
units =
units_sql =
@"SELECT [{0}].[ID], [{0}].[Bedrooms] AS [NumberOfBedrooms], [{0}].[StatusID], [{0}].[RealEstatePropertyID]
FROM [dbo].[Unit] AS [{0}]
WHERE ([{0}].[RealEstatePropertyID] = 1)".Format("T1"),
status =
status_sql =
@"SELECT [{0}].[ID], [{0}].[Bedrooms] AS [NumberOfBedrooms], [{0}].[StatusID], [{0}].[RealEstatePropertyID]
FROM [dbo].[Unit] AS [{0}]
WHERE (([{0}].[RealEstatePropertyID] = 1) AND ([{0}].[StatusID] = 1))".Format("T1"),
Expand All @@ -496,14 +496,18 @@ FROM [dbo].[Unit] AS [{0}]
instance.StatusID = 1;

Context.Database.Instance.AddCommandBehavior(
units,
units_sql,
Units.Where(x => x.RealEstatePropertyID == 1));

Context.Database.Instance.AddCommandBehavior(
status,
status_sql,
Units.Where(x => x.RealEstatePropertyID == 1 && x.StatusID == 1));

Expression select = ((ISubSonicCollection<Unit>)instance.Units.Where(Unit => Unit.StatusID == 1)).Expression;
IQueryable<Unit> units = instance.Units
.AsQueryable()
.Where(Unit => Unit.StatusID == 1);

Expression select = units.Expression;

select.Should().NotBeNull();

Expand Down Expand Up @@ -946,7 +950,7 @@ INNER JOIN page
.Where((property) =>
property.HasParallelPowerGeneration == true)
.OrderBy((property) => property.ID)
.ThenOrderBy((property) => property.StatusID)
.ThenBy((property) => property.StatusID)
.Page(default(int), 5)
.Expression;

Expand Down Expand Up @@ -998,7 +1002,7 @@ INNER JOIN page
.Where((property) =>
property.HasParallelPowerGeneration == true)
.OrderByDescending((property) => property.ID)
.ThenOrderByDescending((property) => property.StatusID)
.ThenByDescending((property) => property.StatusID)
.Page(default(int), 5)
.Expression;

Expand Down
14 changes: 8 additions & 6 deletions SubSonic/Data/ChangeTracking/ChangeTrackerCollection.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
using Microsoft.Extensions.Logging;
using SubSonic.Infrastructure;
using SubSonic.Infrastructure.Logging;
using SubSonic.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

namespace SubSonic.Data.Caching
{
using Infrastructure;
using Infrastructure.Logging;
using Linq;

public class ChangeTrackerCollection
: IEnumerable<KeyValuePair<Type, IEnumerable<IEntityProxy>>>
{
Expand Down Expand Up @@ -109,7 +111,7 @@ public bool SaveChanges(out string feedback)

string error_feedback = "";

if (insert.Count() > 0)
if (insert.Any())
{
success &= collection[dataset.Key].SaveChanges(DbQueryType.Insert, insert, out error_feedback);
if (!success && error_feedback.IsNotNullOrEmpty())
Expand All @@ -120,7 +122,7 @@ public bool SaveChanges(out string feedback)
}
}

if (update.Count() > 0)
if (update.Any())
{
success &= collection[dataset.Key].SaveChanges(DbQueryType.Update, update, out error_feedback);
if (!success && error_feedback.IsNotNullOrEmpty())
Expand All @@ -131,7 +133,7 @@ public bool SaveChanges(out string feedback)
}
}

if (delete.Count() > 0)
if (delete.Any())
{
success &= collection[dataset.Key].SaveChanges(DbQueryType.Delete, delete, out error_feedback);
if (!success && error_feedback.IsNotNullOrEmpty())
Expand Down
Loading