github.com/m3db/m3@v1.5.0/src/query/models/options_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 models
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  func TestDefaultTagOptions(t *testing.T) {
    30  	opts := NewTagOptions()
    31  	assert.NoError(t, opts.Validate())
    32  	assert.Equal(t, defaultMetricName, opts.MetricName())
    33  	assert.Equal(t, TypeQuoted, opts.IDSchemeType())
    34  	assert.Equal(t, defaultMaxTagLiteralLength, opts.MaxTagLiteralLength())
    35  }
    36  
    37  func TestValidTagOptions(t *testing.T) {
    38  	opts := NewTagOptions().
    39  		SetIDSchemeType(TypePrependMeta).
    40  		SetMetricName([]byte("name")).
    41  		SetBucketName([]byte("bucket")).
    42  		SetMaxTagLiteralLength(42)
    43  
    44  	assert.NoError(t, opts.Validate())
    45  	assert.Equal(t, []byte("name"), opts.MetricName())
    46  	assert.Equal(t, []byte("bucket"), opts.BucketName())
    47  	assert.Equal(t, TypePrependMeta, opts.IDSchemeType())
    48  	assert.Equal(t, uint16(42), opts.MaxTagLiteralLength())
    49  }
    50  
    51  func TestBadNameTagOptions(t *testing.T) {
    52  	msg := errNoName.Error()
    53  	opts := NewTagOptions().
    54  		SetMetricName(nil)
    55  	assert.EqualError(t, opts.Validate(), msg)
    56  
    57  	opts = NewTagOptions().
    58  		SetMetricName([]byte{})
    59  	assert.EqualError(t, opts.Validate(), msg)
    60  }
    61  
    62  func TestBadBucketTagOptions(t *testing.T) {
    63  	msg := errNoBucket.Error()
    64  	opts := NewTagOptions().
    65  		SetBucketName(nil)
    66  	assert.EqualError(t, opts.Validate(), msg)
    67  
    68  	opts = NewTagOptions().
    69  		SetBucketName([]byte{})
    70  	assert.EqualError(t, opts.Validate(), msg)
    71  }
    72  
    73  func TestBadSchemeTagOptions(t *testing.T) {
    74  	msg := "invalid config id schema type 'unknown': should be one of" +
    75  		" [quoted prepend_meta graphite]"
    76  	opts := NewTagOptions().
    77  		SetIDSchemeType(IDSchemeType(6))
    78  	assert.EqualError(t, opts.Validate(), msg)
    79  }
    80  
    81  func TestBadMaxLiteralLength(t *testing.T) {
    82  	opts := NewTagOptions().
    83  		SetMaxTagLiteralLength(0)
    84  	assert.EqualError(t, opts.Validate(), errNonPositiveLiteralLength.Error())
    85  }
    86  
    87  func TestOptionsEquals(t *testing.T) {
    88  	opts, other := NewTagOptions(), NewTagOptions()
    89  	assert.True(t, opts.Equals(other))
    90  
    91  	bad := []byte("aaa")
    92  	n := opts.BucketName()
    93  	opts = opts.SetBucketName(bad)
    94  	assert.False(t, opts.Equals(other))
    95  
    96  	opts = opts.SetBucketName(n)
    97  	n = opts.MetricName()
    98  	opts = opts.SetMetricName(bad)
    99  	assert.False(t, opts.Equals(other))
   100  
   101  	opts = opts.SetMetricName(n)
   102  	opts = opts.SetIDSchemeType(IDSchemeType(10))
   103  	assert.False(t, opts.Equals(other))
   104  
   105  	opts = NewTagOptions().SetMaxTagLiteralLength(42)
   106  	assert.False(t, opts.Equals(other))
   107  }