|
| 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.Sum; |
| 16 | + |
| 17 | +public sealed class SumFilterParseTests : BaseParseTests |
| 18 | +{ |
| 19 | + private readonly FilterQueryStringParameterReader _reader; |
| 20 | + |
| 21 | + public SumFilterParseTests() |
| 22 | + { |
| 23 | + var resourceFactory = new ResourceFactory(new ServiceContainer()); |
| 24 | + var scopeParser = new QueryStringParameterScopeParser(); |
| 25 | + var valueParser = new SumFilterParser(resourceFactory); |
| 26 | + |
| 27 | + _reader = new FilterQueryStringParameterReader(scopeParser, valueParser, Request, ResourceGraph, Options); |
| 28 | + } |
| 29 | + |
| 30 | + [Theory] |
| 31 | + [InlineData("filter", "equals(sum^", "( expected.")] |
| 32 | + [InlineData("filter", "equals(sum(^", "To-many relationship expected.")] |
| 33 | + [InlineData("filter", "equals(sum(^ ", "Unexpected whitespace.")] |
| 34 | + [InlineData("filter", "equals(sum(^)", "To-many relationship expected.")] |
| 35 | + [InlineData("filter", "equals(sum(^'a')", "To-many relationship expected.")] |
| 36 | + [InlineData("filter", "equals(sum(^null)", "To-many relationship expected.")] |
| 37 | + [InlineData("filter", "equals(sum(^some)", "Field 'some' does not exist on resource type 'blogs'.")] |
| 38 | + [InlineData("filter", "equals(sum(^title)", |
| 39 | + "Field chain on resource type 'blogs' failed to match the pattern: a to-many relationship. " + |
| 40 | + "To-many relationship on resource type 'blogs' expected.")] |
| 41 | + [InlineData("filter", "equals(sum(posts^))", ", expected.")] |
| 42 | + [InlineData("filter", "equals(sum(posts,^))", "Field name expected.")] |
| 43 | + [InlineData("filter", "equals(sum(posts,author^))", |
| 44 | + "Field chain on resource type 'blogPosts' failed to match the pattern: zero or more to-one relationships, followed by an attribute. " + |
| 45 | + "To-one relationship or attribute on resource type 'webAccounts' expected.")] |
| 46 | + [InlineData("filter", "equals(sum(posts,^url))", "Attribute of a numeric type expected.")] |
| 47 | + [InlineData("filter", "equals(sum(posts,^has(labels)))", "Function that returns a numeric type expected.")] |
| 48 | + public void Reader_Read_Fails(string parameterName, string parameterValue, string errorMessage) |
| 49 | + { |
| 50 | + // Arrange |
| 51 | + var parameterValueSource = new MarkedText(parameterValue, '^'); |
| 52 | + |
| 53 | + // Act |
| 54 | + Action action = () => _reader.Read(parameterName, parameterValueSource.Text); |
| 55 | + |
| 56 | + // Assert |
| 57 | + InvalidQueryStringParameterException exception = action.Should().ThrowExactly<InvalidQueryStringParameterException>().And; |
| 58 | + |
| 59 | + exception.ParameterName.Should().Be(parameterName); |
| 60 | + exception.Errors.ShouldHaveCount(1); |
| 61 | + |
| 62 | + ErrorObject error = exception.Errors[0]; |
| 63 | + error.StatusCode.Should().Be(HttpStatusCode.BadRequest); |
| 64 | + error.Title.Should().Be("The specified filter is invalid."); |
| 65 | + error.Detail.Should().Be($"{errorMessage} {parameterValueSource}"); |
| 66 | + error.Source.ShouldNotBeNull(); |
| 67 | + error.Source.Parameter.Should().Be(parameterName); |
| 68 | + } |
| 69 | + |
| 70 | + [Theory] |
| 71 | + [InlineData("filter", "has(posts,greaterThan(sum(comments,numStars),'5'))", null)] |
| 72 | + [InlineData("filter[posts]", "equals(sum(comments,numStars),'11')", "posts")] |
| 73 | + [InlineData("filter[posts]", "equals(sum(labels,count(posts)),'8')", "posts")] |
| 74 | + public void Reader_Read_Succeeds(string parameterName, string parameterValue, string scopeExpected) |
| 75 | + { |
| 76 | + // Act |
| 77 | + _reader.Read(parameterName, parameterValue); |
| 78 | + |
| 79 | + IReadOnlyCollection<ExpressionInScope> constraints = _reader.GetConstraints(); |
| 80 | + |
| 81 | + // Assert |
| 82 | + ResourceFieldChainExpression? scope = constraints.Select(expressionInScope => expressionInScope.Scope).Single(); |
| 83 | + scope?.ToString().Should().Be(scopeExpected); |
| 84 | + |
| 85 | + QueryExpression value = constraints.Select(expressionInScope => expressionInScope.Expression).Single(); |
| 86 | + value.ToString().Should().Be(parameterValue); |
| 87 | + } |
| 88 | +} |
0 commit comments