-
Notifications
You must be signed in to change notification settings - Fork 37
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
Added a new filter expression for multiple search on strings #205
Changes from all commits
69cb72f
0f66882
e087116
79f90f0
dad182f
218b159
45cb954
e41cbbe
f743a13
cbbadca
58d57d0
66dbc4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using Saule; | ||
using Saule.Queries; | ||
using Saule.Queries.Filtering; | ||
using Saule.Queries.Sorting; | ||
using Tests.Helpers; | ||
using Tests.Models; | ||
using Xunit; | ||
|
@@ -97,6 +95,20 @@ public void WorksOnInts() | |
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact(DisplayName = "Applies filtering on multiple ints")] | ||
public void WorksOnMultipleInts() | ||
{ | ||
var people = Get.People(100).ToList().AsQueryable(); | ||
var expected = people.Where(c => c.Age == 20 || c.Age == 30).ToList(); | ||
|
||
var query = GetQuery(new[] { "Age" }, new[] { "20,30" }); | ||
|
||
var result = Query.ApplyFiltering(people, new FilterContext(query), new PersonResource()) | ||
as IQueryable<Person>; | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact(DisplayName = "Applies filtering on enums (string)")] | ||
public void WorksOnEnumsAsStrings() | ||
{ | ||
|
@@ -111,6 +123,20 @@ public void WorksOnEnumsAsStrings() | |
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact(DisplayName = "Applies filtering on multiple enums (string)")] | ||
public void WorksOnMultipleEnumsAsStrings() | ||
{ | ||
var companies = Get.Companies(100).ToList().AsQueryable(); | ||
var expected = companies.Where(c => c.Location == LocationType.National || c.Location == LocationType.Local).ToList(); | ||
|
||
var query = GetQuery(new[] { "Location" }, new[] { "national,local" }); | ||
|
||
var result = Query.ApplyFiltering(companies, new FilterContext(query), new CompanyResource()) | ||
as IQueryable<Company>; | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact(DisplayName = "Applies filtering on strings")] | ||
public void WorksOnStrings() | ||
{ | ||
|
@@ -125,6 +151,48 @@ public void WorksOnStrings() | |
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact(DisplayName = "Applies filtering on strings with spaces")] | ||
public void WorksOnStringsWithSpaces() | ||
{ | ||
var companies = Get.Companies(100).ToList().AsQueryable(); | ||
var expected = companies.Where(c => c.Name == "Awesome Inc.").ToList(); | ||
|
||
var query = GetQuery(new[] { "Name" }, new[] { "Aweseom Inc." }); | ||
|
||
var result = Query.ApplyFiltering(companies, new FilterContext(query), new CompanyResource()) | ||
as IQueryable<Company>; | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact(DisplayName = "Applies filtering on strings with multiple values")] | ||
public void WorksOnStringsMultiple() | ||
{ | ||
var people = Get.People(100).ToList().AsQueryable(); | ||
var expected = people.Where(c => c.LastName == "Russel" || c.LastName == "Comma,Test").ToList(); | ||
|
||
var query = GetQuery(new[] { "LastName" }, new[] { "Russel,\"Comma,Test\"" }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can also see what happens on some edge cases/malformed strings such as FWIW i'm not sure what should happen in this case.. I would be fine with a 400 although technically that would be a breaking change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now this is failing silently as anything not captured will be ignored. It's certainly possible to add a 400. I am not sure if this would be the best way tho. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would vote that either
I don't think we should let parts drop, throw 500, etc. I think you're right that option 2 is probably better. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a few checks and one more unit test for this. |
||
|
||
var result = Query.ApplyFiltering(people, new FilterContext(query), new PersonResource()) | ||
as IQueryable<Person>; | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact(DisplayName = "Applies filtering on strings with multiple values including quotes and commas")] | ||
public void WorksOnStringsMultipleWithQuotes() | ||
{ | ||
var people = Get.People(100).ToList().AsQueryable(); | ||
var expected = people.Where(c => c.LastName == "\"Quote,Test").ToList(); | ||
|
||
var query = GetQuery(new[] { "LastName" }, new[] { "\"Quote,Test" }); | ||
|
||
var result = Query.ApplyFiltering(people, new FilterContext(query), new PersonResource()) | ||
as IQueryable<Person>; | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact(DisplayName = "Works on enumerables")] | ||
public void WorksOnEnumerables() | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you maybe put a comment that explains this regex? Some time in the future we may want to remember how it works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried my best to explain this one. I hope that it's enough.