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

     1  package aliasQuery
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/go-graphite/carbonapi/expr/interfaces"
     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  var (
    15  	md []interfaces.FunctionMetadata = New("")
    16  )
    17  
    18  func init() {
    19  	for _, m := range md {
    20  		metadata.RegisterFunction(m.Name, m.F)
    21  	}
    22  }
    23  
    24  func TestAliasQuery(t *testing.T) {
    25  	now := time.Now().Unix()
    26  
    27  	tests := []th.EvalTestItem{
    28  		{
    29  			Target: "aliasQuery(channel.power.*, \"channel\\.power\\.([0-9]+)\", \"channel.frequency.\\1\", \"Channel %.f MHz\")",
    30  			M: map[parser.MetricRequest][]*types.MetricData{
    31  				{Metric: "channel.frequency.1", From: 0, Until: 1}: {types.MakeMetricData("channel.frequency.1", []float64{0, 200}, 1, now)},
    32  				{Metric: "channel.frequency.2", From: 0, Until: 1}: {types.MakeMetricData("channel.frequency.2", []float64{400}, 1, now)},
    33  				{Metric: "channel.power.*", From: 0, Until: 1}: {
    34  					types.MakeMetricData("channel.power.1", []float64{1, 2, 3, 4, 5}, 1, now),
    35  					types.MakeMetricData("channel.power.2", []float64{10, 20, 30, 40, 50}, 1, now),
    36  				},
    37  			},
    38  			Want: []*types.MetricData{
    39  				types.MakeMetricData("Channel 200 MHz", []float64{1, 2, 3, 4, 5}, 1, now).SetNameTag("channel.power.1"),
    40  				types.MakeMetricData("Channel 400 MHz", []float64{10, 20, 30, 40, 50}, 1, now).SetNameTag("channel.power.2"),
    41  			},
    42  		},
    43  	}
    44  
    45  	for _, tt := range tests {
    46  		t.Run(tt.Target, func(t *testing.T) {
    47  			eval := th.EvaluatorFromFunc(md[0].F)
    48  			th.TestEvalExpr(t, eval, &tt)
    49  		})
    50  	}
    51  }