-
-
Notifications
You must be signed in to change notification settings - Fork 163
Expand file tree
/
Copy pathIncludeParseTests.cs
More file actions
122 lines (106 loc) · 6.19 KB
/
IncludeParseTests.cs
File metadata and controls
122 lines (106 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System.Net;
using FluentAssertions;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Controllers.Annotations;
using JsonApiDotNetCore.Errors;
using JsonApiDotNetCore.Queries;
using JsonApiDotNetCore.Queries.Expressions;
using JsonApiDotNetCore.Queries.Parsing;
using JsonApiDotNetCore.QueryStrings;
using JsonApiDotNetCore.Serialization.Objects;
using TestBuildingBlocks;
using Xunit;
namespace JsonApiDotNetCoreTests.UnitTests.QueryStringParameters;
public sealed class IncludeParseTests : BaseParseTests
{
private readonly IncludeQueryStringParameterReader _reader;
public IncludeParseTests()
{
var options = new JsonApiOptions();
var parser = new IncludeParser(options);
_reader = new IncludeQueryStringParameterReader(parser, Request, ResourceGraph);
}
[Theory]
[InlineData("include", true)]
[InlineData("include[some]", false)]
[InlineData("includes", false)]
public void Reader_Supports_Parameter_Name(string parameterName, bool expectCanParse)
{
// Act
bool canParse = _reader.CanRead(parameterName);
// Assert
canParse.Should().Be(expectCanParse);
}
[Theory]
[InlineData(JsonApiQueryStringParameters.Include, false)]
[InlineData(JsonApiQueryStringParameters.All, false)]
[InlineData(JsonApiQueryStringParameters.None, true)]
[InlineData(JsonApiQueryStringParameters.Filter, true)]
public void Reader_Is_Enabled(JsonApiQueryStringParameters parametersDisabled, bool expectIsEnabled)
{
// Act
bool isEnabled = _reader.IsEnabled(new DisableQueryStringAttribute(parametersDisabled));
// Assert
isEnabled.Should().Be(expectIsEnabled);
}
[Theory]
[InlineData("includes", "^ ", "Unexpected whitespace.")]
[InlineData("includes", "^,", "Relationship name expected.")]
[InlineData("includes", "posts,^", "Relationship name expected.")]
[InlineData("includes", "posts.^", "Relationship name expected.")]
[InlineData("includes", "posts^[", ", expected.")]
[InlineData("includes", "^title", "Relationship 'title' does not exist on resource type 'blogs'.")]
[InlineData("includes", "posts.comments.^publishTime,", "Relationship 'publishTime' does not exist on resource type 'comments'.")]
[InlineData("includes", "owner.person.children.^unknown", "Relationship 'unknown' does not exist on resource type 'humans' or any of its derived types.")]
[InlineData("includes", "owner.person.friends.^unknown", "Relationship 'unknown' does not exist on resource type 'humans' or any of its derived types.")]
[InlineData("includes", "owner.person.sameGenderFriends.^unknown", "Relationship 'unknown' does not exist on any of the resource types 'men', 'women'.")]
public void Reader_Read_Fails(string parameterName, string parameterValue, string errorMessage)
{
// Arrange
var parameterValueSource = new MarkedText(parameterValue, '^');
// Act
Action action = () => _reader.Read(parameterName, parameterValueSource.Text);
// Assert
InvalidQueryStringParameterException exception = action.Should().ThrowExactly<InvalidQueryStringParameterException>().And;
exception.ParameterName.Should().Be(parameterName);
exception.Errors.Should().HaveCount(1);
ErrorObject error = exception.Errors[0];
error.StatusCode.Should().Be(HttpStatusCode.BadRequest);
error.Title.Should().Be("The specified include is invalid.");
error.Detail.Should().Be($"{errorMessage} {parameterValueSource}");
error.Source.Should().NotBeNull();
error.Source.Parameter.Should().Be(parameterName);
}
[Theory]
[InlineData("includes", "", "", "")]
[InlineData("includes", "owner", "owner", "blogs:owner")]
[InlineData("includes", "posts", "posts", "blogs:posts")]
[InlineData("includes", "owner.posts", "owner.posts", "blogs:owner.webAccounts:posts")]
[InlineData("includes", "posts.author", "posts.author", "blogs:posts.blogPosts:author")]
[InlineData("includes", "posts.comments", "posts.comments", "blogs:posts.blogPosts:comments")]
[InlineData("includes", "posts,posts.comments", "posts.comments", "blogs:posts.blogPosts:comments")]
[InlineData("includes", "posts,posts.labels,posts.comments", "posts.comments,posts.labels", "blogs:posts.blogPosts:comments,blogs:posts.blogPosts:labels")]
[InlineData("includes", "owner.person.children.husband", "owner.person.children.husband",
"blogs:owner.webAccounts:person.men:children.women:husband,blogs:owner.webAccounts:person.women:children.women:husband")]
[InlineData("includes", "owner.person.wife,owner.person.husband", "owner.person.husband,owner.person.wife",
"blogs:owner.webAccounts:person.men:wife,blogs:owner.webAccounts:person.women:husband")]
[InlineData("includes", "owner.person.father.children.wife", "owner.person.father.children.wife",
"blogs:owner.webAccounts:person.men:father.men:children.men:wife,blogs:owner.webAccounts:person.women:father.men:children.men:wife")]
[InlineData("includes", "owner.person.friends", "owner.person.friends",
"blogs:owner.webAccounts:person.men:friends,blogs:owner.webAccounts:person.women:friends")]
[InlineData("includes", "owner.person.friends.friends", "owner.person.friends.friends",
"blogs:owner.webAccounts:person.men:friends.men:friends,blogs:owner.webAccounts:person.men:friends.women:friends," +
"blogs:owner.webAccounts:person.women:friends.men:friends,blogs:owner.webAccounts:person.women:friends.women:friends")]
public void Reader_Read_Succeeds(string parameterName, string parameterValue, string stringExpected, string fullStringExpected)
{
// Act
_reader.Read(parameterName, parameterValue);
IReadOnlyCollection<ExpressionInScope> constraints = _reader.GetConstraints();
// Assert
ResourceFieldChainExpression? scope = constraints.Select(expressionInScope => expressionInScope.Scope).Single();
scope.Should().BeNull();
QueryExpression value = constraints.Select(expressionInScope => expressionInScope.Expression).Single();
value.ToString().Should().Be(stringExpected);
value.ToFullString().Should().Be(fullStringExpected);
}
}