Skip to content
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

Couple bug fixes - exists with case sensitivity and primitive != operator #206

Merged
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
9 changes: 7 additions & 2 deletions src/Gridify/Builder/BaseQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,13 @@ private static object AddIndexerNullCheck(LambdaExpression mapTarget, object que
&& op.Kind is not SyntaxKind.GreaterOrEqualThan
&& op.Kind is not SyntaxKind.LessOrEqualThan)
{
value = value.ToString()?.ToLower();
body = Expression.Call(body, MethodInfoHelper.GetToLowerMethod());
var strLowerValue = value.ToString()?.ToLower();
value = strLowerValue;

if(!string.IsNullOrEmpty(strLowerValue))
{
body = Expression.Call(body, MethodInfoHelper.GetToLowerMethod());
}
}

var query = BuildQueryAccordingToValueType(body, parameter, value, op, valueExpression);
Expand Down
3 changes: 3 additions & 0 deletions src/Gridify/Builder/LinqQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Gridify.Builder;

public class LinqQueryBuilder<T>(IGridifyMapper<T> mapper) : BaseQueryBuilder<Expression<Func<T, bool>>, T>(mapper)

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.

Check warning on line 10 in src/Gridify/Builder/LinqQueryBuilder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'IGridifyMapper<T> mapper' is captured into the state of the enclosing type and its value is also passed to the base constructor. The value might be captured by the base class as well.
{
protected override Expression<Func<T, bool>>? BuildNestedQuery(
Expression body, IGMap<T> gMap, ValueExpressionSyntax value, ISyntaxNode op)
Expand Down Expand Up @@ -364,6 +364,9 @@
if (op.Kind == SyntaxKind.NotEqual)
{
containsExp = Expression.Not(containsExp);
// issue #204 we need to add or null check as well for Not Contains
containsExp = Expression.OrElse(Expression.Equal(prop, Expression.Constant(null)), containsExp);
return Expression.Lambda(containsExp, param);
}
return GetExpressionWithNullCheck(prop, param, containsExp);
}
Expand Down
1 change: 1 addition & 0 deletions test/Gridify.Tests/GridifyExtensionsShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static IEnumerable<TestClass> GetSampleData()
lst.Add(new TestClass(27, "ali reza", null));
lst.Add(new TestClass(27, "[ali]", null));
lst.Add(new TestClass(28, @"Esc/\pe", null));
lst.Add(new TestClass(29, @"NullTag", null, tag: null));

return lst;
}
Expand Down
48 changes: 48 additions & 0 deletions test/Gridify.Tests/IssueTests/Issue202Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Gridify.Tests.IssueTests;

public class Issue202Tests
{
private readonly List<TestClass> _fakeRepository = [.. GridifyExtensionsShould.GetSampleData()];

[Fact]
// Issue #202
public void ApplyFiltering_NotExists_GlobalCaseInsensitiveSearch()
{
var mapper = new GridifyMapper<TestClass>(m => m.CaseInsensitiveFiltering = true).GenerateMappings();

var gq = new GridifyQuery { Filter = "tag=" };

var expected = _fakeRepository.Where(q => string.IsNullOrEmpty(q.Tag)).ToList();

var actual = _fakeRepository.AsQueryable()
.ApplyFiltering(gq, mapper)
.ToList();

Assert.Equal(expected.Count, actual.Count);
Assert.Equal(expected, actual);
Assert.True(actual.Any());
}

[Fact]
// Issue #202
public void ApplyFiltering_Exists_GlobalCaseInsensitiveSearch()
{
var mapper = new GridifyMapper<TestClass>(m => m.CaseInsensitiveFiltering = true).GenerateMappings();

var gq = new GridifyQuery { Filter = "tag!=" };

var expected = _fakeRepository.Where(q => !string.IsNullOrEmpty(q.Tag)).ToList();

var actual = _fakeRepository.AsQueryable()
.ApplyFiltering(gq, mapper)
.ToList();

Assert.Equal(expected.Count, actual.Count);
Assert.Equal(expected, actual);
Assert.True(actual.Any());
}
}
33 changes: 33 additions & 0 deletions test/Gridify.Tests/IssueTests/Issue204Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Gridify.Tests.IssueTests;

public class Issue204Tests
{
[Fact]
public void ApplyFiltering_NotEquals_ShouldMatch_NullItems()
{
// arrange
var dataSource = new List<Test>()
{
new() {FavouriteColorList = ["Green", "Blue"]},
new() {FavouriteColorList = ["White", "Yellow"]},
new() { FavouriteColorList = null },

Check warning on line 17 in test/Gridify.Tests/IssueTests/Issue204Tests.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 17 in test/Gridify.Tests/IssueTests/Issue204Tests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Cannot convert null literal to non-nullable reference type.
}.AsQueryable();

var expected = dataSource.Where(q => q.FavouriteColorList == null || !q.FavouriteColorList.Contains("Green")).ToList();
var actual = dataSource.ApplyFiltering("FavouriteColorList!=Green").ToList();

// assert
Assert.Equal(expected.Count, actual.Count);
Assert.Equal(expected, actual);
Assert.True(actual.Any());
}

private class Test
{
public List<string> FavouriteColorList { get; set; }

Check warning on line 31 in test/Gridify.Tests/IssueTests/Issue204Tests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'FavouriteColorList' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 31 in test/Gridify.Tests/IssueTests/Issue204Tests.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Non-nullable property 'FavouriteColorList' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
}
}
Loading