|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Linq; |
4 | | -using System.Net.Http; |
5 | 4 | using FluentAssertions; |
6 | 5 | using JSONAPI.Core; |
7 | 6 | using JSONAPI.Documents.Builders; |
@@ -73,111 +72,115 @@ private DefaultSortingTransformer GetTransformer() |
73 | 72 | return new DefaultSortingTransformer(registry); |
74 | 73 | } |
75 | 74 |
|
76 | | - private TFixture[] GetArray<TFixture>(string uri, IQueryable<TFixture> fixturesQuery) |
| 75 | + private TFixture[] GetArray<TFixture>(string[] sortExpressions, IQueryable<TFixture> fixturesQuery) |
77 | 76 | { |
78 | | - var request = new HttpRequestMessage(HttpMethod.Get, uri); |
79 | | - return GetTransformer().Sort(fixturesQuery, request).ToArray(); |
| 77 | + return GetTransformer().Sort(fixturesQuery, sortExpressions).ToArray(); |
80 | 78 | } |
81 | 79 |
|
82 | | - private Dummy[] GetDummyArray(string uri) |
| 80 | + private Dummy[] GetDummyArray(string[] sortExpressions) |
83 | 81 | { |
84 | | - return GetArray<Dummy>(uri, _fixturesQuery); |
| 82 | + return GetArray(sortExpressions, _fixturesQuery); |
85 | 83 | } |
86 | 84 |
|
87 | | - private Dummy2[] GetDummy2Array(string uri) |
| 85 | + private Dummy2[] GetDummy2Array(string[] sortExpressions) |
88 | 86 | { |
89 | | - return GetArray<Dummy2>(uri, _fixtures2Query); |
| 87 | + return GetArray(sortExpressions, _fixtures2Query); |
90 | 88 | } |
91 | 89 |
|
92 | | - private void RunTransformAndExpectFailure(string uri, string expectedMessage) |
| 90 | + private void RunTransformAndExpectFailure(string[] sortExpressions, string expectedMessage) |
93 | 91 | { |
94 | 92 | Action action = () => |
95 | 93 | { |
96 | | - var request = new HttpRequestMessage(HttpMethod.Get, uri); |
97 | | - |
98 | 94 | // ReSharper disable once UnusedVariable |
99 | | - var result = GetTransformer().Sort(_fixturesQuery, request).ToArray(); |
| 95 | + var result = GetTransformer().Sort(_fixturesQuery, sortExpressions).ToArray(); |
100 | 96 | }; |
101 | 97 | action.ShouldThrow<JsonApiException>().Which.Error.Detail.Should().Be(expectedMessage); |
102 | 98 | } |
103 | 99 |
|
104 | 100 | [TestMethod] |
105 | 101 | public void Sorts_by_attribute_ascending() |
106 | 102 | { |
107 | | - var array = GetDummyArray("http://api.example.com/dummies?sort=first-name"); |
| 103 | + var array = GetDummyArray(new [] { "first-name" }); |
108 | 104 | array.Should().BeInAscendingOrder(d => d.FirstName); |
109 | 105 | } |
110 | 106 |
|
111 | 107 | [TestMethod] |
112 | 108 | public void Sorts_by_attribute_descending() |
113 | 109 | { |
114 | | - var array = GetDummyArray("http://api.example.com/dummies?sort=-first-name"); |
| 110 | + var array = GetDummyArray(new [] { "-first-name" }); |
115 | 111 | array.Should().BeInDescendingOrder(d => d.FirstName); |
116 | 112 | } |
117 | 113 |
|
118 | 114 | [TestMethod] |
119 | 115 | public void Sorts_by_two_ascending_attributes() |
120 | 116 | { |
121 | | - var array = GetDummyArray("http://api.example.com/dummies?sort=last-name,first-name"); |
| 117 | + var array = GetDummyArray(new [] { "last-name", "first-name" }); |
122 | 118 | array.Should().ContainInOrder(_fixtures.OrderBy(d => d.LastName + d.FirstName)); |
123 | 119 | } |
124 | 120 |
|
125 | 121 | [TestMethod] |
126 | 122 | public void Sorts_by_two_descending_attributes() |
127 | 123 | { |
128 | | - var array = GetDummyArray("http://api.example.com/dummies?sort=-last-name,-first-name"); |
| 124 | + var array = GetDummyArray(new [] { "-last-name", "-first-name" }); |
129 | 125 | array.Should().ContainInOrder(_fixtures.OrderByDescending(d => d.LastName + d.FirstName)); |
130 | 126 | } |
131 | 127 |
|
| 128 | + [TestMethod] |
| 129 | + public void Sorts_by_id_when_expressions_are_empty() |
| 130 | + { |
| 131 | + var array = GetDummyArray(new string[] { }); |
| 132 | + array.Should().ContainInOrder(_fixtures.OrderBy(d => d.Id)); |
| 133 | + } |
| 134 | + |
132 | 135 | [TestMethod] |
133 | 136 | public void Returns_400_if_sort_argument_is_empty() |
134 | 137 | { |
135 | | - RunTransformAndExpectFailure("http://api.example.com/dummies?sort=", "One of the sort expressions is empty."); |
| 138 | + RunTransformAndExpectFailure(new[] { "" }, "One of the sort expressions is empty."); |
136 | 139 | } |
137 | 140 |
|
138 | 141 | [TestMethod] |
139 | 142 | public void Returns_400_if_sort_argument_is_whitespace() |
140 | 143 | { |
141 | | - RunTransformAndExpectFailure("http://api.example.com/dummies?sort= ", "One of the sort expressions is empty."); |
| 144 | + RunTransformAndExpectFailure(new [] { " " }, "One of the sort expressions is empty."); |
142 | 145 | } |
143 | 146 |
|
144 | 147 | [TestMethod] |
145 | 148 | public void Returns_400_if_sort_argument_is_empty_descending() |
146 | 149 | { |
147 | | - RunTransformAndExpectFailure("http://api.example.com/dummies?sort=-", "One of the sort expressions is empty."); |
| 150 | + RunTransformAndExpectFailure(new [] { "-" }, "One of the sort expressions is empty."); |
148 | 151 | } |
149 | 152 |
|
150 | 153 | [TestMethod] |
151 | 154 | public void Returns_400_if_sort_argument_is_whitespace_descending() |
152 | 155 | { |
153 | | - RunTransformAndExpectFailure("http://api.example.com/dummies?sort=- ", "One of the sort expressions is empty."); |
| 156 | + RunTransformAndExpectFailure(new[] { "- " }, "One of the sort expressions is empty."); |
154 | 157 | } |
155 | 158 |
|
156 | 159 | [TestMethod] |
157 | 160 | public void Returns_400_if_no_property_exists() |
158 | 161 | { |
159 | | - RunTransformAndExpectFailure("http://api.example.com/dummies?sort=foobar", |
| 162 | + RunTransformAndExpectFailure(new[] { "foobar" }, |
160 | 163 | "The attribute \"foobar\" does not exist on type \"dummies\"."); |
161 | 164 | } |
162 | 165 |
|
163 | 166 | [TestMethod] |
164 | 167 | public void Returns_400_if_the_same_property_is_specified_more_than_once() |
165 | 168 | { |
166 | | - RunTransformAndExpectFailure("http://api.example.com/dummies?sort=last-name,last-name", |
| 169 | + RunTransformAndExpectFailure(new[] { "last-name", "last-name" }, |
167 | 170 | "The attribute \"last-name\" was specified more than once."); |
168 | 171 | } |
169 | 172 |
|
170 | 173 | [TestMethod] |
171 | 174 | public void Can_sort_by_DateTimeOffset() |
172 | 175 | { |
173 | | - var array = GetDummyArray("http://api.example.com/dummies?sort=birth-date"); |
| 176 | + var array = GetDummyArray(new [] { "birth-date" }); |
174 | 177 | array.Should().BeInAscendingOrder(d => d.BirthDate); |
175 | 178 | } |
176 | 179 |
|
177 | 180 | [TestMethod] |
178 | 181 | public void Can_sort_by_resource_with_integer_key() |
179 | 182 | { |
180 | | - var array = GetDummy2Array("http://api.example.com/dummy2s?sort=name"); |
| 183 | + var array = GetDummy2Array(new [] { "name" }); |
181 | 184 | array.Should().BeInAscendingOrder(d => d.Name); |
182 | 185 | } |
183 | 186 | } |
|
0 commit comments