github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/graphite/storage/converter_test.go (about) 1 // Copyright (c) 2019 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 storage 22 23 import ( 24 "testing" 25 26 "github.com/m3db/m3/src/query/graphite/graphite" 27 "github.com/m3db/m3/src/query/models" 28 29 "github.com/stretchr/testify/assert" 30 "github.com/stretchr/testify/require" 31 ) 32 33 func TestConvertMetricPartToMatcher(t *testing.T) { 34 for i := 0; i < 100; i++ { 35 globAndRegex := "foo*bar[rz]*{qux|quail}" 36 expected := models.Matcher{ 37 Type: models.MatchRegexp, 38 Name: graphite.TagName(i), 39 Value: []byte(`foo[^\.]*bar[rz][^\.]*(qux|quail)`), 40 } 41 42 actual, err := convertMetricPartToMatcher(i, globAndRegex) 43 require.NoError(t, err) 44 assert.Equal(t, expected, actual) 45 } 46 } 47 48 func TestConvertWildcardToMatcher(t *testing.T) { 49 metric := "*" 50 for i := 0; i < 100; i++ { 51 expected := models.Matcher{ 52 Type: models.MatchField, 53 Name: graphite.TagName(i), 54 } 55 if i == 0 { 56 expected = models.Matcher{ 57 Type: models.MatchRegexp, 58 Name: graphite.TagName(0), 59 Value: []byte(".*"), 60 } 61 } 62 63 actual, err := convertMetricPartToMatcher(i, metric) 64 require.NoError(t, err) 65 assert.Equal(t, expected, actual) 66 } 67 } 68 69 func TestConvertAlphanumericMetricPartToMatcher(t *testing.T) { 70 metric := "abcdefg" 71 expected := models.Matcher{ 72 Type: models.MatchEqual, 73 Name: graphite.TagName(0), 74 Value: []byte("abcdefg"), 75 } 76 77 actual, err := convertMetricPartToMatcher(0, metric) 78 require.NoError(t, err) 79 assert.Equal(t, expected, actual) 80 } 81 82 func TestMatcherTerminator(t *testing.T) { 83 for i := 0; i < 100; i++ { 84 expected := models.Matcher{ 85 Type: models.MatchNotField, 86 Name: graphite.TagName(i), 87 } 88 89 actual := matcherTerminator(i) 90 assert.Equal(t, expected, actual) 91 } 92 }