Skip to content

Commit

Permalink
Smoke tests enhancements (#123)
Browse files Browse the repository at this point in the history
This PR:
- generalize query test flow a bit
- adds `validate()`, post condition validation function
- adds facets test that checks returned field types
  • Loading branch information
pdelewski authored May 16, 2024
1 parent 3b2176e commit 6adefb8
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 14 deletions.
115 changes: 102 additions & 13 deletions smoke-test/async_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/qri-io/jsonpointer"
"io"
"log"
"net/http"
"reflect"
"strings"
"time"
)
Expand All @@ -16,6 +18,7 @@ type testQuery struct {
name string
category string
body string
validate func(map[string]interface{}) bool
}

var sampleQueries = []testQuery{
Expand Down Expand Up @@ -96,6 +99,9 @@ var sampleQueries = []testQuery{
"track_total_hits": false,
"version": true
}`,
validate: func(response map[string]interface{}) bool {
return ensureSomeHits(response)
},
},
{
name: "Histogram in explore",
Expand Down Expand Up @@ -146,7 +152,89 @@ var sampleQueries = []testQuery{
],
"track_total_hits": true
}`,
validate: func(m map[string]interface{}) bool {
return true
},
},
{
name: "Facets aggregation, checking field types",
category: "aggregate",
body: `{
"aggs": {
"sample": {
"aggs": {
"sample_count": {
"value_count": {
"field": "service.name"
}
},
"top_values": {
"terms": {
"field": "service.name",
"shard_size": 25,
"size": 10
}
}
},
"sampler": {
"shard_size": 5000
}
}
},
"query": {
"bool": {
"filter": [
{
"range": {
"@timestamp": {
"format": "strict_date_optional_time",
"gte": "now-1d",
"lte": "now-1s"
}
}
},
{
"bool": {
"filter": [],
"must": [],
"must_not": [],
"should": []
}
}
]
}
},
"runtime_mappings": {},
"size": 0,
"track_total_hits": true
}`,
validate: func(response map[string]interface{}) bool {
return checkTypeExpectation("float64", "/response/aggregations/sample/top_values/buckets/0/doc_count", response) &&
checkTypeExpectation("string", "/response/aggregations/sample/top_values/buckets/0/key", response)
},
},
}

func checkTypeExpectation(expectedType string, path string, response map[string]interface{}) bool {
ptr, err := jsonpointer.Parse(path)
if err != nil {
fmt.Println(err)
return false
}
value, err := ptr.Eval(response)
if err != nil {
fmt.Println(err)
return false
}
valueType := reflect.TypeOf(value)

// Check if the type is int
if valueType.Kind().String() != expectedType {
fmt.Printf("Expected %s, got %s\n", expectedType, valueType.Kind().String())
return false
}
return true

}

func waitForAsyncQuery(timeout time.Duration) {
Expand All @@ -160,19 +248,7 @@ func waitForAsyncQuery(timeout time.Duration) {
if resp.StatusCode == 200 {
body, err := io.ReadAll(resp.Body)
if err == nil {
var response map[string]interface{}
_ = json.Unmarshal(body, &response)

if response["completion_time_in_millis"] != nil {
if !sourceClickhouse(resp) {
panic("invalid X-Quesma-Source header value")
}
if query.category == "simple" {
return ensureSomeHits(response)
} else {
return true
}
}
return validateResponse(query, resp, body)
} else {
log.Println(err)
}
Expand Down Expand Up @@ -287,3 +363,16 @@ func ensureSomeHits(jsonBody map[string]interface{}) bool {

return true
}

func validateResponse(query testQuery, resp *http.Response, body []byte) bool {
var response map[string]interface{}
_ = json.Unmarshal(body, &response)

if response["completion_time_in_millis"] != nil {
if !sourceClickhouse(resp) {
panic("invalid X-Quesma-Source header value")
}
return query.validate(response)
}
return true
}
5 changes: 4 additions & 1 deletion smoke-test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ go 1.22.0

require github.com/mailru/go-clickhouse v1.8.0

require github.com/google/uuid v1.2.0 // indirect
require (
github.com/google/uuid v1.2.0 // indirect
github.com/qri-io/jsonpointer v0.1.1 // indirect
)
2 changes: 2 additions & 0 deletions smoke-test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/mailru/go-clickhouse v1.8.0 h1:wqTHVsfR4g+BSwKso7X90RdOsVXaSwqJ96GmgB
github.com/mailru/go-clickhouse v1.8.0/go.mod h1:crHi+yrqslIClnYPm8IOxYVX6GmYVYymJ601I4jDqvo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/qri-io/jsonpointer v0.1.1 h1:prVZBZLL6TW5vsSB9fFHFAMBLI4b0ri5vribQlTJiBA=
github.com/qri-io/jsonpointer v0.1.1/go.mod h1:DnJPaYgiKu56EuDp8TU5wFLdZIcAnb/uH9v37ZaMV64=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

0 comments on commit 6adefb8

Please sign in to comment.