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

     1  package integralWithReset
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"math"
     8  
     9  	"github.com/go-graphite/carbonapi/expr/interfaces"
    10  	"github.com/go-graphite/carbonapi/expr/metadata"
    11  	"github.com/go-graphite/carbonapi/expr/types"
    12  	"github.com/go-graphite/carbonapi/pkg/parser"
    13  	th "github.com/go-graphite/carbonapi/tests"
    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 TestIntegralWithResetMultiReturn(t *testing.T) {
    27  	now32 := time.Now().Unix()
    28  
    29  	tests := []th.MultiReturnEvalTestItem{
    30  		{
    31  			"integralWithReset(metric[12], reset)",
    32  			map[parser.MetricRequest][]*types.MetricData{
    33  				{Metric: "metric[12]", From: 0, Until: 1}: {
    34  					types.MakeMetricData("metric1", []float64{1, 1, 3, 5, 8, 13, 21}, 1, now32),
    35  					types.MakeMetricData("metric2", []float64{1, 1, 1, 1, 1, 1, 1}, 1, now32),
    36  				},
    37  				{Metric: "reset", From: 0, Until: 1}: {
    38  					types.MakeMetricData("reset", []float64{0, 0, 0, 1, 1, 0, 0}, 1, now32),
    39  				},
    40  			},
    41  			"integralWithReset",
    42  			map[string][]*types.MetricData{
    43  				"integralWithReset(metric1,reset)": {types.MakeMetricData(
    44  					"integralWithReset(metric1,reset)",
    45  					[]float64{1, 2, 5, 0, 0, 13, 34},
    46  					1, now32,
    47  				)},
    48  				"integralWithReset(metric2,reset)": {types.MakeMetricData(
    49  					"integralWithReset(metric2,reset)",
    50  					[]float64{1, 2, 3, 0, 0, 1, 2},
    51  					1, now32,
    52  				)},
    53  			},
    54  		},
    55  	}
    56  
    57  	for _, tt := range tests {
    58  		testName := tt.Target
    59  		t.Run(testName, func(t *testing.T) {
    60  			eval := th.EvaluatorFromFunc(md[0].F)
    61  			th.TestMultiReturnEvalExpr(t, eval, &tt)
    62  		})
    63  	}
    64  
    65  }
    66  
    67  func TestIntegralWithReset(t *testing.T) {
    68  	now32 := time.Now().Unix()
    69  
    70  	tests := []th.EvalTestItem{
    71  		{
    72  			"integralWithReset(metric1, metric2)",
    73  			map[parser.MetricRequest][]*types.MetricData{
    74  				{Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{1, math.NaN(), math.NaN(), 3, 4, 12, 15}, 1, now32)},
    75  				{Metric: "metric2", From: 0, Until: 1}: {types.MakeMetricData("metric2", []float64{0, math.NaN(), 0, math.NaN(), 0, 6, 0}, 1, now32)},
    76  			},
    77  			[]*types.MetricData{types.MakeMetricData("integralWithReset(metric1,metric2)",
    78  				[]float64{1, math.NaN(), math.NaN(), 4, 8, 0, 15}, 1, now32)},
    79  		},
    80  	}
    81  
    82  	for _, tt := range tests {
    83  		testName := tt.Target
    84  		t.Run(testName, func(t *testing.T) {
    85  			eval := th.EvaluatorFromFunc(md[0].F)
    86  			th.TestEvalExpr(t, eval, &tt)
    87  		})
    88  	}
    89  
    90  }