Skip to content

Commit 82aca77

Browse files
committed
test(sorting): resources can be sorted by attrs
1 parent 2896850 commit 82aca77

File tree

1 file changed

+85
-2
lines changed

1 file changed

+85
-2
lines changed

test/JsonApiDotNetCoreExampleTests/Acceptance/TodoItemsControllerTests.cs

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,92 @@ public async Task Can_Filter_TodoItems()
119119
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
120120
Assert.NotEmpty(deserializedBody);
121121

122-
foreach(var todoItemResult in deserializedBody)
122+
foreach (var todoItemResult in deserializedBody)
123123
Assert.Equal(todoItem.Ordinal, todoItemResult.Ordinal);
124124
}
125125

126+
[Fact]
127+
public async Task Can_Sort_TodoItems_By_Ordinal_Ascending()
128+
{
129+
// Arrange
130+
_context.TodoItems.RemoveRange(_context.TodoItems);
131+
132+
const int numberOfItems = 5;
133+
var person = new Person();
134+
135+
for (var i = 1; i < numberOfItems; i++)
136+
{
137+
var todoItem = _todoItemFaker.Generate();
138+
todoItem.Ordinal = i;
139+
todoItem.Owner = person;
140+
_context.TodoItems.Add(todoItem);
141+
}
142+
_context.SaveChanges();
143+
144+
var httpMethod = new HttpMethod("GET");
145+
var route = $"/api/v1/todo-items?sort=ordinal";
146+
147+
var description = new RequestProperties("Sort TodoItems Ascending", new Dictionary<string, string> {
148+
{ "?sort=attr", "Sort on attribute" }
149+
});
150+
151+
// Act
152+
var response = await _fixture.MakeRequest<TodoItem>(description, httpMethod, route);
153+
var body = await response.Content.ReadAsStringAsync();
154+
var deserializedBody = JsonApiDeSerializer.DeserializeList<TodoItem>(body, _jsonApiContext);
155+
156+
// Assert
157+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
158+
Assert.NotEmpty(deserializedBody);
159+
160+
var priorOrdinal = 0;
161+
foreach (var todoItemResult in deserializedBody)
162+
{
163+
Assert.True(todoItemResult.Ordinal > priorOrdinal);
164+
}
165+
}
166+
167+
[Fact]
168+
public async Task Can_Sort_TodoItems_By_Ordinal_Descending()
169+
{
170+
// Arrange
171+
_context.TodoItems.RemoveRange(_context.TodoItems);
172+
173+
const int numberOfItems = 5;
174+
var person = new Person();
175+
176+
for (var i = 1; i < numberOfItems; i++)
177+
{
178+
var todoItem = _todoItemFaker.Generate();
179+
todoItem.Ordinal = i;
180+
todoItem.Owner = person;
181+
_context.TodoItems.Add(todoItem);
182+
}
183+
_context.SaveChanges();
184+
185+
var httpMethod = new HttpMethod("GET");
186+
var route = $"/api/v1/todo-items?sort=-ordinal";
187+
188+
var description = new RequestProperties("Sort TodoItems Descending", new Dictionary<string, string> {
189+
{ "?sort=-attr", "Sort on attribute" }
190+
});
191+
192+
// Act
193+
var response = await _fixture.MakeRequest<TodoItem>(description, httpMethod, route);
194+
var body = await response.Content.ReadAsStringAsync();
195+
var deserializedBody = JsonApiDeSerializer.DeserializeList<TodoItem>(body, _jsonApiContext);
196+
197+
// Assert
198+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
199+
Assert.NotEmpty(deserializedBody);
200+
201+
var priorOrdinal = numberOfItems + 1;
202+
foreach (var todoItemResult in deserializedBody)
203+
{
204+
Assert.True(todoItemResult.Ordinal < priorOrdinal);
205+
}
206+
}
207+
126208
[Fact]
127209
public async Task Can_Get_TodoItem_ById()
128210
{
@@ -206,7 +288,8 @@ public async Task Can_Post_TodoItem()
206288
{
207289
owner = new
208290
{
209-
data = new {
291+
data = new
292+
{
210293
type = "people",
211294
id = person.Id.ToString()
212295
}

0 commit comments

Comments
 (0)