forked from elastic/go-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_test.go
168 lines (143 loc) · 4.56 KB
/
store_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
// Licensed to Elasticsearch B.V. under one or more agreements.
// Elasticsearch B.V. licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
package xkcdsearch_test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"testing"
"github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/_examples/xkcdsearch"
)
var (
fixtures = make(map[string]io.ReadCloser)
)
func init() {
fixtureFiles, err := filepath.Glob("testdata/*.json")
if err != nil {
panic(fmt.Sprintf("Cannot glob fixture files: %s", err))
}
for _, fpath := range fixtureFiles {
f, err := ioutil.ReadFile(fpath)
if err != nil {
panic(fmt.Sprintf("Cannot read fixture file: %s", err))
}
fixtures[filepath.Base(fpath)] = ioutil.NopCloser(bytes.NewReader(f))
}
}
func fixture(fname string) io.ReadCloser {
out := new(bytes.Buffer)
b1 := bytes.NewBuffer([]byte{})
b2 := bytes.NewBuffer([]byte{})
tr := io.TeeReader(fixtures[fname], b1)
defer func() { fixtures[fname] = ioutil.NopCloser(b1) }()
io.Copy(b2, tr)
out.ReadFrom(b2)
return ioutil.NopCloser(out)
}
type MockTransport struct {
Response *http.Response
RoundTripFn func(req *http.Request) (*http.Response, error)
}
func (t *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return t.RoundTripFn(req)
}
func TestStore(t *testing.T) {
t.Parallel()
mocktrans := MockTransport{
Response: &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(strings.NewReader(`{}`)),
},
}
mocktrans.RoundTripFn = func(req *http.Request) (*http.Response, error) { return mocktrans.Response, nil }
client, err := elasticsearch.NewClient(elasticsearch.Config{
Transport: &mocktrans,
})
if err != nil {
t.Fatalf("Error creating Elasticsearch client: %s", err)
}
config := xkcdsearch.StoreConfig{Client: client}
store, err := xkcdsearch.NewStore(config)
if err != nil {
t.Fatalf("Error creating the store: %s", err)
}
t.Run("Empty response", func(t *testing.T) {
results, err := store.Search("foobar")
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if results.Total != 0 {
t.Errorf("Unexpected total results, want=0, got=%d", results.Total)
}
})
t.Run("Match all", func(t *testing.T) {
mocktrans.Response = &http.Response{
StatusCode: http.StatusOK,
Body: fixture("match_all.json"),
}
results, err := store.Search("")
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if results.Total != 42 {
t.Errorf("Unexpected total results, want=42, got=%d", results.Total)
}
if results.Hits[0].Title != "Title One" {
t.Errorf("Unexpected title, want=\"Title One\", got=%q", results.Hits[0].Title)
}
})
t.Run("Match all with search_after", func(t *testing.T) {
mocktrans.RoundTripFn = func(req *http.Request) (*http.Response, error) {
var b map[string]interface{}
if err := json.NewDecoder(req.Body).Decode(&b); err != nil {
t.Fatalf("Error parsing search definition: %s", err)
}
if b["search_after"].([]interface{})[0].(float64) != 1 {
t.Fatalf("Unexpected query: %s", req.Body)
}
return &http.Response{StatusCode: http.StatusOK, Body: fixture("match_all.json")}, nil
}
results, err := store.Search("", "1,2")
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if results.Total != 42 {
t.Errorf("Unexpected total results, want=42, got=%d", results.Total)
}
if results.Hits[0].Title != "Title One" {
t.Errorf("Unexpected title, want=\"Title One\", got=%q", results.Hits[0].Title)
}
})
t.Run("Match query", func(t *testing.T) {
mocktrans.RoundTripFn = func(req *http.Request) (*http.Response, error) {
var b map[string]interface{}
if err := json.NewDecoder(req.Body).Decode(&b); err != nil {
t.Fatalf("Error parsing search definition: %s", err)
}
if b["sort"].([]interface{})[0].(map[string]interface{})["_score"] != "desc" {
t.Fatalf("Unexpected query: %s", req.Body)
}
return &http.Response{StatusCode: http.StatusOK, Body: fixture("match_query.json")}, nil
}
results, err := store.Search("numbers")
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if results.Total != 47 {
t.Errorf("Unexpected total results, want=47, got=%d", results.Total)
}
if results.Hits[0].Title != "Numbers" {
t.Errorf("Unexpected title, want=\"Numbers\", got=%q", results.Hits[0].Title)
}
if results.Hits[0].Highlights.Title[0] != "<em>Numbers</em>" {
t.Errorf("Unexpected title, want=\"<em>Numbers</em>\", got=%q", results.Hits[0].Title)
}
})
}