github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/states/resource_test.go (about)

     1  package states
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestResourceInstanceDeposeCurrentObject(t *testing.T) {
     8  	obj := &ResourceInstanceObjectSrc{
     9  		// Empty for the sake of this test, because we're just going to
    10  		// compare by pointer below anyway.
    11  	}
    12  
    13  	is := NewResourceInstance()
    14  	is.Current = obj
    15  	var dk DeposedKey
    16  
    17  	t.Run("first depose", func(t *testing.T) {
    18  		dk = is.deposeCurrentObject(NotDeposed) // dk is randomly-generated but should be eight characters long
    19  		t.Logf("deposedKey is %q", dk)
    20  
    21  		if got := is.Current; got != nil {
    22  			t.Errorf("current is %#v; want nil", got)
    23  		}
    24  		if got, want := is.Deposed[dk], obj; got != want {
    25  			t.Errorf("deposed object pointer is %#v; want %#v", got, want)
    26  		}
    27  		if got, want := len(is.Deposed), 1; got != want {
    28  			t.Errorf("wrong len(is.Deposed) %d; want %d", got, want)
    29  		}
    30  		if got, want := len(dk), 8; got != want {
    31  			t.Errorf("wrong len(deposedkey) %d; want %d", got, want)
    32  		}
    33  	})
    34  
    35  	t.Run("second depose", func(t *testing.T) {
    36  		notDK := is.deposeCurrentObject(NotDeposed)
    37  		if notDK != NotDeposed {
    38  			t.Errorf("got deposedKey %q; want NotDeposed", notDK)
    39  		}
    40  
    41  		// Make sure we really did abort early, and haven't corrupted the
    42  		// state somehow.
    43  		if got := is.Current; got != nil {
    44  			t.Errorf("current is %#v; want nil", got)
    45  		}
    46  		if got, want := is.Deposed[dk], obj; got != want {
    47  			t.Errorf("deposed object pointer is %#v; want %#v", got, want)
    48  		}
    49  		if got, want := len(is.Deposed), 1; got != want {
    50  			t.Errorf("wrong len(is.Deposed) %d; want %d", got, want)
    51  		}
    52  		if got, want := len(dk), 8; got != want {
    53  			t.Errorf("wrong len(deposedkey) %d; want %d", got, want)
    54  		}
    55  	})
    56  }