github.com/go-graphite/carbonapi@v0.17.0/expr/functions/round/function_test.go (about) 1 package round 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 TestRound(t *testing.T) { 26 now32 := int64(time.Now().Unix()) 27 28 tests := []th.EvalTestItem{ 29 { 30 "round(metric1, 3)", 31 map[parser.MetricRequest][]*types.MetricData{ 32 {Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{0.5, 2.298, math.NaN(), 91.019, -524.82, 245}, 1, now32)}, 33 }, 34 []*types.MetricData{types.MakeMetricData("round(metric1,3)", []float64{0.5, 2.298, math.NaN(), 91.019, -524.82, 245}, 1, now32).SetTag("round", "3")}, 35 }, 36 { 37 "round(metric1, 1)", 38 map[parser.MetricRequest][]*types.MetricData{ 39 {Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{0.5, 2.298, math.NaN(), 91.019, -524.82, 245}, 1, now32)}, 40 }, 41 []*types.MetricData{types.MakeMetricData("round(metric1,1)", []float64{0.5, 2.3, math.NaN(), 91.0, -524.8, 245}, 1, now32).SetTag("round", "1")}, 42 }, 43 { 44 "round(metric1)", 45 map[parser.MetricRequest][]*types.MetricData{ 46 {Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{0.5, 1.5, 2.298, math.NaN(), 91.019, -524.82, 245}, 1, now32)}, 47 }, 48 []*types.MetricData{types.MakeMetricData("round(metric1)", []float64{0, 2, 2, math.NaN(), 91, -525, 245}, 1, now32).SetTag("round", "0")}, 49 }, 50 { 51 "round(metric1, -2)", 52 map[parser.MetricRequest][]*types.MetricData{ 53 {Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{0.5, 2.298, math.NaN(), 91.019, -524.82, 275}, 1, now32)}, 54 }, 55 []*types.MetricData{types.MakeMetricData("round(metric1,-2)", []float64{0, 0, math.NaN(), 100, -500, 300}, 1, now32).SetTag("round", "-2")}, 56 }, 57 { 58 "round(metric1, precision=-2)", 59 map[parser.MetricRequest][]*types.MetricData{ 60 {Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{0.5, 2.298, math.NaN(), 91.019, -524.82, 275}, 1, now32)}, 61 }, 62 []*types.MetricData{types.MakeMetricData("round(metric1,-2)", []float64{0, 0, math.NaN(), 100, -500, 300}, 1, now32).SetTag("round", "-2")}, 63 }, 64 { 65 "round(metric1, -10)", 66 map[parser.MetricRequest][]*types.MetricData{ 67 {Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{0.5, 2.298, math.NaN(), 91.019, -524.82, 245}, 1, now32)}, 68 }, 69 []*types.MetricData{types.MakeMetricData("round(metric1,-10)", []float64{0, 0, math.NaN(), 0, 0, 0}, 1, now32).SetTag("round", "-10")}, 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 }