Skip to content
Open
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
19 changes: 19 additions & 0 deletions QueryKit.IntegrationTests/Tests/FilterParsingRegressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace QueryKit.IntegrationTests.Tests;
using System.Globalization;
using System.Linq.Expressions;
using Configuration;
using Exceptions;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Operators;
Expand Down Expand Up @@ -320,4 +321,22 @@ public async Task has_returns_matching_rows(string input, string[] expectedTitle
// Assert
recipes.Select(x => x.Title[prefix.Length..]).Should().BeEquivalentTo(expectedTitles);
}

[Theory]
[InlineData("""Age == "abc" """)]
[InlineData("""Age == abc""")]
[InlineData("""Rating > "abc" """)]
[InlineData("""Rating > abc""")]
public async Task invalid_value_throws_parsing_exception(string input)
{
// Arrange
var testingServiceScope = new TestingServiceScope();

// Act
var queryablePeople = testingServiceScope.DbContext().People;
var act = async () => await queryablePeople.ApplyQueryKitFilter(input).ToListAsync();

// Assert
await act.Should().ThrowAsync<ParsingException>();
}
}
35 changes: 35 additions & 0 deletions QueryKit.UnitTests/FilterParsingRegressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,41 @@ public void has_returns_matching_rows(string input, string[] expectedTitles)
result.Select(x => x.Title).Should().BeEquivalentTo(expectedTitles);
}

[Theory]
[InlineData("""Age == "abc" """)]
[InlineData("""Age == abc""")]
[InlineData("""Rating > "abc" """)]
[InlineData("""Rating > abc""")]
[InlineData("""Age == 99999999999""")]
[InlineData("""Id == "abc" """)]
[InlineData("""SpecificDateTime == "abc" """)]
[InlineData("""Favorite == "abc" """)]
[InlineData("""Age ^^ ["abc"]""")]
public void invalid_value_throws_parsing_exception(string input)
{
var act = () => FilterParser.ParseFilter<TestingPerson>(input);

act.Should().Throw<ParsingException>();
}

[Theory]
[InlineData("Age sideways")]
[InlineData("Title, Age up")]
public void invalid_sort_direction_throws_query_kit_parsing_exception(string input)
{
var act = () => SortParser.ParseSort<TestingPerson>(input);

act.Should().Throw<QueryKitParsingException>().WithMessage("Invalid direction: *");
}

[Fact]
public void unknown_logical_operator_throws_query_kit_parsing_exception()
{
var act = () => LogicalOperator.GetByOperatorString("xor");

act.Should().Throw<QueryKitParsingException>().WithMessage("Operator xor is not supported");
}

private static TResult WithCulture<TResult>(string cultureName, Func<TResult> action)
{
var originalCulture = CultureInfo.CurrentCulture;
Expand Down
8 changes: 8 additions & 0 deletions QueryKit/FilterParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public static Expression<Func<T, bool>> ParseFilter<T>(string input, IQueryKitCo
{
throw new ParsingException(e);
}
catch (FormatException e)
{
throw new ParsingException(e);
}
catch (OverflowException e)
{
throw new ParsingException(e);
}

return Expression.Lambda<Func<T, bool>>(expr, parameter);
}
Expand Down
3 changes: 2 additions & 1 deletion QueryKit/Operators/LogicalOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace QueryKit.Operators;
using System.Linq.Expressions;
using Ardalis.SmartEnum;
using Configuration;
using Exceptions;

public abstract class LogicalOperator : SmartEnum<LogicalOperator>
{
Expand All @@ -15,7 +16,7 @@ public static LogicalOperator GetByOperatorString(string op)
var logicalOperator = List.FirstOrDefault(x => x.Operator() == op);
if (logicalOperator == null)
{
throw new Exception($"Operator {op} is not supported");
throw new QueryKitParsingException($"Operator {op} is not supported");
}
return logicalOperator;
}
Expand Down
2 changes: 1 addition & 1 deletion QueryKit/SortParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private static SortExpressionInfo<T> CreateSortExpression<T>(string sortClause,

if (direction != Ascending && direction != Descending)
{
throw new ArgumentException($"Invalid direction: {direction}. Allowed values are '{Ascending}' and '{Descending}'.");
throw new QueryKitParsingException($"Invalid direction: {direction}. Allowed values are '{Ascending}' and '{Descending}'.");
}

var propertyPath = config?.GetPropertyPathByQueryName(propertyName) ?? propertyName;
Expand Down
Loading