github.com/m3db/m3@v1.5.0/src/metrics/aggregation/id_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 aggregation
    22  
    23  import (
    24  	"encoding/json"
    25  	"testing"
    26  
    27  	"github.com/m3db/m3/src/metrics/generated/proto/aggregationpb"
    28  	"github.com/m3db/m3/src/x/test/testmarshal"
    29  	"github.com/stretchr/testify/assert"
    30  	"gopkg.in/yaml.v2"
    31  
    32  	"github.com/stretchr/testify/require"
    33  )
    34  
    35  var (
    36  	testID      = ID{6}
    37  	testIDProto = aggregationpb.AggregationID{Id: 6}
    38  )
    39  
    40  func TestIDToProto(t *testing.T) {
    41  	var pb aggregationpb.AggregationID
    42  	testID.ToProto(&pb)
    43  	require.Equal(t, testIDProto, pb)
    44  }
    45  
    46  func TestIDFromProto(t *testing.T) {
    47  	var res ID
    48  	res.FromProto(testIDProto)
    49  	require.Equal(t, testID, res)
    50  }
    51  
    52  func TestIDRoundTrip(t *testing.T) {
    53  	var (
    54  		pb  aggregationpb.AggregationID
    55  		res ID
    56  	)
    57  	testID.ToProto(&pb)
    58  	res.FromProto(pb)
    59  	require.Equal(t, testID, res)
    60  }
    61  
    62  func TestIDMarshalJSON(t *testing.T) {
    63  	inputs := []struct {
    64  		id       ID
    65  		expected string
    66  	}{
    67  		{id: DefaultID, expected: `null`},
    68  		{id: testID, expected: `["Last","Min"]`},
    69  	}
    70  	for _, input := range inputs {
    71  		b, err := json.Marshal(input.id)
    72  		require.NoError(t, err)
    73  		require.Equal(t, input.expected, string(b))
    74  	}
    75  }
    76  
    77  func TestIDMarshalJSONError(t *testing.T) {
    78  	inputs := []ID{
    79  		ID{1234235235},
    80  	}
    81  	for _, input := range inputs {
    82  		_, err := json.Marshal(input)
    83  		require.Error(t, err)
    84  	}
    85  }
    86  
    87  func TestIDMarshalRoundtrip(t *testing.T) {
    88  	inputs := []ID{
    89  		DefaultID,
    90  		testID,
    91  	}
    92  	testmarshal.TestMarshalersRoundtrip(t, inputs, []testmarshal.Marshaler{testmarshal.YAMLMarshaler, testmarshal.JSONMarshaler})
    93  }
    94  
    95  func TestIDYAMLMarshaling(t *testing.T) {
    96  	cases := []struct {
    97  		In       ID
    98  		Expected string
    99  	}{{
   100  		In: MustCompressTypes(Last, Min),
   101  		Expected: `- Last
   102  - Min
   103  `,
   104  	}}
   105  
   106  	t.Run("marshal", func(t *testing.T) {
   107  		for _, tc := range cases {
   108  			testmarshal.AssertMarshals(t, testmarshal.YAMLMarshaler, tc.In, []byte(tc.Expected))
   109  		}
   110  	})
   111  
   112  	t.Run("unmarshal", func(t *testing.T) {
   113  		for _, tc := range cases {
   114  			testmarshal.AssertUnmarshals(t, testmarshal.YAMLMarshaler, tc.In, []byte(tc.Expected))
   115  		}
   116  	})
   117  
   118  	t.Run("marshal_error", func(t *testing.T) {
   119  		_, err := yaml.Marshal(ID{40559696})
   120  		assert.Error(t, err)
   121  	})
   122  
   123  	t.Run("unmarshal_error", func(t *testing.T) {
   124  		var id ID
   125  		err := yaml.Unmarshal([]byte(`["Foobar"]`), &id)
   126  		assert.EqualError(t, err, "invalid aggregation type: Foobar")
   127  	})
   128  }