-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_test.go
102 lines (95 loc) · 2.14 KB
/
search_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestMatch(t *testing.T) {
tables := []struct {
QueryArgs map[string]Constraint
FileArgs map[string][]string
ExpectedResult bool
}{
{
map[string]Constraint{
"tags": Constraint{[]string{"vegan", "summer"}, "or"},
},
map[string][]string{"tags": []string{"vegan", "soup"}},
true,
},
{
map[string]Constraint{
"ingredients": Constraint{[]string{"salmon"}, "or"},
},
map[string][]string{"ingredients": []string{"carrots", "salmon"}},
true,
},
{
map[string]Constraint{
"texture": Constraint{[]string{"crunchy"}, "or"},
},
map[string][]string{"texture": []string{"creamy"}},
false,
},
}
for _, table := range tables {
actualResult := Match(table.QueryArgs, table.FileArgs)
verboseDescription := fmt.Sprintf("QueryArgs: %s\nFileArgs: %s\n", table.QueryArgs, table.FileArgs)
assert.Equal(t, table.ExpectedResult, actualResult, verboseDescription)
}
}
func TestCleanFields(t *testing.T) {
tables := []struct {
Arg []string
ExpectedResult []string
}{
{
[]string{"zucchini", "eggplant", "peppers"},
[]string{"zucchini", "eggplant", "peppers"},
},
{
[]string{"minced beef ", " carrots", " potatoes "},
[]string{"minced beef", "carrots", "potatoes"},
},
{
[]string{"", " ", " ", " ", "avocadoes"},
[]string{"avocadoes"},
},
}
for _, table := range tables {
actualResult := CleanFields(table.Arg)
assert.Equal(t, table.ExpectedResult, actualResult)
}
}
func TestParseSearchQuery(t *testing.T) {
tables := []struct {
Args []string
ExpectedResult map[string]Constraint
ExpectedError error
}{
{
[]string{"tags:breakfast"},
map[string]Constraint{
"tags": Constraint{
[]string{"breakfast"},
"or",
},
},
nil,
},
{
[]string{"tags:soup,vegetarian"},
map[string]Constraint{
"tags": Constraint{
[]string{"soup", "vegetarian"},
"or",
},
},
nil,
},
}
for _, table := range tables {
actualResult, _ := ParseSearchQuery(table.Args)
assert.Equal(t, table.ExpectedResult, actualResult)
}
}