github.com/crossplane/upjet@v1.3.0/pkg/terraform/operation_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package terraform
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/google/go-cmp/cmp"
    11  	"github.com/pkg/errors"
    12  )
    13  
    14  func TestOperation(t *testing.T) {
    15  	testErr := errors.New("test error")
    16  	type args struct {
    17  		calls func(o *Operation)
    18  	}
    19  	type want struct {
    20  		checks func(o *Operation) bool
    21  		result bool
    22  	}
    23  
    24  	cases := map[string]struct {
    25  		args
    26  		want
    27  	}{
    28  		"Running": {
    29  			args: args{
    30  				calls: func(o *Operation) {
    31  					o.MarkStart("type")
    32  				},
    33  			},
    34  			want: want{
    35  				checks: func(o *Operation) bool {
    36  					return o.IsRunning() && !o.IsEnded()
    37  				},
    38  				result: true,
    39  			},
    40  		},
    41  		"Ended": {
    42  			args: args{
    43  				calls: func(o *Operation) {
    44  					o.MarkStart("type")
    45  					o.MarkEnd()
    46  				},
    47  			},
    48  			want: want{
    49  				checks: func(o *Operation) bool {
    50  					return !o.IsRunning() && o.IsEnded()
    51  				},
    52  				result: true,
    53  			},
    54  		},
    55  		"Flushed": {
    56  			args: args{
    57  				calls: func(o *Operation) {
    58  					o.MarkStart("type")
    59  					o.SetError(testErr)
    60  					o.MarkEnd()
    61  					o.Flush()
    62  				},
    63  			},
    64  			want: want{
    65  				checks: func(o *Operation) bool {
    66  					return o.Type == "" && o.startTime == nil && o.endTime == nil && o.err == nil
    67  				},
    68  				result: true,
    69  			},
    70  		},
    71  		"ClearedIncludingErrors": {
    72  			args: args{
    73  				calls: func(o *Operation) {
    74  					o.MarkStart("type")
    75  					o.SetError(testErr)
    76  					o.MarkEnd()
    77  					o.Clear(false)
    78  				},
    79  			},
    80  			want: want{
    81  				checks: func(o *Operation) bool {
    82  					return o.Type == "" && o.startTime == nil && o.endTime == nil && o.err == nil
    83  				},
    84  				result: true,
    85  			},
    86  		},
    87  		"ClearedPreservingErrors": {
    88  			args: args{
    89  				calls: func(o *Operation) {
    90  					o.MarkStart("type")
    91  					o.SetError(testErr)
    92  					o.MarkEnd()
    93  					o.Clear(true)
    94  				},
    95  			},
    96  			want: want{
    97  				checks: func(o *Operation) bool {
    98  					return o.Type == "" && o.startTime == nil && o.endTime == nil && errors.Is(o.err, testErr)
    99  				},
   100  				result: true,
   101  			},
   102  		},
   103  	}
   104  	for name, tc := range cases {
   105  		t.Run(name, func(t *testing.T) {
   106  			o := &Operation{}
   107  			tc.args.calls(o)
   108  			if diff := cmp.Diff(tc.want.result, tc.checks(o)); diff != "" {
   109  				t.Errorf("\n%s\nOperation(...): -want error, +got error:\n%s", name, diff)
   110  			}
   111  		})
   112  	}
   113  }