github.com/m3db/m3@v1.5.0/src/query/source/source_test.go (about)

     1  // Copyright (c) 2021 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 source
    22  
    23  import (
    24  	"context"
    25  	"errors"
    26  	"testing"
    27  
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  type testSource struct {
    32  	name string
    33  }
    34  
    35  var testDeserialize = func(bytes []byte) (interface{}, error) {
    36  	return testSource{string(bytes)}, nil
    37  }
    38  
    39  func TestSource(t *testing.T) {
    40  	ctx, err := NewContext(context.Background(), []byte("foobar"), testDeserialize)
    41  	require.NoError(t, err)
    42  
    43  	typed, ok := FromContext(ctx)
    44  	require.True(t, ok)
    45  	require.Equal(t, testSource{"foobar"}, typed.(testSource))
    46  
    47  	raw, ok := RawFromContext(ctx)
    48  	require.True(t, ok)
    49  	require.Equal(t, []byte("foobar"), raw)
    50  }
    51  
    52  func TestNoTypedSource(t *testing.T) {
    53  	ctx, err := NewContext(context.Background(), []byte("foobar"), nil)
    54  	require.NoError(t, err)
    55  
    56  	typed, ok := FromContext(ctx)
    57  	require.False(t, ok)
    58  	require.Nil(t, typed)
    59  
    60  	raw, ok := RawFromContext(ctx)
    61  	require.True(t, ok)
    62  	require.Equal(t, []byte("foobar"), raw)
    63  }
    64  
    65  func TestNoSource(t *testing.T) {
    66  	typed, ok := FromContext(context.Background())
    67  	require.False(t, ok)
    68  	require.Nil(t, typed)
    69  
    70  	raw, ok := RawFromContext(context.Background())
    71  	require.False(t, ok)
    72  	require.Nil(t, raw)
    73  }
    74  
    75  func TestNilSource(t *testing.T) {
    76  	ctx, err := NewContext(context.Background(), nil, func(bytes []byte) (interface{}, error) {
    77  		return nil, errors.New("should never happen")
    78  	})
    79  	require.NoError(t, err)
    80  
    81  	_, ok := FromContext(ctx)
    82  	require.False(t, ok)
    83  
    84  	raw, ok := RawFromContext(ctx)
    85  	require.False(t, ok)
    86  	require.Nil(t, raw)
    87  }
    88  
    89  func TestFromContextErr(t *testing.T) {
    90  	ctx, err := NewContext(context.Background(), []byte("foobar"), func(bytes []byte) (interface{}, error) {
    91  		return nil, errors.New("boom")
    92  	})
    93  	require.Error(t, err)
    94  	require.Contains(t, err.Error(), "boom")
    95  	require.Nil(t, ctx)
    96  }