blob: f2d28b4f964eb6c16fed6c9c5953d5078c51f636 [file] [log] [blame]
Scott Baker2b0ad652019-08-21 14:57:07 -07001/*
2 * Copyright 2019-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Zack Williamse940c7a2019-08-21 14:25:39 -070017package order
18
19import (
Scott Baker9a2d9a42020-06-09 18:11:26 -070020 "github.com/stretchr/testify/assert"
Zack Williamse940c7a2019-08-21 14:25:39 -070021 "testing"
22)
23
Scott Baker9a2d9a42020-06-09 18:11:26 -070024type SortIncludedStruct struct {
25 Seven string
26}
27
Zack Williamse940c7a2019-08-21 14:25:39 -070028type SortTestStruct struct {
29 Id int
30 One string
31 Two string
32 Three uint
33 Four int
Scott Baker9a2d9a42020-06-09 18:11:26 -070034 Six SortIncludedStruct
Scott Bakerd466f402020-06-12 15:40:42 -070035 Eight *SortIncludedStruct
Zack Williamse940c7a2019-08-21 14:25:39 -070036}
37
38var testSetOne = []SortTestStruct{
39 {
40 Id: 0,
41 One: "a",
42 Two: "x",
43 Three: 10,
44 Four: 1,
Scott Baker9a2d9a42020-06-09 18:11:26 -070045 Six: SortIncludedStruct{Seven: "o"},
Scott Bakerd466f402020-06-12 15:40:42 -070046 Eight: &SortIncludedStruct{Seven: "o"},
Zack Williamse940c7a2019-08-21 14:25:39 -070047 },
48 {
49 Id: 1,
50 One: "a",
51 Two: "c",
52 Three: 1,
53 Four: 10,
Scott Baker9a2d9a42020-06-09 18:11:26 -070054 Six: SortIncludedStruct{Seven: "p"},
Scott Bakerd466f402020-06-12 15:40:42 -070055 Eight: &SortIncludedStruct{Seven: "p"},
Zack Williamse940c7a2019-08-21 14:25:39 -070056 },
57 {
58 Id: 2,
59 One: "a",
60 Two: "b",
61 Three: 2,
62 Four: 1000,
Scott Baker9a2d9a42020-06-09 18:11:26 -070063 Six: SortIncludedStruct{Seven: "q"},
Scott Bakerd466f402020-06-12 15:40:42 -070064 Eight: &SortIncludedStruct{Seven: "q"},
Zack Williamse940c7a2019-08-21 14:25:39 -070065 },
66 {
67 Id: 3,
68 One: "a",
69 Two: "a",
70 Three: 3,
71 Four: 100,
Scott Baker9a2d9a42020-06-09 18:11:26 -070072 Six: SortIncludedStruct{Seven: "r"},
Scott Bakerd466f402020-06-12 15:40:42 -070073 Eight: &SortIncludedStruct{Seven: "r"},
Zack Williamse940c7a2019-08-21 14:25:39 -070074 },
75 {
76 Id: 4,
77 One: "b",
78 Two: "a",
79 Three: 3,
80 Four: 0,
Scott Baker9a2d9a42020-06-09 18:11:26 -070081 Six: SortIncludedStruct{Seven: "s"},
Scott Bakerd466f402020-06-12 15:40:42 -070082 Eight: &SortIncludedStruct{Seven: "s"},
Zack Williamse940c7a2019-08-21 14:25:39 -070083 },
84}
85
86var testSetTwo = []SortTestStruct{
87 {
88 Id: 0,
89 One: "a",
90 Two: "x",
91 Three: 10,
92 Four: 10,
93 },
94 {
95 Id: 1,
96 One: "a",
97 Two: "y",
98 Three: 1,
99 Four: 1,
100 },
101}
102
103func Verify(v []SortTestStruct, order []int) bool {
104 for i, item := range v {
105 if item.Id != order[i] {
106 return false
107 }
108 }
109 return true
110}
111
112func TestSort(t *testing.T) {
113 s, err := Parse("+One,-Two")
114 if err != nil {
115 t.Errorf("Unable to parse sort specification")
116 }
117 o, err := s.Process(testSetOne)
118 if err != nil {
119 t.Errorf("Sort failed: %s", err.Error())
120 }
121
122 if !Verify(o.([]SortTestStruct), []int{0, 1, 2, 3, 4}) {
123 t.Errorf("incorrect sort")
124 }
125}
126
127func TestSortASC(t *testing.T) {
128 s, err := Parse("+One,Two")
129 if err != nil {
130 t.Errorf("Unable to parse sort specification")
131 }
132 o, err := s.Process(testSetTwo)
133 if err != nil {
134 t.Errorf("Sort failed: %s", err.Error())
135 }
136
137 if !Verify(o.([]SortTestStruct), []int{0, 1}) {
138 t.Errorf("incorrect sort")
139 }
140}
141
142func TestSortUintASC(t *testing.T) {
143 s, err := Parse("Three,One")
144 if err != nil {
145 t.Errorf("Unable to parse sort specification")
146 }
147 o, err := s.Process(testSetOne)
148 if err != nil {
149 t.Errorf("Sort failed: %s", err.Error())
150 }
151
152 if !Verify(o.([]SortTestStruct), []int{1, 2, 3, 4, 0}) {
153 t.Errorf("incorrect sort")
154 }
155}
156
157func TestSortUintDSC(t *testing.T) {
158 s, err := Parse("-Three,One")
159 if err != nil {
160 t.Errorf("Unable to parse sort specification")
161 }
162 o, err := s.Process(testSetOne)
163 if err != nil {
164 t.Errorf("Sort failed: %s", err.Error())
165 }
166
167 if !Verify(o.([]SortTestStruct), []int{0, 3, 4, 2, 1}) {
168 t.Errorf("incorrect sort")
169 }
170}
171
172func TestSortUintDSC2(t *testing.T) {
173 s, err := Parse("-Three,One")
174 if err != nil {
175 t.Errorf("Unable to parse sort specification")
176 }
177 o, err := s.Process(testSetTwo)
178 if err != nil {
179 t.Errorf("Sort failed: %s", err.Error())
180 }
181
182 if !Verify(o.([]SortTestStruct), []int{0, 1}) {
183 t.Errorf("incorrect sort")
184 }
185}
186
187func TestSortIntASC(t *testing.T) {
188 s, err := Parse("Four,One")
189 if err != nil {
190 t.Errorf("Unable to parse sort specification")
191 }
192 o, err := s.Process(testSetOne)
193 if err != nil {
194 t.Errorf("Sort failed: %s", err.Error())
195 }
196 if !Verify(o.([]SortTestStruct), []int{4, 0, 1, 3, 2}) {
197 t.Errorf("incorrect sort")
198 }
199}
200
201func TestSortIntDSC(t *testing.T) {
202 s, err := Parse("-Four,One")
203 if err != nil {
204 t.Errorf("Unable to parse sort specification")
205 }
206 o, err := s.Process(testSetOne)
207 if err != nil {
208 t.Errorf("Sort failed: %s", err.Error())
209 }
210 if !Verify(o.([]SortTestStruct), []int{2, 3, 1, 0, 4}) {
211 t.Errorf("incorrect sort")
212 }
213}
214
215func TestSortIntDSC2(t *testing.T) {
216 s, err := Parse("-Four,One")
217 if err != nil {
218 t.Errorf("Unable to parse sort specification")
219 }
220 o, err := s.Process(testSetTwo)
221 if err != nil {
222 t.Errorf("Sort failed: %s", err.Error())
223 }
224 if !Verify(o.([]SortTestStruct), []int{0, 1}) {
225 t.Errorf("incorrect sort")
226 }
227}
228
229func TestOperString(t *testing.T) {
230 if ASC.String() != "ASC" {
231 t.Errorf("ASC to string failed")
232 }
233 if DSC.String() != "DSC" {
234 t.Errorf("DSC to string failed")
235 }
236 var o Operation = 5 // Invalid
237 if o.String() != "ASC" {
238 t.Errorf("to string default failed")
239 }
240}
241
242func TestSortSingle(t *testing.T) {
243 s, err := Parse("-Four,One")
244 if err != nil {
245 t.Errorf("Unable to parse sort specification")
246 }
247 o, err := s.Process(testSetOne[0])
248 if err != nil {
249 t.Errorf("Sort failed: %s", err.Error())
250 }
251
252 if o == nil {
253 t.Errorf("expected value, got nil")
254 }
255
256 r, ok := o.(SortTestStruct)
257 if !ok {
258 t.Errorf("Unexpected result type")
259 }
260
261 if r.Id != testSetOne[0].Id {
262 t.Errorf("results don't match input")
263 }
264}
Scott Baker9a2d9a42020-06-09 18:11:26 -0700265
266func TestSortDotted(t *testing.T) {
267 s, err := Parse("+Six.Seven")
268 if err != nil {
269 t.Errorf("Unable to parse sort specification")
270 }
271 o, err := s.Process(testSetOne)
272 if err != nil {
273 t.Errorf("Sort failed: %s", err.Error())
274 }
275
276 if !Verify(o.([]SortTestStruct), []int{0, 1, 2, 3, 4}) {
277 t.Errorf("incorrect sort")
278 }
279}
280
Scott Bakerd466f402020-06-12 15:40:42 -0700281func TestSortDottedPointer(t *testing.T) {
282 s, err := Parse("+Eight.Seven")
283 if err != nil {
284 t.Errorf("Unable to parse sort specification")
285 }
286 o, err := s.Process(testSetOne)
287 if err != nil {
288 t.Errorf("Sort failed: %s", err.Error())
289 }
290
291 if !Verify(o.([]SortTestStruct), []int{0, 1, 2, 3, 4}) {
292 t.Errorf("incorrect sort")
293 }
294}
295
296func TestInvalidDotted(t *testing.T) {
Scott Baker9a2d9a42020-06-09 18:11:26 -0700297 s, err := Parse("+Six.Nonexistent")
298 if err != nil {
299 t.Errorf("Unable to parse sort specification")
300 }
301 o, err := s.Process(testSetOne)
302 assert.EqualError(t, err, "Failed to find field Nonexistent while sorting")
303 if o != nil {
304 t.Errorf("expected no results, got some")
305 }
306}
307
308func TestDotOnString(t *testing.T) {
309 s, err := Parse("+One.IsNotAStruct")
310 if err != nil {
311 t.Errorf("Unable to parse sort specification")
312 }
313 o, err := s.Process(testSetOne)
314 assert.EqualError(t, err, "Dotted field name specified in filter did not resolve to a valid field")
315 if o != nil {
316 t.Errorf("expected no results, got some")
317 }
318}
319
Scott Bakerd466f402020-06-12 15:40:42 -0700320func TestSortOnStuct(t *testing.T) {
321 s, err := Parse("+Six")
322 if err != nil {
323 t.Errorf("Unable to parse sort specification")
324 }
325 o, err := s.Process(testSetOne)
326 assert.EqualError(t, err, "Cannot sort on a field that is a struct")
327 if o != nil {
328 t.Errorf("expected no results, got some")
329 }
330}
331
332func TestSortOnPointerStuct(t *testing.T) {
333 s, err := Parse("+Eight")
334 if err != nil {
335 t.Errorf("Unable to parse sort specification")
336 }
337 o, err := s.Process(testSetOne)
338 assert.EqualError(t, err, "Cannot sort on a field that is a struct")
339 if o != nil {
340 t.Errorf("expected no results, got some")
341 }
342}
343
Scott Baker9a2d9a42020-06-09 18:11:26 -0700344func TestTrailingDot(t *testing.T) {
345 s, err := Parse("+Six.Seven.")
346 if err != nil {
347 t.Errorf("Unable to parse sort specification")
348 }
349 o, err := s.Process(testSetOne)
350 assert.EqualError(t, err, "Dotted field name specified in filter did not resolve to a valid field")
351 if o != nil {
352 t.Errorf("expected no results, got some")
353 }
354}