github.com/m3db/m3@v1.5.0/src/query/block/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 block
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/query/models"
    28  
    29  	"github.com/stretchr/testify/assert"
    30  	"github.com/stretchr/testify/require"
    31  )
    32  
    33  var (
    34  	val    = 13.37
    35  	bounds = models.Bounds{
    36  		Start:    start,
    37  		Duration: time.Minute,
    38  		StepSize: time.Second * 10,
    39  	}
    40  )
    41  
    42  func TestScalarBlock(t *testing.T) {
    43  	tagOpts := models.NewTagOptions().SetBucketName([]byte("custom_bucket"))
    44  	tags := models.NewTags(1, tagOpts).AddTag(models.Tag{
    45  		Name:  []byte("a"),
    46  		Value: []byte("b"),
    47  	})
    48  
    49  	block := NewScalar(
    50  		val,
    51  		Metadata{
    52  			Bounds: bounds,
    53  			Tags:   tags,
    54  		},
    55  	)
    56  
    57  	assert.Equal(t, BlockScalar, block.Info().Type())
    58  	require.IsType(t, block, &Scalar{})
    59  	stepIter, err := block.StepIter()
    60  	require.NoError(t, err)
    61  	require.NotNil(t, stepIter)
    62  	meta := block.Meta()
    63  	require.True(t, meta.ResultMetadata.Exhaustive)
    64  	require.True(t, meta.ResultMetadata.LocalOnly)
    65  	require.Equal(t, 0, len(meta.ResultMetadata.Warnings))
    66  	verifyMetas(t, block.Meta(), stepIter.SeriesMeta(), tagOpts)
    67  
    68  	assert.Equal(t, 6, stepIter.StepCount())
    69  	valCounts := 0
    70  	for stepIter.Next() {
    71  		v := stepIter.Current()
    72  		require.NotNil(t, v)
    73  
    74  		expectedTime := start.Add(time.Duration(valCounts) * 10 * time.Second)
    75  		assert.Equal(t, expectedTime, v.Time())
    76  
    77  		vals := v.Values()
    78  		require.Len(t, vals, 1)
    79  		require.Equal(t, val, vals[0])
    80  
    81  		valCounts++
    82  	}
    83  
    84  	require.NoError(t, stepIter.Err())
    85  	assert.Equal(t, 6, valCounts)
    86  	meta = block.Meta()
    87  	require.True(t, meta.ResultMetadata.Exhaustive)
    88  	require.True(t, meta.ResultMetadata.LocalOnly)
    89  	require.Equal(t, 0, len(meta.ResultMetadata.Warnings))
    90  
    91  	require.NoError(t, block.Close())
    92  }
    93  
    94  func verifyMetas(
    95  	t *testing.T,
    96  	meta Metadata,
    97  	seriesMeta []SeriesMeta,
    98  	opts models.TagOptions,
    99  ) {
   100  	// Verify meta
   101  	assert.True(t, bounds.Equals(meta.Bounds))
   102  
   103  	// Verify seriesMeta
   104  	assert.Len(t, seriesMeta, 1)
   105  	sMeta := seriesMeta[0]
   106  	assert.Equal(t, 0, sMeta.Tags.Len())
   107  	assert.Equal(t, []byte(nil), sMeta.Name)
   108  }