Skip to content

Commit 334038a

Browse files
authored
Merge pull request #68 from nyaruka/tests_without_client
Rewrite tests to make HTTP calls to elastic and not use library
2 parents b3c8289 + 3ff2347 commit 334038a

File tree

4 files changed

+92
-211
lines changed

4 files changed

+92
-211
lines changed

go.mod

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
github.com/lib/pq v1.10.9
88
github.com/nyaruka/ezconf v0.3.0
99
github.com/nyaruka/gocommon v1.53.2
10-
github.com/olivere/elastic/v7 v7.0.32
1110
github.com/pkg/errors v0.9.1
1211
github.com/samber/slog-multi v1.0.2
1312
github.com/samber/slog-sentry v1.2.2
@@ -20,9 +19,7 @@ require (
2019
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
2120
github.com/go-chi/chi/v5 v5.0.12 // indirect
2221
github.com/gorilla/websocket v1.5.1 // indirect
23-
github.com/josharian/intern v1.0.0 // indirect
2422
github.com/kylelemons/godebug v1.1.0 // indirect
25-
github.com/mailru/easyjson v0.7.7 // indirect
2623
github.com/naoina/go-stringutil v0.1.0 // indirect
2724
github.com/naoina/toml v0.1.1 // indirect
2825
github.com/nyaruka/librato v1.1.1 // indirect

go.sum

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
44
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
5-
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
6-
github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g=
75
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
86
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
97
github.com/getsentry/sentry-go v0.27.0 h1:Pv98CIbtB3LkMWmXi4Joa5OOcwbmnX88sF5qbK3r3Ps=
@@ -18,14 +16,10 @@ github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/
1816
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
1917
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
2018
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
21-
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
22-
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
2319
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
2420
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
2521
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
2622
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
27-
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
28-
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
2923
github.com/naoina/go-stringutil v0.1.0 h1:rCUeRUHjBjGTSHl0VC00jUPLz8/F9dDzYI70Hzifhks=
3024
github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0=
3125
github.com/naoina/toml v0.1.1 h1:PT/lllxVVN0gzzSqSlHEmP8MJB4MY2U7STGxiouV4X8=
@@ -40,8 +34,6 @@ github.com/nyaruka/null/v2 v2.0.3 h1:rdmMRQyVzrOF3Jff/gpU/7BDR9mQX0lcLl4yImsA3kw
4034
github.com/nyaruka/null/v2 v2.0.3/go.mod h1:OCVeCkCXwrg5/qE6RU0c1oUVZBy+ZDrT+xYg1XSaIWA=
4135
github.com/nyaruka/phonenumbers v1.3.4 h1:bF1Wdh++fxw09s3surhVeBhXEcUKG07pHeP8HQXqjn8=
4236
github.com/nyaruka/phonenumbers v1.3.4/go.mod h1:Ut+eFwikULbmCenH6InMKL9csUNLyxHuBLyfkpum11s=
43-
github.com/olivere/elastic/v7 v7.0.32 h1:R7CXvbu8Eq+WlsLgxmKVKPox0oOwAE/2T9Si5BnvK6E=
44-
github.com/olivere/elastic/v7 v7.0.32/go.mod h1:c7PVmLe3Fxq77PIfY/bZmxY/TAamBhCzZ8xDOE09a9k=
4537
github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4=
4638
github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8=
4739
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

indexers/base_test.go

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
package indexers_test
22

33
import (
4-
"context"
4+
"bytes"
55
"database/sql"
6-
"log"
6+
"encoding/json"
7+
"io"
78
"log/slog"
9+
"net/http"
810
"os"
911
"sort"
1012
"strconv"
1113
"strings"
1214
"testing"
1315

16+
"github.com/nyaruka/gocommon/httpx"
17+
"github.com/nyaruka/gocommon/jsonx"
1418
"github.com/nyaruka/rp-indexer/v9/indexers"
15-
"github.com/olivere/elastic/v7"
1619
"github.com/stretchr/testify/assert"
1720
"github.com/stretchr/testify/require"
1821
)
1922

2023
const elasticURL = "http://localhost:9200"
2124
const aliasName = "indexer_test"
2225

23-
func setup(t *testing.T) (*sql.DB, *elastic.Client) {
26+
func setup(t *testing.T) *sql.DB {
2427
testDB, err := os.ReadFile("../testdb.sql")
2528
require.NoError(t, err)
2629

@@ -30,44 +33,41 @@ func setup(t *testing.T) (*sql.DB, *elastic.Client) {
3033
_, err = db.Exec(string(testDB))
3134
require.NoError(t, err)
3235

33-
es, err := elastic.NewClient(elastic.SetURL(elasticURL), elastic.SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)), elastic.SetSniff(false))
34-
require.NoError(t, err)
35-
3636
// delete all indexes with our alias prefix
37-
existing, err := es.IndexNames()
38-
require.NoError(t, err)
37+
existing := elasticRequest(t, http.MethodGet, "/_aliases", nil)
3938

40-
for _, name := range existing {
39+
for name := range existing {
4140
if strings.HasPrefix(name, aliasName) {
42-
_, err = es.DeleteIndex(name).Do(context.Background())
43-
require.NoError(t, err)
41+
elasticRequest(t, http.MethodDelete, "/"+name, nil)
4442
}
4543
}
4644

4745
slog.SetDefault(slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})))
4846

49-
return db, es
47+
return db
5048
}
5149

52-
func assertQuery(t *testing.T, client *elastic.Client, query elastic.Query, expected []int64, msgAndArgs ...interface{}) {
53-
results, err := client.Search().Index(aliasName).Query(query).Sort("id", true).Pretty(true).Do(context.Background())
54-
assert.NoError(t, err)
50+
func assertQuery(t *testing.T, query []byte, expected []int64, msgAndArgs ...interface{}) {
51+
results := elasticRequest(t, http.MethodPost, "/"+aliasName+"/_search",
52+
map[string]any{"query": json.RawMessage(query), "sort": []map[string]any{{"id": "asc"}}},
53+
)
54+
hits := results["hits"].(map[string]any)["hits"].([]any)
5555

56-
actual := make([]int64, len(results.Hits.Hits))
57-
for h, hit := range results.Hits.Hits {
58-
asInt, _ := strconv.Atoi(hit.Id)
56+
actual := make([]int64, len(hits))
57+
for h, hit := range hits {
58+
idStr := hit.(map[string]any)["_id"].(string)
59+
asInt, _ := strconv.Atoi(idStr)
5960
actual[h] = int64(asInt)
6061
}
6162

6263
assert.Equal(t, expected, actual, msgAndArgs...)
6364
}
6465

65-
func assertIndexesWithPrefix(t *testing.T, es *elastic.Client, prefix string, expected []string) {
66-
all, err := es.IndexNames()
67-
require.NoError(t, err)
66+
func assertIndexesWithPrefix(t *testing.T, prefix string, expected []string) {
67+
all := elasticRequest(t, http.MethodGet, "/_aliases", nil)
6868

6969
actual := []string{}
70-
for _, name := range all {
70+
for name := range all {
7171
if strings.HasPrefix(name, prefix) {
7272
actual = append(actual, name)
7373
}
@@ -81,3 +81,20 @@ func assertIndexerStats(t *testing.T, ix indexers.Indexer, expectedIndexed, expe
8181
assert.Equal(t, expectedIndexed, actual.Indexed, "indexed mismatch")
8282
assert.Equal(t, expectedDeleted, actual.Deleted, "deleted mismatch")
8383
}
84+
85+
func elasticRequest(t *testing.T, method, path string, data map[string]any) map[string]any {
86+
var body io.Reader
87+
if data != nil {
88+
body = bytes.NewReader(jsonx.MustMarshal(data))
89+
}
90+
req, err := httpx.NewRequest(method, elasticURL+path, body, map[string]string{"Content-Type": "application/json"})
91+
require.NoError(t, err)
92+
93+
trace, err := httpx.DoTrace(http.DefaultClient, req, nil, nil, -1)
94+
require.NoError(t, err)
95+
96+
resp, err := jsonx.DecodeGeneric(trace.ResponseBody)
97+
require.NoError(t, err)
98+
99+
return resp.(map[string]any)
100+
}

0 commit comments

Comments
 (0)