2018-05-15 20:11:32 +00:00
|
|
|
# Query Tests
|
|
|
|
This package simulates an end-to-end query test without requiring launching a full service. This is currently achieved
|
2018-06-08 20:02:59 +00:00
|
|
|
by dynamically replacing instances of from()---which access a storage location---with instances of fromCSV() which is
|
2018-05-15 20:11:32 +00:00
|
|
|
a data source that takes a single CSV text argument.
|
|
|
|
|
|
|
|
# Getting Started
|
2018-06-08 20:02:59 +00:00
|
|
|
You do not need to use fromCSV in your query. You can write your query as you normally would, or copy it from an
|
2018-05-15 20:11:32 +00:00
|
|
|
application as-is. What you will need to provide, along with the query file, are two csv files that contain the input
|
2018-06-08 20:02:59 +00:00
|
|
|
and output data for the query. The test framework will identify the Flux query by the file extension `.flux` and
|
|
|
|
then locate the required input/output files using the same root filename.
|
2018-05-15 20:11:32 +00:00
|
|
|
|
2018-06-08 20:02:59 +00:00
|
|
|
As an example, consider the test `simple_max` which is defined by the file `simple_max.flux`:
|
2018-05-15 20:11:32 +00:00
|
|
|
|
2018-06-08 20:02:59 +00:00
|
|
|
### simple_max.flux
|
2018-05-15 20:11:32 +00:00
|
|
|
```
|
|
|
|
from(db:"test") |> range(start:-5m) |> group(by:["_measurement"]) |> max(useRowTime:true) |> map(fn: (r) => {max:r._value})
|
|
|
|
```
|
|
|
|
|
2018-06-08 20:02:59 +00:00
|
|
|
The test will read in this query, as well as the associated CSV files that are in the flux-spec compatible CSV format:
|
2018-05-15 20:11:32 +00:00
|
|
|
|
|
|
|
### simple_max_in.csv
|
|
|
|
```
|
|
|
|
#datatype,long,dateTime:RFC3339,dateTime:RFC3339,dateTime:RFC3339,tag,tag,double
|
|
|
|
,blkid,_start,_stop,_time,_measurement,_field,_value
|
|
|
|
,0,2018-04-17T00:00:00Z,2018-04-17T00:05:00Z,2018-04-17T00:00:00Z,m1,f1,42.0
|
|
|
|
,0,2018-04-17T00:00:00Z,2018-04-17T00:05:00Z,2018-04-17T00:00:01Z,m1,f1,43.0
|
|
|
|
```
|
|
|
|
|
|
|
|
### simple_max_out.csv
|
|
|
|
```
|
|
|
|
#datatype,long,dateTime:RFC3339Nano,dateTime:RFC3339Nano,dateTime:RFC3339Nano,string,double
|
|
|
|
,blkid,_start,_stop,_time,_measurement,max
|
|
|
|
,0,2018-04-17T00:00:00Z,2018-04-17T00:05:00Z,2018-04-17T00:00:01Z,m1,43
|
|
|
|
```
|
|
|
|
|
|
|
|
Note that all of the test files are identified by the test name, `simple_max`. So given a <FILENAME>, a new test must
|
2018-06-08 20:02:59 +00:00
|
|
|
provide the following files:
|
2018-05-15 20:11:32 +00:00
|
|
|
|
2018-06-08 20:02:59 +00:00
|
|
|
- <FILENAME>.flux: a Flux query file
|
2018-05-15 20:11:32 +00:00
|
|
|
- <FILENAME>_in.csv: input file for the query
|
|
|
|
- <FILENAME>_out.csv: output file for the query
|
2018-06-08 20:02:59 +00:00
|
|
|
- <FILENAME>.influxql: if present, this query will be transpiled and tested against the input and output csv files,
|
|
|
|
but only if there is a corresponding `.flux` query.
|
2018-05-15 20:11:32 +00:00
|
|
|
|
|
|
|
## Notes about text formatting
|
2018-06-08 20:02:59 +00:00
|
|
|
Compliant to the HTTP spec for CSV data, the output produced by the fluxd process has some specific requirements:
|
2018-05-15 20:11:32 +00:00
|
|
|
- UTF-8 normalized
|
|
|
|
- line endings are `\r\n`
|
|
|
|
|
2018-06-08 20:02:59 +00:00
|
|
|
The go program query/querytest/prepcsvtests/prepcsvtests.go can be run to prepare tests. A valid test case must have
|
|
|
|
<CASENAME>.flux and <CASENAME>.in.csv files. The prepcsvtests executable will iterate over all such test cases in
|
|
|
|
the user-supplied directory, run the Flux query on the input and prompt the user to approve saving the result as
|
|
|
|
<CASENAME>.out.csv.
|
2018-05-25 18:38:50 +00:00
|
|
|
```go build ./querytest/prepcsvtests/prepcsvtests.go && ./prepcsvtests query/querytests/test_cases```
|
|
|
|
|
2018-06-08 20:02:59 +00:00
|
|
|
Optionally, you can give a CASENAME to prep the output for a single case:
|
2018-05-25 18:38:50 +00:00
|
|
|
```go build ./querytest/prepcsvtests/prepcsvtests.go && ./prepcsvtests query/querytests/test_cases CASENAME```
|