github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/block/column_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 block 22 23 import ( 24 "context" 25 "fmt" 26 "testing" 27 "time" 28 29 "github.com/m3db/m3/src/query/models" 30 31 "github.com/stretchr/testify/assert" 32 "github.com/stretchr/testify/require" 33 "github.com/uber-go/tally" 34 ) 35 36 func makeTestQueryContext() *models.QueryContext { 37 return models.NewQueryContext(context.Background(), 38 tally.NoopScope, 39 models.QueryContextOptions{}) 40 } 41 42 func TestColumnBuilderInfoTypes(t *testing.T) { 43 ctx := makeTestQueryContext() 44 builder := NewColumnBlockBuilder(ctx, Metadata{}, []SeriesMeta{}) 45 block := builder.Build() 46 assert.Equal(t, BlockDecompressed, block.Info().blockType) 47 48 block = builder.BuildAsType(BlockScalar) 49 assert.Equal(t, BlockScalar, block.Info().blockType) 50 } 51 52 func TestSetRow(t *testing.T) { 53 buildMeta := func(i int) SeriesMeta { 54 name := fmt.Sprint(i) 55 56 return SeriesMeta{ 57 Name: []byte(name), 58 Tags: models.MustMakeTags("name", name), 59 } 60 } 61 62 size := 10 63 metas := make([]SeriesMeta, size) 64 for i := range metas { 65 metas[i] = buildMeta(i) 66 } 67 68 ctx := makeTestQueryContext() 69 builder := NewColumnBlockBuilder(ctx, Metadata{ 70 Bounds: models.Bounds{StepSize: time.Minute, Duration: time.Minute}, 71 }, nil) 72 73 require.NoError(t, builder.AddCols(1)) 74 builder.PopulateColumns(size) 75 // NB: set the row metas backwards. 76 j := 0 77 for i := size - 1; i >= 0; i-- { 78 err := builder.SetRow(j, []float64{float64(i)}, metas[i]) 79 require.NoError(t, err) 80 j++ 81 } 82 83 bl := builder.Build() 84 it, err := bl.StepIter() 85 require.NoError(t, err) 86 87 actualMetas := it.SeriesMeta() 88 for i, m := range actualMetas { 89 ex := fmt.Sprint(size - 1 - i) 90 assert.Equal(t, ex, string(m.Name)) 91 require.Equal(t, 1, m.Tags.Len()) 92 tag, found := m.Tags.Get([]byte("name")) 93 require.True(t, found) 94 assert.Equal(t, ex, string(tag)) 95 } 96 97 assert.True(t, it.Next()) 98 exVals := make([]float64, size) 99 for i := range exVals { 100 exVals[i] = float64(size - 1 - i) 101 } 102 103 vals := it.Current().Values() 104 assert.Equal(t, exVals, vals) 105 assert.False(t, it.Next()) 106 assert.NoError(t, it.Err()) 107 }