|
| 1 | +using System.ComponentModel.Design; |
| 2 | +using System.Net; |
| 3 | +using FluentAssertions; |
| 4 | +using JsonApiDotNetCore.Errors; |
| 5 | +using JsonApiDotNetCore.Queries; |
| 6 | +using JsonApiDotNetCore.Queries.Expressions; |
| 7 | +using JsonApiDotNetCore.Queries.Parsing; |
| 8 | +using JsonApiDotNetCore.QueryStrings; |
| 9 | +using JsonApiDotNetCore.Resources; |
| 10 | +using JsonApiDotNetCore.Serialization.Objects; |
| 11 | +using JsonApiDotNetCoreTests.UnitTests.QueryStringParameters; |
| 12 | +using TestBuildingBlocks; |
| 13 | +using Xunit; |
| 14 | + |
| 15 | +namespace JsonApiDotNetCoreTests.IntegrationTests.QueryStrings.CustomFunctions.StringLength; |
| 16 | + |
| 17 | +public sealed class LengthFilterParseTests : BaseParseTests |
| 18 | +{ |
| 19 | + private readonly FilterQueryStringParameterReader _reader; |
| 20 | + |
| 21 | + public LengthFilterParseTests() |
| 22 | + { |
| 23 | + var resourceFactory = new ResourceFactory(new ServiceContainer()); |
| 24 | + var scopeParser = new QueryStringParameterScopeParser(); |
| 25 | + var valueParser = new LengthFilterParser(resourceFactory); |
| 26 | + |
| 27 | + _reader = new FilterQueryStringParameterReader(scopeParser, valueParser, Request, ResourceGraph, Options); |
| 28 | + } |
| 29 | + |
| 30 | + [Theory] |
| 31 | + [InlineData("filter", "equals(length^", "( expected.")] |
| 32 | + [InlineData("filter", "equals(length(^", "Field name expected.")] |
| 33 | + [InlineData("filter", "equals(length(^ ", "Unexpected whitespace.")] |
| 34 | + [InlineData("filter", "equals(length(^)", "Field name expected.")] |
| 35 | + [InlineData("filter", "equals(length(^'a')", "Field name expected.")] |
| 36 | + [InlineData("filter", "equals(length(^some)", "Field 'some' does not exist on resource type 'blogs'.")] |
| 37 | + [InlineData("filter", "equals(length(^caption)", "Field 'caption' does not exist on resource type 'blogs'.")] |
| 38 | + [InlineData("filter", "equals(length(^null)", "Field name expected.")] |
| 39 | + [InlineData("filter", "equals(length(title)^)", ", expected.")] |
| 40 | + [InlineData("filter", "equals(length(owner.preferences.^useDarkTheme)", "Attribute of type 'String' expected.")] |
| 41 | + public void Reader_Read_Fails(string parameterName, string parameterValue, string errorMessage) |
| 42 | + { |
| 43 | + // Arrange |
| 44 | + var parameterValueSource = new MarkedText(parameterValue, '^'); |
| 45 | + |
| 46 | + // Act |
| 47 | + Action action = () => _reader.Read(parameterName, parameterValueSource.Text); |
| 48 | + |
| 49 | + // Assert |
| 50 | + InvalidQueryStringParameterException exception = action.Should().ThrowExactly<InvalidQueryStringParameterException>().And; |
| 51 | + |
| 52 | + exception.ParameterName.Should().Be(parameterName); |
| 53 | + exception.Errors.ShouldHaveCount(1); |
| 54 | + |
| 55 | + ErrorObject error = exception.Errors[0]; |
| 56 | + error.StatusCode.Should().Be(HttpStatusCode.BadRequest); |
| 57 | + error.Title.Should().Be("The specified filter is invalid."); |
| 58 | + error.Detail.Should().Be($"{errorMessage} {parameterValueSource}"); |
| 59 | + error.Source.ShouldNotBeNull(); |
| 60 | + error.Source.Parameter.Should().Be(parameterName); |
| 61 | + } |
| 62 | + |
| 63 | + [Theory] |
| 64 | + [InlineData("filter", "equals(length(title),'1')", null)] |
| 65 | + [InlineData("filter", "greaterThan(length(owner.userName),'1')", null)] |
| 66 | + [InlineData("filter", "has(posts,lessThan(length(author.userName),'1'))", null)] |
| 67 | + [InlineData("filter", "or(equals(length(title),'1'),equals(length(platformName),'1'))", null)] |
| 68 | + [InlineData("filter[posts]", "equals(length(author.userName),'1')", "posts")] |
| 69 | + public void Reader_Read_Succeeds(string parameterName, string parameterValue, string scopeExpected) |
| 70 | + { |
| 71 | + // Act |
| 72 | + _reader.Read(parameterName, parameterValue); |
| 73 | + |
| 74 | + IReadOnlyCollection<ExpressionInScope> constraints = _reader.GetConstraints(); |
| 75 | + |
| 76 | + // Assert |
| 77 | + ResourceFieldChainExpression? scope = constraints.Select(expressionInScope => expressionInScope.Scope).Single(); |
| 78 | + scope?.ToString().Should().Be(scopeExpected); |
| 79 | + |
| 80 | + QueryExpression value = constraints.Select(expressionInScope => expressionInScope.Expression).Single(); |
| 81 | + value.ToString().Should().Be(parameterValue); |
| 82 | + } |
| 83 | +} |
0 commit comments