Scott Baker | 2b0ad65 | 2019-08-21 14:57:07 -0700 | [diff] [blame] | 1 | /* |
| 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 Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 17 | package filter |
| 18 | |
| 19 | import ( |
| 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | type TestFilterStruct struct { |
| 24 | One string |
| 25 | Two string |
| 26 | Three string |
| 27 | } |
| 28 | |
| 29 | func TestFilterList(t *testing.T) { |
| 30 | f, err := Parse("One=a,Two=b") |
| 31 | if err != nil { |
| 32 | t.Errorf("Unable to parse filter: %s", err.Error()) |
| 33 | } |
| 34 | |
| 35 | data := []interface{}{ |
| 36 | TestFilterStruct{ |
| 37 | One: "a", |
| 38 | Two: "b", |
| 39 | Three: "c", |
| 40 | }, |
| 41 | TestFilterStruct{ |
| 42 | One: "1", |
| 43 | Two: "2", |
| 44 | Three: "3", |
| 45 | }, |
| 46 | TestFilterStruct{ |
| 47 | One: "a", |
| 48 | Two: "b", |
| 49 | Three: "z", |
| 50 | }, |
| 51 | } |
| 52 | |
| 53 | r, _ := f.Process(data) |
| 54 | |
| 55 | if _, ok := r.([]interface{}); !ok { |
| 56 | t.Errorf("Expected list, but didn't get one") |
| 57 | } |
| 58 | |
| 59 | if len(r.([]interface{})) != 2 { |
| 60 | t.Errorf("Expected %d got %d", 2, len(r.([]interface{}))) |
| 61 | } |
| 62 | |
| 63 | if r.([]interface{})[0] != data[0] { |
| 64 | t.Errorf("Filtered list did not match, item %d", 0) |
| 65 | } |
| 66 | if r.([]interface{})[1] != data[2] { |
| 67 | t.Errorf("Filtered list did not match, item %d", 1) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestFilterItem(t *testing.T) { |
| 72 | f, err := Parse("One=a,Two=b") |
| 73 | if err != nil { |
| 74 | t.Errorf("Unable to parse filter: %s", err.Error()) |
| 75 | } |
| 76 | |
| 77 | data := TestFilterStruct{ |
| 78 | One: "a", |
| 79 | Two: "b", |
| 80 | Three: "c", |
| 81 | } |
| 82 | |
| 83 | r, _ := f.Process(data) |
| 84 | |
| 85 | if r == nil { |
| 86 | t.Errorf("Expected item, got nil") |
| 87 | } |
| 88 | |
| 89 | if _, ok := r.([]interface{}); ok { |
| 90 | t.Errorf("Expected item, but got list") |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | func TestGoodFilters(t *testing.T) { |
| 95 | var f Filter |
| 96 | var err error |
| 97 | f, err = Parse("One=a,Two=b") |
| 98 | if err != nil { |
| 99 | t.Errorf("1. Unable to parse filter: %s", err.Error()) |
| 100 | } |
| 101 | if len(f) != 2 || |
| 102 | f["One"].Value != "a" || |
| 103 | f["One"].Op != EQ || |
| 104 | f["Two"].Value != "b" || |
| 105 | f["Two"].Op != EQ { |
| 106 | t.Errorf("1. Filter did not parse correctly") |
| 107 | } |
| 108 | |
| 109 | f, err = Parse("One=a") |
| 110 | if err != nil { |
| 111 | t.Errorf("2. Unable to parse filter: %s", err.Error()) |
| 112 | } |
| 113 | if len(f) != 1 || |
| 114 | f["One"].Value != "a" || |
| 115 | f["One"].Op != EQ { |
| 116 | t.Errorf("2. Filter did not parse correctly") |
| 117 | } |
| 118 | |
| 119 | f, err = Parse("One<a") |
| 120 | if err != nil { |
| 121 | t.Errorf("3. Unable to parse filter: %s", err.Error()) |
| 122 | } |
| 123 | if len(f) != 1 || |
| 124 | f["One"].Value != "a" || |
| 125 | f["One"].Op != LT { |
| 126 | t.Errorf("3. Filter did not parse correctly") |
| 127 | } |
| 128 | |
| 129 | f, err = Parse("One!=a") |
| 130 | if err != nil { |
| 131 | t.Errorf("4. Unable to parse filter: %s", err.Error()) |
| 132 | } |
| 133 | if len(f) != 1 || |
| 134 | f["One"].Value != "a" || |
| 135 | f["One"].Op != NE { |
| 136 | t.Errorf("4. Filter did not parse correctly") |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | func TestBadFilters(t *testing.T) { |
| 141 | _, err := Parse("One%a") |
| 142 | if err == nil { |
| 143 | t.Errorf("Parsed filter when it shouldn't have") |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func TestSingleRecord(t *testing.T) { |
| 148 | f, err := Parse("One=d") |
| 149 | if err != nil { |
| 150 | t.Errorf("Unable to parse filter: %s", err.Error()) |
| 151 | } |
| 152 | |
| 153 | data := TestFilterStruct{ |
| 154 | One: "a", |
| 155 | Two: "b", |
| 156 | Three: "c", |
| 157 | } |
| 158 | |
| 159 | r, err := f.Process(data) |
| 160 | if err != nil { |
| 161 | t.Errorf("Error processing data") |
| 162 | } |
| 163 | |
| 164 | if r != nil { |
| 165 | t.Errorf("expected no results, got some") |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // Invalid fields are ignored (i.e. an error is returned, but need to |
| 170 | // cover the code path in tests |
| 171 | func TestInvalidField(t *testing.T) { |
| 172 | f, err := Parse("Four=a") |
| 173 | if err != nil { |
| 174 | t.Errorf("Unable to parse filter: %s", err.Error()) |
| 175 | } |
| 176 | |
| 177 | data := TestFilterStruct{ |
| 178 | One: "a", |
| 179 | Two: "b", |
| 180 | Three: "c", |
| 181 | } |
| 182 | |
| 183 | r, err := f.Process(data) |
| 184 | if err != nil { |
| 185 | t.Errorf("Error processing data") |
| 186 | } |
| 187 | |
| 188 | if r != nil { |
| 189 | t.Errorf("expected no results, got some") |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | func TestREFilter(t *testing.T) { |
| 194 | var f Filter |
| 195 | var err error |
| 196 | f, err = Parse("One~a") |
| 197 | if err != nil { |
| 198 | t.Errorf("Unable to parse RE expression") |
| 199 | } |
| 200 | if len(f) != 1 { |
| 201 | t.Errorf("filter parsed incorrectly") |
| 202 | } |
| 203 | |
| 204 | data := []interface{}{ |
| 205 | TestFilterStruct{ |
| 206 | One: "a", |
| 207 | Two: "b", |
| 208 | Three: "c", |
| 209 | }, |
| 210 | TestFilterStruct{ |
| 211 | One: "1", |
| 212 | Two: "2", |
| 213 | Three: "3", |
| 214 | }, |
| 215 | TestFilterStruct{ |
| 216 | One: "a", |
| 217 | Two: "b", |
| 218 | Three: "z", |
| 219 | }, |
| 220 | } |
| 221 | |
David Bainbridge | 12f036f | 2019-10-15 22:09:04 +0000 | [diff] [blame] | 222 | if _, err = f.Process(data); err != nil { |
| 223 | t.Errorf("Error processing data") |
| 224 | } |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | func TestBadRE(t *testing.T) { |
| 228 | _, err := Parse("One~(qs*") |
| 229 | if err == nil { |
| 230 | t.Errorf("Expected RE parse error, got none") |
| 231 | } |
| 232 | } |