github.com/kubevela/workflow@v0.6.0/pkg/utils/test_utils_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 utils
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/require"
    24  	"k8s.io/apimachinery/pkg/api/errors"
    25  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  )
    27  
    28  func TestAlreadyExistMatcher(t *testing.T) {
    29  	type want struct {
    30  		success bool
    31  		err     error
    32  	}
    33  
    34  	cases := map[string]struct {
    35  		input interface{}
    36  		want  want
    37  	}{
    38  		"Matches": {
    39  			input: errors.NewAlreadyExists(schema.GroupResource{
    40  				Group:    "g",
    41  				Resource: "r",
    42  			}, "name"),
    43  			want: want{
    44  				success: true,
    45  				err:     nil,
    46  			},
    47  		},
    48  		"Does not match": {
    49  			input: errors.NewNotFound(schema.GroupResource{
    50  				Group:    "g",
    51  				Resource: "r",
    52  			}, "name"),
    53  			want: want{
    54  				success: false,
    55  				err:     nil,
    56  			},
    57  		},
    58  		"Does not match nil": {
    59  			input: nil,
    60  			want: want{
    61  				success: false,
    62  				err:     nil,
    63  			},
    64  		},
    65  	}
    66  	matcher := AlreadyExistMatcher{}
    67  	for name, tc := range cases {
    68  		r := require.New(t)
    69  		success, err := matcher.Match(tc.input)
    70  		t.Log(fmt.Sprint("Running test: ", name))
    71  		r.Equal(tc.want.success, success)
    72  		if tc.want.err == nil {
    73  			r.NoError(err)
    74  		} else {
    75  			r.Error(tc.want.err, err)
    76  		}
    77  	}
    78  
    79  	// Error messages
    80  	r := require.New(t)
    81  	r.Equal("Expected\n    <string>: myerror\nto be already exist", matcher.FailureMessage("myerror"))
    82  	r.Equal("Expected\n    <string>: myerror\nnot to be already exist", matcher.NegatedFailureMessage("myerror"))
    83  }
    84  
    85  func TestNotFoundMatcher(t *testing.T) {
    86  	type want struct {
    87  		success bool
    88  		err     error
    89  	}
    90  
    91  	cases := map[string]struct {
    92  		input interface{}
    93  		want  want
    94  	}{
    95  		"Does not matche": {
    96  			input: errors.NewAlreadyExists(schema.GroupResource{
    97  				Group:    "g",
    98  				Resource: "r",
    99  			}, "name"),
   100  			want: want{
   101  				success: false,
   102  				err:     nil,
   103  			},
   104  		},
   105  		"Matches": {
   106  			input: errors.NewNotFound(schema.GroupResource{
   107  				Group:    "g",
   108  				Resource: "r",
   109  			}, "name"),
   110  			want: want{
   111  				success: true,
   112  				err:     nil,
   113  			},
   114  		},
   115  		"Does not match nil": {
   116  			input: nil,
   117  			want: want{
   118  				success: false,
   119  				err:     nil,
   120  			},
   121  		},
   122  	}
   123  
   124  	matcher := NotFoundMatcher{}
   125  	for name, tc := range cases {
   126  		r := require.New(t)
   127  		success, err := matcher.Match(tc.input)
   128  		t.Log(fmt.Sprint("Running test: ", name))
   129  		r.Equal(tc.want.success, success)
   130  		if tc.want.err == nil {
   131  			r.NoError(err)
   132  		} else {
   133  			r.Equal(tc.want.err, err)
   134  		}
   135  	}
   136  
   137  	// Error messages
   138  	r := require.New(t)
   139  	r.Equal("Expected\n    <string>: myerror\nto be not found", matcher.FailureMessage("myerror"))
   140  	r.Equal("Expected\n    <string>: myerror\nnot to be not found", matcher.NegatedFailureMessage("myerror"))
   141  }
   142  
   143  func TestErrorMatcher(t *testing.T) {
   144  	type input struct {
   145  		expected error
   146  		input    error
   147  	}
   148  
   149  	type want struct {
   150  		success               bool
   151  		err                   error
   152  		failureMessage        string
   153  		negatedFailureMessage string
   154  	}
   155  
   156  	cases := map[string]struct {
   157  		input input
   158  		want  want
   159  	}{
   160  		"Matches": {
   161  			input: input{
   162  				expected: fmt.Errorf("my error"),
   163  				input:    fmt.Errorf("my error"),
   164  			},
   165  			want: want{
   166  				success:               true,
   167  				err:                   nil,
   168  				failureMessage:        "Expected\n    <string>: my error\nto equal\n    <string>: my error",
   169  				negatedFailureMessage: "Expected\n    <string>: my error\nnot to equal\n    <string>: my error",
   170  			},
   171  		},
   172  		"Matches nil": {
   173  			input: input{
   174  				expected: nil,
   175  				input:    nil,
   176  			},
   177  			want: want{
   178  				success:               true,
   179  				err:                   nil,
   180  				failureMessage:        "Expected\n    <nil>: nil\nto equal\n    <nil>: nil",
   181  				negatedFailureMessage: "Expected\n    <nil>: nil\nnot to equal\n    <nil>: nil",
   182  			},
   183  		},
   184  		"Does not match": {
   185  			input: input{
   186  				expected: fmt.Errorf("my error"),
   187  				input:    fmt.Errorf("my other error"),
   188  			},
   189  			want: want{
   190  				success:               false,
   191  				err:                   nil,
   192  				failureMessage:        "Expected\n    <string>: my other error\nto equal\n    <string>: my error",
   193  				negatedFailureMessage: "Expected\n    <string>: my other error\nnot to equal\n    <string>: my error",
   194  			},
   195  		},
   196  		"Does not match nil": {
   197  			input: input{
   198  				expected: fmt.Errorf("my error"),
   199  				input:    nil,
   200  			},
   201  			want: want{
   202  				success:               false,
   203  				err:                   nil,
   204  				failureMessage:        "Expected\n    <nil>: nil\nto equal\n    <string>: my error",
   205  				negatedFailureMessage: "Expected\n    <nil>: nil\nnot to equal\n    <string>: my error",
   206  			},
   207  		},
   208  	}
   209  	for name, tc := range cases {
   210  		r := require.New(t)
   211  		matcher := ErrorMatcher{
   212  			ExpectedError: tc.input.expected,
   213  		}
   214  		success, err := matcher.Match(tc.input.input)
   215  		t.Log(fmt.Sprint("Running test: ", name))
   216  		r.Equal(tc.want.success, success)
   217  		if tc.want.err == nil {
   218  			r.NoError(err)
   219  		} else {
   220  			r.Equal(tc.want.err, err)
   221  		}
   222  
   223  		r.Equal(tc.want.failureMessage, matcher.FailureMessage(tc.input.input))
   224  		r.Equal(tc.want.negatedFailureMessage, matcher.NegatedFailureMessage(tc.input.input))
   225  	}
   226  }