github.com/go-graphite/carbonapi@v0.17.0/expr/functions/removeBelowSeries/function_test.go (about)

     1  package removeBelowSeries
     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 TestFunction(t *testing.T) {
    26  	now32 := int64(time.Now().Unix())
    27  
    28  	tests := []th.EvalTestItem{
    29  		{
    30  			"removeBelowValue(metric1, 0)",
    31  			map[parser.MetricRequest][]*types.MetricData{
    32  				{Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 30, math.NaN()}, 1, now32)},
    33  			},
    34  			[]*types.MetricData{types.MakeMetricData("removeBelowValue(metric1, 0)",
    35  				[]float64{1, 2, math.NaN(), 7, 8, 20, 30, math.NaN()}, 1, now32).SetTag("removeBelowSeries", "0")},
    36  		},
    37  		{
    38  			"removeAboveValue(metric1, 10)",
    39  			map[parser.MetricRequest][]*types.MetricData{
    40  				{Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 30, math.NaN()}, 1, now32)},
    41  			},
    42  			[]*types.MetricData{types.MakeMetricData("removeAboveValue(metric1, 10)",
    43  				[]float64{1, 2, -1, 7, 8, math.NaN(), math.NaN(), math.NaN()}, 1, now32).SetTag("removeBelowSeries", "10")},
    44  		},
    45  		{
    46  			"removeBelowPercentile(metric1, 50)",
    47  			map[parser.MetricRequest][]*types.MetricData{
    48  				{Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 30, math.NaN()}, 1, now32)},
    49  			},
    50  			[]*types.MetricData{types.MakeMetricData("removeBelowPercentile(metric1, 50)",
    51  				[]float64{math.NaN(), math.NaN(), math.NaN(), 7, 8, 20, 30, math.NaN()}, 1, now32).SetTag("removeBelowSeries", "7")},
    52  		},
    53  		{
    54  			"removeAbovePercentile(metric1, 50)",
    55  			map[parser.MetricRequest][]*types.MetricData{
    56  				{Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 30, math.NaN()}, 1, now32)},
    57  			},
    58  			[]*types.MetricData{types.MakeMetricData("removeAbovePercentile(metric1, 50)",
    59  				[]float64{1, 2, -1, 7, math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32).SetTag("removeBelowSeries", "7")},
    60  		},
    61  	}
    62  
    63  	for _, tt := range tests {
    64  		testName := tt.Target
    65  		t.Run(testName, func(t *testing.T) {
    66  			eval := th.EvaluatorFromFunc(md[0].F)
    67  			th.TestEvalExpr(t, eval, &tt)
    68  		})
    69  	}
    70  
    71  }