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

     1  //go:build cairo
     2  // +build cairo
     3  
     4  package verticalLine
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/go-graphite/carbonapi/expr/interfaces"
    11  	"github.com/go-graphite/carbonapi/expr/metadata"
    12  	"github.com/go-graphite/carbonapi/expr/tags"
    13  	"github.com/go-graphite/carbonapi/expr/types"
    14  	"github.com/go-graphite/carbonapi/pkg/parser"
    15  	th "github.com/go-graphite/carbonapi/tests"
    16  	pb "github.com/go-graphite/protocol/carbonapi_v3_pb"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  var (
    21  	md []interfaces.FunctionMetadata = New("")
    22  )
    23  
    24  func init() {
    25  	for _, m := range md {
    26  		metadata.RegisterFunction(m.Name, m.F)
    27  	}
    28  }
    29  
    30  func TestFunction(t *testing.T) {
    31  	now := time.Now()
    32  	nowUnix := now.Unix()
    33  
    34  	duration, err := time.ParseDuration("-30m")
    35  	if err != nil {
    36  		assert.NoError(t, err)
    37  	}
    38  	from := now.Add(duration).Unix()
    39  
    40  	wantedDuration, err := time.ParseDuration("-5m")
    41  	if err != nil {
    42  		assert.NoError(t, err)
    43  	}
    44  	wantedTs := now.Add(wantedDuration).Unix()
    45  
    46  	tests := []th.EvalTestItemWithRange{
    47  		{
    48  			"verticalLine(\"-5m\")",
    49  			map[parser.MetricRequest][]*types.MetricData{
    50  				{Metric: "foo", From: from, Until: nowUnix}: {types.MakeMetricData("foo", []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, 1, nowUnix)},
    51  			},
    52  			[]*types.MetricData{makeMetricData("", []float64{1.0, 1.0}, 1, wantedTs, wantedTs)},
    53  			from,
    54  			nowUnix,
    55  		},
    56  		{
    57  			"verticalLine(\"-5m\", \"label\")",
    58  			map[parser.MetricRequest][]*types.MetricData{
    59  				{Metric: "foo", From: from, Until: nowUnix}: {types.MakeMetricData("foo", []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, 1, nowUnix)},
    60  			},
    61  			[]*types.MetricData{makeMetricData("label", []float64{1.0, 1.0}, 1, wantedTs, wantedTs)},
    62  			from,
    63  			nowUnix,
    64  		},
    65  	}
    66  
    67  	for _, test := range tests {
    68  		t.Run(test.Target, func(t *testing.T) {
    69  			eval := th.EvaluatorFromFunc(md[0].F)
    70  			th.TestEvalExprWithRange(t, eval, &test)
    71  		})
    72  	}
    73  }
    74  
    75  func TestFunctionErrors(t *testing.T) {
    76  	now := time.Now()
    77  	nowUnix := now.Unix()
    78  
    79  	duration, err := time.ParseDuration("-30m")
    80  	if err != nil {
    81  		assert.NoError(t, err)
    82  	}
    83  	from := now.Add(duration).Unix()
    84  
    85  	tests := []th.EvalTestItemWithError{
    86  		{
    87  			"verticalLine(\"-50m\")",
    88  			map[parser.MetricRequest][]*types.MetricData{
    89  				{Metric: "foo", From: from, Until: nowUnix}: {types.MakeMetricData("foo", []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, 1, nowUnix)},
    90  			},
    91  			[]*types.MetricData{},
    92  			TsOutOfRangeError,
    93  		},
    94  		{
    95  			"verticalLine(\"+5m\")",
    96  			map[parser.MetricRequest][]*types.MetricData{
    97  				{Metric: "foo", From: from, Until: nowUnix}: {types.MakeMetricData("foo", []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, 1, nowUnix)},
    98  			},
    99  			[]*types.MetricData{},
   100  			TsOutOfRangeError,
   101  		},
   102  	}
   103  
   104  	for _, test := range tests {
   105  		t.Run(test.Target, func(t *testing.T) {
   106  			eval := th.EvaluatorFromFunc(md[0].F)
   107  			th.TestEvalExprWithError(t, eval, &test)
   108  		})
   109  	}
   110  }
   111  
   112  func makeMetricData(name string, values []float64, step, start, stop int64) *types.MetricData {
   113  	return &types.MetricData{
   114  		FetchResponse: pb.FetchResponse{
   115  			Name:      name,
   116  			Values:    values,
   117  			StartTime: start,
   118  			StepTime:  step,
   119  			StopTime:  stop,
   120  		},
   121  		Tags: tags.ExtractTags(name),
   122  	}
   123  }