2021-01-31 23:10:16 +00:00
|
|
|
package influx
|
|
|
|
|
|
|
|
import (
|
2021-10-18 12:10:44 +00:00
|
|
|
"encoding/json"
|
2021-01-31 23:10:16 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2024-07-12 06:59:31 +00:00
|
|
|
func TestSeriesUnmarshal(t *testing.T) {
|
|
|
|
f := func(s string, resultExpected *Series) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
result := &Series{}
|
|
|
|
if err := result.unmarshal(s); err != nil {
|
|
|
|
t.Fatalf("cannot unmarshal series from %q: %s", s, err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(result, resultExpected) {
|
|
|
|
t.Fatalf("unexpected result\ngot\n%v\nwant\n%v", result, resultExpected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 23:10:16 +00:00
|
|
|
tag := func(name, value string) LabelPair {
|
|
|
|
return LabelPair{
|
|
|
|
Name: name,
|
|
|
|
Value: value,
|
|
|
|
}
|
|
|
|
}
|
2024-07-12 06:59:31 +00:00
|
|
|
series := func(measurement string, lp ...LabelPair) *Series {
|
|
|
|
return &Series{
|
2021-01-31 23:10:16 +00:00
|
|
|
Measurement: measurement,
|
|
|
|
LabelPairs: lp,
|
|
|
|
}
|
|
|
|
}
|
2024-07-12 06:59:31 +00:00
|
|
|
|
|
|
|
f("cpu", series("cpu"))
|
|
|
|
|
|
|
|
f("cpu,host=localhost", series("cpu", tag("host", "localhost")))
|
|
|
|
|
|
|
|
f("cpu,host=localhost,instance=instance", series("cpu", tag("host", "localhost"), tag("instance", "instance")))
|
|
|
|
|
|
|
|
f(`fo\,bar\=baz,x\=\b=\\a\,\=\q\ `, series("fo,bar=baz", tag(`x=\b`, `\a,=\q `)))
|
|
|
|
|
|
|
|
f("cpu,host=192.168.0.1,instance=fe80::fdc8:5e36:c2c6:baac%utun1", series("cpu", tag("host", "192.168.0.1"), tag("instance", "fe80::fdc8:5e36:c2c6:baac%utun1")))
|
|
|
|
|
|
|
|
f(`cpu,db=db1,host=localhost,server=host\=localhost\ user\=user\ `, series("cpu", tag("db", "db1"), tag("host", "localhost"), tag("server", "host=localhost user=user ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestToFloat64_Failure(t *testing.T) {
|
|
|
|
f := func(in any) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
_, err := toFloat64(in)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error")
|
2021-01-31 23:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-12 06:59:31 +00:00
|
|
|
|
|
|
|
f("text")
|
2021-01-31 23:10:16 +00:00
|
|
|
}
|
2021-10-18 12:10:44 +00:00
|
|
|
|
2024-07-12 06:59:31 +00:00
|
|
|
func TestToFloat64_Success(t *testing.T) {
|
|
|
|
f := func(in any, resultExpected float64) {
|
2021-10-18 12:10:44 +00:00
|
|
|
t.Helper()
|
2024-07-12 06:59:31 +00:00
|
|
|
|
|
|
|
result, err := toFloat64(in)
|
2021-10-18 12:10:44 +00:00
|
|
|
if err != nil {
|
2024-07-12 06:59:31 +00:00
|
|
|
t.Fatalf("unexpected error: %s", err)
|
2021-10-18 12:10:44 +00:00
|
|
|
}
|
2024-07-12 06:59:31 +00:00
|
|
|
if result != resultExpected {
|
|
|
|
t.Fatalf("unexpected result: got %v; want %v", result, resultExpected)
|
2021-10-18 12:10:44 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-12 06:59:31 +00:00
|
|
|
|
2021-10-18 12:10:44 +00:00
|
|
|
f("123.4", 123.4)
|
|
|
|
f(float64(123.4), 123.4)
|
|
|
|
f(float32(12), 12)
|
|
|
|
f(123, 123)
|
|
|
|
f(true, 1)
|
|
|
|
f(false, 0)
|
|
|
|
f(json.Number("123456.789"), 123456.789)
|
|
|
|
}
|