|
| 1 | +using System; |
1 | 2 | using JsonApiDotNetCoreExample;
|
2 | 3 | using System.Net;
|
3 | 4 | using System.Net.Http;
|
4 | 5 | using System.Threading.Tasks;
|
| 6 | +using JsonApiDotNetCore.Models; |
5 | 7 | using JsonApiDotNetCore.Models.JsonApiDocuments;
|
| 8 | +using JsonApiDotNetCoreExample.Models; |
6 | 9 | using Newtonsoft.Json;
|
7 | 10 | using Xunit;
|
8 | 11 |
|
@@ -40,5 +43,66 @@ public async Task Cannot_Sort_If_Explicitly_Forbidden()
|
40 | 43 | Assert.Equal("Sorting on attribute 'achievedDate' is not allowed.", errorDocument.Errors[0].Detail);
|
41 | 44 | Assert.Equal("sort", errorDocument.Errors[0].Source.Parameter);
|
42 | 45 | }
|
| 46 | + |
| 47 | + [Fact] |
| 48 | + public async Task Can_Sort_On_Multiple_Attributes() |
| 49 | + { |
| 50 | + // Arrange |
| 51 | + var category = Guid.NewGuid().ToString(); |
| 52 | + |
| 53 | + var persons = new[] |
| 54 | + { |
| 55 | + new Person |
| 56 | + { |
| 57 | + Category = category, |
| 58 | + FirstName = "Alice", |
| 59 | + LastName = "Smith", |
| 60 | + Age = 23 |
| 61 | + }, |
| 62 | + new Person |
| 63 | + { |
| 64 | + Category = category, |
| 65 | + FirstName = "John", |
| 66 | + LastName = "Doe", |
| 67 | + Age = 49 |
| 68 | + }, |
| 69 | + new Person |
| 70 | + { |
| 71 | + Category = category, |
| 72 | + FirstName = "John", |
| 73 | + LastName = "Doe", |
| 74 | + Age = 31 |
| 75 | + }, |
| 76 | + new Person |
| 77 | + { |
| 78 | + Category = category, |
| 79 | + FirstName = "Jane", |
| 80 | + LastName = "Doe", |
| 81 | + Age = 19 |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + _fixture.Context.People.AddRange(persons); |
| 86 | + _fixture.Context.SaveChanges(); |
| 87 | + |
| 88 | + var httpMethod = new HttpMethod("GET"); |
| 89 | + var route = "/api/v1/people?filter[category]=" + category + "&sort=lastName,-firstName,the-Age"; |
| 90 | + var request = new HttpRequestMessage(httpMethod, route); |
| 91 | + |
| 92 | + // Act |
| 93 | + var response = await _fixture.Client.SendAsync(request); |
| 94 | + |
| 95 | + // Assert |
| 96 | + var body = await response.Content.ReadAsStringAsync(); |
| 97 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 98 | + |
| 99 | + var document = JsonConvert.DeserializeObject<Document>(body); |
| 100 | + Assert.Equal(4, document.ManyData.Count); |
| 101 | + |
| 102 | + Assert.Equal(document.ManyData[0].Id, persons[2].StringId); |
| 103 | + Assert.Equal(document.ManyData[1].Id, persons[1].StringId); |
| 104 | + Assert.Equal(document.ManyData[2].Id, persons[3].StringId); |
| 105 | + Assert.Equal(document.ManyData[3].Id, persons[0].StringId); |
| 106 | + } |
43 | 107 | }
|
44 | 108 | }
|
0 commit comments