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

     1  package pearsonClosest
     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  			"pearsonClosest(metric1,metric2,1,direction=\"abs\")",
    31  			map[parser.MetricRequest][]*types.MetricData{
    32  				{Metric: "metric1", From: 0, Until: 1}: {
    33  					types.MakeMetricData("metricX", []float64{3, 4, 5, 6, 7, 8}, 1, now32),
    34  				},
    35  				{Metric: "metric2", From: 0, Until: 1}: {
    36  					types.MakeMetricData("metricA", []float64{0, 0, 0, 0, 0, 0}, 1, now32),
    37  					types.MakeMetricData("metricB", []float64{3, math.NaN(), 5, 6, 7, 8}, 1, now32),
    38  					types.MakeMetricData("metricC", []float64{4, 4, 5, 5, 6, 6}, 1, now32),
    39  				},
    40  			},
    41  			[]*types.MetricData{types.MakeMetricData("metricB",
    42  				[]float64{3, math.NaN(), 5, 6, 7, 8}, 1, now32)},
    43  		},
    44  	}
    45  
    46  	for _, tt := range tests {
    47  		testName := tt.Target
    48  		t.Run(testName, func(t *testing.T) {
    49  			eval := th.EvaluatorFromFunc(md[0].F)
    50  			th.TestEvalExpr(t, eval, &tt)
    51  		})
    52  	}
    53  
    54  }
    55  
    56  func TestFunctionMultiReturn(t *testing.T) {
    57  	now32 := int64(time.Now().Unix())
    58  
    59  	tests := []th.MultiReturnEvalTestItem{
    60  		{
    61  			"pearsonClosest(metricC,metric*,2)",
    62  			map[parser.MetricRequest][]*types.MetricData{
    63  				{Metric: "metric*", From: 0, Until: 1}: {
    64  					types.MakeMetricData("metricA", []float64{0, 0, 0, 0, 0, 0}, 1, now32),
    65  					types.MakeMetricData("metricB", []float64{3, 4, 5, 6, 7, 8}, 1, now32),
    66  					types.MakeMetricData("metricC", []float64{4, 4, 5, 5, 6, 6}, 1, now32),
    67  					types.MakeMetricData("metricD", []float64{4, 4, 5, 5, 6, 6}, 1, now32),
    68  					types.MakeMetricData("metricE", []float64{4, 7, 7, 7, 7, 1}, 1, now32),
    69  				},
    70  				{Metric: "metricC", From: 0, Until: 1}: {
    71  					types.MakeMetricData("metricC", []float64{4, 4, 5, 5, 6, 6}, 1, now32),
    72  				},
    73  			},
    74  			"pearsonClosest",
    75  			map[string][]*types.MetricData{
    76  				"metricC": {types.MakeMetricData("metricC", []float64{4, 4, 5, 5, 6, 6}, 1, now32)},
    77  				"metricD": {types.MakeMetricData("metricD", []float64{4, 4, 5, 5, 6, 6}, 1, now32)},
    78  			},
    79  		},
    80  		{
    81  			"pearsonClosest(metricC,metric*,3)",
    82  			map[parser.MetricRequest][]*types.MetricData{
    83  				{Metric: "metric*", From: 0, Until: 1}: {
    84  					types.MakeMetricData("metricA", []float64{0, 0, 0, 0, 0, 0}, 1, now32),
    85  					types.MakeMetricData("metricB", []float64{3, 4, 5, 6, 7, 8}, 1, now32),
    86  					types.MakeMetricData("metricC", []float64{4, 4, 5, 5, 6, 6}, 1, now32),
    87  					types.MakeMetricData("metricD", []float64{4, 4, 5, 5, 6, 6}, 1, now32),
    88  					types.MakeMetricData("metricE", []float64{4, 7, 7, 7, 7, 1}, 1, now32),
    89  				},
    90  				{Metric: "metricC", From: 0, Until: 1}: {
    91  					types.MakeMetricData("metricC", []float64{4, 4, 5, 5, 6, 6}, 1, now32),
    92  				},
    93  			},
    94  			"pearsonClosest",
    95  			map[string][]*types.MetricData{
    96  				"metricB": {types.MakeMetricData("metricB", []float64{3, 4, 5, 6, 7, 8}, 1, now32)},
    97  				"metricC": {types.MakeMetricData("metricC", []float64{4, 4, 5, 5, 6, 6}, 1, now32)},
    98  				"metricD": {types.MakeMetricData("metricD", []float64{4, 4, 5, 5, 6, 6}, 1, now32)},
    99  			},
   100  		},
   101  	}
   102  
   103  	for _, tt := range tests {
   104  		testName := tt.Target
   105  		t.Run(testName, func(t *testing.T) {
   106  			eval := th.EvaluatorFromFunc(md[0].F)
   107  			th.TestMultiReturnEvalExpr(t, eval, &tt)
   108  		})
   109  	}
   110  
   111  }