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

     1  package customdiff
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  )
     8  
     9  func TestComputedIf(t *testing.T) {
    10  	t.Run("true", func(t *testing.T) {
    11  		var condCalls int
    12  		var gotOld, gotNew string
    13  
    14  		provider := testProvider(
    15  			map[string]*schema.Schema{
    16  				"foo": {
    17  					Type:     schema.TypeString,
    18  					Optional: true,
    19  				},
    20  				"comp": {
    21  					Type:     schema.TypeString,
    22  					Computed: true,
    23  				},
    24  			},
    25  			ComputedIf("comp", func(d *schema.ResourceDiff, meta interface{}) bool {
    26  				// When we set "ForceNew", our CustomizeDiff function is actually
    27  				// called a second time to construct the "create" portion of
    28  				// the replace diff. On the second call, the old value is masked
    29  				// as "" to suggest that the object is being created rather than
    30  				// updated.
    31  
    32  				condCalls++
    33  				old, new := d.GetChange("foo")
    34  				gotOld = old.(string)
    35  				gotNew = new.(string)
    36  
    37  				return true
    38  			}),
    39  		)
    40  
    41  		diff, err := testDiff(
    42  			provider,
    43  			map[string]string{
    44  				"foo":  "bar",
    45  				"comp": "old",
    46  			},
    47  			map[string]string{
    48  				"foo": "baz",
    49  			},
    50  		)
    51  
    52  		if err != nil {
    53  			t.Fatalf("Diff failed with error: %s", err)
    54  		}
    55  
    56  		if condCalls != 1 {
    57  			t.Fatalf("Wrong number of conditional callback calls %d; want %d", condCalls, 1)
    58  		} else {
    59  			if got, want := gotOld, "bar"; got != want {
    60  				t.Errorf("wrong old value %q on first call; want %q", got, want)
    61  			}
    62  			if got, want := gotNew, "baz"; got != want {
    63  				t.Errorf("wrong new value %q on first call; want %q", got, want)
    64  			}
    65  		}
    66  
    67  		if !diff.Attributes["comp"].NewComputed {
    68  			t.Error("Attribute 'comp' is not marked as NewComputed")
    69  		}
    70  	})
    71  	t.Run("false", func(t *testing.T) {
    72  		var condCalls int
    73  		var gotOld, gotNew string
    74  
    75  		provider := testProvider(
    76  			map[string]*schema.Schema{
    77  				"foo": {
    78  					Type:     schema.TypeString,
    79  					Optional: true,
    80  				},
    81  				"comp": {
    82  					Type:     schema.TypeString,
    83  					Computed: true,
    84  				},
    85  			},
    86  			ComputedIf("comp", func(d *schema.ResourceDiff, meta interface{}) bool {
    87  				condCalls++
    88  				old, new := d.GetChange("foo")
    89  				gotOld = old.(string)
    90  				gotNew = new.(string)
    91  
    92  				return false
    93  			}),
    94  		)
    95  
    96  		diff, err := testDiff(
    97  			provider,
    98  			map[string]string{
    99  				"foo":  "bar",
   100  				"comp": "old",
   101  			},
   102  			map[string]string{
   103  				"foo": "baz",
   104  			},
   105  		)
   106  
   107  		if err != nil {
   108  			t.Fatalf("Diff failed with error: %s", err)
   109  		}
   110  
   111  		if condCalls != 1 {
   112  			t.Fatalf("Wrong number of conditional callback calls %d; want %d", condCalls, 1)
   113  		} else {
   114  			if got, want := gotOld, "bar"; got != want {
   115  				t.Errorf("wrong old value %q on first call; want %q", got, want)
   116  			}
   117  			if got, want := gotNew, "baz"; got != want {
   118  				t.Errorf("wrong new value %q on first call; want %q", got, want)
   119  			}
   120  		}
   121  
   122  		if diff.Attributes["comp"] != nil && diff.Attributes["comp"].NewComputed {
   123  			t.Error("Attribute 'foo' is marked as NewComputed, but should not be")
   124  		}
   125  	})
   126  }