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

     1  package moving_refetch
     2  
     3  import (
     4  	"math"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/go-graphite/carbonapi/expr"
     9  	"github.com/go-graphite/carbonapi/expr/functions/moving"
    10  	"github.com/go-graphite/carbonapi/expr/interfaces"
    11  	"github.com/go-graphite/carbonapi/expr/metadata"
    12  	"github.com/go-graphite/carbonapi/expr/types"
    13  	"github.com/go-graphite/carbonapi/pkg/parser"
    14  	th "github.com/go-graphite/carbonapi/tests"
    15  )
    16  
    17  var (
    18  	md []interfaces.FunctionMetadata = moving.New("")
    19  
    20  	M = map[parser.MetricRequest][]*types.MetricData{
    21  		// for refetch
    22  		{Metric: "metric*", From: 10, Until: 25}: {
    23  			types.MakeMetricData("metric1", []float64{math.NaN(), math.NaN(), 2, math.NaN(), 4, math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, 10).
    24  				SetNameTag(`movingAverage(metric1,10)`).SetPathExpression("metric*"),
    25  		},
    26  		{Metric: "metric1", From: 10, Until: 25}: {
    27  			types.MakeMetricData("metric1", []float64{math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, 10).
    28  				SetNameTag(`movingAverage(metric1,10)`).SetPathExpression("metric1"),
    29  		},
    30  	}
    31  )
    32  
    33  func init() {
    34  	for _, m := range md {
    35  		metadata.RegisterFunction(m.Name, m.F)
    36  	}
    37  }
    38  
    39  func TestMovingRefetch(t *testing.T) {
    40  	tests := []th.EvalTestItemWithRange{
    41  		{
    42  			Target: "movingAverage(metric*,10)",
    43  			M: map[parser.MetricRequest][]*types.MetricData{
    44  				{Metric: "metric*", From: 20, Until: 25}: {types.MakeMetricData("metric1", th.GenerateValues(10, 25, 1), 1, 20).SetPathExpression("metric*")},
    45  			},
    46  			Want: []*types.MetricData{types.MakeMetricData(`movingAverage(metric1,10)`,
    47  				[]float64{3, 3, 4, 4, math.NaN()}, 1, 20).SetTag("movingAverage", "10").
    48  				SetNameTag(`movingAverage(metric1,10)`).SetPathExpression("metric*"),
    49  			},
    50  			From:  20,
    51  			Until: 25,
    52  		},
    53  		{
    54  			Target: "movingAverage(metric1,10)",
    55  			M: map[parser.MetricRequest][]*types.MetricData{
    56  				{Metric: "metric1", From: 20, Until: 25}: {types.MakeMetricData("metric1", th.GenerateValues(10, 25, 1), 1, 20).SetPathExpression("metric1")},
    57  			},
    58  			Want: []*types.MetricData{types.MakeMetricData(`movingAverage(metric1,10)`,
    59  				[]float64{math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, 20).SetTag("movingAverage", "10").SetNameTag(`movingAverage(metric1,10)`)},
    60  			From:  20,
    61  			Until: 25,
    62  		},
    63  	}
    64  
    65  	for n, tt := range tests {
    66  		testName := tt.Target
    67  		t.Run(testName+"#"+strconv.Itoa(n), func(t *testing.T) {
    68  			eval, err := expr.NewEvaluator(nil, th.NewTestZipper(M), false)
    69  			if err == nil {
    70  				th.TestEvalExprWithRange(t, eval, &tt)
    71  			} else {
    72  				t.Errorf("error='%v'", err)
    73  			}
    74  		})
    75  	}
    76  }