github.com/m3db/m3@v1.5.0/src/query/functions/temporal/functions_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 temporal 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/m3db/m3/src/query/executor/transform" 28 29 "github.com/stretchr/testify/require" 30 ) 31 32 var temporalFunctionTestCases = []testCase{ 33 { 34 name: "resets", 35 opType: ResetsType, 36 vals: [][]float64{ 37 {nan, 0, 2, nan, 1, 1, 0, 2, nan, 1}, 38 {6, 4, 4, 2, 5, 6, 4, 4, 2, 5}, 39 }, 40 expected: [][]float64{ 41 {nan, 0, 0, 0, 1, 1, 2, 1, 1, 2}, 42 {nan, 1, 1, 2, 2, 1, 2, 1, 2, 2}, 43 }, 44 }, 45 { 46 name: "changes", 47 opType: ChangesType, 48 vals: [][]float64{ 49 {nan, 0, 2, nan, 1, 1, 0, 2, nan, 1}, 50 {6, 4, 4, 2, 5, 6, 4, 4, 2, 5}, 51 }, 52 expected: [][]float64{ 53 {nan, 0, 1, 1, 2, 2, 2, 2, 2, 3}, 54 {nan, 1, 1, 2, 3, 3, 4, 3, 3, 3}, 55 }, 56 }, 57 { 58 name: "resets all NaNs", 59 opType: ResetsType, 60 vals: [][]float64{ 61 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 62 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 63 }, 64 expected: [][]float64{ 65 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 66 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 67 }, 68 }, 69 { 70 name: "changes all NaNs", 71 opType: ChangesType, 72 vals: [][]float64{ 73 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 74 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 75 }, 76 expected: [][]float64{ 77 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 78 {nan, nan, nan, nan, nan, nan, nan, nan, nan, nan}, 79 }, 80 }, 81 { 82 name: "resets first and last NaN", 83 opType: ResetsType, 84 vals: [][]float64{ 85 {nan, 0, 2, 1, nan, nan, 0, 2, 1, nan}, 86 {nan, 4, 4, 2, nan, nan, 4, 4, 2, nan}, 87 }, 88 expected: [][]float64{ 89 {nan, 0, 0, 1, 1, 1, 2, 1, 1, 1}, 90 {nan, 0, 0, 1, 1, 1, 1, 0, 1, 1}, 91 }, 92 }, 93 { 94 name: "changes first and last NaN", 95 opType: ChangesType, 96 vals: [][]float64{ 97 {nan, 0, 2, 5, nan, nan, 0, 2, 5, nan}, 98 {nan, 4, 4, 2, nan, nan, 4, 4, 2, nan}, 99 }, 100 expected: [][]float64{ 101 {nan, 0, 1, 2, 2, 2, 2, 2, 2, 2}, 102 {nan, 0, 0, 1, 1, 1, 2, 1, 1, 1}, 103 }, 104 }, 105 } 106 107 func TestTemporalFunctionProcess(t *testing.T) { 108 opGen := func(t *testing.T, tc testCase) transform.Params { 109 op, err := NewFunctionOp([]interface{}{5 * time.Minute}, tc.opType) 110 require.NoError(t, err) 111 return op 112 } 113 114 testTemporalFunc(t, opGen, temporalFunctionTestCases) 115 } 116 117 func TestUnknownFunction(t *testing.T) { 118 _, err := NewFunctionOp([]interface{}{5 * time.Minute}, "unknown_func") 119 require.Error(t, err) 120 }