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

     1  package removeEmptySeries
     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  			"removeEmptySeries(metric*)",
    31  			map[parser.MetricRequest][]*types.MetricData{
    32  				{Metric: "metric*", From: 0, Until: 1}: {
    33  					types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 30, math.NaN()}, 1, now32),
    34  					types.MakeMetricData("metric2", []float64{math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32),
    35  					types.MakeMetricData("metric3", []float64{0, 0, 0, 0, 0, 0, 0, 0}, 1, now32),
    36  				},
    37  			},
    38  			[]*types.MetricData{
    39  				types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 30, math.NaN()}, 1, now32).SetTag("removeEmptySeries", "0"),
    40  				types.MakeMetricData("metric3", []float64{0, 0, 0, 0, 0, 0, 0, 0}, 1, now32).SetTag("removeEmptySeries", "0"),
    41  			},
    42  		},
    43  		{
    44  			"removeZeroSeries(metric*)",
    45  			map[parser.MetricRequest][]*types.MetricData{
    46  				{Metric: "metric*", From: 0, Until: 1}: {
    47  					types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 30, math.NaN()}, 1, now32),
    48  					types.MakeMetricData("metric2", []float64{math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32),
    49  					types.MakeMetricData("metric3", []float64{0, 0, 0, 0, 0, 0, 0, 0}, 1, now32),
    50  				},
    51  			},
    52  			[]*types.MetricData{
    53  				types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 30, math.NaN()}, 1, now32).SetTag("removeZeroSeries", "0"),
    54  			},
    55  		},
    56  		{
    57  			"removeEmptySeries(metric*,0.00001)",
    58  			map[parser.MetricRequest][]*types.MetricData{
    59  				{Metric: "metric*", From: 0, Until: 1}: {
    60  					types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 30, math.NaN()}, 1, now32),
    61  					types.MakeMetricData("metric2", []float64{math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32),
    62  					types.MakeMetricData("metric3", []float64{0, 0, 0, 0, 0, 0, 0, 0}, 1, now32),
    63  				},
    64  			},
    65  			[]*types.MetricData{
    66  				types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 30, math.NaN()}, 1, now32).SetTag("removeEmptySeries", "0.00001"),
    67  				types.MakeMetricData("metric3", []float64{0, 0, 0, 0, 0, 0, 0, 0}, 1, now32).SetTag("removeEmptySeries", "0.00001"),
    68  			},
    69  		},
    70  		{
    71  			"removeZeroSeries(metric*,0.000001)",
    72  			map[parser.MetricRequest][]*types.MetricData{
    73  				{Metric: "metric*", From: 0, Until: 1}: {
    74  					types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 30, math.NaN()}, 1, now32),
    75  					types.MakeMetricData("metric2", []float64{math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32),
    76  					types.MakeMetricData("metric3", []float64{0, 0, 0, 0, 0, 0, 0, 0}, 1, now32),
    77  				},
    78  			},
    79  			[]*types.MetricData{
    80  				types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 30, math.NaN()}, 1, now32).SetTag("removeZeroSeries", "0.000001"),
    81  			},
    82  		},
    83  		{
    84  			"removeEmptySeries(metric*,0.8)",
    85  			map[parser.MetricRequest][]*types.MetricData{
    86  				{Metric: "metric*", From: 0, Until: 1}: {
    87  					types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 23, 12, 8, math.NaN()}, 1, now32),
    88  					types.MakeMetricData("metric2", []float64{1, 2, -1, 7, 8, 20, 23, 12, math.NaN(), math.NaN()}, 1, now32),
    89  					types.MakeMetricData("metric3", []float64{1, 2, -1, 7, 8, 20, 23, math.NaN(), math.NaN(), math.NaN()}, 1, now32),
    90  					types.MakeMetricData("metric4", []float64{math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32),
    91  					types.MakeMetricData("metric5", []float64{0, 0, 0, 0, 0, 0, 0, 0}, 1, now32),
    92  				},
    93  			},
    94  			[]*types.MetricData{
    95  				types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 23, 12, 8, math.NaN()}, 1, now32).SetTag("removeEmptySeries", "0.8"),
    96  				types.MakeMetricData("metric2", []float64{1, 2, -1, 7, 8, 20, 23, 12, math.NaN(), math.NaN()}, 1, now32).SetTag("removeEmptySeries", "0.8"),
    97  				types.MakeMetricData("metric5", []float64{0, 0, 0, 0, 0, 0, 0, 0}, 1, now32).SetTag("removeEmptySeries", "0.8"),
    98  			},
    99  		},
   100  		{
   101  			"removeZeroSeries(metric*,0.8)",
   102  			map[parser.MetricRequest][]*types.MetricData{
   103  				{Metric: "metric*", From: 0, Until: 1}: {
   104  					types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 23, 12, 8, math.NaN()}, 1, now32),
   105  					types.MakeMetricData("metric2", []float64{1, 2, -1, 7, 8, 20, 23, 12, math.NaN(), math.NaN()}, 1, now32),
   106  					types.MakeMetricData("metric3", []float64{1, 2, -1, 7, 8, 20, 23, math.NaN(), math.NaN(), math.NaN()}, 1, now32),
   107  					types.MakeMetricData("metric4", []float64{math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32),
   108  					types.MakeMetricData("metric5", []float64{0, 0, 0, 0, 0, 0, 0, 0}, 1, now32),
   109  				},
   110  			},
   111  			[]*types.MetricData{
   112  				types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 23, 12, 8, math.NaN()}, 1, now32).SetTag("removeZeroSeries", "0.8"),
   113  				types.MakeMetricData("metric2", []float64{1, 2, -1, 7, 8, 20, 23, 12, math.NaN(), math.NaN()}, 1, now32).SetTag("removeZeroSeries", "0.8"),
   114  			},
   115  		},
   116  		{
   117  			"removeEmptySeries(metric*,1)",
   118  			map[parser.MetricRequest][]*types.MetricData{
   119  				{Metric: "metric*", From: 0, Until: 1}: {
   120  					types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 23, 12, 8, -2.3}, 1, now32),
   121  					types.MakeMetricData("metric2", []float64{1, 2, -1, 7, 8, 20, 23, 12, 8, math.NaN()}, 1, now32),
   122  				},
   123  			},
   124  			[]*types.MetricData{
   125  				types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 23, 12, 8, -2.3}, 1, now32).SetTag("removeEmptySeries", "1"),
   126  			},
   127  		},
   128  		{
   129  			"removeZeroSeries(metric*,1)",
   130  			map[parser.MetricRequest][]*types.MetricData{
   131  				{Metric: "metric*", From: 0, Until: 1}: {
   132  					types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 23, 12, 8, -2.3}, 1, now32),
   133  					types.MakeMetricData("metric2", []float64{1, 2, -1, 7, 8, 20, 23, 12, 8, 0}, 1, now32),
   134  				},
   135  			},
   136  			[]*types.MetricData{
   137  				types.MakeMetricData("metric1", []float64{1, 2, -1, 7, 8, 20, 23, 12, 8, -2.3}, 1, now32).SetTag("removeZeroSeries", "1"),
   138  			},
   139  		},
   140  		{
   141  			"removeEmptySeries(metric*,0.5)", // Verify that passing in empty series with an xFilesFactor does not result in an error
   142  			nil,
   143  			[]*types.MetricData{},
   144  		},
   145  		{
   146  			"removeZeroSeries(metric*,0.5)", // Verify that passing in empty series with an xFilesFactor does not result in an error
   147  			nil,
   148  			[]*types.MetricData{},
   149  		},
   150  	}
   151  
   152  	for _, tt := range tests {
   153  		testName := tt.Target
   154  		t.Run(testName, func(t *testing.T) {
   155  			eval := th.EvaluatorFromFunc(md[0].F)
   156  			th.TestEvalExpr(t, eval, &tt)
   157  		})
   158  	}
   159  
   160  }