go.uber.org/cadence@v1.2.9/internal/headers_test.go (about)

     1  // Copyright (c) 2017 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 internal
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  
    28  	"go.uber.org/cadence/.gen/go/shared"
    29  )
    30  
    31  func TestHeaderWriter(t *testing.T) {
    32  	t.Parallel()
    33  	tests := []struct {
    34  		name     string
    35  		initial  *shared.Header
    36  		expected *shared.Header
    37  		vals     map[string][]byte
    38  	}{
    39  		{
    40  			"no values",
    41  			&shared.Header{
    42  				Fields: map[string][]byte{},
    43  			},
    44  			&shared.Header{
    45  				Fields: map[string][]byte{},
    46  			},
    47  			map[string][]byte{},
    48  		},
    49  		{
    50  			"add values",
    51  			&shared.Header{
    52  				Fields: map[string][]byte{},
    53  			},
    54  			&shared.Header{
    55  				Fields: map[string][]byte{
    56  					"key1": []byte("val1"),
    57  					"key2": []byte("val2"),
    58  				},
    59  			},
    60  			map[string][]byte{
    61  				"key1": []byte("val1"),
    62  				"key2": []byte("val2"),
    63  			},
    64  		},
    65  		{
    66  			"overwrite values",
    67  			&shared.Header{
    68  				Fields: map[string][]byte{
    69  					"key1": []byte("unexpected"),
    70  				},
    71  			},
    72  			&shared.Header{
    73  				Fields: map[string][]byte{
    74  					"key1": []byte("val1"),
    75  					"key2": []byte("val2"),
    76  				},
    77  			},
    78  			map[string][]byte{
    79  				"key1": []byte("val1"),
    80  				"key2": []byte("val2"),
    81  			},
    82  		},
    83  	}
    84  
    85  	for _, test := range tests {
    86  		test := test
    87  		t.Run(test.name, func(t *testing.T) {
    88  			t.Parallel()
    89  			writer := NewHeaderWriter(test.initial)
    90  			for key, val := range test.vals {
    91  				writer.Set(key, val)
    92  			}
    93  			assert.Equal(t, test.expected, test.initial)
    94  		})
    95  	}
    96  }
    97  
    98  func TestHeaderReader(t *testing.T) {
    99  	t.Parallel()
   100  	tests := []struct {
   101  		name    string
   102  		header  *shared.Header
   103  		keys    map[string]struct{}
   104  		isError bool
   105  	}{
   106  		{
   107  			"valid values",
   108  			&shared.Header{
   109  				Fields: map[string][]byte{
   110  					"key1": []byte("val1"),
   111  					"key2": []byte("val2"),
   112  				},
   113  			},
   114  			map[string]struct{}{"key1": struct{}{}, "key2": struct{}{}},
   115  			false,
   116  		},
   117  		{
   118  			"invalid values",
   119  			&shared.Header{
   120  				Fields: map[string][]byte{
   121  					"key1": []byte("val1"),
   122  					"key2": []byte("val2"),
   123  				},
   124  			},
   125  			map[string]struct{}{"key2": struct{}{}},
   126  			true,
   127  		},
   128  	}
   129  
   130  	for _, test := range tests {
   131  		test := test
   132  		t.Run(test.name, func(t *testing.T) {
   133  			t.Parallel()
   134  			reader := NewHeaderReader(test.header)
   135  			err := reader.ForEachKey(func(key string, val []byte) error {
   136  				if _, ok := test.keys[key]; !ok {
   137  					return assert.AnError
   138  				}
   139  				return nil
   140  			})
   141  			if test.isError {
   142  				assert.Error(t, err)
   143  			} else {
   144  				assert.NoError(t, err)
   145  			}
   146  		})
   147  	}
   148  }