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

     1  package offset
     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  			"offset(metric1,10)",
    31  			map[parser.MetricRequest][]*types.MetricData{
    32  				{Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{93, 94, 95, math.NaN(), 97, 98, 99, 100, 101}, 1, now32)},
    33  			},
    34  			[]*types.MetricData{types.MakeMetricData("offset(metric1,10)",
    35  				[]float64{103, 104, 105, math.NaN(), 107, 108, 109, 110, 111}, 1, now32).SetTag("offset", "10")},
    36  		},
    37  		{
    38  			"offset(metric1,'10')", // Verify string input can be parsed into int or float
    39  			map[parser.MetricRequest][]*types.MetricData{
    40  				{Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{93, 94, 95, math.NaN(), 97, 98, 99, 100, 101}, 1, now32)},
    41  			},
    42  			[]*types.MetricData{types.MakeMetricData("offset(metric1,10)",
    43  				[]float64{103, 104, 105, math.NaN(), 107, 108, 109, 110, 111}, 1, now32).SetTag("offset", "10")},
    44  		},
    45  		{
    46  			"add(metric*,-10)",
    47  			map[parser.MetricRequest][]*types.MetricData{
    48  				{Metric: "metric*", From: 0, Until: 1}: {
    49  					types.MakeMetricData("metric1", []float64{93, 94, 95, math.NaN(), 97, 98, 99, 100, 101}, 1, now32),
    50  					types.MakeMetricData("metric2", []float64{193, 194, 195, math.NaN(), 197, 198, 199, 200, 201}, 1, now32),
    51  				},
    52  			},
    53  			[]*types.MetricData{
    54  				types.MakeMetricData("add(metric1,-10)", []float64{83, 84, 85, math.NaN(), 87, 88, 89, 90, 91}, 1, now32).SetTag("add", "-10"),
    55  				types.MakeMetricData("add(metric2,-10)", []float64{183, 184, 185, math.NaN(), 187, 188, 189, 190, 191}, 1, now32).SetTag("add", "-10"),
    56  			},
    57  		},
    58  		{
    59  			"add(metric*,'-10')", // Verify string input can be parsed into int or float
    60  			map[parser.MetricRequest][]*types.MetricData{
    61  				{Metric: "metric*", From: 0, Until: 1}: {
    62  					types.MakeMetricData("metric1", []float64{93, 94, 95, math.NaN(), 97, 98, 99, 100, 101}, 1, now32),
    63  					types.MakeMetricData("metric2", []float64{193, 194, 195, math.NaN(), 197, 198, 199, 200, 201}, 1, now32),
    64  				},
    65  			},
    66  			[]*types.MetricData{
    67  				types.MakeMetricData("add(metric1,-10)", []float64{83, 84, 85, math.NaN(), 87, 88, 89, 90, 91}, 1, now32).SetTag("add", "-10"),
    68  				types.MakeMetricData("add(metric2,-10)", []float64{183, 184, 185, math.NaN(), 187, 188, 189, 190, 191}, 1, now32).SetTag("add", "-10"),
    69  			},
    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  
    81  }