github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/schema/set_test.go (about)

     1  package schema
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestSetAdd(t *testing.T) {
     9  	s := &Set{F: testSetInt}
    10  	s.Add(1)
    11  	s.Add(5)
    12  	s.Add(25)
    13  
    14  	expected := []interface{}{1, 25, 5}
    15  	actual := s.List()
    16  	if !reflect.DeepEqual(actual, expected) {
    17  		t.Fatalf("bad: %#v", actual)
    18  	}
    19  }
    20  
    21  func TestSetAdd_negative(t *testing.T) {
    22  	// Since we don't allow negative hashes, this should just hash to the
    23  	// same thing...
    24  	s := &Set{F: testSetInt}
    25  	s.Add(-1)
    26  	s.Add(1)
    27  
    28  	expected := []interface{}{-1}
    29  	actual := s.List()
    30  	if !reflect.DeepEqual(actual, expected) {
    31  		t.Fatalf("bad: %#v", actual)
    32  	}
    33  }
    34  
    35  func TestSetContains(t *testing.T) {
    36  	s := &Set{F: testSetInt}
    37  	s.Add(5)
    38  	s.Add(-5)
    39  
    40  	if s.Contains(2) {
    41  		t.Fatal("should not contain")
    42  	}
    43  	if !s.Contains(5) {
    44  		t.Fatal("should contain")
    45  	}
    46  	if !s.Contains(-5) {
    47  		t.Fatal("should contain")
    48  	}
    49  }
    50  
    51  func TestSetDifference(t *testing.T) {
    52  	s1 := &Set{F: testSetInt}
    53  	s2 := &Set{F: testSetInt}
    54  
    55  	s1.Add(1)
    56  	s1.Add(5)
    57  
    58  	s2.Add(5)
    59  	s2.Add(25)
    60  
    61  	difference := s1.Difference(s2)
    62  	difference.Add(2)
    63  
    64  	expected := []interface{}{1, 2}
    65  	actual := difference.List()
    66  	if !reflect.DeepEqual(actual, expected) {
    67  		t.Fatalf("bad: %#v", actual)
    68  	}
    69  }
    70  
    71  func TestSetIntersection(t *testing.T) {
    72  	s1 := &Set{F: testSetInt}
    73  	s2 := &Set{F: testSetInt}
    74  
    75  	s1.Add(1)
    76  	s1.Add(5)
    77  
    78  	s2.Add(5)
    79  	s2.Add(25)
    80  
    81  	intersection := s1.Intersection(s2)
    82  	intersection.Add(2)
    83  
    84  	expected := []interface{}{2, 5}
    85  	actual := intersection.List()
    86  	if !reflect.DeepEqual(actual, expected) {
    87  		t.Fatalf("bad: %#v", actual)
    88  	}
    89  }
    90  
    91  func TestSetUnion(t *testing.T) {
    92  	s1 := &Set{F: testSetInt}
    93  	s2 := &Set{F: testSetInt}
    94  
    95  	s1.Add(1)
    96  	s1.Add(5)
    97  
    98  	s2.Add(5)
    99  	s2.Add(25)
   100  
   101  	union := s1.Union(s2)
   102  	union.Add(2)
   103  
   104  	expected := []interface{}{1, 2, 25, 5}
   105  	actual := union.List()
   106  	if !reflect.DeepEqual(actual, expected) {
   107  		t.Fatalf("bad: %#v", actual)
   108  	}
   109  }
   110  
   111  func testSetInt(v interface{}) int {
   112  	return v.(int)
   113  }
   114  
   115  // Same test function implementation but not same address
   116  func testSetInt2(v interface{}) int {
   117  	return v.(int)
   118  }
   119  
   120  func TestHashResource_nil(t *testing.T) {
   121  	resource := &Resource{
   122  		Schema: map[string]*Schema{
   123  			"name": {
   124  				Type:     TypeString,
   125  				Optional: true,
   126  			},
   127  		},
   128  	}
   129  	f := HashResource(resource)
   130  
   131  	idx := f(nil)
   132  	if idx != 0 {
   133  		t.Fatalf("Expected 0 when hashing nil, given: %d", idx)
   134  	}
   135  }
   136  
   137  func TestHashEqual(t *testing.T) {
   138  	nested := &Resource{
   139  		Schema: map[string]*Schema{
   140  			"foo": {
   141  				Type:     TypeString,
   142  				Optional: true,
   143  			},
   144  		},
   145  	}
   146  	root := &Resource{
   147  		Schema: map[string]*Schema{
   148  			"bar": {
   149  				Type:     TypeString,
   150  				Optional: true,
   151  			},
   152  			"nested": {
   153  				Type:     TypeSet,
   154  				Optional: true,
   155  				Elem:     nested,
   156  			},
   157  		},
   158  	}
   159  	n1 := map[string]interface{}{"foo": "bar"}
   160  	n2 := map[string]interface{}{"foo": "baz"}
   161  
   162  	r1 := map[string]interface{}{
   163  		"bar":    "baz",
   164  		"nested": NewSet(HashResource(nested), []interface{}{n1}),
   165  	}
   166  	r2 := map[string]interface{}{
   167  		"bar":    "qux",
   168  		"nested": NewSet(HashResource(nested), []interface{}{n2}),
   169  	}
   170  	r3 := map[string]interface{}{
   171  		"bar":    "baz",
   172  		"nested": NewSet(HashResource(nested), []interface{}{n2}),
   173  	}
   174  	r4 := map[string]interface{}{
   175  		"bar":    "qux",
   176  		"nested": NewSet(HashResource(nested), []interface{}{n1}),
   177  	}
   178  	s1 := NewSet(HashResource(root), []interface{}{r1})
   179  	s2 := NewSet(HashResource(root), []interface{}{r2})
   180  	s3 := NewSet(HashResource(root), []interface{}{r3})
   181  	s4 := NewSet(HashResource(root), []interface{}{r4})
   182  
   183  	cases := []struct {
   184  		name     string
   185  		set      *Set
   186  		compare  *Set
   187  		expected bool
   188  	}{
   189  		{
   190  			name:     "equal",
   191  			set:      s1,
   192  			compare:  s1,
   193  			expected: true,
   194  		},
   195  		{
   196  			name:     "not equal",
   197  			set:      s1,
   198  			compare:  s2,
   199  			expected: false,
   200  		},
   201  		{
   202  			name:     "outer equal, should still not be equal",
   203  			set:      s1,
   204  			compare:  s3,
   205  			expected: false,
   206  		},
   207  		{
   208  			name:     "inner equal, should still not be equal",
   209  			set:      s1,
   210  			compare:  s4,
   211  			expected: false,
   212  		},
   213  	}
   214  	for _, tc := range cases {
   215  		t.Run(tc.name, func(t *testing.T) {
   216  			actual := tc.set.HashEqual(tc.compare)
   217  			if tc.expected != actual {
   218  				t.Fatalf("expected %t, got %t", tc.expected, actual)
   219  			}
   220  		})
   221  	}
   222  }
   223  
   224  func TestSetEqualNested(t *testing.T) {
   225  	s := &Set{F: testSetInt}
   226  	s.Add(1)
   227  	s.Add(5)
   228  	s.Add(25)
   229  
   230  	s1 := &Set{F: testSetInt}
   231  	s1.Add(100)
   232  	s.m["dummy"] = s1
   233  
   234  	// Same nested structure but different function address
   235  	s2 := &Set{F: testSetInt2}
   236  	s2.Add(1)
   237  	s2.Add(5)
   238  	s2.Add(25)
   239  
   240  	s3 := &Set{F: testSetInt2}
   241  	s3.Add(100)
   242  	s2.m["dummy"] = s3
   243  
   244  	if !s.Equal(s2) {
   245  		t.Fatalf("Nested Sets structures differ")
   246  	}
   247  }