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

     1  // The MIT License
     2  //
     3  // Copyright (c) 2024 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  //
    12  // The above copyright notice and this permission notice shall be included in
    13  // all copies or substantial portions of the Software.
    14  //
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    21  // THE SOFTWARE.
    22  
    23  package query
    24  
    25  import (
    26  	"errors"
    27  	"testing"
    28  	"time"
    29  
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func TestParseExecutionDurationStr(t *testing.T) {
    34  	s := assert.New(t)
    35  
    36  	testCases := []struct {
    37  		input         string
    38  		expectedValue time.Duration
    39  		expectedErr   error
    40  	}{
    41  		{
    42  			input:         "123",
    43  			expectedValue: time.Duration(123),
    44  		},
    45  		{
    46  			input:         "123s",
    47  			expectedValue: 123 * time.Second,
    48  		},
    49  		{
    50  			input:         "123m",
    51  			expectedValue: 123 * time.Minute,
    52  		},
    53  		{
    54  			input:         "123h",
    55  			expectedValue: 123 * time.Hour,
    56  		},
    57  		{
    58  			input:         "123d",
    59  			expectedValue: 123 * 24 * time.Hour,
    60  		},
    61  		{
    62  			input:         "01:02:03",
    63  			expectedValue: 1*time.Hour + 2*time.Minute + 3*time.Second,
    64  		},
    65  		{
    66  			input:       "01:60:03",
    67  			expectedErr: errors.New("invalid duration"),
    68  		},
    69  		{
    70  			input:       "123q",
    71  			expectedErr: errors.New("invalid duration"),
    72  		},
    73  	}
    74  
    75  	for _, tc := range testCases {
    76  		got, err := ParseExecutionDurationStr(tc.input)
    77  		if tc.expectedErr == nil {
    78  			s.Equal(tc.expectedValue, got)
    79  			s.NoError(err)
    80  		} else {
    81  			s.Error(err)
    82  			s.Contains(err.Error(), tc.expectedErr.Error())
    83  		}
    84  	}
    85  }