github.com/m3db/m3@v1.5.0/src/query/functions/linear/round_test.go (about)

     1  // Copyright (c) 2018 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package linear
    22  
    23  import (
    24  	"math"
    25  	"testing"
    26  	"time"
    27  
    28  	"github.com/m3db/m3/src/query/executor/transform"
    29  	"github.com/m3db/m3/src/query/models"
    30  	"github.com/m3db/m3/src/query/parser"
    31  	"github.com/m3db/m3/src/query/test"
    32  	"github.com/m3db/m3/src/query/test/compare"
    33  	"github.com/m3db/m3/src/query/test/executor"
    34  
    35  	"github.com/stretchr/testify/require"
    36  )
    37  
    38  var (
    39  	emptyArgs = []interface{}{}
    40  	nan       = math.NaN()
    41  	step      = time.Second
    42  	tests     = []struct {
    43  		name     string
    44  		v        []float64
    45  		args     []interface{}
    46  		expected []float64
    47  	}{
    48  		{"default", []float64{1.2, 4.5, 6, nan},
    49  			emptyArgs, []float64{1, 5, 6, nan}},
    50  
    51  		{"1.2", []float64{1.2, 4.5, 6, nan},
    52  			toArgs(1.2), []float64{1.2, 4.8, 6, nan}},
    53  
    54  		{"-3", []float64{1.2, 4.5, 6, nan},
    55  			toArgs(-3), []float64{0, 3, 6, nan}},
    56  
    57  		{"0", []float64{1.2, 4.5, 6, nan},
    58  			toArgs(0), []float64{nan, nan, nan, nan}},
    59  	}
    60  )
    61  
    62  func toArgs(f float64) []interface{} { return []interface{}{f} }
    63  
    64  func TestRoundWithArgs(t *testing.T) {
    65  	for _, tt := range tests {
    66  		t.Run(tt.name, func(t *testing.T) {
    67  			bounds := models.Bounds{
    68  				StepSize: step,
    69  				Duration: step * time.Duration(len(tt.v)),
    70  			}
    71  
    72  			v := [][]float64{tt.v}
    73  			block := test.NewBlockFromValues(bounds, v)
    74  			c, sink := executor.NewControllerWithSink(parser.NodeID(rune(1)))
    75  			roundOp, err := NewRoundOp(tt.args)
    76  			require.NoError(t, err)
    77  
    78  			op, ok := roundOp.(transform.Params)
    79  			require.True(t, ok)
    80  
    81  			node := op.Node(c, transform.Options{})
    82  			err = node.Process(models.NoopQueryContext(), parser.NodeID(rune(0)), block)
    83  			require.NoError(t, err)
    84  			require.Len(t, sink.Values, 1)
    85  			compare.EqualsWithNans(t, tt.expected, sink.Values[0])
    86  		})
    87  	}
    88  }