github.com/go-graphite/carbonapi@v0.17.0/expr/functions/transformNull/function_test.go (about) 1 package transformNull 2 3 import ( 4 "math" 5 "testing" 6 "time" 7 8 "github.com/go-graphite/carbonapi/expr/interfaces" 9 "github.com/go-graphite/carbonapi/expr/metadata" 10 "github.com/go-graphite/carbonapi/expr/types" 11 "github.com/go-graphite/carbonapi/pkg/parser" 12 th "github.com/go-graphite/carbonapi/tests" 13 ) 14 15 var ( 16 md []interfaces.FunctionMetadata = New("") 17 ) 18 19 func init() { 20 for _, m := range md { 21 metadata.RegisterFunction(m.Name, m.F) 22 } 23 } 24 25 func TestTransformNull(t *testing.T) { 26 now := time.Now().Unix() 27 28 tests := []th.EvalTestItem{ 29 { 30 `transformNull(metric1)`, 31 map[parser.MetricRequest][]*types.MetricData{ 32 {Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{1, math.NaN(), math.NaN(), 3, 4, 12}, 1, now)}, 33 }, 34 []*types.MetricData{types.MakeMetricData("transformNull(metric1)", 35 []float64{1, 0, 0, 3, 4, 12}, 1, now).SetTag("transformNull", "0").SetNameTag("metric1")}, 36 }, 37 { 38 `transformNull(metric1, default=5)`, 39 map[parser.MetricRequest][]*types.MetricData{ 40 {Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{1, math.NaN(), math.NaN(), 3, 4, 12}, 1, now)}, 41 }, 42 []*types.MetricData{types.MakeMetricData("transformNull(metric1,5)", 43 []float64{1, 5, 5, 3, 4, 12}, 1, now).SetTag("transformNull", "5").SetNameTag("metric1")}, 44 }, 45 { 46 `transformNull(metric1, default=5, referenceSeries=metric2.*)`, 47 map[parser.MetricRequest][]*types.MetricData{ 48 {Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{1, math.NaN(), math.NaN(), math.NaN(), 4, 12}, 1, now)}, 49 {Metric: "metric2.*", From: 0, Until: 1}: { 50 types.MakeMetricData("metric2.foo", []float64{math.NaN(), 3, math.NaN(), 3, math.NaN(), 12}, 1, now), 51 types.MakeMetricData("metric2.bar", []float64{1, math.NaN(), math.NaN(), 3, 4, 12}, 1, now)}, 52 }, 53 []*types.MetricData{types.MakeMetricData("transformNull(metric1,5)", 54 []float64{1, 5, math.NaN(), 5, 4, 12}, 1, now).SetTag("transformNull", "5").SetNameTag("metric1")}, 55 }, 56 { 57 `transformNull(metric1, default=5, defaultOnAbsent=True)`, 58 map[parser.MetricRequest][]*types.MetricData{}, 59 []*types.MetricData{types.MakeMetricData("transformNull(metric1, default=5, defaultOnAbsent=True)", 60 []float64{5, 5}, 1, 0).SetTag("transformNull", "5").SetNameTag(`metric1`)}, 61 }, 62 // tagged metric 63 { 64 `transformNull(seriesByTag('name=metric1'), 0)`, 65 map[parser.MetricRequest][]*types.MetricData{ 66 {Metric: "seriesByTag('name=metric1')", From: 0, Until: 1}: {types.MakeMetricData("metric1;env=prod", []float64{1, math.NaN(), math.NaN(), 3, 4, 12}, 1, now)}, 67 }, 68 []*types.MetricData{types.MakeMetricData("transformNull(metric1;env=prod,0)", 69 []float64{1, 0, 0, 3, 4, 12}, 1, now).SetTag("transformNull", "0").SetNameTag("metric1").SetTag("env", "prod")}, 70 }, 71 } 72 73 for _, tt := range tests { 74 testName := tt.Target 75 t.Run(testName, func(t *testing.T) { 76 eval := th.EvaluatorFromFunc(md[0].F) 77 th.TestEvalExpr(t, eval, &tt) 78 }) 79 } 80 }