11package indexers_test
22
33import (
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
2023const elasticURL = "http://localhost:9200"
2124const 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