fix(query/querytest): Use t.Run to give tests clear names
parent
c5e0477b74
commit
cf7d3c1864
|
@ -6,6 +6,7 @@ import (
|
|||
"fmt"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"os"
|
||||
|
||||
"github.com/influxdata/platform"
|
||||
"github.com/influxdata/platform/query"
|
||||
|
@ -52,7 +53,7 @@ func GetQueryServiceBridge() *query.QueryServiceBridge {
|
|||
}
|
||||
}
|
||||
|
||||
func GetQueryEncodedResults(qs *query.QueryServiceBridge, spec *query.Spec, inputFile string) (string, error) {
|
||||
func GetQueryEncodedResults(qs query.QueryService, spec *query.Spec, inputFile string) (string, error) {
|
||||
results, err := qs.Query(context.Background(), staticResultID, spec)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
@ -75,6 +76,9 @@ func GetQueryEncodedResults(qs *query.QueryServiceBridge, spec *query.Spec, inpu
|
|||
|
||||
func GetTestData(prefix, suffix string) (string, error) {
|
||||
datafile := prefix + suffix
|
||||
if _, err := os.Stat(datafile); err != nil {
|
||||
return "", err
|
||||
}
|
||||
csv, err := ioutil.ReadFile(datafile)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to open file: %s", datafile)
|
||||
|
|
|
@ -2,7 +2,6 @@ package querytest
|
|||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
@ -37,64 +36,71 @@ func Test_QueryEndToEnd(t *testing.T) {
|
|||
ext := filepath.Ext(ifqlFile)
|
||||
prefix := ifqlFile[0 : len(ifqlFile)-len(ext)]
|
||||
_, caseName := filepath.Split(prefix)
|
||||
csvIn := prefix + ".in.csv"
|
||||
|
||||
ifqlName := caseName + ".ifql"
|
||||
influxqlName := caseName + ".influxql"
|
||||
t.Run(ifqlName, func(t *testing.T) {
|
||||
queryTester(t, qs, ifqlName, prefix, ".ifql")
|
||||
})
|
||||
t.Run(influxqlName, func(t *testing.T) {
|
||||
queryTranspileTester(t, influxqlTranspiler, qs, influxqlName, prefix, ".influxql")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func queryTester(t *testing.T, qs query.QueryService, name, prefix, queryExt string) {
|
||||
q, err := GetTestData(prefix, queryExt)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
csvOut, err := GetTestData(prefix, ".out.csv")
|
||||
if err != nil {
|
||||
t.Fatalf("error in test case %s: %s", prefix, err)
|
||||
}
|
||||
|
||||
ifqlQuery, err := GetTestData(prefix, ".ifql")
|
||||
spec, err := query.Compile(context.Background(), q)
|
||||
if err != nil {
|
||||
t.Fatalf("error in test case %s: %s", prefix, err)
|
||||
}
|
||||
|
||||
ifqlSpec, err := query.Compile(context.Background(), ifqlQuery)
|
||||
if err != nil {
|
||||
t.Fatalf("error in test case %s: %s", prefix, err)
|
||||
}
|
||||
|
||||
err = QueryTestCheckSpec(t, qs, ifqlSpec, caseName+".ifql", csvIn, csvOut)
|
||||
if err != nil {
|
||||
t.Errorf("failed to run ifql query spec for test case %s. error=%s", prefix, err)
|
||||
}
|
||||
|
||||
influxqlQuery, err := GetTestData(prefix, ".influxql")
|
||||
if err != nil {
|
||||
t.Logf("skipping influxql for test case %s: %s", prefix, err)
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Fatalf("error in test case %s: %s", prefix, err)
|
||||
}
|
||||
|
||||
influxqlSpec, err := influxqlTranspiler.Transpile(context.Background(), influxqlQuery)
|
||||
if err != nil {
|
||||
t.Errorf("failed to obtain transpiled influxql query spec for test case %s. error=%s", prefix, err)
|
||||
}
|
||||
|
||||
err = QueryTestCheckSpec(t, qs, influxqlSpec, "influxql::"+caseName, csvIn, csvOut)
|
||||
if err != nil {
|
||||
t.Errorf("failed to run influxql query spec for test case %s. error=%s", prefix, err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
csvIn := prefix + ".in.csv"
|
||||
QueryTestCheckSpec(t, qs, spec, csvIn, csvOut)
|
||||
}
|
||||
|
||||
func QueryTestCheckSpec(t *testing.T, qs *query.QueryServiceBridge, spec *query.Spec, caseName, inputFile, want string) error {
|
||||
func queryTranspileTester(t *testing.T, transpiler query.Transpiler, qs query.QueryService, name, prefix, queryExt string) {
|
||||
q, err := GetTestData(prefix, queryExt)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
t.Skip("query missing")
|
||||
} else {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
csvOut, err := GetTestData(prefix, ".out.csv")
|
||||
if err != nil {
|
||||
t.Fatalf("error in test case %s: %s", prefix, err)
|
||||
}
|
||||
|
||||
spec, err := transpiler.Transpile(context.Background(), q)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to transpile query to spec error=%s", err)
|
||||
}
|
||||
|
||||
csvIn := prefix + ".in.csv"
|
||||
QueryTestCheckSpec(t, qs, spec, csvIn, csvOut)
|
||||
}
|
||||
|
||||
func QueryTestCheckSpec(t *testing.T, qs query.QueryService, spec *query.Spec, inputFile, want string) {
|
||||
t.Helper()
|
||||
ReplaceFromSpec(spec, inputFile)
|
||||
|
||||
//log.Println("QueryTestCheckSpec", query.Formatted(spec, query.FmtJSON))
|
||||
log.Println("QueryTestCheckSpec")
|
||||
|
||||
got, err := GetQueryEncodedResults(qs, spec, inputFile)
|
||||
if err != nil {
|
||||
t.Errorf("case %s: failed to run query spec error=%s", caseName, err)
|
||||
return err
|
||||
t.Fatalf("failed to run query error=%s", err)
|
||||
}
|
||||
if g, w := strings.TrimSpace(got), strings.TrimSpace(want); g != w {
|
||||
t.Errorf("case %s: result not as expected want(-) got (+):\n%v", caseName, diff.LineDiff(w, g))
|
||||
t.Fatalf("result not as expected want(-) got (+):\n%v", diff.LineDiff(w, g))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue