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

     1  package filter
     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 TestConstantLine(t *testing.T) {
    26  	now32 := int64(time.Now().Unix())
    27  
    28  	tests := []th.EvalTestItem{
    29  		{
    30  			"filterSeries(metric[123], 'max', '>', 5)",
    31  			map[parser.MetricRequest][]*types.MetricData{
    32  				{Metric: "metric[123]", From: 0, Until: 1}: {
    33  					types.MakeMetricData("metric1", []float64{1.0, math.NaN(), 2.0, 3.0, 4.0, 5.0}, 1, now32),
    34  					types.MakeMetricData("metric2", []float64{2.0, math.NaN(), 3.0, math.NaN(), 5.0, 6.0}, 1, now32),
    35  					types.MakeMetricData("metric3", []float64{3.0, math.NaN(), 4.0, 5.0, 6.0, math.NaN()}, 1, now32),
    36  				},
    37  			},
    38  			[]*types.MetricData{
    39  				types.MakeMetricData("metric2", []float64{2.0, math.NaN(), 3.0, math.NaN(), 5.0, 6.0}, 1, now32),
    40  				types.MakeMetricData("metric3", []float64{3.0, math.NaN(), 4.0, 5.0, 6.0, math.NaN()}, 1, now32),
    41  			},
    42  		},
    43  		{
    44  			"filterSeries(metric[123], 'max', '=', 5)",
    45  			map[parser.MetricRequest][]*types.MetricData{
    46  				{Metric: "metric[123]", From: 0, Until: 1}: {
    47  					types.MakeMetricData("metric1", []float64{1.0, math.NaN(), 2.0, 3.0, 4.0, 5.0}, 1, now32),
    48  					types.MakeMetricData("metric2", []float64{2.0, math.NaN(), 3.0, math.NaN(), 5.0, 6.0}, 1, now32),
    49  					types.MakeMetricData("metric3", []float64{3.0, math.NaN(), 4.0, 5.0, 6.0, math.NaN()}, 1, now32),
    50  				},
    51  			},
    52  			[]*types.MetricData{
    53  				types.MakeMetricData("metric1", []float64{1.0, math.NaN(), 2.0, 3.0, 4.0, 5.0}, 1, now32),
    54  			},
    55  		},
    56  		{
    57  			"filterSeries(metric[123], 'max', '!=', 6)",
    58  			map[parser.MetricRequest][]*types.MetricData{
    59  				{Metric: "metric[123]", From: 0, Until: 1}: {
    60  					types.MakeMetricData("metric1", []float64{1.0, math.NaN(), 2.0, 3.0, 4.0, 5.0}, 1, now32),
    61  					types.MakeMetricData("metric2", []float64{2.0, math.NaN(), 3.0, math.NaN(), 5.0, 6.0}, 1, now32),
    62  					types.MakeMetricData("metric3", []float64{3.0, math.NaN(), 4.0, 5.0, 6.0, math.NaN()}, 1, now32),
    63  				},
    64  			},
    65  			[]*types.MetricData{
    66  				types.MakeMetricData("metric1", []float64{1.0, math.NaN(), 2.0, 3.0, 4.0, 5.0}, 1, now32),
    67  			},
    68  		},
    69  		{
    70  			"filterSeries(metric[123], 'max', '<', 6)",
    71  			map[parser.MetricRequest][]*types.MetricData{
    72  				{Metric: "metric[123]", From: 0, Until: 1}: {
    73  					types.MakeMetricData("metric1", []float64{1.0, math.NaN(), 2.0, 3.0, 4.0, 5.0}, 1, now32),
    74  					types.MakeMetricData("metric2", []float64{2.0, math.NaN(), 3.0, math.NaN(), 5.0, 6.0}, 1, now32),
    75  					types.MakeMetricData("metric3", []float64{3.0, math.NaN(), 4.0, 5.0, 6.0, math.NaN()}, 1, now32),
    76  				},
    77  			},
    78  			[]*types.MetricData{
    79  				types.MakeMetricData("metric1", []float64{1.0, math.NaN(), 2.0, 3.0, 4.0, 5.0}, 1, now32),
    80  			},
    81  		},
    82  		{
    83  			"filterSeries(metric[123], 'max', '>=', 5)",
    84  			map[parser.MetricRequest][]*types.MetricData{
    85  				{Metric: "metric[123]", From: 0, Until: 1}: {
    86  					types.MakeMetricData("metric1", []float64{1.0, math.NaN(), 2.0, 3.0, 4.0, 5.0}, 1, now32),
    87  					types.MakeMetricData("metric2", []float64{2.0, math.NaN(), 3.0, math.NaN(), 5.0, 6.0}, 1, now32),
    88  					types.MakeMetricData("metric3", []float64{3.0, math.NaN(), 4.0, 5.0, 6.0, math.NaN()}, 1, now32),
    89  				},
    90  			},
    91  			[]*types.MetricData{
    92  				types.MakeMetricData("metric1", []float64{1.0, math.NaN(), 2.0, 3.0, 4.0, 5.0}, 1, now32),
    93  				types.MakeMetricData("metric2", []float64{2.0, math.NaN(), 3.0, math.NaN(), 5.0, 6.0}, 1, now32),
    94  				types.MakeMetricData("metric3", []float64{3.0, math.NaN(), 4.0, 5.0, 6.0, math.NaN()}, 1, now32),
    95  			},
    96  		},
    97  		{
    98  			"filterSeries(metric[123], 'max', '<=', 5)",
    99  			map[parser.MetricRequest][]*types.MetricData{
   100  				{Metric: "metric[123]", From: 0, Until: 1}: {
   101  					types.MakeMetricData("metric1", []float64{1.0, math.NaN(), 2.0, 3.0, 4.0, 5.0}, 1, now32),
   102  					types.MakeMetricData("metric2", []float64{2.0, math.NaN(), 3.0, math.NaN(), 5.0, 6.0}, 1, now32),
   103  					types.MakeMetricData("metric3", []float64{3.0, math.NaN(), 4.0, 5.0, 6.0, math.NaN()}, 1, now32),
   104  				},
   105  			},
   106  			[]*types.MetricData{
   107  				types.MakeMetricData("metric1", []float64{1.0, math.NaN(), 2.0, 3.0, 4.0, 5.0}, 1, now32),
   108  			},
   109  		},
   110  	}
   111  
   112  	for _, tt := range tests {
   113  		testName := tt.Target
   114  		t.Run(testName, func(t *testing.T) {
   115  			eval := th.EvaluatorFromFunc(md[0].F)
   116  			th.TestEvalExpr(t, eval, &tt)
   117  		})
   118  	}
   119  
   120  }