-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #392 from LuccaSA/fix_hierarchy_problem
FIX : issue on order by on property on base class
- Loading branch information
Showing
3 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
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
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,97 @@ | ||
using Rdd.Domain.Helpers.Expressions; | ||
using Rdd.Domain.Models.Querying; | ||
using Rdd.Domain.Tests.Models; | ||
using System; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Rdd.Infra.Tests | ||
{ | ||
public class GenericKey | ||
{ | ||
public long Sum { get; set; } | ||
public string BaseProperty { get; set; } | ||
} | ||
|
||
public class TypeOneKey : GenericKey | ||
{ | ||
public string SuperProperty { get; set; } | ||
} | ||
|
||
public class Metric<TKey> | ||
where TKey : GenericKey | ||
{ | ||
public long Sum { get; set; } | ||
public TKey Key { get; set; } | ||
} | ||
|
||
public class TypeOneMetric : Metric<TypeOneKey> | ||
{ | ||
public long TypeOneProperty { get; set; } | ||
} | ||
|
||
public class HierarchyTests : DatabaseTest | ||
{ | ||
[Fact] | ||
public async Task OrderByTest() | ||
{ | ||
await RunCodeInsideIsolatedDatabaseAsync((context) => | ||
{ | ||
using (var newContext = GetContext()) | ||
{ | ||
newContext.Set<Super>().Add(new Super | ||
{ | ||
BaseProperty = "a", | ||
Value = 1 | ||
}); | ||
newContext.Set<Super>().Add(new Super | ||
{ | ||
BaseProperty = "a", | ||
Value = 2 | ||
}); | ||
newContext.Set<Super>().Add(new Super | ||
{ | ||
BaseProperty = "b", | ||
Value = 2 | ||
}); | ||
newContext.SaveChanges(); | ||
|
||
var query = newContext.Set<Super>().GroupBy(e => new | ||
{ | ||
e.BaseProperty, | ||
e.SuperProperty | ||
}) | ||
.Select(g => new TypeOneMetric | ||
{ | ||
Sum = g.Sum(e => e.Value), | ||
Key = new TypeOneKey | ||
{ | ||
Sum = g.Sum(e => e.Value2), | ||
BaseProperty = g.Key.BaseProperty, | ||
SuperProperty = g.Key.SuperProperty | ||
} | ||
}); | ||
|
||
var set = new Service<TypeOneMetric, TypeOneKey>().ApplyOrderBys(query); | ||
|
||
var response = set.ToList(); | ||
} | ||
|
||
return Task.CompletedTask; | ||
}); | ||
} | ||
} | ||
|
||
public class Service<TMetric, TKey> where TMetric : Metric<TKey> | ||
where TKey : GenericKey | ||
{ | ||
public IQueryable<TMetric> ApplyOrderBys(IQueryable<TMetric> entities) | ||
{ | ||
var expression = new ExpressionParser().Parse<TMetric>("Sum"); | ||
var orderBy = new OrderBy<TMetric>(expression.ToLambdaExpression()); | ||
return orderBy.ApplyOrderBy(entities); | ||
} | ||
} | ||
} |