github.com/sfdevops1/terrra4orm@v0.11.12-beta1/helper/customdiff/condition_test.go (about)

     1  package customdiff
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func TestIf(t *testing.T) {
    11  	t.Run("true", func(t *testing.T) {
    12  		var condCalled, customCalled bool
    13  		var gotOld, gotNew string
    14  
    15  		provider := testProvider(
    16  			map[string]*schema.Schema{
    17  				"foo": {
    18  					Type:     schema.TypeString,
    19  					Optional: true,
    20  				},
    21  			},
    22  			If(
    23  				func(d *schema.ResourceDiff, meta interface{}) bool {
    24  					condCalled = true
    25  					old, new := d.GetChange("foo")
    26  					gotOld = old.(string)
    27  					gotNew = new.(string)
    28  					return true
    29  				},
    30  				func(d *schema.ResourceDiff, meta interface{}) error {
    31  					customCalled = true
    32  					return errors.New("bad")
    33  				},
    34  			),
    35  		)
    36  
    37  		_, err := testDiff(
    38  			provider,
    39  			map[string]string{
    40  				"foo": "bar",
    41  			},
    42  			map[string]string{
    43  				"foo": "baz",
    44  			},
    45  		)
    46  
    47  		if err == nil {
    48  			t.Fatal("Diff succeeded; want error")
    49  		}
    50  		if got, want := err.Error(), "bad"; got != want {
    51  			t.Fatalf("wrong error message %q; want %q", got, want)
    52  		}
    53  
    54  		if !condCalled {
    55  			t.Error("condition callback was not called")
    56  		} else {
    57  			if got, want := gotOld, "bar"; got != want {
    58  				t.Errorf("wrong old value %q; want %q", got, want)
    59  			}
    60  			if got, want := gotNew, "baz"; got != want {
    61  				t.Errorf("wrong new value %q; want %q", got, want)
    62  			}
    63  		}
    64  
    65  		if !customCalled {
    66  			t.Error("customize callback was not called")
    67  		}
    68  	})
    69  	t.Run("false", func(t *testing.T) {
    70  		var condCalled, customCalled bool
    71  		var gotOld, gotNew string
    72  
    73  		provider := testProvider(
    74  			map[string]*schema.Schema{
    75  				"foo": {
    76  					Type:     schema.TypeString,
    77  					Optional: true,
    78  				},
    79  			},
    80  			If(
    81  				func(d *schema.ResourceDiff, meta interface{}) bool {
    82  					condCalled = true
    83  					old, new := d.GetChange("foo")
    84  					gotOld = old.(string)
    85  					gotNew = new.(string)
    86  					return false
    87  				},
    88  				func(d *schema.ResourceDiff, meta interface{}) error {
    89  					customCalled = true
    90  					return errors.New("bad")
    91  				},
    92  			),
    93  		)
    94  
    95  		_, err := testDiff(
    96  			provider,
    97  			map[string]string{
    98  				"foo": "bar",
    99  			},
   100  			map[string]string{
   101  				"foo": "baz",
   102  			},
   103  		)
   104  
   105  		if err != nil {
   106  			t.Fatalf("Diff error %q; want success", err.Error())
   107  		}
   108  
   109  		if !condCalled {
   110  			t.Error("condition callback was not called")
   111  		} else {
   112  			if got, want := gotOld, "bar"; got != want {
   113  				t.Errorf("wrong old value %q; want %q", got, want)
   114  			}
   115  			if got, want := gotNew, "baz"; got != want {
   116  				t.Errorf("wrong new value %q; want %q", got, want)
   117  			}
   118  		}
   119  
   120  		if customCalled {
   121  			t.Error("customize callback was called (should not have been)")
   122  		}
   123  	})
   124  }
   125  
   126  func TestIfValueChange(t *testing.T) {
   127  	t.Run("true", func(t *testing.T) {
   128  		var condCalled, customCalled bool
   129  		var gotOld, gotNew string
   130  
   131  		provider := testProvider(
   132  			map[string]*schema.Schema{
   133  				"foo": {
   134  					Type:     schema.TypeString,
   135  					Optional: true,
   136  				},
   137  			},
   138  			IfValueChange(
   139  				"foo",
   140  				func(old, new, meta interface{}) bool {
   141  					condCalled = true
   142  					gotOld = old.(string)
   143  					gotNew = new.(string)
   144  					return true
   145  				},
   146  				func(d *schema.ResourceDiff, meta interface{}) error {
   147  					customCalled = true
   148  					return errors.New("bad")
   149  				},
   150  			),
   151  		)
   152  
   153  		_, err := testDiff(
   154  			provider,
   155  			map[string]string{
   156  				"foo": "bar",
   157  			},
   158  			map[string]string{
   159  				"foo": "baz",
   160  			},
   161  		)
   162  
   163  		if err == nil {
   164  			t.Fatal("Diff succeeded; want error")
   165  		}
   166  		if got, want := err.Error(), "bad"; got != want {
   167  			t.Fatalf("wrong error message %q; want %q", got, want)
   168  		}
   169  
   170  		if !condCalled {
   171  			t.Error("condition callback was not called")
   172  		} else {
   173  			if got, want := gotOld, "bar"; got != want {
   174  				t.Errorf("wrong old value %q; want %q", got, want)
   175  			}
   176  			if got, want := gotNew, "baz"; got != want {
   177  				t.Errorf("wrong new value %q; want %q", got, want)
   178  			}
   179  		}
   180  
   181  		if !customCalled {
   182  			t.Error("customize callback was not called")
   183  		}
   184  	})
   185  	t.Run("false", func(t *testing.T) {
   186  		var condCalled, customCalled bool
   187  		var gotOld, gotNew string
   188  
   189  		provider := testProvider(
   190  			map[string]*schema.Schema{
   191  				"foo": {
   192  					Type:     schema.TypeString,
   193  					Optional: true,
   194  				},
   195  			},
   196  			IfValueChange(
   197  				"foo",
   198  				func(old, new, meta interface{}) bool {
   199  					condCalled = true
   200  					gotOld = old.(string)
   201  					gotNew = new.(string)
   202  					return false
   203  				},
   204  				func(d *schema.ResourceDiff, meta interface{}) error {
   205  					customCalled = true
   206  					return errors.New("bad")
   207  				},
   208  			),
   209  		)
   210  
   211  		_, err := testDiff(
   212  			provider,
   213  			map[string]string{
   214  				"foo": "bar",
   215  			},
   216  			map[string]string{
   217  				"foo": "baz",
   218  			},
   219  		)
   220  
   221  		if err != nil {
   222  			t.Fatalf("Diff error %q; want success", err.Error())
   223  		}
   224  
   225  		if !condCalled {
   226  			t.Error("condition callback was not called")
   227  		} else {
   228  			if got, want := gotOld, "bar"; got != want {
   229  				t.Errorf("wrong old value %q; want %q", got, want)
   230  			}
   231  			if got, want := gotNew, "baz"; got != want {
   232  				t.Errorf("wrong new value %q; want %q", got, want)
   233  			}
   234  		}
   235  
   236  		if customCalled {
   237  			t.Error("customize callback was called (should not have been)")
   238  		}
   239  	})
   240  }
   241  
   242  func TestIfValue(t *testing.T) {
   243  	t.Run("true", func(t *testing.T) {
   244  		var condCalled, customCalled bool
   245  		var gotValue string
   246  
   247  		provider := testProvider(
   248  			map[string]*schema.Schema{
   249  				"foo": {
   250  					Type:     schema.TypeString,
   251  					Optional: true,
   252  				},
   253  			},
   254  			IfValue(
   255  				"foo",
   256  				func(value, meta interface{}) bool {
   257  					condCalled = true
   258  					gotValue = value.(string)
   259  					return true
   260  				},
   261  				func(d *schema.ResourceDiff, meta interface{}) error {
   262  					customCalled = true
   263  					return errors.New("bad")
   264  				},
   265  			),
   266  		)
   267  
   268  		_, err := testDiff(
   269  			provider,
   270  			map[string]string{
   271  				"foo": "bar",
   272  			},
   273  			map[string]string{
   274  				"foo": "baz",
   275  			},
   276  		)
   277  
   278  		if err == nil {
   279  			t.Fatal("Diff succeeded; want error")
   280  		}
   281  		if got, want := err.Error(), "bad"; got != want {
   282  			t.Fatalf("wrong error message %q; want %q", got, want)
   283  		}
   284  
   285  		if !condCalled {
   286  			t.Error("condition callback was not called")
   287  		} else {
   288  			if got, want := gotValue, "baz"; got != want {
   289  				t.Errorf("wrong value %q; want %q", got, want)
   290  			}
   291  		}
   292  
   293  		if !customCalled {
   294  			t.Error("customize callback was not called")
   295  		}
   296  	})
   297  	t.Run("false", func(t *testing.T) {
   298  		var condCalled, customCalled bool
   299  		var gotValue string
   300  
   301  		provider := testProvider(
   302  			map[string]*schema.Schema{
   303  				"foo": {
   304  					Type:     schema.TypeString,
   305  					Optional: true,
   306  				},
   307  			},
   308  			IfValue(
   309  				"foo",
   310  				func(value, meta interface{}) bool {
   311  					condCalled = true
   312  					gotValue = value.(string)
   313  					return false
   314  				},
   315  				func(d *schema.ResourceDiff, meta interface{}) error {
   316  					customCalled = true
   317  					return errors.New("bad")
   318  				},
   319  			),
   320  		)
   321  
   322  		_, err := testDiff(
   323  			provider,
   324  			map[string]string{
   325  				"foo": "bar",
   326  			},
   327  			map[string]string{
   328  				"foo": "baz",
   329  			},
   330  		)
   331  
   332  		if err != nil {
   333  			t.Fatalf("Diff error %q; want success", err.Error())
   334  		}
   335  
   336  		if !condCalled {
   337  			t.Error("condition callback was not called")
   338  		} else {
   339  			if got, want := gotValue, "baz"; got != want {
   340  				t.Errorf("wrong value %q; want %q", got, want)
   341  			}
   342  		}
   343  
   344  		if customCalled {
   345  			t.Error("customize callback was called (should not have been)")
   346  		}
   347  	})
   348  }