github.com/m3db/m3@v1.5.0/src/query/functions/lazy/base_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 lazy 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/m3db/m3/src/query/block" 28 "github.com/m3db/m3/src/query/executor/transform" 29 xtime "github.com/m3db/m3/src/x/time" 30 31 "github.com/golang/mock/gomock" 32 "github.com/stretchr/testify/assert" 33 "github.com/stretchr/testify/require" 34 ) 35 36 func testLazyOpts(timeOffset time.Duration, valOffset float64) block.LazyOptions { 37 tt := func(t xtime.UnixNano) xtime.UnixNano { return t.Add(timeOffset) } 38 mt := func(meta block.Metadata) block.Metadata { 39 meta.Bounds.Start = meta.Bounds.Start.Add(timeOffset) 40 return meta 41 } 42 vt := func(val float64) float64 { return val * valOffset } 43 44 return block.NewLazyOptions().SetTimeTransform(tt).SetMetaTransform(mt).SetValueTransform(vt) 45 } 46 47 func TestOffsetOp(t *testing.T) { 48 offset := time.Minute 49 op, err := NewLazyOp(OffsetType, testLazyOpts(offset, 1.0)) 50 assert.NoError(t, err) 51 52 assert.Equal(t, "offset", op.OpType()) 53 assert.Equal(t, "type: offset", op.String()) 54 55 base, ok := op.(baseOp) 56 require.True(t, ok) 57 58 node := base.Node(nil, transform.Options{}) 59 n, ok := node.(*baseNode) 60 require.True(t, ok) 61 62 ctrl := gomock.NewController(t) 63 defer ctrl.Finish() 64 b := block.NewMockBlock(ctrl) 65 66 bl := n.processBlock(b) 67 it := block.NewMockStepIter(ctrl) 68 b.EXPECT().StepIter().Return(it, nil) 69 70 iter, err := bl.StepIter() 71 require.NoError(t, err) 72 73 vals := []float64{1, 2, 3, 4} 74 now := xtime.Now() 75 76 step := block.NewMockStep(ctrl) 77 step.EXPECT().Time().Return(now) 78 step.EXPECT().Values().Return(vals) 79 it.EXPECT().Current().Return(step) 80 actual := iter.Current() 81 82 assert.Equal(t, vals, actual.Values()) 83 assert.Equal(t, now.Add(offset), actual.Time()) 84 } 85 86 func TestUnaryOp(t *testing.T) { 87 offset := time.Duration(0) 88 op, err := NewLazyOp(UnaryType, testLazyOpts(offset, -1.0)) 89 assert.NoError(t, err) 90 91 assert.Equal(t, "unary", op.OpType()) 92 assert.Equal(t, "type: unary", op.String()) 93 94 base, ok := op.(baseOp) 95 require.True(t, ok) 96 97 node := base.Node(nil, transform.Options{}) 98 n, ok := node.(*baseNode) 99 require.True(t, ok) 100 101 ctrl := gomock.NewController(t) 102 defer ctrl.Finish() 103 b := block.NewMockBlock(ctrl) 104 105 bl := n.processBlock(b) 106 it := block.NewMockStepIter(ctrl) 107 b.EXPECT().StepIter().Return(it, nil) 108 109 iter, err := bl.StepIter() 110 require.NoError(t, err) 111 112 vals := []float64{1, 2, 3, 4} 113 now := xtime.Now() 114 115 step := block.NewMockStep(ctrl) 116 step.EXPECT().Time().Return(now) 117 step.EXPECT().Values().Return(vals) 118 it.EXPECT().Current().Return(step) 119 actual := iter.Current() 120 121 expectedVals := []float64{-1, -2, -3, -4} 122 assert.Equal(t, expectedVals, actual.Values()) 123 assert.Equal(t, now, actual.Time()) 124 }