Skip to content

Commit 278b507

Browse files
authored
Merge pull request #2 from pdevito3/release/v0.3.0
Release/v0.3.0
2 parents 4a5f831 + 78ff318 commit 278b507

23 files changed

Lines changed: 954 additions & 174 deletions

QueryKit.IntegrationTests/Tests/DatabaseFilteringTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace QueryKit.IntegrationTests.Tests;
22

33
using System.Linq.Expressions;
44
using Bogus;
5+
using Configuration;
56
using FluentAssertions;
67
using Microsoft.EntityFrameworkCore;
78
using SharedTestingHelper.Fakes;

QueryKit.UnitTests/CustomFilterPropertyTests.cs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
namespace QueryKit.UnitTests;
22

33
using Bogus;
4+
using Configuration;
5+
using Exceptions;
46
using FluentAssertions;
57
using Operators;
68
using WebApiTestProject.Entities;
9+
using WebApiTestProject.Entities.Recipes;
710

811
public class CustomFilterPropertyTests
912
{
@@ -16,7 +19,7 @@ public void can_have_child_prop_name_ownsone()
1619
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input);
1720
filterExpression.ToString().Should().Be($"""x => (x.PhysicalAddress.State == "{value}")""");
1821
}
19-
22+
2023
[Fact]
2124
public void can_have_custom_child_prop_name_ownsone()
2225
{
@@ -72,6 +75,36 @@ public void can_have_custom_prop_name_for_string()
7275
filterExpression.ToString().Should().Be($"""x => (x.Title == "{value}")""");
7376
}
7477

78+
[Fact]
79+
public void can_handle_alias_in_value()
80+
{
81+
var faker = new Faker();
82+
var value = faker.Lorem.Word();
83+
var input = $"""special_title == "{value} with special_value" """;
84+
85+
var config = new QueryKitConfiguration(config =>
86+
{
87+
config.Property<TestingPerson>(x => x.Title).HasQueryName("special_title");
88+
});
89+
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
90+
filterExpression.ToString().Should().Be($"""x => (x.Title == "{value} with special_value")""");
91+
}
92+
93+
[Fact]
94+
public void can_handle_alias_in_value_with_operator_after_it()
95+
{
96+
var faker = new Faker();
97+
var value = faker.Lorem.Word();
98+
var input = $"""special_title == "{value} with special_value @=* a thing" """;
99+
100+
var config = new QueryKitConfiguration(config =>
101+
{
102+
config.Property<TestingPerson>(x => x.Title).HasQueryName("special_title");
103+
});
104+
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
105+
filterExpression.ToString().Should().Be($"""x => (x.Title == "{value} with special_value @=* a thing")""");
106+
}
107+
75108
[Fact]
76109
public void can_have_custom_prop_name_for_multiple_props()
77110
{
@@ -169,4 +202,35 @@ public void filter_prevented_props_always_have_true_equals_true_regardless_of_co
169202
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
170203
filterExpression.ToString().Should().Be($"""x => (True == True)""");
171204
}
205+
206+
[Fact]
207+
public void can_throw_error_when_property_has_space()
208+
{
209+
var faker = new Faker();
210+
var propertyName = faker.Lorem.Sentence();
211+
var firstWord = propertyName.Split(' ').First();
212+
var input = $"""{propertyName} == 25""";
213+
214+
var config = new QueryKitConfiguration(config =>
215+
{
216+
config.Property<TestingPerson>(x => x.Id).PreventFilter();
217+
});
218+
var act = () => FilterParser.ParseFilter<TestingPerson>(input, config);
219+
act.Should().Throw<UnknownFilterPropertyException>()
220+
.WithMessage($"The filter property '{firstWord}' was not recognized.");
221+
}
222+
223+
[Fact]
224+
public void can_handle_nonexistent_property()
225+
{
226+
var faker = new Faker();
227+
var input = $"""{faker.Lorem.Word()} == 25""";
228+
229+
var config = new QueryKitConfiguration(config =>
230+
{
231+
config.AllowUnknownProperties = true;
232+
});
233+
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input, config);
234+
filterExpression.ToString().Should().Be("x => (True == True)");
235+
}
172236
}

QueryKit.UnitTests/FilterParserTests.cs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace QueryKit.UnitTests;
22

33
using Bogus;
4+
using Exceptions;
45
using FluentAssertions;
56
using WebApiTestProject.Entities;
67

@@ -424,11 +425,52 @@ public void equals_doesnt_fail_with_non_string_types()
424425
}
425426

426427
[Fact]
427-
public void can_handle_nonexistent_property()
428+
public void can_throw_error_when_property_not_recognized()
428429
{
429430
var faker = new Faker();
430-
var input = $"""{faker.Lorem.Word()} == 25""";
431-
var filterExpression = FilterParser.ParseFilter<TestingPerson>(input);
432-
filterExpression.ToString().Should().Be("x => (True == True)");
431+
var propertyName = faker.Lorem.Word();
432+
var input = $"""{propertyName} == 25""";
433+
var act = () => FilterParser.ParseFilter<TestingPerson>(input);
434+
act.Should().Throw<UnknownFilterPropertyException>()
435+
.WithMessage($"The filter property '{propertyName}' was not recognized.");
436+
}
437+
438+
[Fact]
439+
public void can_throw_error_when_comparison_operator_not_recognized()
440+
{
441+
var input = $"""Age ^#$%^%@ 25""";
442+
var act = () => FilterParser.ParseFilter<TestingPerson>(input);
443+
act.Should().Throw<ParsingException>()
444+
.WithMessage("There was a parsing failure, likely due to an invalid comparison or logical operator. You may also be missing double quotes surrounding a string or guid.");
445+
}
446+
447+
[Fact]
448+
public void can_throw_error_when_logical_operator_not_recognized()
449+
{
450+
var input = $"""Title == "temp" %$@#^ Age == 25""";
451+
var act = () => FilterParser.ParseFilter<TestingPerson>(input);
452+
act.Should().Throw<ParsingException>()
453+
.WithMessage("There was a parsing failure, likely due to an invalid comparison or logical operator. You may also be missing double quotes surrounding a string or guid.");
454+
}
455+
456+
[Fact]
457+
public void can_throw_error_when_missing_double_quotes_not_recognized()
458+
{
459+
var input = $"""Title == temp string here""";
460+
var act = () => FilterParser.ParseFilter<TestingPerson>(input);
461+
act.Should().Throw<ParsingException>()
462+
.WithMessage("There was a parsing failure, likely due to an invalid comparison or logical operator. You may also be missing double quotes surrounding a string or guid.");
463+
}
464+
465+
[Fact]
466+
public void can_throw_error_when_property_has_space()
467+
{
468+
var faker = new Faker();
469+
var propertyName = faker.Lorem.Sentence();
470+
var firstWord = propertyName.Split(' ').First();
471+
var input = $"""{propertyName} == 25""";
472+
var act = () => FilterParser.ParseFilter<TestingPerson>(input);
473+
act.Should().Throw<UnknownFilterPropertyException>()
474+
.WithMessage($"The filter property '{firstWord}' was not recognized.");
433475
}
434476
}

0 commit comments

Comments
 (0)