-
Notifications
You must be signed in to change notification settings - Fork 254
Add a fallback to LINQ for search evaluators/validator in case of custom user specifications. #514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+653
−51
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
105 changes: 105 additions & 0 deletions
105
...alis.Specification.EntityFrameworkCore.Tests/Evaluators/SearchEvaluatorCustomSpecTests.cs
This file contains hidden or 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,105 @@ | ||
namespace Tests.Evaluators; | ||
|
||
// This is a special case where users have custom ISpecification<T> implementation but use our evaluator. | ||
[Collection("SharedCollection")] | ||
public class SearchEvaluatorCustomSpecTests(TestFactory factory) : IntegrationTest(factory) | ||
{ | ||
private static readonly SearchEvaluator _evaluator = SearchEvaluator.Instance; | ||
|
||
[Fact] | ||
public void QueriesMatch_GivenNoSearch() | ||
{ | ||
var spec = new CustomSpecification<Store>(); | ||
spec.Where.Add(new WhereExpressionInfo<Store>(x => x.Id > 0)); | ||
|
||
var actual = _evaluator.GetQuery(DbContext.Stores, spec) | ||
.ToQueryString(); | ||
|
||
var expected = DbContext.Stores | ||
.ToQueryString(); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
|
||
[Fact] | ||
public void QueriesMatch_GivenSingleSearch() | ||
{ | ||
var storeTerm = "ab1"; | ||
|
||
var spec = new CustomSpecification<Store>(); | ||
spec.Where.Add(new WhereExpressionInfo<Store>(x => x.Id > 0)); | ||
spec.Search.Add(new SearchExpressionInfo<Store>(x => x.Name, $"%{storeTerm}%")); | ||
|
||
var actual = _evaluator.GetQuery(DbContext.Stores, spec) | ||
.ToQueryString(); | ||
|
||
var expected = DbContext.Stores | ||
.Where(x => EF.Functions.Like(x.Name, $"%{storeTerm}%")) | ||
.ToQueryString(); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
|
||
[Fact] | ||
public void QueriesMatch_GivenMultipleSearch() | ||
{ | ||
var storeTerm = "ab1"; | ||
var companyTerm = "ab2"; | ||
var countryTerm = "ab3"; | ||
var streetTerm = "ab4"; | ||
|
||
var spec = new CustomSpecification<Store>(); | ||
spec.Where.Add(new WhereExpressionInfo<Store>(x => x.Id > 0)); | ||
spec.Search.Add(new SearchExpressionInfo<Store>(x => x.Name, $"%{storeTerm}%")); | ||
spec.Search.Add(new SearchExpressionInfo<Store>(x => x.Company.Name, $"%{companyTerm}%")); | ||
spec.Search.Add(new SearchExpressionInfo<Store>(x => x.Address.Street, $"%{streetTerm}%", 2)); | ||
spec.Search.Add(new SearchExpressionInfo<Store>(x => x.Company.Country.Name, $"%{countryTerm}%", 3)); | ||
|
||
var actual = _evaluator.GetQuery(DbContext.Stores, spec) | ||
.ToQueryString(); | ||
|
||
var expected = DbContext.Stores | ||
.Where(x => EF.Functions.Like(x.Name, $"%{storeTerm}%") | ||
|| EF.Functions.Like(x.Company.Name, $"%{companyTerm}%")) | ||
.Where(x => EF.Functions.Like(x.Address.Street, $"%{streetTerm}%")) | ||
.Where(x => EF.Functions.Like(x.Company.Country.Name, $"%{countryTerm}%")) | ||
.ToQueryString(); | ||
|
||
actual.Should().Be(expected); | ||
} | ||
|
||
public class CustomSpecification<T> : ISpecification<T> | ||
{ | ||
public List<WhereExpressionInfo<T>> Where { get; set; } = new(); | ||
public List<SearchExpressionInfo<T>> Search { get; set; } = new(); | ||
public IEnumerable<SearchExpressionInfo<T>> SearchCriterias => Search; | ||
public IEnumerable<WhereExpressionInfo<T>> WhereExpressions => Where; | ||
|
||
public ISpecificationBuilder<T> Query => throw new NotImplementedException(); | ||
public IEnumerable<OrderExpressionInfo<T>> OrderExpressions => throw new NotImplementedException(); | ||
public IEnumerable<IncludeExpressionInfo> IncludeExpressions => throw new NotImplementedException(); | ||
public IEnumerable<string> IncludeStrings => throw new NotImplementedException(); | ||
public Dictionary<string, object> Items => throw new NotImplementedException(); | ||
public int Take => throw new NotImplementedException(); | ||
public int Skip => throw new NotImplementedException(); | ||
public Func<IEnumerable<T>, IEnumerable<T>>? PostProcessingAction => throw new NotImplementedException(); | ||
public IEnumerable<string> QueryTags => throw new NotImplementedException(); | ||
public bool CacheEnabled => throw new NotImplementedException(); | ||
public string? CacheKey => throw new NotImplementedException(); | ||
public bool AsTracking => throw new NotImplementedException(); | ||
public bool AsNoTracking => throw new NotImplementedException(); | ||
public bool AsSplitQuery => throw new NotImplementedException(); | ||
public bool AsNoTrackingWithIdentityResolution => throw new NotImplementedException(); | ||
public bool IgnoreQueryFilters => throw new NotImplementedException(); | ||
public bool IgnoreAutoIncludes => throw new NotImplementedException(); | ||
public IEnumerable<T> Evaluate(IEnumerable<T> entities) | ||
=> throw new NotImplementedException(); | ||
public bool IsSatisfiedBy(T entity) | ||
=> throw new NotImplementedException(); | ||
|
||
void ISpecification<T>.CopyTo(Specification<T> otherSpec) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.