github.com/m3db/m3@v1.5.0/src/query/functions/binary/common_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 binary 22 23 import ( 24 "math" 25 "testing" 26 27 "github.com/m3db/m3/src/query/block" 28 "github.com/m3db/m3/src/query/models" 29 "github.com/m3db/m3/src/query/test" 30 xtime "github.com/m3db/m3/src/x/time" 31 32 "github.com/golang/mock/gomock" 33 "github.com/stretchr/testify/assert" 34 "github.com/stretchr/testify/require" 35 ) 36 37 var nan = math.NaN() 38 39 func builderMockWithExpectedValues( 40 ctrl *gomock.Controller, 41 indices []int, 42 values [][]float64, 43 ) block.Builder { 44 builder := block.NewMockBuilder(ctrl) 45 for i, val := range values { 46 for _, idx := range indices { 47 builder.EXPECT().AppendValue(i, val[idx]) 48 } 49 } 50 51 return builder 52 } 53 54 func stepIterWithExpectedValues( 55 ctrl *gomock.Controller, 56 _ []int, 57 values [][]float64, 58 ) block.StepIter { 59 stepIter := block.NewMockStepIter(ctrl) 60 stepIter.EXPECT().Next().Return(true).Times(len(values)) 61 for _, val := range values { 62 bl := block.NewMockStep(ctrl) 63 bl.EXPECT().Values().Return(val) 64 stepIter.EXPECT().Current().Return(bl) 65 } 66 67 stepIter.EXPECT().Next().Return(false) 68 stepIter.EXPECT().Err().Return(nil) 69 70 return stepIter 71 } 72 73 var combineMetaAndSeriesMetaTests = []struct { 74 name string 75 tags, otherTags, expectedTags test.StringTags 76 seriesTags, otherSeriesTags test.StringTags 77 expectedSeriesTags, expectedOtherSeriesTags test.StringTags 78 }{ 79 { 80 "no right tags", 81 test.StringTags{{N: "a", V: "b"}}, 82 test.StringTags{}, 83 test.StringTags{}, 84 85 test.StringTags{{N: "c", V: "d"}}, 86 test.StringTags{{N: "1", V: "2"}}, 87 test.StringTags{{N: "a", V: "b"}, {N: "c", V: "d"}}, 88 test.StringTags{{N: "1", V: "2"}}, 89 }, 90 { 91 "no left tags", 92 test.StringTags{}, 93 test.StringTags{{N: "a", V: "b"}}, 94 test.StringTags{}, 95 96 test.StringTags{}, 97 test.StringTags{}, 98 test.StringTags{}, 99 test.StringTags{{N: "a", V: "b"}}, 100 }, 101 { 102 "same tags", 103 test.StringTags{{N: "a", V: "b"}}, 104 test.StringTags{{N: "a", V: "b"}}, 105 test.StringTags{{N: "a", V: "b"}}, 106 107 test.StringTags{{N: "a", V: "b"}, {N: "c", V: "d"}}, 108 test.StringTags{}, 109 test.StringTags{{N: "a", V: "b"}, {N: "c", V: "d"}}, 110 test.StringTags{}, 111 }, 112 { 113 "different tags", 114 test.StringTags{{N: "a", V: "b"}}, 115 test.StringTags{{N: "c", V: "d"}}, 116 test.StringTags{}, 117 118 test.StringTags{{N: "1", V: "2"}}, 119 test.StringTags{{N: "3", V: "4"}}, 120 test.StringTags{{N: "1", V: "2"}, {N: "a", V: "b"}}, 121 test.StringTags{{N: "3", V: "4"}, {N: "c", V: "d"}}, 122 }, 123 { 124 "conflicting tags", 125 test.StringTags{{N: "a", V: "b"}}, 126 test.StringTags{{N: "a", V: "*b"}}, 127 test.StringTags{}, 128 129 test.StringTags{{N: "1", V: "2"}}, 130 test.StringTags{{N: "3", V: "4"}}, 131 test.StringTags{{N: "1", V: "2"}, {N: "a", V: "b"}}, 132 test.StringTags{{N: "3", V: "4"}, {N: "a", V: "*b"}}, 133 }, 134 { 135 "mixed tags", 136 test.StringTags{{N: "a", V: "b"}, {N: "c", V: "d"}, {N: "e", V: "f"}}, 137 test.StringTags{{N: "a", V: "b"}, {N: "c", V: "*d"}, {N: "g", V: "h"}}, 138 test.StringTags{{N: "a", V: "b"}}, 139 140 test.StringTags{{N: "1", V: "2"}}, 141 test.StringTags{{N: "3", V: "4"}}, 142 test.StringTags{{N: "1", V: "2"}, {N: "c", V: "d"}, {N: "e", V: "f"}}, 143 test.StringTags{{N: "3", V: "4"}, {N: "c", V: "*d"}, {N: "g", V: "h"}}, 144 }, 145 } 146 147 func TestCombineMetaAndSeriesMeta(t *testing.T) { 148 for _, tt := range combineMetaAndSeriesMetaTests { 149 t.Run(tt.name, func(t *testing.T) { 150 var ( 151 tags = test.StringTagsToTags(tt.tags) 152 otherTags = test.StringTagsToTags(tt.otherTags) 153 seriesTags = test.StringTagsToTags(tt.seriesTags) 154 expectedTags = test.StringTagsToTags(tt.expectedTags) 155 otherSeriesTags = test.StringTagsToTags(tt.otherSeriesTags) 156 expectedSeriesTags = test.StringTagsToTags(tt.expectedSeriesTags) 157 expectedOtherTags = test.StringTagsToTags(tt.expectedOtherSeriesTags) 158 159 meta = block.Metadata{Tags: tags} 160 otherMeta = block.Metadata{Tags: otherTags} 161 162 metas = []block.SeriesMeta{{Tags: seriesTags}, {Tags: seriesTags}} 163 otherMetas = []block.SeriesMeta{{Tags: otherSeriesTags}} 164 ) 165 166 meta, seriesMeta, otherSeriesMeta, err := combineMetaAndSeriesMeta(meta, 167 otherMeta, metas, otherMetas) 168 require.NoError(t, err) 169 assert.Equal(t, expectedTags, meta.Tags) 170 171 require.Equal(t, 2, len(seriesMeta)) 172 for _, meta := range seriesMeta { 173 assert.Equal(t, expectedSeriesTags, meta.Tags) 174 } 175 176 require.Equal(t, 1, len(otherSeriesMeta)) 177 for _, otherMeta := range otherSeriesMeta { 178 assert.Equal(t, expectedOtherTags, otherMeta.Tags) 179 } 180 }) 181 } 182 } 183 184 func TestCombineMetaAndSeriesMetaError(t *testing.T) { 185 now := xtime.Now() 186 meta, otherMeta := 187 block.Metadata{Bounds: models.Bounds{Start: now}}, 188 block.Metadata{Bounds: models.Bounds{Start: now.Add(2)}} 189 190 metas, otherMetas := []block.SeriesMeta{}, []block.SeriesMeta{} 191 _, _, _, err := combineMetaAndSeriesMeta(meta, otherMeta, metas, otherMetas) 192 assert.Error(t, err, errMismatchedBounds.Error()) 193 } 194 195 func toTag(n, v string) models.Tag { 196 return models.Tag{Name: []byte(n), Value: []byte(v)} 197 } 198 199 func TestRemoveNameTags(t *testing.T) { 200 mTags := models.NewTags(2, models.NewTagOptions()).AddTags([]models.Tag{ 201 toTag("foo", "bar"), 202 }).SetName([]byte("baz")) 203 204 meta := block.Metadata{ 205 Tags: mTags, 206 } 207 208 firstTags := models.NewTags(2, models.NewTagOptions()).AddTags([]models.Tag{ 209 toTag("qux", "qaz"), 210 }).SetName([]byte("quail")) 211 212 secondTags := models.NewTags(1, models.NewTagOptions()).AddTags([]models.Tag{ 213 toTag("foobar", "baz"), 214 }) 215 216 metas := []block.SeriesMeta{ 217 {Tags: firstTags}, 218 {Tags: secondTags}, 219 } 220 221 // NB: ensure the name tags exist initially 222 ex := []models.Tag{toTag("__name__", "baz"), toTag("foo", "bar")} 223 assert.Equal(t, ex, meta.Tags.Tags) 224 225 ex = []models.Tag{toTag("__name__", "quail"), toTag("qux", "qaz")} 226 assert.Equal(t, ex, metas[0].Tags.Tags) 227 228 ex = []models.Tag{toTag("foobar", "baz")} 229 assert.Equal(t, ex, metas[1].Tags.Tags) 230 231 meta, metas = removeNameTags(meta, metas) 232 233 // NB: ensure name tags are removed 234 ex = []models.Tag{toTag("foo", "bar")} 235 assert.Equal(t, ex, meta.Tags.Tags) 236 237 ex = []models.Tag{toTag("qux", "qaz")} 238 assert.Equal(t, ex, metas[0].Tags.Tags) 239 240 ex = []models.Tag{toTag("foobar", "baz")} 241 assert.Equal(t, ex, metas[1].Tags.Tags) 242 243 }