Skip to content

Commit 2c8961a

Browse files
committed
test: Fixed composing API url
1 parent 52feb08 commit 2c8961a

File tree

2 files changed

+29
-49
lines changed

2 files changed

+29
-49
lines changed

writeApiBlocking_test.go

+6-18
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@ import (
1515
)
1616

1717
func TestWritePoint(t *testing.T) {
18-
client := &client{serverUrl: "http://locahost:4444", options: DefaultOptions()}
19-
service := &testHttpService{
20-
t: t,
21-
options: client.Options(),
22-
serverUrl: client.ServerUrl(),
23-
}
18+
client := newTestClient()
19+
service := newTestService(t, client)
2420
client.options.SetBatchSize(5)
2521
writeApi := newWriteApiBlockingImpl("my-org", "my-bucket", service, client)
2622
points := genPoints(10)
@@ -36,12 +32,8 @@ func TestWritePoint(t *testing.T) {
3632
}
3733

3834
func TestWriteRecord(t *testing.T) {
39-
client := &client{serverUrl: "http://locahost:4444", options: DefaultOptions()}
40-
service := &testHttpService{
41-
t: t,
42-
options: client.Options(),
43-
serverUrl: client.ServerUrl(),
44-
}
35+
client := newTestClient()
36+
service := newTestService(t, client)
4537
client.options.SetBatchSize(5)
4638
writeApi := newWriteApiBlockingImpl("my-org", "my-bucket", service, client)
4739
lines := genRecords(10)
@@ -64,12 +56,8 @@ func TestWriteRecord(t *testing.T) {
6456
}
6557

6658
func TestWriteContextCancel(t *testing.T) {
67-
client := &client{serverUrl: "http://locahost:4444", options: DefaultOptions()}
68-
service := &testHttpService{
69-
t: t,
70-
options: client.Options(),
71-
serverUrl: client.ServerUrl(),
72-
}
59+
client := newTestClient()
60+
service := newTestService(t, client)
7361
client.options.SetBatchSize(5)
7462
writeApi := newWriteApiBlockingImpl("my-org", "my-bucket", service, client)
7563
lines := genRecords(10)

write_test.go

+23-31
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (t *testHttpService) PostRequest(_ context.Context, url string, body io.Rea
8484
body, _ = gzip.NewReader(body)
8585
t.wasGzip = true
8686
}
87-
assert.Equal(t.t, url, fmt.Sprintf("%s/api/v2/write?bucket=my-bucket&org=my-org&precision=ns", t.serverUrl))
87+
assert.Equal(t.t, fmt.Sprintf("%swrite?bucket=my-bucket&org=my-org&precision=ns", t.serverUrl), url)
8888

8989
if t.ReplyError() != nil {
9090
return t.ReplyError()
@@ -121,6 +121,18 @@ func (t *testHttpService) Lines() []string {
121121
return t.lines
122122
}
123123

124+
func newTestClient() *client {
125+
return &client{serverUrl: "http://locahost:4444", options: DefaultOptions()}
126+
}
127+
128+
func newTestService(t *testing.T, client InfluxDBClient) *testHttpService {
129+
return &testHttpService{
130+
t: t,
131+
options: client.Options(),
132+
serverUrl: client.ServerUrl() + "/api/v2/",
133+
}
134+
}
135+
124136
func genPoints(num int) []*Point {
125137
points := make([]*Point, num)
126138
rand.Seed(321)
@@ -165,12 +177,8 @@ func genRecords(num int) []string {
165177
}
166178

167179
func TestWriteApiImpl_Write(t *testing.T) {
168-
client := &client{serverUrl: "http://locahost:4444", options: DefaultOptions()}
169-
service := &testHttpService{
170-
t: t,
171-
options: client.Options(),
172-
serverUrl: client.ServerUrl(),
173-
}
180+
client := newTestClient()
181+
service := newTestService(t, client)
174182
client.options.SetBatchSize(5)
175183
writeApi := newWriteApiImpl("my-org", "my-bucket", service, client)
176184
points := genPoints(10)
@@ -188,12 +196,8 @@ func TestWriteApiImpl_Write(t *testing.T) {
188196
}
189197

190198
func TestGzipWithFlushing(t *testing.T) {
191-
client := &client{serverUrl: "http://locahost:4444", options: DefaultOptions()}
192-
service := &testHttpService{
193-
t: t,
194-
options: client.Options(),
195-
serverUrl: client.ServerUrl(),
196-
}
199+
client := newTestClient()
200+
service := newTestService(t, client)
197201
client.options.SetBatchSize(5).SetUseGZip(true)
198202
writeApi := newWriteApiImpl("my-org", "my-bucket", service, client)
199203
points := genPoints(5)
@@ -216,12 +220,8 @@ func TestGzipWithFlushing(t *testing.T) {
216220
writeApi.Close()
217221
}
218222
func TestFlushInterval(t *testing.T) {
219-
client := &client{serverUrl: "http://locahost:4444", options: DefaultOptions()}
220-
service := &testHttpService{
221-
t: t,
222-
options: client.Options(),
223-
serverUrl: client.ServerUrl(),
224-
}
223+
client := newTestClient()
224+
service := newTestService(t, client)
225225
client.options.SetBatchSize(10).SetFlushInterval(500)
226226
writeApi := newWriteApiImpl("my-org", "my-bucket", service, client)
227227
points := genPoints(5)
@@ -247,12 +247,8 @@ func TestFlushInterval(t *testing.T) {
247247
}
248248

249249
func TestRetry(t *testing.T) {
250-
client := &client{serverUrl: "http://locahost:4444", options: DefaultOptions()}
251-
service := &testHttpService{
252-
t: t,
253-
options: client.Options(),
254-
serverUrl: client.ServerUrl(),
255-
}
250+
client := newTestClient()
251+
service := newTestService(t, client)
256252
client.options.SetLogLevel(3).
257253
SetBatchSize(5).
258254
SetRetryInterval(10000)
@@ -291,12 +287,8 @@ func TestRetry(t *testing.T) {
291287
}
292288

293289
func TestWriteError(t *testing.T) {
294-
client := &client{serverUrl: "http://locahost:4444", options: DefaultOptions()}
295-
service := &testHttpService{
296-
t: t,
297-
options: client.Options(),
298-
serverUrl: client.ServerUrl(),
299-
}
290+
client := newTestClient()
291+
service := newTestService(t, client)
300292
client.options.SetLogLevel(3).SetBatchSize(5)
301293
service.replyError = &ihttp.Error{
302294
StatusCode: 400,

0 commit comments

Comments
 (0)