fix(query/querytest): Use t.Run to give tests clear names

pull/10616/head
Nathaniel Cook 2018-05-25 13:18:02 -06:00
parent c5e0477b74
commit cf7d3c1864
2 changed files with 60 additions and 50 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math" "math"
"os"
"github.com/influxdata/platform" "github.com/influxdata/platform"
"github.com/influxdata/platform/query" "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) results, err := qs.Query(context.Background(), staticResultID, spec)
if err != nil { if err != nil {
return "", err return "", err
@ -75,6 +76,9 @@ func GetQueryEncodedResults(qs *query.QueryServiceBridge, spec *query.Spec, inpu
func GetTestData(prefix, suffix string) (string, error) { func GetTestData(prefix, suffix string) (string, error) {
datafile := prefix + suffix datafile := prefix + suffix
if _, err := os.Stat(datafile); err != nil {
return "", err
}
csv, err := ioutil.ReadFile(datafile) csv, err := ioutil.ReadFile(datafile)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to open file: %s", datafile) return "", fmt.Errorf("failed to open file: %s", datafile)

View File

@ -2,7 +2,6 @@ package querytest
import ( import (
"context" "context"
"log"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -37,64 +36,71 @@ func Test_QueryEndToEnd(t *testing.T) {
ext := filepath.Ext(ifqlFile) ext := filepath.Ext(ifqlFile)
prefix := ifqlFile[0 : len(ifqlFile)-len(ext)] prefix := ifqlFile[0 : len(ifqlFile)-len(ext)]
_, caseName := filepath.Split(prefix) _, caseName := filepath.Split(prefix)
csvIn := prefix + ".in.csv"
csvOut, err := GetTestData(prefix, ".out.csv")
if err != nil {
t.Fatalf("error in test case %s: %s", prefix, err)
}
ifqlQuery, err := GetTestData(prefix, ".ifql")
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)
}
}
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 QueryTestCheckSpec(t *testing.T, qs *query.QueryServiceBridge, spec *query.Spec, caseName, inputFile, want string) error { 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)
}
spec, err := query.Compile(context.Background(), q)
if err != nil {
t.Fatalf("error in test case %s: %s", prefix, err)
}
csvIn := prefix + ".in.csv"
QueryTestCheckSpec(t, qs, spec, csvIn, csvOut)
}
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() t.Helper()
ReplaceFromSpec(spec, inputFile) ReplaceFromSpec(spec, inputFile)
//log.Println("QueryTestCheckSpec", query.Formatted(spec, query.FmtJSON))
log.Println("QueryTestCheckSpec")
got, err := GetQueryEncodedResults(qs, spec, inputFile) got, err := GetQueryEncodedResults(qs, spec, inputFile)
if err != nil { if err != nil {
t.Errorf("case %s: failed to run query spec error=%s", caseName, err) t.Fatalf("failed to run query error=%s", err)
return err
} }
if g, w := strings.TrimSpace(got), strings.TrimSpace(want); g != w { 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
} }