Skip to content

Commit 849d96d

Browse files
authored
Merge branch 'main' into fix_github_actions_and_unit_tests
2 parents 662ff59 + edd4e14 commit 849d96d

11 files changed

Lines changed: 43 additions & 35 deletions

QueryKit.IntegrationTests/Tests/DatabaseSortingTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace QueryKit.IntegrationTests.Tests;
33
using Bogus;
44
using FluentAssertions;
55
using Microsoft.EntityFrameworkCore;
6+
using QueryKit.Exceptions;
67
using SharedTestingHelper.Fakes;
78
using WebApiTestProject.Entities;
89

@@ -165,7 +166,7 @@ public async Task a_prop_with_hasconversion_must_match_that_configuration()
165166
var act = () => appliedQueryable.ToListAsync();
166167

167168
// Assert
168-
await act.Should().ThrowAsync<InvalidOperationException>();
169+
await act.Should().ThrowAsync<ParsingException>();
169170
}
170171

171172
[Fact]
@@ -242,4 +243,4 @@ public async Task can_sort_items_with_null_value_property()
242243
people[2].Id.Should().Be(fakePersonFour.Id);
243244
people[3].Id.Should().Be(fakePersonThree.Id);
244245
}
245-
}
246+
}

QueryKit.UnitTests/FilterParserTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,5 +808,14 @@ public void primitive_collection_does_not_have_case_insensitive()
808808
filterExpression.ToString().Should()
809809
.Be(""""x => x.Tags.Any(z => (z.ToLower() != "winner".ToLower()))"""");
810810
}
811+
812+
[Fact]
813+
public void can_throw_exception_when_invalid_enum_value()
814+
{
815+
var input = $"""BirthMonth == invalid""";
816+
var act = () => FilterParser.ParseFilter<TestingPerson>(input);
817+
act.Should().Throw<ParsingException>()
818+
.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.");
819+
}
811820
}
812821

QueryKit/Exceptions/FilterParsingException.cs renamed to QueryKit/Exceptions/ParsingException.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace QueryKit.Exceptions;
22

33
public sealed class ParsingException : Exception
44
{
5-
public ParsingException(Exception exception)
5+
public ParsingException(Exception exception)
66
: base($"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.", exception)
77
{
88
}

QueryKit/Exceptions/QueryKitDbContextTypeException.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace QueryKit.Exceptions;
22

3-
public sealed class QueryKitDbContextTypeException : Exception
3+
public sealed class QueryKitDbContextTypeException : QueryKitException
44
{
5-
public QueryKitDbContextTypeException(string specificMessage)
5+
public QueryKitDbContextTypeException(string specificMessage)
66
: base($"There no DbContext type provided in your QueryKit config, but one was needed for this operation. {specificMessage}")
77
{
88
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace QueryKit.Exceptions;
2+
3+
/// <summary>
4+
/// Base QueryKit exception, all exceptions thrown by QueryKit inherit this.
5+
/// </summary>
6+
public class QueryKitException : Exception
7+
{
8+
public QueryKitException(string message, Exception exception) : base(message, exception) {}
9+
public QueryKitException(string message) : base(message) {}
10+
}

QueryKit/Exceptions/QueryKitParsingException.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace QueryKit.Exceptions;
22

3-
public sealed class QueryKitParsingException : Exception
3+
public sealed class QueryKitParsingException : QueryKitException
44
{
5-
public QueryKitParsingException(string message)
5+
public QueryKitParsingException(string message)
66
: base(message)
77
{
88
}

QueryKit/Exceptions/SortParsingException.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace QueryKit.Exceptions;
22

3-
public sealed class SortParsingException : Exception
3+
public sealed class SortParsingException : QueryKitException
44
{
5-
public SortParsingException(string propertyName)
5+
public SortParsingException(string propertyName)
66
: base($"Parsing failed during sorting. '{propertyName}' was not recognized.")
77
{
88
}

QueryKit/Exceptions/SoundsLikeNotImplementedException.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace QueryKit.Exceptions;
22

3-
public sealed class SoundsLikeNotImplementedException : Exception
3+
public sealed class SoundsLikeNotImplementedException : QueryKitException
44
{
5-
public SoundsLikeNotImplementedException(string dbContextType)
5+
public SoundsLikeNotImplementedException(string dbContextType)
66
: base($"The DbContext type {dbContextType} does not have a SoundsLike method.")
77
{
88
}

QueryKit/Exceptions/UnknownFilterPropertyException.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace QueryKit.Exceptions;
22

3-
public sealed class UnknownFilterPropertyException : Exception
3+
public sealed class UnknownFilterPropertyException : QueryKitException
44
{
5-
public UnknownFilterPropertyException(string propertyName)
5+
public UnknownFilterPropertyException(string propertyName)
66
: base($"The filter property '{propertyName}' was not recognized.")
77
{
88
}

QueryKit/FilterParser.cs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public static Expression<Func<T, bool>> ParseFilter<T>(string input, IQueryKitCo
3131
{
3232
expr = ExprParser<T>(parameter, config).End().Parse(input);
3333
}
34+
catch (InvalidOperationException e) {
35+
throw new ParsingException(e);
36+
}
3437
catch (ParseException e)
3538
{
3639
throw new ParsingException(e);
@@ -314,29 +317,12 @@ private static Expression CreateRightExprFromType(Type leftExprType, string righ
314317
return Expression.Constant(null, rawType);
315318
}
316319

317-
if (right.StartsWith("[") && right.EndsWith("]"))
320+
var parsed = Enum.TryParse(enumType, right, out var enumValue);
321+
if (!parsed)
318322
{
319-
var values = right.Trim('[', ']').Split(',').Select(x => x.Trim()).ToList();
320-
var elementType = targetType.IsArray ? targetType.GetElementType() : targetType;
321-
322-
var expressions = values.Select<string, Expression>(x =>
323-
{
324-
if (elementType == typeof(string) && x.StartsWith("\"") && x.EndsWith("\""))
325-
{
326-
x = x.Trim('"');
327-
}
328-
329-
var enumValue = Enum.Parse(enumType, x);
330-
var constant = Expression.Constant(enumValue, enumType);
331-
332-
return constant;
333-
}).ToArray();
334-
335-
var newArrayExpression = Expression.NewArrayInit(enumType, expressions);
336-
return newArrayExpression;
323+
throw new InvalidOperationException($"Unsupported value '{right}' for type '{targetType.Name}'");
337324
}
338325

339-
var enumValue = Enum.Parse(enumType, right);
340326
var constant = Expression.Constant(enumValue, enumType);
341327

342328
if (rawType == enumType) return constant;

0 commit comments

Comments
 (0)