github.com/m3db/m3@v1.5.0/src/query/parser/promql/resolve_scalar_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 promql 22 23 import ( 24 "math" 25 "testing" 26 "time" 27 28 "github.com/m3db/m3/src/query/models" 29 "github.com/m3db/m3/src/query/test/compare" 30 31 "github.com/stretchr/testify/assert" 32 "github.com/stretchr/testify/require" 33 ) 34 35 var scalarResolverTests = []struct { 36 funcString string 37 expected float64 38 }{ 39 {"7+2-1", 8}, 40 {"(9+scalar(vector(-10)))", -1}, 41 {"(9+scalar(some_fetch))", math.NaN()}, 42 {"scalar(9+vector(4)) / 2", 6.5}, 43 { 44 `scalar( 45 scalar( 46 scalar( 47 vector( 20 - 4 ) ^ 0.5 - 2 48 ) - vector( 2 ) 49 ) + vector(2) 50 ) * 9`, 51 18, 52 }, 53 { 54 `5 - scalar( 55 scalar( 56 scalar( 57 vector( 20 - 4 ) ^ vector(0.5) - vector(2) 58 ) - vector( 2 ) 59 ) + vector(2) 60 )`, 61 3, 62 }, 63 {"scalar(vector(1) + vector(2))", 3}, 64 {"scalar(vector(1) + scalar(vector(1) + vector(2)))", 4}, 65 {"scalar(vector(1) + scalar(vector(1) + scalar(vector(1) + vector(2))))", 5}, 66 {"(scalar(9+vector(4)) * 4 - 9+scalar(vector(3)))", 46}, 67 {"scalar(vector(1) + scalar(some_fetch == 1))", math.NaN()}, 68 {"scalar(vector(1) + scalar(1 == some_fetch))", math.NaN()}, 69 {"scalar(1 +vector(2 != bool 1))", 2}, 70 {"scalar(1 +vector(1 != bool 1))", 1}, 71 {"1 >= bool 1", 1}, 72 {"1 >= bool 2", 0}, 73 } 74 75 func TestScalarResolver(t *testing.T) { 76 for _, tt := range scalarResolverTests { 77 t.Run(tt.funcString, func(t *testing.T) { 78 parsed, err := Parse(tt.funcString, time.Second, 79 models.NewTagOptions(), NewParseOptions()) 80 require.NoError(t, err) 81 expr := parsed.(*promParser).expr 82 actual, err := resolveScalarArgument(expr) 83 84 require.NoError(t, err) 85 compare.EqualsWithNans(t, tt.expected, actual) 86 }) 87 } 88 } 89 90 var stringResolverTests = []struct { 91 funcString string 92 expected string 93 }{ 94 {`((("value")))`, "value"}, 95 {`"value"`, "value"}, 96 } 97 98 func TestStringResolver(t *testing.T) { 99 for _, tt := range stringResolverTests { 100 t.Run(tt.funcString, func(t *testing.T) { 101 parsed, err := Parse(tt.funcString, time.Second, 102 models.NewTagOptions(), NewParseOptions()) 103 require.NoError(t, err) 104 expr := parsed.(*promParser).expr 105 e := unwrapParenExpr(expr) 106 label, err := resolveStringArgument(e) 107 require.NoError(t, err) 108 assert.Equal(t, tt.expected, label) 109 }) 110 } 111 } 112 113 func TestResolveStringWhenExprIsNil(t *testing.T) { 114 _, err := resolveStringArgument(nil) 115 require.Error(t, err) 116 } 117