blob: 28132d9a3a4717dfdec1a5a8141837b510d85843 [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
Zack Williamse940c7a2019-08-21 14:25:39 -070035}
36
37var testSetOne = []SortTestStruct{
38 {
39 Id: 0,
40 One: "a",
41 Two: "x",
42 Three: 10,
43 Four: 1,
Scott Baker9a2d9a42020-06-09 18:11:26 -070044 Six: SortIncludedStruct{Seven: "o"},
Zack Williamse940c7a2019-08-21 14:25:39 -070045 },
46 {
47 Id: 1,
48 One: "a",
49 Two: "c",
50 Three: 1,
51 Four: 10,
Scott Baker9a2d9a42020-06-09 18:11:26 -070052 Six: SortIncludedStruct{Seven: "p"},
Zack Williamse940c7a2019-08-21 14:25:39 -070053 },
54 {
55 Id: 2,
56 One: "a",
57 Two: "b",
58 Three: 2,
59 Four: 1000,
Scott Baker9a2d9a42020-06-09 18:11:26 -070060 Six: SortIncludedStruct{Seven: "q"},
Zack Williamse940c7a2019-08-21 14:25:39 -070061 },
62 {
63 Id: 3,
64 One: "a",
65 Two: "a",
66 Three: 3,
67 Four: 100,
Scott Baker9a2d9a42020-06-09 18:11:26 -070068 Six: SortIncludedStruct{Seven: "r"},
Zack Williamse940c7a2019-08-21 14:25:39 -070069 },
70 {
71 Id: 4,
72 One: "b",
73 Two: "a",
74 Three: 3,
75 Four: 0,
Scott Baker9a2d9a42020-06-09 18:11:26 -070076 Six: SortIncludedStruct{Seven: "s"},
Zack Williamse940c7a2019-08-21 14:25:39 -070077 },
78}
79
80var testSetTwo = []SortTestStruct{
81 {
82 Id: 0,
83 One: "a",
84 Two: "x",
85 Three: 10,
86 Four: 10,
87 },
88 {
89 Id: 1,
90 One: "a",
91 Two: "y",
92 Three: 1,
93 Four: 1,
94 },
95}
96
97func Verify(v []SortTestStruct, order []int) bool {
98 for i, item := range v {
99 if item.Id != order[i] {
100 return false
101 }
102 }
103 return true
104}
105
106func TestSort(t *testing.T) {
107 s, err := Parse("+One,-Two")
108 if err != nil {
109 t.Errorf("Unable to parse sort specification")
110 }
111 o, err := s.Process(testSetOne)
112 if err != nil {
113 t.Errorf("Sort failed: %s", err.Error())
114 }
115
116 if !Verify(o.([]SortTestStruct), []int{0, 1, 2, 3, 4}) {
117 t.Errorf("incorrect sort")
118 }
119}
120
121func TestSortASC(t *testing.T) {
122 s, err := Parse("+One,Two")
123 if err != nil {
124 t.Errorf("Unable to parse sort specification")
125 }
126 o, err := s.Process(testSetTwo)
127 if err != nil {
128 t.Errorf("Sort failed: %s", err.Error())
129 }
130
131 if !Verify(o.([]SortTestStruct), []int{0, 1}) {
132 t.Errorf("incorrect sort")
133 }
134}
135
136func TestSortUintASC(t *testing.T) {
137 s, err := Parse("Three,One")
138 if err != nil {
139 t.Errorf("Unable to parse sort specification")
140 }
141 o, err := s.Process(testSetOne)
142 if err != nil {
143 t.Errorf("Sort failed: %s", err.Error())
144 }
145
146 if !Verify(o.([]SortTestStruct), []int{1, 2, 3, 4, 0}) {
147 t.Errorf("incorrect sort")
148 }
149}
150
151func TestSortUintDSC(t *testing.T) {
152 s, err := Parse("-Three,One")
153 if err != nil {
154 t.Errorf("Unable to parse sort specification")
155 }
156 o, err := s.Process(testSetOne)
157 if err != nil {
158 t.Errorf("Sort failed: %s", err.Error())
159 }
160
161 if !Verify(o.([]SortTestStruct), []int{0, 3, 4, 2, 1}) {
162 t.Errorf("incorrect sort")
163 }
164}
165
166func TestSortUintDSC2(t *testing.T) {
167 s, err := Parse("-Three,One")
168 if err != nil {
169 t.Errorf("Unable to parse sort specification")
170 }
171 o, err := s.Process(testSetTwo)
172 if err != nil {
173 t.Errorf("Sort failed: %s", err.Error())
174 }
175
176 if !Verify(o.([]SortTestStruct), []int{0, 1}) {
177 t.Errorf("incorrect sort")
178 }
179}
180
181func TestSortIntASC(t *testing.T) {
182 s, err := Parse("Four,One")
183 if err != nil {
184 t.Errorf("Unable to parse sort specification")
185 }
186 o, err := s.Process(testSetOne)
187 if err != nil {
188 t.Errorf("Sort failed: %s", err.Error())
189 }
190 if !Verify(o.([]SortTestStruct), []int{4, 0, 1, 3, 2}) {
191 t.Errorf("incorrect sort")
192 }
193}
194
195func TestSortIntDSC(t *testing.T) {
196 s, err := Parse("-Four,One")
197 if err != nil {
198 t.Errorf("Unable to parse sort specification")
199 }
200 o, err := s.Process(testSetOne)
201 if err != nil {
202 t.Errorf("Sort failed: %s", err.Error())
203 }
204 if !Verify(o.([]SortTestStruct), []int{2, 3, 1, 0, 4}) {
205 t.Errorf("incorrect sort")
206 }
207}
208
209func TestSortIntDSC2(t *testing.T) {
210 s, err := Parse("-Four,One")
211 if err != nil {
212 t.Errorf("Unable to parse sort specification")
213 }
214 o, err := s.Process(testSetTwo)
215 if err != nil {
216 t.Errorf("Sort failed: %s", err.Error())
217 }
218 if !Verify(o.([]SortTestStruct), []int{0, 1}) {
219 t.Errorf("incorrect sort")
220 }
221}
222
223func TestOperString(t *testing.T) {
224 if ASC.String() != "ASC" {
225 t.Errorf("ASC to string failed")
226 }
227 if DSC.String() != "DSC" {
228 t.Errorf("DSC to string failed")
229 }
230 var o Operation = 5 // Invalid
231 if o.String() != "ASC" {
232 t.Errorf("to string default failed")
233 }
234}
235
236func TestSortSingle(t *testing.T) {
237 s, err := Parse("-Four,One")
238 if err != nil {
239 t.Errorf("Unable to parse sort specification")
240 }
241 o, err := s.Process(testSetOne[0])
242 if err != nil {
243 t.Errorf("Sort failed: %s", err.Error())
244 }
245
246 if o == nil {
247 t.Errorf("expected value, got nil")
248 }
249
250 r, ok := o.(SortTestStruct)
251 if !ok {
252 t.Errorf("Unexpected result type")
253 }
254
255 if r.Id != testSetOne[0].Id {
256 t.Errorf("results don't match input")
257 }
258}
Scott Baker9a2d9a42020-06-09 18:11:26 -0700259
260func TestSortDotted(t *testing.T) {
261 s, err := Parse("+Six.Seven")
262 if err != nil {
263 t.Errorf("Unable to parse sort specification")
264 }
265 o, err := s.Process(testSetOne)
266 if err != nil {
267 t.Errorf("Sort failed: %s", err.Error())
268 }
269
270 if !Verify(o.([]SortTestStruct), []int{0, 1, 2, 3, 4}) {
271 t.Errorf("incorrect sort")
272 }
273}
274
275func TestInvaliodDotted(t *testing.T) {
276 s, err := Parse("+Six.Nonexistent")
277 if err != nil {
278 t.Errorf("Unable to parse sort specification")
279 }
280 o, err := s.Process(testSetOne)
281 assert.EqualError(t, err, "Failed to find field Nonexistent while sorting")
282 if o != nil {
283 t.Errorf("expected no results, got some")
284 }
285}
286
287func TestDotOnString(t *testing.T) {
288 s, err := Parse("+One.IsNotAStruct")
289 if err != nil {
290 t.Errorf("Unable to parse sort specification")
291 }
292 o, err := s.Process(testSetOne)
293 assert.EqualError(t, err, "Dotted field name specified in filter did not resolve to a valid field")
294 if o != nil {
295 t.Errorf("expected no results, got some")
296 }
297}
298
299func TestTrailingDot(t *testing.T) {
300 s, err := Parse("+Six.Seven.")
301 if err != nil {
302 t.Errorf("Unable to parse sort specification")
303 }
304 o, err := s.Process(testSetOne)
305 assert.EqualError(t, err, "Dotted field name specified in filter did not resolve to a valid field")
306 if o != nil {
307 t.Errorf("expected no results, got some")
308 }
309}