-
-
Notifications
You must be signed in to change notification settings - Fork 43
Allow case-insensitive comparison for Equals as well just like Contains #79
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 all commits
Commits
Show all changes
2 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
30 changes: 30 additions & 0 deletions
30
src/AutoFilterer/Attributes/ToLowerEqualsComparisonAttribute.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,30 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq.Expressions; | ||
| using System.Reflection; | ||
| using System.Text; | ||
|
|
||
| namespace AutoFilterer.Attributes; | ||
|
|
||
| /// <summary> | ||
| /// This class generates equal query, for string fields. | ||
| /// </summary> | ||
| public class ToLowerEqualsComparisonAttribute : FilteringOptionsBaseAttribute | ||
| { | ||
| public override Expression BuildExpression(ExpressionBuildContext context) | ||
| { | ||
| var equalsMethod = typeof(string).GetMethod(nameof(string.Equals), types: new[] { typeof(string) }); | ||
|
|
||
| var toLowerMethod = typeof(string).GetMethod(nameof(string.ToLower), types: new Type[0]); | ||
|
|
||
| var comparison = Expression.Equal( | ||
| Expression.Call( | ||
| method: equalsMethod, | ||
| instance: Expression.Call(method: toLowerMethod, instance: Expression.Property(context.ExpressionBody, context.TargetProperty.Name) | ||
| ), | ||
| arguments: new[] { Expression.Call(method: toLowerMethod, instance: context.FilterPropertyExpression) }), | ||
| Expression.Constant(true)); | ||
|
enisn marked this conversation as resolved.
enisn marked this conversation as resolved.
|
||
|
|
||
|
enisn marked this conversation as resolved.
|
||
| return comparison; | ||
| } | ||
| } | ||
208 changes: 208 additions & 0 deletions
208
tests/AutoFilterer.Tests/Attributes/ToLowerEqualsComparisonAttributeTests.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,208 @@ | ||
| using AutoFilterer.Extensions; | ||
| using AutoFilterer.Tests.Core; | ||
| using AutoFilterer.Tests.Environment.Dtos; | ||
| using AutoFilterer.Tests.Environment.Models; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Xunit; | ||
| using AutoFilterer.Attributes; | ||
| using AutoFilterer.Types; | ||
| using System; | ||
|
|
||
| namespace AutoFilterer.Tests.Attributes; | ||
|
|
||
| public class ToLowerEqualsComparisonAttributeTests | ||
| { | ||
| [Theory, AutoMoqData(count: 16)] | ||
| public void BuildExpression_ShouldGenerateQueryCorrect_WithAttribute(List<Book> dummyData) | ||
| { | ||
| // Arrange | ||
| var filter = new BookFilter_LowerEquals | ||
| { | ||
| Title = "Test Book" | ||
| }; | ||
| IQueryable<Book> query = dummyData.AsQueryable(); | ||
|
|
||
| // Act | ||
| var filteredQuery = query.ApplyFilter(filter); | ||
| var result = filteredQuery.ToList(); | ||
|
|
||
| // Assert | ||
| var actualResult = query.Where(x => x.Title.ToLower().Equals(filter.Title.ToLower())).ToList(); | ||
|
|
||
| Assert.Equal(result.Count, actualResult.Count); | ||
| } | ||
|
|
||
| [Theory, AutoMoqData(count: 16)] | ||
| public void BuildExpression_ShouldBeCaseInsensitive(List<Book> dummyData) | ||
| { | ||
| // Arrange | ||
| var filter = new BookFilter_LowerEquals | ||
| { | ||
| Title = "TEST BOOK" | ||
| }; | ||
| IQueryable<Book> query = dummyData.AsQueryable(); | ||
|
|
||
| // Act | ||
| var filteredQuery = query.ApplyFilter(filter); | ||
| var result = filteredQuery.ToList(); | ||
|
|
||
| // Assert | ||
| var actualResult = query.Where(x => x.Title.ToLower().Equals(filter.Title.ToLower())).ToList(); | ||
|
|
||
| Assert.Equal(result.Count, actualResult.Count); | ||
| } | ||
|
|
||
| [Theory, AutoMoqData(count: 16)] | ||
| public void BuildExpression_ShouldHandleEmptyString(List<Book> dummyData) | ||
| { | ||
| // Arrange | ||
| var filter = new BookFilter_LowerEquals | ||
| { | ||
| Title = "" | ||
| }; | ||
| IQueryable<Book> query = dummyData.AsQueryable(); | ||
|
|
||
| // Act | ||
| var filteredQuery = query.ApplyFilter(filter); | ||
| var result = filteredQuery.ToList(); | ||
|
|
||
| // Assert | ||
| var actualResult = query.Where(x => x.Title.ToLower().Equals(filter.Title.ToLower())).ToList(); | ||
|
|
||
| Assert.Equal(result.Count, actualResult.Count); | ||
| } | ||
|
|
||
| [Theory, AutoMoqData(count: 16)] | ||
| public void BuildExpression_ShouldHandleNullValue(List<Book> dummyData) | ||
| { | ||
| // Arrange | ||
| var filter = new BookFilter_LowerEquals | ||
| { | ||
| Title = null | ||
| }; | ||
| IQueryable<Book> query = dummyData.AsQueryable(); | ||
|
|
||
| // Act | ||
| var filteredQuery = query.ApplyFilter(filter); | ||
| var result = filteredQuery.ToList(); | ||
|
|
||
| // Assert | ||
| // When filter value is null, the framework should skip the filter entirely | ||
| // and return all results (no filtering applied) | ||
| Assert.Equal(dummyData.Count, result.Count); | ||
| } | ||
|
|
||
| [Theory, AutoMoqData(count: 16)] | ||
| public void BuildExpression_ShouldHandleSpecialCharacters(List<Book> dummyData) | ||
| { | ||
| // Arrange | ||
| var filter = new BookFilter_LowerEquals | ||
| { | ||
| Title = "Test-Book_123" | ||
| }; | ||
| IQueryable<Book> query = dummyData.AsQueryable(); | ||
|
|
||
| // Act | ||
| var filteredQuery = query.ApplyFilter(filter); | ||
| var result = filteredQuery.ToList(); | ||
|
|
||
| // Assert | ||
| var actualResult = query.Where(x => x.Title.ToLower().Equals(filter.Title.ToLower())).ToList(); | ||
|
|
||
| Assert.Equal(result.Count, actualResult.Count); | ||
| } | ||
|
|
||
| [Theory, AutoMoqData(count: 16)] | ||
| public void BuildExpression_ShouldHandleWhitespace(List<Book> dummyData) | ||
| { | ||
| // Arrange | ||
| var filter = new BookFilter_LowerEquals | ||
| { | ||
| Title = " Test Book " | ||
| }; | ||
| IQueryable<Book> query = dummyData.AsQueryable(); | ||
|
|
||
| // Act | ||
| var filteredQuery = query.ApplyFilter(filter); | ||
| var result = filteredQuery.ToList(); | ||
|
|
||
| // Assert | ||
| var actualResult = query.Where(x => x.Title.ToLower().Equals(filter.Title.ToLower())).ToList(); | ||
|
|
||
| Assert.Equal(result.Count, actualResult.Count); | ||
| } | ||
|
|
||
| [Theory, AutoMoqData(count: 16)] | ||
| public void BuildExpression_ShouldHandleUnicodeCharacters(List<Book> dummyData) | ||
| { | ||
| // Arrange | ||
| var filter = new BookFilter_LowerEquals | ||
| { | ||
| Title = "Café Book" | ||
| }; | ||
| IQueryable<Book> query = dummyData.AsQueryable(); | ||
|
|
||
| // Act | ||
| var filteredQuery = query.ApplyFilter(filter); | ||
| var result = filteredQuery.ToList(); | ||
|
|
||
| // Assert | ||
| var actualResult = query.Where(x => x.Title.ToLower().Equals(filter.Title.ToLower())).ToList(); | ||
|
|
||
| Assert.Equal(result.Count, actualResult.Count); | ||
| } | ||
|
|
||
| [Theory, AutoMoqData(count: 16)] | ||
| public void BuildExpression_ShouldReturnEmptyResult_WhenNoMatch(List<Book> dummyData) | ||
| { | ||
| // Arrange | ||
| var filter = new BookFilter_LowerEquals | ||
| { | ||
| Title = "NonExistentBook" | ||
| }; | ||
| IQueryable<Book> query = dummyData.AsQueryable(); | ||
|
|
||
| // Act | ||
| var filteredQuery = query.ApplyFilter(filter); | ||
| var result = filteredQuery.ToList(); | ||
|
|
||
| // Assert | ||
| var actualResult = query.Where(x => x.Title.ToLower().Equals(filter.Title.ToLower())).ToList(); | ||
|
|
||
| Assert.Equal(result.Count, actualResult.Count); | ||
| Assert.Empty(result); | ||
| } | ||
|
|
||
| [Theory, AutoMoqData(count: 16)] | ||
| public void BuildExpression_ShouldReturnExactMatchesOnly(List<Book> dummyData) | ||
| { | ||
| // Arrange | ||
| var filter = new BookFilter_LowerEquals | ||
| { | ||
| Title = "Test" | ||
| }; | ||
| IQueryable<Book> query = dummyData.AsQueryable(); | ||
|
|
||
| // Act | ||
| var filteredQuery = query.ApplyFilter(filter); | ||
| var result = filteredQuery.ToList(); | ||
|
|
||
| // Assert | ||
| var actualResult = query.Where(x => x.Title.ToLower().Equals(filter.Title.ToLower())).ToList(); | ||
|
|
||
| Assert.Equal(result.Count, actualResult.Count); | ||
|
|
||
| // Verify that all results are exact matches (not partial matches) | ||
| foreach (var book in result) | ||
| { | ||
| Assert.Equal(filter.Title.ToLower(), book.Title.ToLower()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public class BookFilter_LowerEquals : FilterBase | ||
| { | ||
| [ToLowerEqualsComparison] | ||
| public string Title { get; set; } | ||
| } |
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.