influxdb/query/influxql/spectests/selectors.go

47 lines
1015 B
Go

package spectests
import (
"fmt"
"path/filepath"
"runtime"
)
var selectorFuncNames = []string{
"first",
"last",
"max",
"min",
}
func SelectorTest(fn func(name string) (stmt, want string)) Fixture {
_, file, line, _ := runtime.Caller(1)
fixture := &collection{
file: filepath.Base(file),
line: line,
}
for _, name := range selectorFuncNames {
stmt, want := fn(name)
fixture.Add(stmt, want)
}
return fixture
}
func init() {
RegisterFixture(
SelectorTest(func(name string) (stmt, want string) {
return fmt.Sprintf(`SELECT %s(value) FROM db0..cpu`, name),
`package main
` + fmt.Sprintf(`from(bucketID: "%s")`, bucketID.String()) + `
|> range(start: 1677-09-21T00:12:43.145224194Z, stop: 2262-04-11T23:47:16.854775806Z)
|> filter(fn: (r) => r._measurement == "cpu" and r._field == "value")
|> group(columns: ["_measurement", "_start"], mode: "by")
|> ` + name + `()
|> map(fn: (r) => ({_time: r._time, ` + name + `: r._value}), mergeKey: true)
|> yield(name: "0")
`
}),
)
}