Select Git revision
writer_test.go
writer_test.go 3.68 KiB
package search_test
import (
"fmt"
"math/rand"
"sort"
"testing"
"time"
"gitlab.com/uafrica/go-utils/logger"
"gitlab.com/uafrica/go-utils/search"
)
func TestLocalWriter(t *testing.T) {
test(t, search.Config{
Addresses: []string{"https://localhost:9200"},
})
}
func TestDevWriter(t *testing.T) {
test(t, search.Config{
Addresses: []string{"https://search-uafrica-v3-api-logs-fefgiypvmb3sg5wqohgsbqnzvq.af-south-1.es.amazonaws.com/"}, //from AWS Console OpenSearch Service > Domains > uafrica-v3-api-logs > General Information: Domain Endpoints
Username: "uafrica",
Password: "Aiz}a4ee",
})
}
func test(t *testing.T, c search.Config) {
logger.SetGlobalFormat(logger.NewConsole())
logger.SetGlobalLevel(logger.LevelDebug)
a, err := search.New(c)
if err != nil {
t.Fatalf("failed to create writer: %+v", err)
}
indexName := "go-utils-audit-test"
ts, err := a.TimeSeries(indexName, testStruct{})
if err != nil {
t.Fatalf("failed to create time series: %+v", err)
}
//write N records
methods := []string{"GET", "POST", "GET", "PATCH", "GET", "GET", "DELETE", "GET", "GET"} //more gets than others
paths := []string{"/users", "/orders", "/accounts", "/shipment", "/rates", "/accounts", "/shipment", "/rates", "/accounts", "/shipment", "/rates", "/accounts", "/shipment", "/rates"}
N := 100
testTime := time.Now().Add(-time.Hour * time.Duration(N))
for i := 0; i < N; i++ {
testTime = testTime.Add(time.Duration(float64(rand.Intn(100)) / 60.0 * float64(time.Hour)))
method := methods[i%len(methods)]
path := paths[i%len(paths)]
if err := ts.Write(
testTime,
testTime.Add(-time.Duration(float64(time.Second)*(float64(rand.Intn(100))/100.0+0.1))),
testStruct{
TimeSeriesHeader: search.TimeSeriesHeader{},
Test1: fmt.Sprintf("%d", i+1), //1,2,3,...
Test2: fmt.Sprintf("ACC_%05d", 93+i%7), //ACC_00093..ACC00100
Test3: i%3 + 8, //8,9, or 10
HTTP: httpData{
Method: method,
Path: path,
},
HTTPMethod: method,
HTTPPath: path,
}); err != nil {
t.Fatalf("failed to add doc: %+v", err)
}
}
docs, totalCount, err := ts.Search(10)