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

     1  package fallbackSeries
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/go-graphite/carbonapi/expr/interfaces"
     8  	"github.com/go-graphite/carbonapi/expr/metadata"
     9  	"github.com/go-graphite/carbonapi/expr/types"
    10  	"github.com/go-graphite/carbonapi/pkg/parser"
    11  	th "github.com/go-graphite/carbonapi/tests"
    12  )
    13  
    14  var (
    15  	md []interfaces.FunctionMetadata = New("")
    16  )
    17  
    18  func init() {
    19  	for _, m := range md {
    20  		metadata.RegisterFunction(m.Name, m.F)
    21  	}
    22  }
    23  
    24  func TestFallbackSeries(t *testing.T) {
    25  	now32 := int64(time.Now().Unix())
    26  
    27  	tests := []th.EvalTestItem{
    28  		{
    29  			"fallbackSeries(metric*,fallbackmetric)",
    30  			map[parser.MetricRequest][]*types.MetricData{
    31  				{Metric: "metric1", From: 0, Until: 1}:        {types.MakeMetricData("metric1", []float64{0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9}, 1, now32)},
    32  				{Metric: "fallbackmetric", From: 0, Until: 1}: {types.MakeMetricData("fallbackmetric", []float64{0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7}, 1, now32)},
    33  			},
    34  			[]*types.MetricData{
    35  				types.MakeMetricData("fallbackmetric", []float64{0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7}, 1, now32),
    36  			},
    37  		},
    38  		{
    39  			"fallbackSeries(metric1,metrc2)",
    40  			map[parser.MetricRequest][]*types.MetricData{
    41  				{Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9}, 1, now32)},
    42  				{Metric: "metric2", From: 0, Until: 1}: {types.MakeMetricData("metric2", []float64{0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7}, 1, now32)},
    43  			},
    44  			[]*types.MetricData{
    45  				types.MakeMetricData("metric1", []float64{0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9}, 1, now32),
    46  			},
    47  		},
    48  		{
    49  			"fallbackSeries(absentmetric,fallbackmetric)",
    50  			map[parser.MetricRequest][]*types.MetricData{
    51  				{Metric: "metric1", From: 0, Until: 1}:        {types.MakeMetricData("metric1", []float64{0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9}, 1, now32)},
    52  				{Metric: "fallbackmetric", From: 0, Until: 1}: {types.MakeMetricData("fallbackmetric", []float64{0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7}, 1, now32)},
    53  			},
    54  			[]*types.MetricData{
    55  				types.MakeMetricData("fallbackmetric", []float64{0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7}, 1, now32),
    56  			},
    57  		},
    58  		{
    59  			"fallbackSeries(metric1,metrc2)",
    60  			map[parser.MetricRequest][]*types.MetricData{
    61  				{Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9}, 1, now32)},
    62  			},
    63  			[]*types.MetricData{
    64  				types.MakeMetricData("metric1", []float64{0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9}, 1, now32),
    65  			},
    66  		},
    67  	}
    68  
    69  	for _, tt := range tests {
    70  		testName := tt.Target
    71  		t.Run(testName, func(t *testing.T) {
    72  			eval := th.EvaluatorFromFunc(md[0].F)
    73  			th.TestEvalExpr(t, eval, &tt)
    74  		})
    75  	}
    76  
    77  }
    78  
    79  func TestErrorMissingTimeSeriesFunction(t *testing.T) {
    80  	now32 := int64(time.Now().Unix())
    81  
    82  	tests := []th.EvalTestItemWithError{
    83  		{
    84  			"fallbackSeries(metric*)",
    85  			map[parser.MetricRequest][]*types.MetricData{
    86  				{Metric: "metric*", From: 0, Until: 1}: {
    87  					types.MakeMetricData("metricA", []float64{0, 0, 0, 0, 0, 0}, 1, now32),
    88  					types.MakeMetricData("metricB", []float64{4, 4, 5, 5, 6, 6}, 1, now32),
    89  					types.MakeMetricData("metricC", []float64{3, 4, 5, 6, 7, 8}, 1, now32),
    90  				},
    91  			},
    92  			nil,
    93  			parser.ErrMissingTimeseries,
    94  		},
    95  	}
    96  
    97  	for _, testCase := range tests {
    98  		testName := testCase.Target
    99  		t.Run(testName, func(t *testing.T) {
   100  			eval := th.EvaluatorFromFunc(md[0].F)
   101  			th.TestEvalExprWithError(t, eval, &testCase)
   102  		})
   103  	}
   104  }