go.temporal.io/server@v1.23.0/common/persistence/visibility/store/elasticsearch/query_interceptors_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 elasticsearch
    26  
    27  import (
    28  	"testing"
    29  
    30  	"github.com/golang/mock/gomock"
    31  	"github.com/stretchr/testify/require"
    32  	"github.com/stretchr/testify/suite"
    33  
    34  	"go.temporal.io/server/common/persistence/visibility/store/query"
    35  	"go.temporal.io/server/common/searchattribute"
    36  )
    37  
    38  type (
    39  	QueryInterceptorSuite struct {
    40  		suite.Suite
    41  		*require.Assertions
    42  		controller *gomock.Controller
    43  	}
    44  )
    45  
    46  func TestQueryInterceptorSuite(t *testing.T) {
    47  	suite.Run(t, &QueryInterceptorSuite{})
    48  }
    49  
    50  func (s *QueryInterceptorSuite) SetupTest() {
    51  	s.Assertions = require.New(s.T())
    52  	s.controller = gomock.NewController(s.T())
    53  }
    54  
    55  func (s *QueryInterceptorSuite) TearDownTest() {
    56  	s.controller.Finish()
    57  }
    58  
    59  func (s *QueryInterceptorSuite) TestTimeProcessFunc() {
    60  	vi := NewValuesInterceptor(
    61  		"test-namespace",
    62  		searchattribute.TestNameTypeMap,
    63  		searchattribute.NewTestMapperProvider(nil),
    64  	)
    65  
    66  	cases := []struct {
    67  		key   string
    68  		value interface{}
    69  	}{
    70  		{key: searchattribute.StartTime, value: int64(1528358645123456789)},
    71  		{key: searchattribute.CloseTime, value: "2018-06-07T15:04:05+07:00"},
    72  		{key: searchattribute.ExecutionTime, value: "some invalid time string"},
    73  		{key: searchattribute.WorkflowID, value: "should not be modified"},
    74  	}
    75  	expected := []struct {
    76  		value     string
    77  		returnErr bool
    78  	}{
    79  		{value: "2018-06-07T08:04:05.123456789Z", returnErr: false},
    80  		{value: "2018-06-07T15:04:05+07:00", returnErr: false},
    81  		{value: "", returnErr: true},
    82  		{value: "should not be modified", returnErr: false},
    83  	}
    84  
    85  	for i, testCase := range cases {
    86  		v, err := vi.Values(testCase.key, testCase.value)
    87  		if expected[i].returnErr {
    88  			s.Error(err)
    89  			continue
    90  		}
    91  		s.NoError(err)
    92  		s.Len(v, 1)
    93  		s.Equal(expected[i].value, v[0])
    94  	}
    95  }
    96  
    97  func (s *QueryInterceptorSuite) TestStatusProcessFunc() {
    98  	vi := NewValuesInterceptor(
    99  		"test-namespace",
   100  		searchattribute.TestNameTypeMap,
   101  		searchattribute.NewTestMapperProvider(nil),
   102  	)
   103  
   104  	cases := []struct {
   105  		key   string
   106  		value interface{}
   107  	}{
   108  		{key: searchattribute.ExecutionStatus, value: "Completed"},
   109  		{key: searchattribute.ExecutionStatus, value: int64(1)},
   110  		{key: searchattribute.ExecutionStatus, value: "1"},
   111  		{key: searchattribute.ExecutionStatus, value: int64(100)},
   112  		{key: searchattribute.ExecutionStatus, value: "100"},
   113  		{key: searchattribute.ExecutionStatus, value: "BadStatus"},
   114  		{key: searchattribute.WorkflowID, value: "should not be modified"},
   115  	}
   116  	expected := []struct {
   117  		value     string
   118  		returnErr bool
   119  	}{
   120  		{value: "Completed", returnErr: false},
   121  		{value: "Running", returnErr: false},
   122  		{value: "1", returnErr: false},
   123  		{value: "", returnErr: true},
   124  		{value: "100", returnErr: false},
   125  		{value: "BadStatus", returnErr: false},
   126  		{value: "should not be modified", returnErr: false},
   127  	}
   128  
   129  	for i, testCase := range cases {
   130  		v, err := vi.Values(testCase.key, testCase.value)
   131  		if expected[i].returnErr {
   132  			s.Error(err)
   133  			continue
   134  		}
   135  		s.NoError(err)
   136  		s.Len(v, 1)
   137  		s.Equal(expected[i].value, v[0])
   138  	}
   139  }
   140  
   141  func (s *QueryInterceptorSuite) TestDurationProcessFunc() {
   142  	vi := NewValuesInterceptor(
   143  		"test-namespace",
   144  		searchattribute.TestNameTypeMap,
   145  		searchattribute.NewTestMapperProvider(nil),
   146  	)
   147  
   148  	cases := []struct {
   149  		key   string
   150  		value interface{}
   151  	}{
   152  		{key: searchattribute.ExecutionDuration, value: "1"},
   153  		{key: searchattribute.ExecutionDuration, value: int64(1)},
   154  		{key: searchattribute.ExecutionDuration, value: "5h3m"},
   155  		{key: searchattribute.ExecutionDuration, value: "00:00:01"},
   156  		{key: searchattribute.ExecutionDuration, value: "00:00:61"},
   157  		{key: searchattribute.ExecutionDuration, value: "bad value"},
   158  		{key: searchattribute.WorkflowID, value: "should not be modified"},
   159  	}
   160  	expected := []struct {
   161  		value     interface{}
   162  		returnErr bool
   163  	}{
   164  		{value: int64(1), returnErr: false},
   165  		{value: int64(1), returnErr: false},
   166  		{value: int64(18180000000000), returnErr: false},
   167  		{value: int64(1000000000), returnErr: false},
   168  		{value: nil, returnErr: true},
   169  		{value: nil, returnErr: true},
   170  		{value: "should not be modified", returnErr: false},
   171  	}
   172  
   173  	for i, testCase := range cases {
   174  		v, err := vi.Values(testCase.key, testCase.value)
   175  		if expected[i].returnErr {
   176  			s.Error(err)
   177  			var converterErr *query.ConverterError
   178  			s.ErrorAs(err, &converterErr)
   179  			continue
   180  		}
   181  		s.NoError(err)
   182  		s.Len(v, 1)
   183  		s.Equal(expected[i].value, v[0])
   184  	}
   185  }