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

     1  package aboveSeries
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/go-graphite/carbonapi/expr"
     8  	"github.com/go-graphite/carbonapi/expr/metadata"
     9  	"github.com/go-graphite/carbonapi/expr/types"
    10  	"github.com/go-graphite/carbonapi/pkg/parser"
    11  	th "github.com/go-graphite/carbonapi/tests"
    12  )
    13  
    14  func init() {
    15  	md := New("")
    16  	for _, m := range md {
    17  		metadata.RegisterRewriteFunction(m.Name, m.F)
    18  	}
    19  }
    20  
    21  func TestDiffSeries(t *testing.T) {
    22  	now32 := int64(time.Now().Unix())
    23  
    24  	tests := []th.RewriteTestItem{
    25  		{
    26  			`aboveSeries(metric1, 7, "Kotik", "Bog")`,
    27  			map[parser.MetricRequest][]*types.MetricData{
    28  				{Metric: "metric1", From: 0, Until: 1}: {
    29  					types.MakeMetricData("metricSobaka", []float64{0, 0, 0, 0, 0, 0}, 1, now32),
    30  					types.MakeMetricData("metricKotik", []float64{3, 4, 5, 6, 7, 8}, 1, now32),
    31  					types.MakeMetricData("metricHomyak", []float64{4, 4, 5, 5, 6, 6}, 1, now32),
    32  				},
    33  			},
    34  			th.RewriteTestResult{
    35  				Rewritten: true,
    36  				Targets:   []string{"metricBog"},
    37  				Err:       nil,
    38  			},
    39  		},
    40  		{
    41  			`aboveSeries(metric1, 7, ".*Ko.ik$", "Bog")`,
    42  			map[parser.MetricRequest][]*types.MetricData{
    43  				{Metric: "metric1", From: 0, Until: 1}: {
    44  					types.MakeMetricData("metricSobaka", []float64{0, 0, 0, 0, 0, 0}, 1, now32),
    45  					types.MakeMetricData("metricKotik", []float64{3, 4, 5, 6, 7, 8}, 1, now32),
    46  					types.MakeMetricData("metricHomyak", []float64{4, 4, 5, 5, 6, 8}, 1, now32),
    47  				},
    48  			},
    49  			th.RewriteTestResult{
    50  				Rewritten: true,
    51  				Targets:   []string{"Bog", "metricHomyak"},
    52  				Err:       nil,
    53  			},
    54  		},
    55  		{
    56  			`aboveSeries(statsd.timers.metric.rate, 1000, 'rate', 'median')`,
    57  			map[parser.MetricRequest][]*types.MetricData{
    58  				{Metric: "statsd.timers.metric.rate", From: 0, Until: 1}: {
    59  					types.MakeMetricData("statsd.timers.metric.rate", []float64{500, 1500}, 1, now32),
    60  					types.MakeMetricData("statsd.timers.metric.median", []float64{50, 55}, 1, now32),
    61  				},
    62  			},
    63  			th.RewriteTestResult{
    64  				Rewritten: true,
    65  				Targets:   []string{"statsd.timers.metric.median"},
    66  				Err:       nil,
    67  			},
    68  		},
    69  	}
    70  
    71  	for _, tt := range tests {
    72  		testName := tt.Target
    73  		t.Run(testName, func(t *testing.T) {
    74  			eval, err := expr.NewEvaluator(nil, th.NewTestZipper(nil), false)
    75  			if err == nil {
    76  				th.TestRewriteExpr(t, eval, &tt)
    77  			} else {
    78  				t.Errorf("error='%v'", err)
    79  			}
    80  		})
    81  	}
    82  
    83  }