|
| 1 | +// Copyright 2021 The Serverless Workflow Specification Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package model |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + val "github.com/serverlessworkflow/sdk-go/v2/validator" |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | +) |
| 23 | + |
| 24 | +func TestEventRefStructLevelValidation(t *testing.T) { |
| 25 | + type testCase struct { |
| 26 | + name string |
| 27 | + eventRef EventRef |
| 28 | + err string |
| 29 | + } |
| 30 | + |
| 31 | + testCases := []testCase{ |
| 32 | + { |
| 33 | + name: "valid resultEventTimeout", |
| 34 | + eventRef: EventRef{ |
| 35 | + TriggerEventRef: "example valid", |
| 36 | + ResultEventRef: "example valid", |
| 37 | + ResultEventTimeout: "PT1H", |
| 38 | + Invoke: InvokeKindSync, |
| 39 | + }, |
| 40 | + err: ``, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "invalid resultEventTimeout", |
| 44 | + eventRef: EventRef{ |
| 45 | + TriggerEventRef: "example invalid", |
| 46 | + ResultEventRef: "example invalid red", |
| 47 | + ResultEventTimeout: "10hs", |
| 48 | + Invoke: InvokeKindSync, |
| 49 | + }, |
| 50 | + err: `Key: 'EventRef.ResultEventTimeout' Error:Field validation for 'ResultEventTimeout' failed on the 'iso8601duration' tag`, |
| 51 | + }, |
| 52 | + } |
| 53 | + |
| 54 | + for _, tc := range testCases { |
| 55 | + t.Run(tc.name, func(t *testing.T) { |
| 56 | + err := val.GetValidator().Struct(tc.eventRef) |
| 57 | + |
| 58 | + if tc.err != "" { |
| 59 | + assert.Error(t, err) |
| 60 | + assert.Regexp(t, tc.err, err) |
| 61 | + return |
| 62 | + } |
| 63 | + assert.NoError(t, err) |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
0 commit comments