Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 903c69d | 2024-02-01 19:46:39 -0500 | [diff] [blame] | 2 | * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 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 Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 17 | package order |
| 18 | |
| 19 | import ( |
Scott Baker | 9a2d9a4 | 2020-06-09 18:11:26 -0700 | [diff] [blame] | 20 | "github.com/stretchr/testify/assert" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 21 | "testing" |
| 22 | ) |
| 23 | |
Scott Baker | 9a2d9a4 | 2020-06-09 18:11:26 -0700 | [diff] [blame] | 24 | type SortIncludedStruct struct { |
| 25 | Seven string |
| 26 | } |
| 27 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 28 | type SortTestStruct struct { |
| 29 | Id int |
| 30 | One string |
| 31 | Two string |
| 32 | Three uint |
| 33 | Four int |
Scott Baker | 9a2d9a4 | 2020-06-09 18:11:26 -0700 | [diff] [blame] | 34 | Six SortIncludedStruct |
Scott Baker | d466f40 | 2020-06-12 15:40:42 -0700 | [diff] [blame] | 35 | Eight *SortIncludedStruct |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | var testSetOne = []SortTestStruct{ |
| 39 | { |
| 40 | Id: 0, |
| 41 | One: "a", |
| 42 | Two: "x", |
| 43 | Three: 10, |
| 44 | Four: 1, |
Scott Baker | 9a2d9a4 | 2020-06-09 18:11:26 -0700 | [diff] [blame] | 45 | Six: SortIncludedStruct{Seven: "o"}, |
Scott Baker | d466f40 | 2020-06-12 15:40:42 -0700 | [diff] [blame] | 46 | Eight: &SortIncludedStruct{Seven: "o"}, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 47 | }, |
| 48 | { |
| 49 | Id: 1, |
| 50 | One: "a", |
| 51 | Two: "c", |
| 52 | Three: 1, |
| 53 | Four: 10, |
Scott Baker | 9a2d9a4 | 2020-06-09 18:11:26 -0700 | [diff] [blame] | 54 | Six: SortIncludedStruct{Seven: "p"}, |
Scott Baker | d466f40 | 2020-06-12 15:40:42 -0700 | [diff] [blame] | 55 | Eight: &SortIncludedStruct{Seven: "p"}, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 56 | }, |
| 57 | { |
| 58 | Id: 2, |
| 59 | One: "a", |
| 60 | Two: "b", |
| 61 | Three: 2, |
| 62 | Four: 1000, |
Scott Baker | 9a2d9a4 | 2020-06-09 18:11:26 -0700 | [diff] [blame] | 63 | Six: SortIncludedStruct{Seven: "q"}, |
Scott Baker | d466f40 | 2020-06-12 15:40:42 -0700 | [diff] [blame] | 64 | Eight: &SortIncludedStruct{Seven: "q"}, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 65 | }, |
| 66 | { |
| 67 | Id: 3, |
| 68 | One: "a", |
| 69 | Two: "a", |
| 70 | Three: 3, |
| 71 | Four: 100, |
Scott Baker | 9a2d9a4 | 2020-06-09 18:11:26 -0700 | [diff] [blame] | 72 | Six: SortIncludedStruct{Seven: "r"}, |
Scott Baker | d466f40 | 2020-06-12 15:40:42 -0700 | [diff] [blame] | 73 | Eight: &SortIncludedStruct{Seven: "r"}, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 74 | }, |
| 75 | { |
| 76 | Id: 4, |
| 77 | One: "b", |
| 78 | Two: "a", |
| 79 | Three: 3, |
| 80 | Four: 0, |
Scott Baker | 9a2d9a4 | 2020-06-09 18:11:26 -0700 | [diff] [blame] | 81 | Six: SortIncludedStruct{Seven: "s"}, |
Scott Baker | d466f40 | 2020-06-12 15:40:42 -0700 | [diff] [blame] | 82 | Eight: &SortIncludedStruct{Seven: "s"}, |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 83 | }, |
| 84 | } |
| 85 | |
| 86 | var 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 | |
| 103 | func 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 | |
| 112 | func 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 | |
| 127 | func 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 | |
| 142 | func 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 | |
| 157 | func 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 | |
| 172 | func 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 | |
| 187 | func 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 | |
| 201 | func 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 | |
| 215 | func 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 | |
| 229 | func 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 | |
| 242 | func 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 Baker | 9a2d9a4 | 2020-06-09 18:11:26 -0700 | [diff] [blame] | 265 | |
| 266 | func 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 Baker | d466f40 | 2020-06-12 15:40:42 -0700 | [diff] [blame] | 281 | func 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 | |
| 296 | func TestInvalidDotted(t *testing.T) { |
Scott Baker | 9a2d9a4 | 2020-06-09 18:11:26 -0700 | [diff] [blame] | 297 | 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 | |
| 308 | func 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 Baker | d466f40 | 2020-06-12 15:40:42 -0700 | [diff] [blame] | 320 | func 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 | |
| 332 | func 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 Baker | 9a2d9a4 | 2020-06-09 18:11:26 -0700 | [diff] [blame] | 344 | func 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 | } |