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

     1  package polyfit
     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  			"polyfit(metric1,3)",
    31  			map[parser.MetricRequest][]*types.MetricData{
    32  				{Metric: "metric1", From: 0, Until: 1}: {
    33  					types.MakeMetricData("metric1",
    34  						[]float64{math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32),
    35  				},
    36  			},
    37  			[]*types.MetricData{
    38  				types.MakeMetricData("polyfit(metric1,3)",
    39  					[]float64{math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN()}, 1, now32),
    40  			},
    41  		},
    42  		{
    43  			"polyfit(metric1)",
    44  			map[parser.MetricRequest][]*types.MetricData{
    45  				{Metric: "metric1", From: 0, Until: 1}: {
    46  					types.MakeMetricData("metric1",
    47  						[]float64{7.79, 7.7, 7.92, 5.25, 6.24, 7.25, 7.15, 8.56, 7.82, 8.52}, 1, now32),
    48  				},
    49  			},
    50  			[]*types.MetricData{
    51  				types.MakeMetricData("polyfit(metric1)",
    52  					[]float64{6.94763636364, 7.05260606061, 7.15757575758, 7.26254545455, 7.36751515152,
    53  						7.47248484848, 7.57745454545, 7.68242424242, 7.78739393939, 7.89236363636}, 1, now32),
    54  			},
    55  		},
    56  		{
    57  			"polyfit(metric1,2)",
    58  			map[parser.MetricRequest][]*types.MetricData{
    59  				{Metric: "metric1", From: 0, Until: 1}: {
    60  					types.MakeMetricData("metric1",
    61  						[]float64{7.79, 7.7, 7.92, 5.25, 6.24, math.NaN(), 7.15, 8.56, 7.82, 8.52}, 1, now32),
    62  				},
    63  			},
    64  			[]*types.MetricData{
    65  				types.MakeMetricData("polyfit(metric1,2)",
    66  					[]float64{7.9733096590909085, 7.364842329545457, 6.933910511363642, 6.680514204545464, 6.604653409090922,
    67  						6.706328125000017, 6.985538352272748, 7.442284090909116, 8.07656534090912, 8.888382102272761}, 1, now32),
    68  			},
    69  		},
    70  		{
    71  			"polyfit(metric1,3,'5sec')",
    72  			map[parser.MetricRequest][]*types.MetricData{
    73  				{Metric: "metric1", From: 0, Until: 1}: {
    74  					types.MakeMetricData("metric1",
    75  						[]float64{7.79, 7.7, 7.92, 5.25, 6.24, 7.25, 7.15, 8.56, 7.82, 8.52}, 1, now32),
    76  				},
    77  			},
    78  			[]*types.MetricData{
    79  				types.MakeMetricData("polyfit(metric1,3,'5sec')",
    80  					[]float64{8.22944055944, 7.26958041958, 6.73364801865, 6.54653846154, 6.63314685315,
    81  						6.91836829837, 7.3270979021, 7.78423076923, 8.21466200466, 8.54328671329,
    82  						8.695, 8.5946969697, 8.16727272727, 7.33762237762, 6.03064102564}, 1, now32),
    83  			},
    84  		},
    85  	}
    86  
    87  	for _, tt := range tests {
    88  		testName := tt.Target
    89  		t.Run(testName, func(t *testing.T) {
    90  			eval := th.EvaluatorFromFunc(md[0].F)
    91  			th.TestEvalExpr(t, eval, &tt)
    92  		})
    93  	}
    94  
    95  }