github.com/go-graphite/carbonapi@v0.17.0/expr/functions/perSecond/function_test.go (about) 1 package perSecond 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 TestPerSecond(t *testing.T) { 26 now32 := int64(time.Now().Unix()) 27 28 tests := []th.EvalTestItem{ 29 { 30 "perSecond(metric1)", 31 map[parser.MetricRequest][]*types.MetricData{ 32 {Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{27, 19, math.NaN(), 10, 1, 100, 1.5, 10.20}, 1, now32)}, 33 }, 34 []*types.MetricData{types.MakeMetricData("perSecond(metric1)", []float64{math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), 99, math.NaN(), 8.7}, 1, now32).SetTag("perSecond", "1")}, 35 }, 36 { 37 "perSecond(metric1,32)", 38 map[parser.MetricRequest][]*types.MetricData{ 39 {Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{math.NaN(), 1, 2, 3, 4, 30, 0, 32, math.NaN()}, 1, now32)}, 40 }, 41 []*types.MetricData{types.MakeMetricData("perSecond(metric1,32)", []float64{math.NaN(), math.NaN(), 1, 1, 1, 26, 3, 32, math.NaN()}, 1, now32).SetTag("perSecond", "1")}, 42 }, 43 { 44 "perSecond(metric1,minValue=1)", 45 map[parser.MetricRequest][]*types.MetricData{ 46 {Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{math.NaN(), 1, 2, 3, 4, 30, 3, 32, math.NaN()}, 1, now32)}, 47 }, 48 []*types.MetricData{types.MakeMetricData("perSecond(metric1,minValue=1)", []float64{math.NaN(), math.NaN(), 1, 1, 1, 26, 2, 29, math.NaN()}, 1, now32).SetTag("perSecond", "1")}, 49 }, 50 { 51 "perSecond(metric1)", 52 map[parser.MetricRequest][]*types.MetricData{ 53 {Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{}, 1, now32)}, 54 }, 55 []*types.MetricData{types.MakeMetricData("perSecond(metric1)", []float64{}, 1, now32).SetTag("perSecond", "1")}, 56 }, 57 } 58 59 for _, tt := range tests { 60 testName := tt.Target 61 t.Run(testName, func(t *testing.T) { 62 eval := th.EvaluatorFromFunc(md[0].F) 63 th.TestEvalExpr(t, eval, &tt) 64 }) 65 } 66 67 }