-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathworker_test.go
141 lines (134 loc) · 3.97 KB
/
worker_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
package solrbulk
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// TestBulkIndex tests the BulkIndex function
func TestBulkIndex(t *testing.T) {
tests := []struct {
name string
docs []string
options Options
serverStatus int
responseBody string
expectError bool
validateRequest func(t *testing.T, r *http.Request)
}{
{
name: "index success",
docs: []string{
`{"id":"1","title":"Test Document 1"}`,
`{"id":"2","title":"Test Document 2"}`,
},
options: Options{
BatchSize: 100,
CommitSize: 100,
Verbose: false,
UpdateRequestHandlerName: "/update/json",
},
serverStatus: http.StatusOK,
responseBody: `{"responseHeader":{"status":0,"QTime":5}}`,
expectError: false,
validateRequest: func(t *testing.T, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("Failed to read request body: %v", err)
}
bodyStr := string(body)
if !strings.Contains(bodyStr, `"id":"1"`) || !strings.Contains(bodyStr, `"id":"2"`) {
t.Errorf("Request body missing expected documents: %s", bodyStr)
}
if !strings.HasPrefix(bodyStr, "[") || !strings.HasSuffix(bodyStr, "]\n") {
t.Errorf("Request body not properly formatted: %s", bodyStr)
}
},
},
{
name: "skip empty docs",
docs: []string{
`{"id":"1","title":"Test Document 1"}`,
"",
" ",
`{"id":"2","title":"Test Document 2"}`,
},
options: Options{
BatchSize: 100,
CommitSize: 100,
Verbose: false,
UpdateRequestHandlerName: "/update/json",
},
serverStatus: http.StatusOK,
responseBody: `{"responseHeader":{"status":0,"QTime":5}}`,
expectError: false,
validateRequest: func(t *testing.T, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {
t.Fatalf("Failed to read request body: %v", err)
}
bodyStr := string(body)
count := strings.Count(bodyStr, `"id"`)
if count != 2 {
t.Errorf("Expected 2 documents after filtering, got approximately %d", count)
}
},
},
{
name: "server error",
docs: []string{
`{"id":"1","title":"Test Document 1"}`,
},
options: Options{
BatchSize: 100,
CommitSize: 100,
Verbose: false,
UpdateRequestHandlerName: "/update/json",
},
serverStatus: http.StatusInternalServerError,
responseBody: `{"responseHeader":{"status":1,"QTime":5,"errors":[{"id":"doc1","type":"error","message":"Error indexing document"}]}}`,
expectError: true,
validateRequest: nil,
},
{
name: "server error with tolerant handler",
docs: []string{
`{"id":"1","title":"Test Document 1"}`,
},
options: Options{
BatchSize: 100,
CommitSize: 100,
Verbose: false,
UpdateRequestHandlerName: "/update/json",
},
serverStatus: http.StatusOK,
responseBody: `{"responseHeader":{"status":1,"QTime":5,"errors":[{"id":"doc1","type":"error","message":"Error indexing document"}]}}`,
expectError: false,
validateRequest: nil,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("Expected POST request, got %s", r.Method)
}
if r.URL.Path != tc.options.UpdateRequestHandlerName {
t.Errorf("Expected path %s, got %s", tc.options.UpdateRequestHandlerName, r.URL.Path)
}
if tc.validateRequest != nil {
tc.validateRequest(t, r)
}
w.WriteHeader(tc.serverStatus)
w.Write([]byte(tc.responseBody))
}))
defer server.Close()
tc.options.Server = server.URL
err := BulkIndex(tc.docs, tc.options)
if (err != nil) != tc.expectError {
t.Errorf("BulkIndex() error = %v, expectError %v", err, tc.expectError)
}
})
}
}