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

     1  package randomWalk
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/go-graphite/carbonapi/expr/interfaces"
     7  	"github.com/go-graphite/carbonapi/expr/metadata"
     8  	"github.com/go-graphite/carbonapi/expr/types"
     9  	"github.com/go-graphite/carbonapi/pkg/parser"
    10  	th "github.com/go-graphite/carbonapi/tests"
    11  	"github.com/stretchr/testify/assert"
    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 TestRandomWalk(t *testing.T) {
    25  	tests := []th.EvalTestItemWithCustomValidation{
    26  		{
    27  			Target: "randomWalk('foo')",
    28  			M:      map[parser.MetricRequest][]*types.MetricData{},
    29  			From:   0,
    30  			Until:  120,
    31  			Validator: func(t *testing.T, md []*types.MetricData) {
    32  				assert.Equal(t, 1, len(md))
    33  				m := md[0]
    34  				assert.Equal(t, "foo", m.Name)
    35  				assert.Equal(t, int64(60), m.StepTime)
    36  				assert.Equal(t, 2, len(m.Values))
    37  			},
    38  		},
    39  		{
    40  			Target: "randomWalk('foo', step=3)",
    41  			M:      map[parser.MetricRequest][]*types.MetricData{},
    42  			From:   0,
    43  			Until:  120,
    44  			Validator: func(t *testing.T, md []*types.MetricData) {
    45  				assert.Equal(t, 1, len(md))
    46  				m := md[0]
    47  				assert.Equal(t, "foo", m.Name)
    48  				assert.Equal(t, int64(3), m.StepTime)
    49  				assert.Equal(t, 40, len(m.Values))
    50  			},
    51  		},
    52  		{
    53  			Target: "randomWalk('foo', 4)",
    54  			M:      map[parser.MetricRequest][]*types.MetricData{},
    55  			From:   0,
    56  			Until:  120,
    57  			Validator: func(t *testing.T, md []*types.MetricData) {
    58  				assert.Equal(t, 1, len(md))
    59  				m := md[0]
    60  				assert.Equal(t, "foo", m.Name)
    61  				assert.Equal(t, int64(4), m.StepTime)
    62  				assert.Equal(t, 30, len(m.Values))
    63  			},
    64  		},
    65  		{
    66  			Target: "randomWalk('foo', 5)",
    67  			M:      map[parser.MetricRequest][]*types.MetricData{},
    68  			From:   0,
    69  			Until:  121, // Should be rounded to 120
    70  			Validator: func(t *testing.T, md []*types.MetricData) {
    71  				assert.Equal(t, 1, len(md))
    72  				m := md[0]
    73  				assert.Equal(t, "foo", m.Name)
    74  				assert.Equal(t, int64(5), m.StepTime)
    75  				assert.Equal(t, 24, len(m.Values))
    76  				assert.Equal(t, int64(120), m.StopTime)
    77  			},
    78  		},
    79  	}
    80  
    81  	for _, tt := range tests {
    82  		testName := tt.Target
    83  		t.Run(testName, func(t *testing.T) {
    84  			eval := th.EvaluatorFromFunc(md[0].F)
    85  			th.TestEvalExprWithCustomValidation(t, eval, &tt)
    86  		})
    87  	}
    88  }