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

     1  package scale
     2  
     3  import (
     4  	"fmt"
     5  	"math"
     6  	"testing"
     7  	"time"
     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 TestFunction(t *testing.T) {
    27  	now32 := int64(time.Now().Unix())
    28  
    29  	tests := []th.EvalTestItem{
    30  		{
    31  			"scale(metric1,2.5)",
    32  			map[parser.MetricRequest][]*types.MetricData{
    33  				{Metric: "metric1", From: 0, Until: 1}: {types.MakeMetricData("metric1", []float64{1, 2, math.NaN(), 4, 5}, 1, now32)},
    34  			},
    35  			[]*types.MetricData{types.MakeMetricData("scale(metric1,2.5)", []float64{2.5, 5.0, math.NaN(), 10.0, 12.5}, 1, now32).SetTag("scale", "2.5")},
    36  		},
    37  		{
    38  			fmt.Sprintf("scale(x.y.z, -2.5, %d)", int(now32+14)),
    39  			map[parser.MetricRequest][]*types.MetricData{
    40  				parser.MetricRequest{
    41  					Metric: "x.y.z",
    42  					From:   0,
    43  					Until:  1,
    44  				}: {
    45  					types.MakeMetricData(
    46  						"x.y.z",
    47  						[]float64{1, -2, -3, 4, math.NaN(), 0, math.NaN(), 5, 6},
    48  						5,
    49  						now32,
    50  					),
    51  				},
    52  			},
    53  			[]*types.MetricData{
    54  				types.MakeMetricData(
    55  					fmt.Sprintf("scale(x.y.z,-2.5,%d)", now32+14),
    56  					[]float64{1, -2, -3, -10, math.NaN(), 0, math.NaN(), -12.5, -15},
    57  					5,
    58  					now32,
    59  				).SetTag("scale", "-2.5"),
    60  			},
    61  		},
    62  	}
    63  
    64  	for _, tt := range tests {
    65  		testName := tt.Target
    66  		t.Run(testName, func(t *testing.T) {
    67  			eval := th.EvaluatorFromFunc(md[0].F)
    68  			th.TestEvalExpr(t, eval, &tt)
    69  		})
    70  	}
    71  
    72  }