-
-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathPaginationParseTests.cs
More file actions
193 lines (169 loc) · 10.1 KB
/
PaginationParseTests.cs
File metadata and controls
193 lines (169 loc) · 10.1 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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 PaginationParseTests : BaseParseTests
{
private readonly PaginationQueryStringParameterReader _reader;
public PaginationParseTests()
{
Options.DefaultPageSize = new PageSize(25);
var parser = new PaginationParser();
_reader = new PaginationQueryStringParameterReader(parser, Request, ResourceGraph, Options);
}
[Theory]
[InlineData("page[size]", true)]
[InlineData("page[number]", true)]
[InlineData("page", false)]
[InlineData("page[", false)]
[InlineData("page[some]", 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.Page, false)]
[InlineData(JsonApiQueryStringParameters.All, false)]
[InlineData(JsonApiQueryStringParameters.None, true)]
[InlineData(JsonApiQueryStringParameters.Sort, 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("^", "Number or relationship name expected.")]
[InlineData("^.", "Number or relationship name expected.")]
[InlineData("1,^", "Number or relationship name expected.")]
[InlineData("^(", "Number or relationship name expected.")]
[InlineData("^ ", "Unexpected whitespace.")]
[InlineData("-^", "Digits expected.")]
[InlineData("^-1", "Page number cannot be negative or zero.")]
[InlineData("posts^", ": expected.")]
[InlineData("posts:^", "Number expected.")]
[InlineData("posts:^abc", "Number expected.")]
[InlineData("1^(", ", expected.")]
[InlineData("posts:-^abc", "Digits expected.")]
[InlineData("posts:^-1", "Page number cannot be negative or zero.")]
[InlineData("^unknown", "To-many relationship 'unknown' does not exist on resource type 'blogs'.")]
[InlineData("^unknown.other", "Relationship 'unknown' does not exist on resource type 'blogs'.")]
[InlineData("posts.^", "To-many relationship name expected.")]
[InlineData("posts.^unknown", "To-many relationship 'unknown' does not exist on resource type 'blogPosts'.")]
[InlineData("posts.^id", "To-many relationship 'id' does not exist on resource type 'blogPosts'.")]
[InlineData("posts.^author", "To-many relationship 'author' does not exist on resource type 'blogPosts'.")]
[InlineData("posts.author.^", "To-many relationship name expected.")]
[InlineData("posts.comments.^id", "To-many relationship 'id' does not exist on resource type 'comments'.")]
[InlineData("owner.person.^unknown", "To-many relationship 'unknown' does not exist on resource type 'humans' or any of its derived types.")]
[InlineData("owner.person.^unknown.other", "Relationship 'unknown' does not exist on resource type 'humans' or any of its derived types.")]
[InlineData("owner.person.^hasBeard", "To-many relationship 'hasBeard' does not exist on resource type 'humans' or any of its derived types.")]
[InlineData("owner.person.^wife", "To-many relationship 'wife' does not exist on resource type 'humans' or any of its derived types.")]
public void Reader_Read_Page_Number_Fails(string parameterValue, string errorMessage)
{
// Arrange
var parameterValueSource = new MarkedText(parameterValue, '^');
// Act
Action action = () => _reader.Read("page[number]", parameterValueSource.Text);
// Assert
InvalidQueryStringParameterException exception = action.Should().ThrowExactly<InvalidQueryStringParameterException>().And;
exception.ParameterName.Should().Be("page[number]");
exception.Errors.Should().HaveCount(1);
ErrorObject error = exception.Errors[0];
error.StatusCode.Should().Be(HttpStatusCode.BadRequest);
error.Title.Should().Be("The specified pagination is invalid.");
error.Detail.Should().Be($"{errorMessage} {parameterValueSource}");
error.Source.Should().NotBeNull();
error.Source.Parameter.Should().Be("page[number]");
}
[Theory]
[InlineData("^", "Number or relationship name expected.")]
[InlineData("^.", "Number or relationship name expected.")]
[InlineData("1,^", "Number or relationship name expected.")]
[InlineData("^(", "Number or relationship name expected.")]
[InlineData("^ ", "Unexpected whitespace.")]
[InlineData("-^", "Digits expected.")]
[InlineData("^-1", "Page size cannot be negative.")]
[InlineData("posts^", ": expected.")]
[InlineData("posts:^", "Number expected.")]
[InlineData("posts:^abc", "Number expected.")]
[InlineData("1^(", ", expected.")]
[InlineData("posts:-^abc", "Digits expected.")]
[InlineData("posts:^-1", "Page size cannot be negative.")]
[InlineData("^unknown", "To-many relationship 'unknown' does not exist on resource type 'blogs'.")]
[InlineData("^unknown.other", "Relationship 'unknown' does not exist on resource type 'blogs'.")]
[InlineData("posts.^", "To-many relationship name expected.")]
[InlineData("posts.^unknown", "To-many relationship 'unknown' does not exist on resource type 'blogPosts'.")]
[InlineData("posts.^id", "To-many relationship 'id' does not exist on resource type 'blogPosts'.")]
[InlineData("posts.^author", "To-many relationship 'author' does not exist on resource type 'blogPosts'.")]
[InlineData("posts.author.^", "To-many relationship name expected.")]
[InlineData("posts.comments.^id", "To-many relationship 'id' does not exist on resource type 'comments'.")]
[InlineData("owner.person.^unknown", "To-many relationship 'unknown' does not exist on resource type 'humans' or any of its derived types.")]
[InlineData("owner.person.^unknown.other", "Relationship 'unknown' does not exist on resource type 'humans' or any of its derived types.")]
[InlineData("owner.person.^hasBeard", "To-many relationship 'hasBeard' does not exist on resource type 'humans' or any of its derived types.")]
[InlineData("owner.person.^wife", "To-many relationship 'wife' does not exist on resource type 'humans' or any of its derived types.")]
public void Reader_Read_Page_Size_Fails(string parameterValue, string errorMessage)
{
// Arrange
var parameterValueSource = new MarkedText(parameterValue, '^');
// Act
Action action = () => _reader.Read("page[size]", parameterValueSource.Text);
// Assert
InvalidQueryStringParameterException exception = action.Should().ThrowExactly<InvalidQueryStringParameterException>().And;
exception.ParameterName.Should().Be("page[size]");
exception.Errors.Should().HaveCount(1);
ErrorObject error = exception.Errors[0];
error.StatusCode.Should().Be(HttpStatusCode.BadRequest);
error.Title.Should().Be("The specified pagination is invalid.");
error.Detail.Should().Be($"{errorMessage} {parameterValueSource}");
error.Source.Should().NotBeNull();
error.Source.Parameter.Should().Be("page[size]");
}
[Theory]
[InlineData(null, "5", "", "Page number: 1, size: 5")]
[InlineData("2", null, "", "Page number: 2, size: 25")]
[InlineData("2", "5", "", "Page number: 2, size: 5")]
[InlineData("posts:4", "posts:2", "|posts", "Page number: 1, size: 25|Page number: 4, size: 2")]
[InlineData("posts:4", "5", "|posts", "Page number: 1, size: 5|Page number: 4, size: 25")]
[InlineData("4", "posts:5", "|posts", "Page number: 4, size: 25|Page number: 1, size: 5")]
[InlineData("3,owner.posts:4", "20,owner.posts:10", "|owner.posts", "Page number: 3, size: 20|Page number: 4, size: 10")]
[InlineData("posts:4,3", "posts:10,20", "|posts", "Page number: 3, size: 20|Page number: 4, size: 10")]
[InlineData("posts:4,posts.comments:5,3", "posts:10,posts.comments:15,20", "|posts|posts.comments",
"Page number: 3, size: 20|Page number: 4, size: 10|Page number: 5, size: 15")]
[InlineData("owner.person.wife.children:5,2", "owner.person.husband.children:8,3", "|owner.person.husband.children|owner.person.wife.children",
"Page number: 2, size: 3|Page number: 1, size: 8|Page number: 5, size: 25")]
public void Reader_Read_Pagination_Succeeds(string? pageNumber, string? pageSize, string scopeTreesExpected, string valueTreesExpected)
{
// Act
if (pageNumber != null)
{
_reader.Read("page[number]", pageNumber);
}
if (pageSize != null)
{
_reader.Read("page[size]", pageSize);
}
IReadOnlyCollection<ExpressionInScope> constraints = _reader.GetConstraints();
// Assert
string[] scopeTreesExpectedArray = scopeTreesExpected.Split("|");
ResourceFieldChainExpression?[] scopeTrees = constraints.Select(expressionInScope => expressionInScope.Scope).ToArray();
scopeTrees.Should().HaveSameCount(scopeTreesExpectedArray);
scopeTrees.Select(tree => tree?.ToString() ?? string.Empty).Should().BeEquivalentTo(scopeTreesExpectedArray, options => options.WithStrictOrdering());
string[] valueTreesExpectedArray = valueTreesExpected.Split("|");
QueryExpression[] valueTrees = constraints.Select(expressionInScope => expressionInScope.Expression).ToArray();
valueTrees.Should().HaveSameCount(valueTreesExpectedArray);
valueTrees.Select(tree => tree.ToString()).Should().BeEquivalentTo(valueTreesExpectedArray, options => options.WithStrictOrdering());
}
}