@@ -119,10 +119,92 @@ public async Task Can_Filter_TodoItems()
119
119
Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
120
120
Assert . NotEmpty ( deserializedBody ) ;
121
121
122
- foreach ( var todoItemResult in deserializedBody )
122
+ foreach ( var todoItemResult in deserializedBody )
123
123
Assert . Equal ( todoItem . Ordinal , todoItemResult . Ordinal ) ;
124
124
}
125
125
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
+
126
208
[ Fact ]
127
209
public async Task Can_Get_TodoItem_ById ( )
128
210
{
@@ -206,7 +288,8 @@ public async Task Can_Post_TodoItem()
206
288
{
207
289
owner = new
208
290
{
209
- data = new {
291
+ data = new
292
+ {
210
293
type = "people" ,
211
294
id = person . Id . ToString ( )
212
295
}
0 commit comments