-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcex_test.go
241 lines (209 loc) · 5.89 KB
/
cex_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package cexfind
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/rorycl/cexfind/location"
"github.com/shopspring/decimal"
)
// TestBoxInQuery tests strict query/Box.Name matches
func TestBoxInQuery(t *testing.T) {
tests := []struct {
box Box
queries []string
result bool
}{
{
box: Box{Name: "ABC def hij"},
queries: []string{"xyz ntz", "hij abc"},
result: true,
},
{
box: Box{Name: "ABC def hij"},
queries: []string{"xyz ntz", "hij dbc"},
result: false,
},
{
box: Box{Name: "abc def hij"},
queries: []string{"HIJ ABC"},
result: true,
},
{
box: Box{Name: "abc def hij", Model: "lenovo"},
queries: []string{"HIJ ABC Lenovo"},
result: true,
},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("subtest %d", i), func(t *testing.T) {
if got, want := tt.box.inQuery(tt.queries), tt.result; got != want {
t.Errorf("got %t != want %t", got, want)
}
})
}
}
func TestSearch(t *testing.T) {
f, err := os.Open("testdata/example.json")
if err != nil {
t.Fatal(err)
}
contents, err := io.ReadAll(f)
if err != nil {
t.Fatal(err)
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, string(contents))
}))
defer ts.Close()
// overwrite global URL with test URL
URL = ts.URL
// non-strict search
cex := NewCexFind()
results, err := cex.Search([]string{"lenovo x390s"}, false, "")
if err != nil {
t.Fatal(err)
}
if got, want := len(results), 6; got != want {
t.Fatalf("expected %d box results, got %d", want, got)
}
// verbose output (use test -v)
for _, v := range results {
t.Log("\t", v)
}
// strict search for non-existing model
cex = NewCexFind()
_, err = cex.Search([]string{"lenovo x390st"}, true, "")
if err == nil || err.Error() != "no results" {
t.Fatalf("expected no results error, got %v", err)
}
}
// Search for terminator search string
func TestSearchTerminator(t *testing.T) {
f, err := os.Open("testdata/terminator.json")
if err != nil {
t.Fatal(err)
}
contents, err := io.ReadAll(f)
if err != nil {
t.Fatal(err)
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, string(contents))
}))
defer ts.Close()
// overwrite global URL with test URL
URL = ts.URL
// non-strict search
cex := NewCexFind()
results, err := cex.Search([]string{"terminator"}, false, "")
if err != nil {
t.Fatal(err)
}
if got, want := len(results), 17; got != want {
t.Fatalf("expected %d box results, got %d", want, got)
}
// verbose output (use test -v)
for _, v := range results {
t.Log("\t", v)
}
}
// TestBoxSort tests box sorting
func TestBoxSort(t *testing.T) {
var toSortBoxes boxes
toSortBoxes = append(toSortBoxes,
[]Box{
{Model: "bb", Name: "bb", ID: "id1a", Price: decimal.NewFromInt(20), PriceCash: decimal.NewFromInt(15), PriceExchange: decimal.NewFromInt(17)},
{Model: "bc", Name: "cc", ID: "id2a", Price: decimal.NewFromInt(25), PriceCash: decimal.NewFromInt(15), PriceExchange: decimal.NewFromInt(17)},
{Model: "ba", Name: "aa", ID: "id3a", Price: decimal.NewFromInt(15), PriceCash: decimal.NewFromInt(15), PriceExchange: decimal.NewFromInt(17)},
{Model: "ab", Name: "db", ID: "id3b", Price: decimal.NewFromInt(30), PriceCash: decimal.NewFromInt(15), PriceExchange: decimal.NewFromInt(17)},
{Model: "ac", Name: "dc", ID: "id2z", Price: decimal.NewFromInt(35), PriceCash: decimal.NewFromInt(15), PriceExchange: decimal.NewFromInt(17)},
{Model: "aa", Name: "da", ID: "id1a", Price: decimal.NewFromInt(35), PriceCash: decimal.NewFromInt(15), PriceExchange: decimal.NewFromInt(17)},
{Model: "aa", Name: "la", ID: "id1b", Price: decimal.NewFromInt(30), PriceCash: decimal.NewFromInt(15), PriceExchange: decimal.NewFromInt(17)}, // 0
}...,
)
var sortedBoxes = make(boxes, len(toSortBoxes))
copy(sortedBoxes, toSortBoxes)
sortedBoxes.sort()
// t.Logf("\n%d: %v\n", len(toSortBoxes), toSortBoxes)
// t.Logf("\n%d: %v\n", len(sortedBoxes), sortedBoxes)
// t.Log(sortedBoxes)
// compaction does not happen here
if got, want := len(sortedBoxes), len(toSortBoxes); got != want {
t.Errorf("expected compaction want %d items, got %d", want, got)
}
if diff := cmp.Diff(
toSortBoxes[6],
sortedBoxes[0],
cmpopts.IgnoreFields(Box{}, "storeNames"),
); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(
toSortBoxes[0],
sortedBoxes[5],
cmpopts.IgnoreFields(Box{}, "storeNames"),
); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
func TestBoxStoresString(t *testing.T) {
tests := []struct {
length int
boxString []string
want string
}{
{
length: 6,
boxString: []string{},
want: "",
},
{
length: 7,
boxString: []string{"a", "b", "c"},
want: "a, b, c",
},
{
length: 5,
boxString: []string{"a", "b", "c"},
want: "a, b…",
},
{
length: 14,
boxString: []string{"a", "b", "c", "d", "e", "f", "g", "h"},
want: "a, b, c, d, e…",
},
}
for i, tt := range tests {
box := Box{ID: "whatever"}
for _, bs := range tt.boxString {
box.Stores = append(box.Stores, location.StoreWithDistance{
StoreName: bs,
})
}
t.Run(fmt.Sprintf("subtest %d", i), func(t *testing.T) {
if got, want := box.StoresString(tt.length), tt.want; got != want {
t.Errorf("got %s != want %s", got, want)
}
})
}
}
// TestBoxIDUrl checks a valid url is returned
func TestBoxIDUrl(t *testing.T) {
b := Box{ID: "xyz"}
if got, want := b.IDUrl(), urlDetail+b.ID; got != want {
t.Errorf("url got %s want %s", got, want)
}
}
func TestCexInitialised(t *testing.T) {
cex := &CexFind{
storeDistances: location.NewStoreDistances(false),
}
if got, want := cex.LocationDistancesOK(), false; got != want {
t.Errorf("got %t want %t", got, want)
}
}