Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.

Commit 5349c23

Browse files
authored
ITs: RequestToQuesma boilerplate cleanup (#910)
Since I'll be adding new integration tests, let's do some cleanup of boilerplate of existing tests to make it nicer to write new tests (avoiding further code duplication/boilerplace). This PR tackles `RequestToQuesma`: move error handling/reading of body into `RequestToQuesma` function itself - no need to duplicate this logic all over the test code.
1 parent b520aca commit 5349c23

File tree

5 files changed

+32
-127
lines changed

5 files changed

+32
-127
lines changed

ci/it/testcases/base.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"database/sql"
1010
"fmt"
1111
"github.com/ClickHouse/clickhouse-go/v2"
12+
"io"
1213
"net/http"
1314
"testing"
1415
"time"
@@ -113,9 +114,18 @@ func (tc *IntegrationTestcaseBase) ExecuteClickHouseStatement(ctx context.Contex
113114
return res, nil
114115
}
115116

116-
func (tc *IntegrationTestcaseBase) RequestToQuesma(ctx context.Context, method, uri string, body []byte) (*http.Response, error) {
117+
func (tc *IntegrationTestcaseBase) RequestToQuesma(ctx context.Context, t *testing.T, method, uri string, requestBody []byte) (*http.Response, []byte) {
117118
endpoint := tc.getQuesmaEndpoint()
118-
return tc.doRequest(ctx, method, endpoint+uri, body, nil)
119+
resp, err := tc.doRequest(ctx, method, endpoint+uri, requestBody, nil)
120+
if err != nil {
121+
t.Fatalf("Error sending %s request to the endpoint '%s': %s", method, uri, err)
122+
}
123+
defer resp.Body.Close()
124+
responseBody, err := io.ReadAll(resp.Body)
125+
if err != nil {
126+
t.Fatalf("Failed to read response body of %s request to the endpoint '%s': %s", method, uri, err)
127+
}
128+
return resp, responseBody
119129
}
120130

121131
func (tc *IntegrationTestcaseBase) RequestToElasticsearch(ctx context.Context, method, uri string, body []byte) (*http.Response, error) {

ci/it/testcases/test_dual_write_and_common_table.go

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,11 @@ func (a *DualWriteAndCommonTableTestcase) RunTests(ctx context.Context, t *testi
4444
}
4545

4646
func (a *DualWriteAndCommonTableTestcase) testBasicRequest(ctx context.Context, t *testing.T) {
47-
resp, err := a.RequestToQuesma(ctx, "GET", "/", nil)
48-
if err != nil {
49-
t.Fatalf("Failed to make GET request: %s", err)
50-
}
51-
defer resp.Body.Close()
47+
resp, _ := a.RequestToQuesma(ctx, t, "GET", "/", nil)
5248
assert.Equal(t, http.StatusOK, resp.StatusCode)
5349
}
5450
func (a *DualWriteAndCommonTableTestcase) testIngestToCommonTableWorks(ctx context.Context, t *testing.T) {
55-
resp, err := a.RequestToQuesma(ctx, "POST", "/logs-4/_doc", []byte(`{"name": "Przemyslaw", "age": 31337}`))
56-
if err != nil {
57-
t.Fatalf("Failed to insert document: %s", err)
58-
}
59-
defer resp.Body.Close()
51+
resp, _ := a.RequestToQuesma(ctx, t, "POST", "/logs-4/_doc", []byte(`{"name": "Przemyslaw", "age": 31337}`))
6052
assert.Equal(t, http.StatusOK, resp.StatusCode)
6153

6254
chQuery := "SELECT * FROM 'quesma_common_table'"
@@ -101,27 +93,14 @@ func (a *DualWriteAndCommonTableTestcase) testIngestToCommonTableWorks(ctx conte
10193
assert.Equal(t, 31337, age)
10294
assert.Equal(t, "logs-4", quesmaIndexName)
10395

104-
resp, err = a.RequestToQuesma(ctx, "GET", "/logs-4/_search", []byte(`{"query": {"match_all": {}}}`))
105-
if err != nil {
106-
t.Fatalf("Failed to make GET request: %s", err)
107-
}
108-
defer resp.Body.Close()
109-
bodyBytes, err := io.ReadAll(resp.Body)
110-
if err != nil {
111-
t.Fatalf("Failed to read response body: %s", err)
112-
}
113-
96+
resp, bodyBytes := a.RequestToQuesma(ctx, t, "GET", "/logs-4/_search", []byte(`{"query": {"match_all": {}}}`))
11497
assert.Equal(t, http.StatusOK, resp.StatusCode)
11598
assert.Contains(t, string(bodyBytes), "Przemyslaw")
11699
assert.Contains(t, "Clickhouse", resp.Header.Get("X-Quesma-Source"))
117100
}
118101

119102
func (a *DualWriteAndCommonTableTestcase) testDualQueryReturnsDataFromClickHouse(ctx context.Context, t *testing.T) {
120-
resp, err := a.RequestToQuesma(ctx, "POST", "/logs-dual-query/_doc", []byte(`{"name": "Przemyslaw", "age": 31337}`))
121-
if err != nil {
122-
t.Fatalf("Failed to insert document: %s", err)
123-
}
124-
defer resp.Body.Close()
103+
resp, _ := a.RequestToQuesma(ctx, t, "POST", "/logs-dual-query/_doc", []byte(`{"name": "Przemyslaw", "age": 31337}`))
125104
assert.Equal(t, http.StatusOK, resp.StatusCode)
126105

127106
chQuery := "SELECT * FROM 'logs-dual-query'"
@@ -166,27 +145,14 @@ func (a *DualWriteAndCommonTableTestcase) testDualQueryReturnsDataFromClickHouse
166145
t.Fatalf("Failed to make DELETE request: %s", err)
167146
}
168147
// FINAL TEST - WHETHER QUESMA RETURNS DATA FROM CLICKHOUSE
169-
resp, err = a.RequestToQuesma(ctx, "GET", "/logs-dual-query/_search", []byte(`{"query": {"match_all": {}}}`))
170-
if err != nil {
171-
t.Fatalf("Failed to make GET request: %s", err)
172-
}
173-
defer resp.Body.Close()
174-
bodyBytes, err := io.ReadAll(resp.Body)
175-
if err != nil {
176-
t.Fatalf("Failed to read response body: %s", err)
177-
}
178-
148+
resp, bodyBytes := a.RequestToQuesma(ctx, t, "GET", "/logs-dual-query/_search", []byte(`{"query": {"match_all": {}}}`))
179149
assert.Equal(t, http.StatusOK, resp.StatusCode)
180150
assert.Contains(t, string(bodyBytes), "Przemyslaw")
181151
assert.Contains(t, "Clickhouse", resp.Header.Get("X-Quesma-Source"))
182152
}
183153

184154
func (a *DualWriteAndCommonTableTestcase) testIngestToClickHouseWorks(ctx context.Context, t *testing.T) {
185-
resp, err := a.RequestToQuesma(ctx, "POST", "/logs-2/_doc", []byte(`{"name": "Przemyslaw", "age": 31337}`))
186-
if err != nil {
187-
t.Fatalf("Failed to insert document: %s", err)
188-
}
189-
defer resp.Body.Close()
155+
resp, _ := a.RequestToQuesma(ctx, t, "POST", "/logs-2/_doc", []byte(`{"name": "Przemyslaw", "age": 31337}`))
190156
assert.Equal(t, http.StatusOK, resp.StatusCode)
191157

192158
chQuery := "SELECT * FROM 'logs-2'"
@@ -241,11 +207,7 @@ func (a *DualWriteAndCommonTableTestcase) testIngestToClickHouseWorks(ctx contex
241207
}
242208

243209
func (a *DualWriteAndCommonTableTestcase) testDualWritesWork(ctx context.Context, t *testing.T) {
244-
resp, err := a.RequestToQuesma(ctx, "POST", "/logs-3/_doc", []byte(`{"name": "Przemyslaw", "age": 31337}`))
245-
if err != nil {
246-
t.Fatalf("Failed to insert document: %s", err)
247-
}
248-
defer resp.Body.Close()
210+
resp, _ := a.RequestToQuesma(ctx, t, "POST", "/logs-3/_doc", []byte(`{"name": "Przemyslaw", "age": 31337}`))
249211
assert.Equal(t, http.StatusOK, resp.StatusCode)
250212

251213
chQuery := "SELECT * FROM 'logs-3'"
@@ -313,15 +275,8 @@ func (a *DualWriteAndCommonTableTestcase) testWildcardGoesToElastic(ctx context.
313275
t.Fatalf("Failed to refresh index: %s", err)
314276
}
315277
// When Quesma searches for that document
316-
resp, err := a.RequestToQuesma(ctx, "POST", "/unmentioned_index/_search", []byte(`{"query": {"match_all": {}}}`))
317-
if err != nil {
318-
t.Fatalf("Failed to make GET request: %s", err)
319-
}
320-
defer resp.Body.Close()
321-
bodyBytes, err := io.ReadAll(resp.Body)
322-
if err != nil {
323-
t.Fatalf("Failed to read response body: %s", err)
324-
}
278+
resp, bodyBytes := a.RequestToQuesma(ctx, t, "POST", "/unmentioned_index/_search", []byte(`{"query": {"match_all": {}}}`))
279+
325280
var jsonResponse map[string]interface{}
326281
if err := json.Unmarshal(bodyBytes, &jsonResponse); err != nil {
327282
t.Fatalf("Failed to unmarshal response body: %s", err)

ci/it/testcases/test_reading_clickhouse_tables.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"encoding/json"
99
"github.com/stretchr/testify/assert"
10-
"io"
1110
"net/http"
1211
"testing"
1312
)
@@ -41,11 +40,7 @@ func (a *ReadingClickHouseTablesIntegrationTestcase) RunTests(ctx context.Contex
4140
}
4241

4342
func (a *ReadingClickHouseTablesIntegrationTestcase) testBasicRequest(ctx context.Context, t *testing.T) {
44-
resp, err := a.RequestToQuesma(ctx, "GET", "/", nil)
45-
if err != nil {
46-
t.Fatalf("Failed to make GET request: %s", err)
47-
}
48-
defer resp.Body.Close()
43+
resp, _ := a.RequestToQuesma(ctx, t, "GET", "/", nil)
4944
assert.Equal(t, http.StatusOK, resp.StatusCode)
5045
}
5146

@@ -63,11 +58,7 @@ func (a *ReadingClickHouseTablesIntegrationTestcase) testRandomThing(ctx context
6358
// This returns 500 Internal Server Error, but will be tackled in separate PR.
6459
// (The table has not yet been discovered by Quesma )
6560
// ERR quesma/quesma/quesma.go:198 > quesma request failed: Q2002: Missing table. Table: test_table: can't load test_table table opaque_id= path=/test_table/_search reason="Missing table." request_id=01926654-b214-7e1d-944a-a7545cd7d419
66-
resp, err := a.RequestToQuesma(ctx, "GET", "/test_table/_search", []byte(`{"query": {"match_all": {}}}`))
67-
if err != nil {
68-
t.Fatalf("Failed to make GET request: %s", err)
69-
}
70-
defer resp.Body.Close()
61+
resp, _ := a.RequestToQuesma(ctx, t, "GET", "/test_table/_search", []byte(`{"query": {"match_all": {}}}`))
7162
assert.Equal(t, "Clickhouse", resp.Header.Get("X-Quesma-Source"))
7263
}
7364

@@ -84,15 +75,7 @@ func (a *ReadingClickHouseTablesIntegrationTestcase) testWildcardGoesToElastic(c
8475
t.Fatalf("Failed to refresh index: %s", err)
8576
}
8677
// When Quesma searches for that document
87-
resp, err := a.RequestToQuesma(ctx, "POST", "/extra_index/_search", []byte(`{"query": {"match_all": {}}}`))
88-
if err != nil {
89-
t.Fatalf("Failed to make GET request: %s", err)
90-
}
91-
defer resp.Body.Close()
92-
bodyBytes, err := io.ReadAll(resp.Body)
93-
if err != nil {
94-
t.Fatalf("Failed to read response body: %s", err)
95-
}
78+
resp, bodyBytes := a.RequestToQuesma(ctx, t, "POST", "/extra_index/_search", []byte(`{"query": {"match_all": {}}}`))
9679
var jsonResponse map[string]interface{}
9780
if err := json.Unmarshal(bodyBytes, &jsonResponse); err != nil {
9881
t.Fatalf("Failed to unmarshal response body: %s", err)

ci/it/testcases/test_transparent_proxy.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,20 @@ func (a *TransparentProxyIntegrationTestcase) RunTests(ctx context.Context, t *t
4040
}
4141

4242
func (a *TransparentProxyIntegrationTestcase) testBasicRequest(ctx context.Context, t *testing.T) {
43-
resp, err := a.RequestToQuesma(ctx, "GET", "/", nil)
44-
if err != nil {
45-
t.Fatalf("Failed to make GET request: %s", err)
46-
}
47-
defer resp.Body.Close()
43+
resp, _ := a.RequestToQuesma(ctx, t, "GET", "/", nil)
4844
assert.Equal(t, http.StatusOK, resp.StatusCode)
4945
}
5046

5147
func (a *TransparentProxyIntegrationTestcase) testIfCatHealthRequestReachesElasticsearch(ctx context.Context, t *testing.T) {
52-
resp, err := a.RequestToQuesma(ctx, "GET", "/_cat/health", nil)
53-
if err != nil {
54-
t.Fatalf("Failed to make GET request: %s", err)
55-
}
56-
defer resp.Body.Close()
57-
bodyBytes, err := io.ReadAll(resp.Body)
58-
if err != nil {
59-
t.Fatalf("Failed to read response body: %s", err)
60-
}
48+
resp, bodyBytes := a.RequestToQuesma(ctx, t, "GET", "/_cat/health", nil)
6149
assert.Equal(t, http.StatusOK, resp.StatusCode)
6250
assert.Equal(t, "Elasticsearch", resp.Header.Get("X-elastic-product"))
6351
assert.Contains(t, string(bodyBytes), "green")
6452
}
6553

6654
func (a *TransparentProxyIntegrationTestcase) testIfIndexCreationWorks(ctx context.Context, t *testing.T) {
67-
_, err := a.RequestToQuesma(ctx, "PUT", "/index_1", nil)
68-
_, err = a.RequestToQuesma(ctx, "PUT", "/index_2", nil)
55+
_, _ = a.RequestToQuesma(ctx, t, "PUT", "/index_1", nil)
56+
_, _ = a.RequestToQuesma(ctx, t, "PUT", "/index_2", nil)
6957

7058
resp, err := a.RequestToElasticsearch(ctx, "GET", "/_cat/indices", nil)
7159
if err != nil {

ci/it/testcases/test_two_pipelines.go

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"encoding/json"
99
"github.com/stretchr/testify/assert"
10-
"io"
1110
"net/http"
1211
"testing"
1312
)
@@ -42,11 +41,7 @@ func (a *QueryAndIngestPipelineTestcase) RunTests(ctx context.Context, t *testin
4241
}
4342

4443
func (a *QueryAndIngestPipelineTestcase) testBasicRequest(ctx context.Context, t *testing.T) {
45-
resp, err := a.RequestToQuesma(ctx, "GET", "/", nil)
46-
if err != nil {
47-
t.Fatalf("Failed to make GET request: %s", err)
48-
}
49-
defer resp.Body.Close()
44+
resp, _ := a.RequestToQuesma(ctx, t, "GET", "/", nil)
5045
assert.Equal(t, http.StatusOK, resp.StatusCode)
5146
}
5247

@@ -63,15 +58,7 @@ func (a *QueryAndIngestPipelineTestcase) testWildcardGoesToElastic(ctx context.C
6358
t.Fatalf("Failed to refresh index: %s", err)
6459
}
6560
// When Quesma searches for that document
66-
resp, err := a.RequestToQuesma(ctx, "POST", "/unmentioned_index/_search", []byte(`{"query": {"match_all": {}}}`))
67-
if err != nil {
68-
t.Fatalf("Failed to make GET request: %s", err)
69-
}
70-
defer resp.Body.Close()
71-
bodyBytes, err := io.ReadAll(resp.Body)
72-
if err != nil {
73-
t.Fatalf("Failed to read response body: %s", err)
74-
}
61+
resp, bodyBytes := a.RequestToQuesma(ctx, t, "POST", "/unmentioned_index/_search", []byte(`{"query": {"match_all": {}}}`))
7562
var jsonResponse map[string]interface{}
7663
if err := json.Unmarshal(bodyBytes, &jsonResponse); err != nil {
7764
t.Fatalf("Failed to unmarshal response body: %s", err)
@@ -88,16 +75,7 @@ func (a *QueryAndIngestPipelineTestcase) testWildcardGoesToElastic(ctx context.C
8875
}
8976

9077
func (a *QueryAndIngestPipelineTestcase) testEmptyTargetDoc(ctx context.Context, t *testing.T) {
91-
resp, err := a.RequestToQuesma(ctx, "POST", "/logs_disabled/_doc", []byte(`{"name": "Alice"}`))
92-
if err != nil {
93-
t.Fatalf("Error sending POST request: %s", err)
94-
}
95-
defer resp.Body.Close()
96-
bodyBytes, err := io.ReadAll(resp.Body)
97-
if err != nil {
98-
t.Fatalf("Failed to read response body: %s", err)
99-
}
100-
78+
resp, bodyBytes := a.RequestToQuesma(ctx, t, "POST", "/logs_disabled/_doc", []byte(`{"name": "Alice"}`))
10179
assert.Contains(t, string(bodyBytes), "index_closed_exception")
10280
assert.Equal(t, http.StatusOK, resp.StatusCode)
10381
assert.Equal(t, "Clickhouse", resp.Header.Get("X-Quesma-Source"))
@@ -112,16 +90,7 @@ func (a *QueryAndIngestPipelineTestcase) testEmptyTargetBulk(ctx context.Context
11290
{ "name": "Bob", "age": 25 }
11391
11492
`)
115-
resp, err := a.RequestToQuesma(ctx, "POST", "/_bulk", bulkPayload)
116-
if err != nil {
117-
t.Fatalf("Error sending POST request: %s", err)
118-
}
119-
defer resp.Body.Close()
120-
bodyBytes, err := io.ReadAll(resp.Body)
121-
if err != nil {
122-
t.Fatalf("Failed to read response body: %s", err)
123-
}
124-
93+
resp, bodyBytes := a.RequestToQuesma(ctx, t, "POST", "/_bulk", bulkPayload)
12594
assert.Contains(t, string(bodyBytes), "index_closed_exception")
12695
assert.Equal(t, http.StatusOK, resp.StatusCode)
12796
assert.Equal(t, "Clickhouse", resp.Header.Get("X-Quesma-Source"))

0 commit comments

Comments
 (0)