2021-02-01 00:10:16 +01:00
package influx
import "testing"
func TestFetchQuery ( t * testing . T ) {
2024-07-12 08:59:31 +02:00
f := func ( s * Series , timeFilter , resultExpected string ) {
t . Helper ( )
result := s . fetchQuery ( timeFilter )
if result != resultExpected {
t . Fatalf ( "unexpected result\ngot\n%s\nwant\n%s" , result , resultExpected )
}
}
f ( & Series {
Measurement : "cpu" ,
Field : "value" ,
LabelPairs : [ ] LabelPair {
{
Name : "foo" ,
Value : "bar" ,
2021-02-01 00:10:16 +01:00
} ,
} ,
2024-07-12 08:59:31 +02:00
} , "" , ` select "value" from "cpu" where "foo"::tag='bar' ` )
f ( & Series {
Measurement : "cpu" ,
Field : "value" ,
LabelPairs : [ ] LabelPair {
{
Name : "foo" ,
Value : "bar" ,
2021-02-01 00:10:16 +01:00
} ,
2024-07-12 08:59:31 +02:00
{
Name : "baz" ,
Value : "qux" ,
2021-02-01 00:10:16 +01:00
} ,
} ,
2024-07-12 08:59:31 +02:00
} , "" , ` select "value" from "cpu" where "foo"::tag='bar' and "baz"::tag='qux' ` )
f ( & Series {
Measurement : "cpu" ,
Field : "value" ,
LabelPairs : [ ] LabelPair {
{
Name : "foo" ,
Value : "b'ar" ,
2021-02-01 00:10:16 +01:00
} ,
} ,
2024-07-12 08:59:31 +02:00
} , "time >= now()" , ` select "value" from "cpu" where "foo"::tag='b\'ar' and time >= now() ` )
f ( & Series {
Measurement : "cpu" ,
Field : "value" ,
LabelPairs : [ ] LabelPair {
{
Name : "name" ,
Value : ` dev-mapper-centos\x2dswap.swap ` ,
2021-02-01 00:10:16 +01:00
} ,
2024-07-12 08:59:31 +02:00
{
Name : "state" ,
Value : "dev-mapp'er-c'en'tos" ,
2021-02-01 00:10:16 +01:00
} ,
} ,
2024-07-12 08:59:31 +02:00
} , "time >= now()" , ` select "value" from "cpu" where "name"::tag='dev-mapper-centos\\x2dswap.swap' and "state"::tag='dev-mapp\'er-c\'en\'tos' and time >= now() ` )
2021-02-01 00:10:16 +01:00
2024-07-12 08:59:31 +02:00
f ( & Series {
Measurement : "cpu" ,
Field : "value" ,
} , "time >= now()" , ` select "value" from "cpu" where time >= now() ` )
f ( & Series {
Measurement : "cpu" ,
Field : "value" ,
} , "" , ` select "value" from "cpu" ` )
2021-02-01 00:10:16 +01:00
}
func TestTimeFilter ( t * testing . T ) {
2024-07-12 08:59:31 +02:00
f := func ( start , end , resultExpected string ) {
t . Helper ( )
result := timeFilter ( start , end )
if result != resultExpected {
t . Fatalf ( "unexpected result\ngot\n%v\nwant\n%s" , result , resultExpected )
2021-02-01 00:10:16 +01:00
}
}
2024-07-12 08:59:31 +02:00
// no start and end filters
f ( "" , "" , "" )
// missing end filter
f ( "2020-01-01T20:07:00Z" , "" , "time >= '2020-01-01T20:07:00Z'" )
// missing start filter
f ( "" , "2020-01-01T21:07:00Z" , "time <= '2020-01-01T21:07:00Z'" )
// both start and end filters
f ( "2020-01-01T20:07:00Z" , "2020-01-01T21:07:00Z" , "time >= '2020-01-01T20:07:00Z' and time <= '2020-01-01T21:07:00Z'" )
2021-02-01 00:10:16 +01:00
}