-
Notifications
You must be signed in to change notification settings - Fork 254
Add tests for EF6 evaluators. #520
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
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5debf9c
Add AsNoTrackingEvaluator tests.
fiseni d779456
Add IncludeEvaluator tests.
fiseni dbe2464
Add OrderEvaluator tests
fiseni f8b3e9d
Add SearchEvaluator tests.
fiseni 6d4dfe7
Add Skip attributes instead of commented tests.
fiseni 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
27 changes: 27 additions & 0 deletions
27
tests/Ardalis.Specification.EntityFramework6.Tests/Evaluators/AsNoTrackingEvaluatorTests.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,27 @@ | ||
| using Tests.FixtureNew; | ||
|
|
||
| namespace Tests.Evaluators; | ||
|
|
||
| [Collection("SharedCollection")] | ||
| public class AsNoTrackingEvaluatorTests(TestFactory factory) : IntegrationTest(factory) | ||
| { | ||
| private static readonly AsNoTrackingEvaluator _evaluator = AsNoTrackingEvaluator.Instance; | ||
|
|
||
| [Fact] | ||
| public void Applies_GivenAsNoTracking() | ||
| { | ||
| var spec = new Specification<Country>(); | ||
| spec.Query.AsNoTracking(); | ||
|
|
||
| var actual = _evaluator.GetQuery(DbContext.Countries, spec) | ||
| .Expression | ||
| .ToString(); | ||
|
|
||
| var expected = DbContext.Countries | ||
| .AsNoTracking() | ||
| .AsQueryable().Expression | ||
| .ToString(); | ||
|
|
||
| actual.Should().Be(expected); | ||
| } | ||
| } |
114 changes: 114 additions & 0 deletions
114
tests/Ardalis.Specification.EntityFramework6.Tests/Evaluators/IncludeEvaluatorTests.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,114 @@ | ||
| using System.Data.Entity; | ||
| using Tests.FixtureNew; | ||
|
|
||
| namespace Tests.Evaluators; | ||
|
|
||
| [Collection("SharedCollection")] | ||
| public class IncludeEvaluatorTests(TestFactory factory) : IntegrationTest(factory) | ||
| { | ||
| private static readonly IncludeEvaluator _evaluator = IncludeEvaluator.Instance; | ||
|
|
||
| [Fact] | ||
| public void QueriesMatch_GivenNoIncludeExpression() | ||
| { | ||
| var spec = new Specification<Company>(); | ||
|
|
||
| var actual = _evaluator.GetQuery(DbContext.Companies, spec); | ||
| var actualSql = GetQueryString(DbContext, actual); | ||
|
|
||
| var expected = DbContext.Companies.AsQueryable(); | ||
| var expectedSql = GetQueryString(DbContext, expected); | ||
|
|
||
| actualSql.Should().Be(expectedSql); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void QueriesMatch_GivenNoIncludeExpression_WithAutoOneToOne() | ||
| { | ||
| var spec = new Specification<Store>(); | ||
|
|
||
| var actual = _evaluator.GetQuery(DbContext.Stores, spec); | ||
| var actualSql = GetQueryString(DbContext, actual); | ||
|
|
||
| var expected = DbContext.Stores.AsQueryable(); | ||
| var expectedSql = GetQueryString(DbContext, expected); | ||
|
|
||
| actualSql.Should().Be(expectedSql); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void QueriesMatch_GivenSingleIncludeExpression_WithReferenceNavigation() | ||
| { | ||
| var spec = new Specification<Company>(); | ||
| spec.Query | ||
| .Include(x => x.Country); | ||
|
|
||
| var actual = _evaluator.GetQuery(DbContext.Companies, spec); | ||
| var actualSql = GetQueryString(DbContext, actual); | ||
|
|
||
| var expected = DbContext.Companies | ||
| .Include(x => x.Country); | ||
| var expectedSql = GetQueryString(DbContext, expected); | ||
|
|
||
| actualSql.Should().Be(expectedSql); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void QueriesMatch_GivenSingleIncludeExpression_WithCollectionNavigation() | ||
| { | ||
| var spec = new Specification<Company>(); | ||
| spec.Query | ||
| .Include(x => x.Stores); | ||
|
|
||
| var actual = _evaluator.GetQuery(DbContext.Companies, spec); | ||
| var actualSql = GetQueryString(DbContext, actual); | ||
|
|
||
| var expected = DbContext.Companies | ||
| .Include(x => x.Stores); | ||
| var expectedSql = GetQueryString(DbContext, expected); | ||
|
|
||
| actualSql.Should().Be(expectedSql); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void QueriesMatch_GivenMultipleIncludeExpression() | ||
| { | ||
| var spec = new Specification<Company>(); | ||
| spec.Query | ||
| .Include(x => x.Country) | ||
| .Include(x => x.Stores); | ||
|
|
||
| var actual = _evaluator.GetQuery(DbContext.Companies, spec); | ||
| var actualSql = GetQueryString(DbContext, actual); | ||
|
|
||
| var expected = DbContext.Companies | ||
| .Include(x => x.Country) | ||
| .Include(x => x.Stores); | ||
| var expectedSql = GetQueryString(DbContext, expected); | ||
|
|
||
| actualSql.Should().Be(expectedSql); | ||
| } | ||
|
|
||
| // TODO: Found out that EF6 include evaluator fails for multiple include chains. [Fati Iseni, 15/06/2025] | ||
| //[Fact] | ||
| //public void QueriesMatch_GivenThenIncludeExpression() | ||
| //{ | ||
| // var spec = new Specification<Store>(); | ||
| // spec.Query | ||
| // .Include(x => x.Products) | ||
| // .ThenInclude(x => x.Images) | ||
| // .Include(x => x.Company) | ||
| // .ThenInclude(x => x.Country); | ||
|
|
||
| // var actual = _evaluator.GetQuery(DbContext.Stores, spec); | ||
| // var actualSql = GetQueryString(DbContext, actual); | ||
|
|
||
| // // EF6 doe't support ThenInclude, it uses string-based includes | ||
| // var expected = DbContext.Stores | ||
| // .Include($"{nameof(Store.Products)}.{nameof(Product.Images)}") | ||
| // .Include($"{nameof(Store.Company)}.{nameof(Company.Country)}"); | ||
| // var expectedSql = GetQueryString(DbContext, expected); | ||
|
|
||
| // actualSql.Should().Be(expectedSql); | ||
| //} | ||
| } | ||
46 changes: 46 additions & 0 deletions
46
tests/Ardalis.Specification.EntityFramework6.Tests/Evaluators/OrderEvaluatorTests.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,46 @@ | ||
| using Tests.FixtureNew; | ||
|
|
||
| namespace Tests.Evaluators; | ||
|
|
||
| [Collection("SharedCollection")] | ||
| public class OrderEvaluatorTests(TestFactory factory) : IntegrationTest(factory) | ||
| { | ||
| private static readonly Ardalis.Specification.EntityFramework6.OrderEvaluator _evaluator = | ||
| Ardalis.Specification.EntityFramework6.OrderEvaluator.Instance; | ||
|
|
||
| [Fact] | ||
| public void QueriesMatch_GivenOrder() | ||
| { | ||
| var spec = new Specification<Company>(); | ||
| spec.Query | ||
| .OrderBy(x => x.Id); | ||
|
|
||
| var actual = _evaluator.GetQuery(DbContext.Companies, spec); | ||
| var actualSql = GetQueryString(DbContext, actual); | ||
|
|
||
| var expected = DbContext.Companies | ||
| .OrderBy(x => x.Id); | ||
| var expectedSql = GetQueryString(DbContext, expected); | ||
|
|
||
| actualSql.Should().Be(expectedSql); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void QueriesMatch_GivenOrderChain() | ||
| { | ||
| var spec = new Specification<Company>(); | ||
| spec.Query | ||
| .OrderBy(x => x.Id) | ||
| .ThenBy(x => x.Name); | ||
|
|
||
| var actual = _evaluator.GetQuery(DbContext.Companies, spec); | ||
| var actualSql = GetQueryString(DbContext, actual); | ||
|
|
||
| var expected = DbContext.Companies | ||
| .OrderBy(x => x.Id) | ||
| .ThenBy(x => x.Name); | ||
| var expectedSql = GetQueryString(DbContext, expected); | ||
|
|
||
| actualSql.Should().Be(expectedSql); | ||
| } | ||
| } |
77 changes: 77 additions & 0 deletions
77
tests/Ardalis.Specification.EntityFramework6.Tests/Evaluators/SearchEvaluatorTests.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,77 @@ | ||
| using System.Data.Entity; | ||
| using Tests.FixtureNew; | ||
|
|
||
| namespace Tests.Evaluators; | ||
|
|
||
| [Collection("SharedCollection")] | ||
| public class SearchEvaluatorTests(TestFactory factory) : IntegrationTest(factory) | ||
| { | ||
| private static readonly SearchEvaluator _evaluator = SearchEvaluator.Instance; | ||
|
|
||
| [Fact] | ||
| public void QueriesMatch_GivenNoSearch() | ||
| { | ||
| var spec = new Specification<Company>(); | ||
| spec.Query | ||
| .Where(x => x.Id > 0); | ||
|
|
||
| var actual = _evaluator.GetQuery(DbContext.Companies, spec); | ||
| var actualSql = GetQueryString(DbContext, actual); | ||
|
|
||
| var expected = DbContext.Companies.AsQueryable(); | ||
| var expectedSql = GetQueryString(DbContext, expected); | ||
|
|
||
| actualSql.Should().Be(expectedSql); | ||
| } | ||
|
|
||
| //// TODO: Not producing the same query, it's not parameterized and no null check [Fati Iseni, 15/06/2025] | ||
| //[Fact] | ||
| //public void QueriesMatch_GivenSingleSearch() | ||
| //{ | ||
| // var storeTerm = "ab1"; | ||
|
|
||
| // var spec = new Specification<Company>(); | ||
| // spec.Query | ||
| // .Where(x => x.Id > 0) | ||
| // .Search(x => x.Name, $"%{storeTerm}%"); | ||
|
|
||
| // var actual = _evaluator.GetQuery(DbContext.Companies, spec); | ||
| // var actualSql = GetQueryString(DbContext, actual); | ||
|
|
||
| // var expected = DbContext.Companies | ||
| // .Where(x => DbFunctions.Like(x.Name, "%" + storeTerm + "%")); | ||
| // var expectedSql = GetQueryString(DbContext, expected); | ||
|
|
||
| // actualSql.Should().Be(expectedSql); | ||
| //} | ||
|
|
||
| //// TODO: Not producing the same query, it's not parameterized and no null check [Fati Iseni, 15/06/2025] | ||
| //[Fact] | ||
| //public void QueriesMatch_GivenMultipleSearch() | ||
| //{ | ||
| // var storeTerm = "ab1"; | ||
| // var companyTerm = "ab2"; | ||
| // var countryTerm = "ab3"; | ||
| // var streetTerm = "ab4"; | ||
|
|
||
| // var spec = new Specification<Store>(); | ||
| // spec.Query | ||
| // .Where(x => x.Id > 0) | ||
| // .Search(x => x.Name, $"%{storeTerm}%") | ||
| // .Search(x => x.Company.Name, $"%{companyTerm}%") | ||
| // .Search(x => x.Company.Country.Name, $"%{countryTerm}%", 3) | ||
| // .Search(x => x.Address.Street, $"%{streetTerm}%", 2); | ||
|
|
||
| // var actual = _evaluator.GetQuery(DbContext.Stores, spec); | ||
| // var actualSql = GetQueryString(DbContext, actual); | ||
|
|
||
| // var expected = DbContext.Stores | ||
| // .Where(x => DbFunctions.Like(x.Name, "%" + storeTerm + "%") | ||
| // || DbFunctions.Like(x.Company.Name, "%" + storeTerm + "%")) | ||
| // .Where(x => DbFunctions.Like(x.Address.Street, "%" + storeTerm + "%")) | ||
| // .Where(x => DbFunctions.Like(x.Company.Country.Name, "%" + storeTerm + "%")); | ||
| // var expectedSql = GetQueryString(DbContext, expected); | ||
|
|
||
| // actualSql.Should().Be(expectedSql); | ||
| //} | ||
fiseni marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
4 changes: 1 addition & 3 deletions
4
tests/Ardalis.Specification.EntityFramework6.Tests/FixtureNew/Data/Address.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
4 changes: 1 addition & 3 deletions
4
tests/Ardalis.Specification.EntityFramework6.Tests/FixtureNew/Data/Company.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
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
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.