go.temporal.io/server@v1.23.0/common/persistence/visibility/store/standard/converter_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2021 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 standard
    26  
    27  import (
    28  	"fmt"
    29  	"testing"
    30  	"time"
    31  
    32  	"github.com/stretchr/testify/assert"
    33  	enumspb "go.temporal.io/api/enums/v1"
    34  
    35  	"go.temporal.io/server/common/convert"
    36  	"go.temporal.io/server/common/persistence/sql/sqlplugin"
    37  	"go.temporal.io/server/common/searchattribute"
    38  )
    39  
    40  var startTimeFrom = time.Now().Add(-time.Hour)
    41  var startTimeTo = time.Now()
    42  var startTimeRangeFilter = fmt.Sprintf(`StartTime BETWEEN "%v" AND "%v"`, startTimeFrom.Format(time.RFC3339Nano), startTimeTo.Format(time.RFC3339Nano))
    43  
    44  var supportedQuery = map[string]*sqlplugin.VisibilitySelectFilter{
    45  	"":                   {},
    46  	startTimeRangeFilter: {MinTime: &startTimeFrom, MaxTime: &startTimeTo},
    47  	`WorkflowId = "abc"`: {WorkflowID: convert.StringPtr("abc")},
    48  	`WorkflowId = "abc" AND ` + startTimeRangeFilter:          {WorkflowID: convert.StringPtr("abc"), MinTime: &startTimeFrom, MaxTime: &startTimeTo},
    49  	startTimeRangeFilter + ` AND WorkflowId = "abc"`:          {WorkflowID: convert.StringPtr("abc"), MinTime: &startTimeFrom, MaxTime: &startTimeTo},
    50  	`WorkflowType = "abc"`:                                    {WorkflowTypeName: convert.StringPtr("abc")},
    51  	`WorkflowType = "abc" AND ` + startTimeRangeFilter:        {WorkflowTypeName: convert.StringPtr("abc"), MinTime: &startTimeFrom, MaxTime: &startTimeTo},
    52  	startTimeRangeFilter + ` AND WorkflowType = "abc"`:        {WorkflowTypeName: convert.StringPtr("abc"), MinTime: &startTimeFrom, MaxTime: &startTimeTo},
    53  	`ExecutionStatus = "Running"`:                             {Status: int32(enumspb.WORKFLOW_EXECUTION_STATUS_RUNNING)},
    54  	`ExecutionStatus = "Running" AND ` + startTimeRangeFilter: {Status: int32(enumspb.WORKFLOW_EXECUTION_STATUS_RUNNING), MinTime: &startTimeFrom, MaxTime: &startTimeTo},
    55  	startTimeRangeFilter + ` AND ExecutionStatus = "Running"`: {Status: int32(enumspb.WORKFLOW_EXECUTION_STATUS_RUNNING), MinTime: &startTimeFrom, MaxTime: &startTimeTo},
    56  }
    57  
    58  // This is not an exhaustive list.
    59  var unsupportedQuery = []string{
    60  	fmt.Sprintf(`StartTime NOT BETWEEN "%v" AND "%v"`, startTimeFrom.Format(time.RFC3339Nano), startTimeTo.Format(time.RFC3339Nano)),
    61  	"order by WorkflowID",
    62  	`WorkflowID = "abc" AND WorkflowType = "xyz"`,
    63  	`ExecutionStatus = "Running" AND WorkflowType = "xyz"`,
    64  	`WorkflowID = "abc" OR WorkflowType = "xyz"`,
    65  	`WorkflowID != "abc"`,
    66  	`StartTime < "2022-01-15T05:43:12.74127Z"`,
    67  }
    68  
    69  func TestSupportedQueryFilters(t *testing.T) {
    70  	for query, expectedFilter := range supportedQuery {
    71  		converter := newQueryConverter("test-namespace", searchattribute.TestNameTypeMap, nil)
    72  		filter, err := converter.GetFilter(query)
    73  		assert.NoError(t, err)
    74  
    75  		assert.EqualValues(t, expectedFilter.WorkflowID, filter.WorkflowID)
    76  		assert.EqualValues(t, expectedFilter.WorkflowTypeName, filter.WorkflowTypeName)
    77  		assert.EqualValues(t, expectedFilter.Status, filter.Status)
    78  
    79  		if expectedFilter.MinTime == nil {
    80  			assert.True(t, time.Unix(0, 0).Equal(*filter.MinTime))
    81  		} else {
    82  			assert.NotNil(t, filter.MinTime)
    83  			assert.True(t, expectedFilter.MinTime.Equal(*filter.MinTime))
    84  		}
    85  
    86  		if expectedFilter.MaxTime == nil {
    87  			assert.True(t, filter.MaxTime.After(time.Now()))
    88  		} else {
    89  			assert.NotNil(t, filter.MaxTime)
    90  			assert.True(t, expectedFilter.MaxTime.Equal(*filter.MaxTime))
    91  		}
    92  	}
    93  }
    94  
    95  func TestUnsupportedQueryFilters(t *testing.T) {
    96  	for _, query := range unsupportedQuery {
    97  		converter := newQueryConverter("test-namespace", searchattribute.TestNameTypeMap, nil)
    98  		_, err := converter.GetFilter(query)
    99  		assert.Error(t, err)
   100  	}
   101  }