Skip to content

Commit

Permalink
fix: Made goatquery case insensitivity by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamess-Lucass committed Jan 19, 2024
1 parent 0383051 commit 82a59c2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/Extensions/QueryableExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ public static (IQueryable, int?) Apply<T>(this IQueryable<T> queryable, Query qu
}
else
{
where.Append($"{property} {_filterOperations[operand]} {value}");
where.Append($"{property}.ToLower() {_filterOperations[operand]} {value}.ToLower()");
}
}

Console.WriteLine(where.ToString());

result = result.Where(where.ToString());
}

Expand Down
33 changes: 23 additions & 10 deletions tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,20 @@ public void Test_QueryWithFilterEquals()
var (result, _) = _context.Users.AsQueryable().Apply(query, null);
var sql = result.ToQueryString();

var expectedSql = _context.Users.AsQueryable().Where(x => x.Firstname == "goat").ToQueryString();
var expectedSql = _context.Users.AsQueryable().Where(x => x.Firstname.ToLower() == "goat".ToLower()).ToQueryString();

Assert.Equal(expectedSql, sql);
}

[Fact]
public void Test_QueryWithFilterEqualsCaseInsensitivity()
{
var query = new Query() { Filter = "firstname eq 'goat'" };

var (result, _) = _context.Users.AsQueryable().Apply(query, null);
var sql = result.ToQueryString();

var expectedSql = _context.Users.AsQueryable().Where(x => x.Firstname.ToLower() == "goat".ToLower()).ToQueryString();

Assert.Equal(expectedSql, sql);
}
Expand All @@ -232,7 +245,7 @@ public void Test_QueryWithFilterNotEquals()
var (result, _) = _context.Users.AsQueryable().Apply(query, null);
var sql = result.ToQueryString();

var expectedSql = _context.Users.AsQueryable().Where(x => x.Firstname != "goat").ToQueryString();
var expectedSql = _context.Users.AsQueryable().Where(x => x.Firstname.ToLower() != "goat".ToLower()).ToQueryString();

Assert.Equal(expectedSql, sql);
}
Expand All @@ -245,7 +258,7 @@ public void Test_QueryWithFilterEqualsAndEquals()
var (result, _) = _context.Users.AsQueryable().Apply(query, null);
var sql = result.ToQueryString();

var expectedSql = _context.Users.AsQueryable().Where(x => x.Firstname == "goat" && x.Lastname == "query").ToQueryString();
var expectedSql = _context.Users.AsQueryable().Where(x => x.Firstname.ToLower() == "goat".ToLower() && x.Lastname.ToLower() == "query".ToLower()).ToQueryString();

Assert.Equal(expectedSql, sql);
}
Expand All @@ -258,7 +271,7 @@ public void Test_QueryWithFilterEqualsAndNotEquals()
var (result, _) = _context.Users.AsQueryable().Apply(query, null);
var sql = result.ToQueryString();

var expectedSql = _context.Users.AsQueryable().Where(x => x.Firstname == "goat" && x.Lastname != "query").ToQueryString();
var expectedSql = _context.Users.AsQueryable().Where(x => x.Firstname.ToLower() == "goat".ToLower() && x.Lastname.ToLower() != "query".ToLower()).ToQueryString();

Assert.Equal(expectedSql, sql);
}
Expand Down Expand Up @@ -287,7 +300,7 @@ public void Test_QueryWithFilterContainsAndEquals()
var sql = result.ToQueryString();

var expectedSql = _context.Users.AsQueryable()
.Where(x => x.Firstname.Contains("goat") && x.Lastname == "query")
.Where(x => x.Firstname.Contains("goat") && x.Lastname.ToLower() == "query".ToLower())
.ToQueryString();

Assert.Equal(expectedSql, sql);
Expand All @@ -302,7 +315,7 @@ public void Test_QueryWithFilterContainsOrEquals()
var sql = result.ToQueryString();

var expectedSql = _context.Users.AsQueryable()
.Where(x => x.Firstname.Contains("goat") || x.Lastname == "query")
.Where(x => x.Firstname.Contains("goat") || x.Lastname.ToLower() == "query".ToLower())
.ToQueryString();

Assert.Equal(expectedSql, sql);
Expand All @@ -317,7 +330,7 @@ public void Test_QueryWithFilterEqualsWithConjunction()
var sql = result.ToQueryString();

var expectedSql = _context.Users.AsQueryable()
.Where(x => x.Firstname == "goatand")
.Where(x => x.Firstname.ToLower() == "goatand".ToLower())
.ToQueryString();

Assert.Equal(expectedSql, sql);
Expand All @@ -332,7 +345,7 @@ public void Test_QueryWithFilterEqualsWithConjunctionAndSpaces()
var sql = result.ToQueryString();

var expectedSql = _context.Users.AsQueryable()
.Where(x => x.Firstname == " and " || x.Lastname == " and or ")
.Where(x => x.Firstname.ToLower() == " and ".ToLower() || x.Lastname.ToLower() == " and or ".ToLower())
.ToQueryString();

Assert.Equal(expectedSql, sql);
Expand All @@ -347,7 +360,7 @@ public void Test_QueryWithFilterCustomJsonPropertyName()
var sql = result.ToQueryString();

var expectedSql = _context.Users.AsQueryable()
.Where(x => x.UserName == "John")
.Where(x => x.UserName.ToLower() == "John".ToLower())
.ToQueryString();

Assert.Equal(expectedSql, sql);
Expand All @@ -362,7 +375,7 @@ public void Test_QueryWithFilterCustomColumnName()
var sql = result.ToQueryString();

var expectedSql = _context.Users.AsQueryable()
.Where(x => x.Gender == "Male")
.Where(x => x.Gender.ToLower() == "Male".ToLower())
.ToQueryString();

Assert.Equal(expectedSql, sql);
Expand Down

0 comments on commit 82a59c2

Please sign in to comment.