go.temporal.io/server@v1.23.0/common/persistence/visibility/store/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 query
    26  
    27  import (
    28  	"encoding/json"
    29  	"errors"
    30  	"fmt"
    31  	"testing"
    32  
    33  	"github.com/temporalio/sqlparser"
    34  
    35  	"github.com/stretchr/testify/assert"
    36  )
    37  
    38  type (
    39  	testNameInterceptor   struct{}
    40  	testValuesInterceptor struct{}
    41  )
    42  
    43  func (t *testNameInterceptor) Name(name string, usage FieldNameUsage) (string, error) {
    44  	if name == "error" {
    45  		return "", errors.New("interceptor error")
    46  	}
    47  
    48  	return name + "1", nil
    49  }
    50  
    51  func (t *testValuesInterceptor) Values(name string, values ...interface{}) ([]interface{}, error) {
    52  	if name == "error" {
    53  		return nil, errors.New("interceptor error")
    54  	}
    55  
    56  	var result []interface{}
    57  	for _, value := range values {
    58  		if name == "ExecutionStatus" {
    59  			intVal, isIntVal := value.(int64)
    60  			if isIntVal {
    61  				result = append(result, fmt.Sprintf("Status%v", intVal))
    62  				continue
    63  			}
    64  		}
    65  		result = append(result, value)
    66  	}
    67  	return result, nil
    68  }
    69  
    70  func TestNameInterceptor(t *testing.T) {
    71  	c := getTestConverter(&testNameInterceptor{}, nil)
    72  
    73  	queryParams, err := c.ConvertWhereOrderBy("ExecutionStatus='Running' order by StartTime")
    74  	assert.NoError(t, err)
    75  	actualQueryMap, _ := queryParams.Query.Source()
    76  	actualQueryJson, _ := json.Marshal(actualQueryMap)
    77  	assert.Equal(t, `{"bool":{"filter":{"match":{"ExecutionStatus1":{"query":"Running"}}}}}`, string(actualQueryJson))
    78  	var actualSorterMaps []interface{}
    79  	for _, sorter := range queryParams.Sorter {
    80  		actualSorterMap, _ := sorter.Source()
    81  		actualSorterMaps = append(actualSorterMaps, actualSorterMap)
    82  	}
    83  	actualSorterJson, _ := json.Marshal(actualSorterMaps)
    84  	assert.Equal(t, `[{"StartTime1":{"order":"asc"}}]`, string(actualSorterJson))
    85  
    86  	_, err = c.ConvertWhereOrderBy("error='Running' order by StartTime")
    87  	assert.Error(t, err)
    88  	assert.Contains(t, err.Error(), "interceptor error")
    89  }
    90  
    91  func TestValuesInterceptor(t *testing.T) {
    92  	c := getTestConverter(nil, &testValuesInterceptor{})
    93  	queryParams, err := c.ConvertWhereOrderBy("ExecutionStatus=1")
    94  	assert.NoError(t, err)
    95  	actualQueryMap, _ := queryParams.Query.Source()
    96  	actualQueryJson, _ := json.Marshal(actualQueryMap)
    97  	assert.Equal(t, `{"bool":{"filter":{"match":{"ExecutionStatus":{"query":"Status1"}}}}}`, string(actualQueryJson))
    98  
    99  	queryParams, err = c.ConvertWhereOrderBy("ExecutionStatus in (1,2)")
   100  	assert.NoError(t, err)
   101  	actualQueryMap, _ = queryParams.Query.Source()
   102  	actualQueryJson, _ = json.Marshal(actualQueryMap)
   103  	assert.Equal(t, `{"bool":{"filter":{"terms":{"ExecutionStatus":["Status1","Status2"]}}}}`, string(actualQueryJson))
   104  
   105  	queryParams, err = c.ConvertWhereOrderBy("ExecutionStatus between 5 and 7")
   106  	assert.NoError(t, err)
   107  	actualQueryMap, _ = queryParams.Query.Source()
   108  	actualQueryJson, _ = json.Marshal(actualQueryMap)
   109  	assert.Equal(t, `{"bool":{"filter":{"range":{"ExecutionStatus":{"from":"Status5","include_lower":true,"include_upper":true,"to":"Status7"}}}}}`, string(actualQueryJson))
   110  
   111  	_, err = c.ConvertWhereOrderBy("error='Running'")
   112  	assert.Error(t, err)
   113  	assert.Contains(t, err.Error(), "interceptor error")
   114  }
   115  
   116  func getTestConverter(fnInterceptor FieldNameInterceptor, fvInterceptor FieldValuesInterceptor) *Converter {
   117  	whereConverter := NewWhereConverter(
   118  		nil,
   119  		nil,
   120  		NewRangeCondConverter(fnInterceptor, fvInterceptor, false),
   121  		NewComparisonExprConverter(fnInterceptor, fvInterceptor, map[string]struct{}{sqlparser.EqualStr: {}, sqlparser.InStr: {}}),
   122  		nil)
   123  	return NewConverter(fnInterceptor, whereConverter)
   124  }