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

     1  package interpolate
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  	"time"
     7  
     8  	th "github.com/go-graphite/carbonapi/tests"
     9  
    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  )
    15  
    16  var (
    17  	md []interfaces.FunctionMetadata = New("")
    18  )
    19  
    20  func init() {
    21  	for _, m := range md {
    22  		metadata.RegisterFunction(m.Name, m.F)
    23  	}
    24  }
    25  
    26  func TestInterpolate_Do(t *testing.T) {
    27  	nan := math.NaN()
    28  	now32 := time.Now().Unix()
    29  
    30  	testCases := []th.EvalTestItem{
    31  		{
    32  			"interpolate(x1.y1.z1)",
    33  			map[parser.MetricRequest][]*types.MetricData{
    34  				parser.MetricRequest{
    35  					Metric: "x1.y1.z1",
    36  					From:   0,
    37  					Until:  1,
    38  				}: {
    39  					types.MakeMetricData(
    40  						"x1.y1.z1",
    41  						[]float64{1, 2, 3, 4, nan, nan, nan, 6, 7, 8},
    42  						1,
    43  						now32,
    44  					),
    45  				},
    46  			},
    47  			[]*types.MetricData{
    48  				types.MakeMetricData(
    49  					"interpolate(x1.y1.z1)",
    50  					[]float64{1, 2, 3, 4, 4.5, 5, 5.5, 6, 7, 8},
    51  					1,
    52  					now32,
    53  				),
    54  			},
    55  		},
    56  		{
    57  			"interpolate(x1.y1.z1)",
    58  			map[parser.MetricRequest][]*types.MetricData{
    59  				parser.MetricRequest{
    60  					Metric: "x1.y1.z1",
    61  					From:   0,
    62  					Until:  1,
    63  				}: {
    64  					types.MakeMetricData(
    65  						"x1.y1.z1",
    66  						[]float64{1, 2, 3, 4, 5, nan, nan, 8, 9, 10},
    67  						1,
    68  						now32,
    69  					),
    70  				},
    71  			},
    72  			[]*types.MetricData{
    73  				types.MakeMetricData(
    74  					"interpolate(x1.y1.z1)",
    75  					[]float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
    76  					1,
    77  					now32,
    78  				),
    79  			},
    80  		},
    81  		{
    82  			"interpolate(x1.y1.z1, 2)",
    83  			map[parser.MetricRequest][]*types.MetricData{
    84  				parser.MetricRequest{
    85  					Metric: "x1.y1.z1",
    86  					From:   0,
    87  					Until:  1,
    88  				}: {
    89  					types.MakeMetricData(
    90  						"x1.y1.z1",
    91  						[]float64{1, 2, 3, 4, nan, nan, nan, 6, 7, 8},
    92  						1,
    93  						now32,
    94  					),
    95  				},
    96  			},
    97  			[]*types.MetricData{
    98  				types.MakeMetricData(
    99  					"interpolate(x1.y1.z1)",
   100  					[]float64{1, 2, 3, 4, nan, nan, nan, 6, 7, 8}, // limit is 2 gaps, have 3, cannot interpolate
   101  					1,
   102  					now32,
   103  				),
   104  			},
   105  		},
   106  		{
   107  			"interpolate(x1.y1.z1)",
   108  			map[parser.MetricRequest][]*types.MetricData{
   109  				parser.MetricRequest{
   110  					Metric: "x1.y1.z1",
   111  					From:   0,
   112  					Until:  1,
   113  				}: {
   114  					types.MakeMetricData(
   115  						"x1.y1.z1",
   116  						[]float64{nan, nan, nan, 1, 2, 3},
   117  						1,
   118  						now32,
   119  					),
   120  				},
   121  			},
   122  			[]*types.MetricData{
   123  				types.MakeMetricData(
   124  					"interpolate(x1.y1.z1)",
   125  					[]float64{nan, nan, nan, 1, 2, 3}, // there are not values before the gap
   126  					1,
   127  					now32,
   128  				),
   129  			},
   130  		},
   131  		{
   132  			"interpolate(x1.y1.z1, inf)",
   133  			map[parser.MetricRequest][]*types.MetricData{
   134  				parser.MetricRequest{
   135  					Metric: "x1.y1.z1",
   136  					From:   0,
   137  					Until:  1,
   138  				}: {
   139  					types.MakeMetricData(
   140  						"x1.y1.z1",
   141  						[]float64{1, 2, 3, 4, nan, nan, nan, 6, 7, 8},
   142  						1,
   143  						now32,
   144  					),
   145  				},
   146  			},
   147  			[]*types.MetricData{
   148  				types.MakeMetricData(
   149  					"interpolate(x1.y1.z1)",
   150  					[]float64{1, 2, 3, 4, 4.5, 5, 5.5, 6, 7, 8},
   151  					1,
   152  					now32,
   153  				),
   154  			},
   155  		},
   156  	}
   157  
   158  	for _, testCase := range testCases {
   159  		testName := testCase.Target
   160  		t.Run(testName, func(t *testing.T) {
   161  			eval := th.EvaluatorFromFunc(md[0].F)
   162  			th.TestEvalExpr(t, eval, &testCase)
   163  		})
   164  	}
   165  }