github.com/oam-dev/kubevela@v1.9.11/pkg/workflow/providers/time/date_test.go (about) 1 /* 2 Copyright 2021. The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package time 18 19 import ( 20 "testing" 21 22 "github.com/pkg/errors" 23 "github.com/stretchr/testify/require" 24 25 "github.com/kubevela/workflow/pkg/cue/model/value" 26 "github.com/kubevela/workflow/pkg/providers" 27 ) 28 29 func TestTimestamp(t *testing.T) { 30 testcases := map[string]struct { 31 from string 32 expected int64 33 expectedErr error 34 }{ 35 "test convert date with default time layout": { 36 from: `date: "2021-11-07T01:47:51Z" 37 layout: ""`, 38 expected: 1636249671, 39 expectedErr: nil, 40 }, 41 "test convert date with RFC3339 layout": { 42 from: `date: "2021-11-07T01:47:51Z" 43 layout: "2006-01-02T15:04:05Z07:00"`, 44 expected: 1636249671, 45 expectedErr: nil, 46 }, 47 "test convert date with RFC1123 layout": { 48 from: `date: "Fri, 01 Mar 2019 15:00:00 GMT" 49 layout: "Mon, 02 Jan 2006 15:04:05 MST"`, 50 expected: 1551452400, 51 expectedErr: nil, 52 }, 53 "test convert date without time layout": { 54 from: `date: "2021-11-07T01:47:51Z"`, 55 expected: 0, 56 expectedErr: errors.New("failed to lookup value: var(path=layout) not exist"), 57 }, 58 "test convert without date": { 59 from: ``, 60 expected: 0, 61 expectedErr: errors.New("failed to lookup value: var(path=date) not exist"), 62 }, 63 "test convert date with wrong time layout": { 64 from: `date: "2021-11-07T01:47:51Z" 65 layout: "Mon, 02 Jan 2006 15:04:05 MST"`, 66 expected: 0, 67 expectedErr: errors.New(`parsing time "2021-11-07T01:47:51Z" as "Mon, 02 Jan 2006 15:04:05 MST": cannot parse "2021-11-07T01:47:51Z" as "Mon"`), 68 }, 69 } 70 71 for name, tc := range testcases { 72 t.Run(name, func(t *testing.T) { 73 r := require.New(t) 74 v, err := value.NewValue(tc.from, nil, "") 75 r.NoError(err) 76 prd := &provider{} 77 err = prd.Timestamp(nil, nil, v, nil) 78 if tc.expectedErr != nil { 79 r.Equal(tc.expectedErr.Error(), err.Error()) 80 return 81 } 82 r.NoError(err) 83 expected, err := v.LookupValue("timestamp") 84 r.NoError(err) 85 ret, err := expected.CueValue().Int64() 86 r.NoError(err) 87 r.Equal(tc.expected, ret) 88 }) 89 } 90 } 91 92 func TestDate(t *testing.T) { 93 testcases := map[string]struct { 94 from string 95 expected string 96 expectedErr error 97 }{ 98 "test convert timestamp to default time layout": { 99 from: `timestamp: 1636249671 100 layout: "" 101 `, 102 expected: "2021-11-07T01:47:51Z", 103 expectedErr: nil, 104 }, 105 "test convert date to RFC3339 layout": { 106 from: `timestamp: 1636249671 107 layout: "2006-01-02T15:04:05Z07:00" 108 `, 109 expected: "2021-11-07T01:47:51Z", 110 expectedErr: nil, 111 }, 112 "test convert date to RFC1123 layout": { 113 from: `timestamp: 1551452400 114 layout: "Mon, 02 Jan 2006 15:04:05 MST" 115 `, 116 expected: "Fri, 01 Mar 2019 15:00:00 UTC", 117 expectedErr: nil, 118 }, 119 "test convert date without time layout": { 120 from: `timestamp: 1551452400`, 121 expected: "", 122 expectedErr: errors.New("failed to lookup value: var(path=layout) not exist"), 123 }, 124 "test convert without timestamp": { 125 from: ``, 126 expected: "", 127 expectedErr: errors.New("failed to lookup value: var(path=timestamp) not exist"), 128 }, 129 } 130 131 for name, tc := range testcases { 132 t.Run(name, func(t *testing.T) { 133 r := require.New(t) 134 v, err := value.NewValue(tc.from, nil, "") 135 r.NoError(err) 136 prd := &provider{} 137 err = prd.Date(nil, nil, v, nil) 138 if tc.expectedErr != nil { 139 r.Equal(tc.expectedErr.Error(), err.Error()) 140 return 141 } 142 r.NoError(err) 143 expected, err := v.LookupValue("date") 144 r.NoError(err) 145 ret, err := expected.CueValue().String() 146 r.NoError(err) 147 r.Equal(tc.expected, ret) 148 }) 149 } 150 } 151 152 func TestInstall(t *testing.T) { 153 p := providers.NewProviders() 154 Install(p) 155 h, ok := p.GetHandler("time", "timestamp") 156 r := require.New(t) 157 r.Equal(ok, true) 158 r.Equal(h != nil, true) 159 160 h, ok = p.GetHandler("time", "date") 161 r.Equal(ok, true) 162 r.Equal(h != nil, true) 163 }