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

     1  package ewma
     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 TestEWMA(t *testing.T) {
    26  	now32 := int64(time.Now().Unix())
    27  
    28  	tests := []th.EvalTestItem{
    29  		{
    30  			"ewma(metric1,0.9)",
    31  			map[parser.MetricRequest][]*types.MetricData{
    32  				{Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{0, 1, 1, 1, math.NaN(), 1, 1}, 1, now32)},
    33  			},
    34  			[]*types.MetricData{
    35  				types.MakeMetricData("ewma(metric1,0.9)", []float64{0, 0.9, 0.99, 0.999, math.NaN(), 0.9999, 0.99999}, 1, now32),
    36  			},
    37  		},
    38  		{
    39  			"exponentialWeightedMovingAverage(metric1,0.9)",
    40  			map[parser.MetricRequest][]*types.MetricData{
    41  				{Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{0, 1, 1, 1, math.NaN(), 1, 1}, 1, now32)},
    42  			},
    43  			[]*types.MetricData{
    44  				types.MakeMetricData("ewma(metric1,0.9)", []float64{0, 0.9, 0.99, 0.999, math.NaN(), 0.9999, 0.99999}, 1, now32),
    45  			},
    46  		},
    47  	}
    48  
    49  	for _, tt := range tests {
    50  		testName := tt.Target
    51  		t.Run(testName, func(t *testing.T) {
    52  			eval := th.EvaluatorFromFunc(md[0].F)
    53  			th.TestEvalExpr(t, eval, &tt)
    54  		})
    55  	}
    56  
    57  }