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