go.temporal.io/server@v1.23.0/common/searchattribute/search_attribute_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package searchattribute
    26  
    27  import (
    28  	"testing"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  	commonpb "go.temporal.io/api/common/v1"
    32  	enumspb "go.temporal.io/api/enums/v1"
    33  
    34  	"go.temporal.io/server/common/payload"
    35  )
    36  
    37  func Test_ApplyTypeMap_Success(t *testing.T) {
    38  	assert := assert.New(t)
    39  
    40  	payloadInt, err := payload.Encode(123)
    41  	assert.NoError(err)
    42  
    43  	sa := &commonpb.SearchAttributes{
    44  		IndexedFields: map[string]*commonpb.Payload{
    45  			"key1": payload.EncodeString("str"),
    46  			"key2": payload.EncodeString("keyword"),
    47  			"key3": payloadInt,
    48  		},
    49  	}
    50  
    51  	validSearchAttributes := NameTypeMap{customSearchAttributes: map[string]enumspb.IndexedValueType{
    52  		"key1": enumspb.INDEXED_VALUE_TYPE_TEXT,
    53  		"key2": enumspb.INDEXED_VALUE_TYPE_KEYWORD,
    54  		"key3": enumspb.INDEXED_VALUE_TYPE_INT,
    55  		"key4": enumspb.INDEXED_VALUE_TYPE_DOUBLE,
    56  	}}
    57  
    58  	ApplyTypeMap(sa, validSearchAttributes)
    59  	assert.Equal("Text", string(sa.GetIndexedFields()["key1"].Metadata["type"]))
    60  	assert.Equal("Keyword", string(sa.GetIndexedFields()["key2"].Metadata["type"]))
    61  	assert.Equal("Int", string(sa.GetIndexedFields()["key3"].Metadata["type"]))
    62  }
    63  
    64  func Test_ApplyTypeMap_Skip(t *testing.T) {
    65  	assert := assert.New(t)
    66  
    67  	payloadInt, err := payload.Encode(123)
    68  	assert.NoError(err)
    69  	payloadInt.Metadata["type"] = []byte("String")
    70  
    71  	sa := &commonpb.SearchAttributes{
    72  		IndexedFields: map[string]*commonpb.Payload{
    73  			"UnknownKey": payload.EncodeString("str"),
    74  			"key4":       payload.EncodeString("invalid IndexValueType"),
    75  			"key3":       payloadInt, // Another type already set
    76  		},
    77  	}
    78  
    79  	validSearchAttributes := NameTypeMap{customSearchAttributes: map[string]enumspb.IndexedValueType{
    80  		"key1": enumspb.INDEXED_VALUE_TYPE_TEXT,
    81  		"key2": enumspb.INDEXED_VALUE_TYPE_KEYWORD,
    82  		"key3": enumspb.INDEXED_VALUE_TYPE_INT,
    83  	}}
    84  
    85  	ApplyTypeMap(sa, validSearchAttributes)
    86  	assert.Nil(sa.GetIndexedFields()["UnknownKey"].Metadata["type"])
    87  	assert.Equal("String", string(sa.GetIndexedFields()["key3"].Metadata["type"]))
    88  
    89  	validSearchAttributes = NameTypeMap{customSearchAttributes: map[string]enumspb.IndexedValueType{
    90  		"key4": enumspb.IndexedValueType(100),
    91  	}}
    92  
    93  	assert.Panics(func() {
    94  		ApplyTypeMap(sa, validSearchAttributes)
    95  	})
    96  }