github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/schema/serialize_test.go (about) 1 package schema 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 func TestSerializeForHash(t *testing.T) { 9 type testCase struct { 10 Schema interface{} 11 Value interface{} 12 Expected string 13 } 14 15 tests := []testCase{ 16 { 17 Schema: &Schema{ 18 Type: TypeInt, 19 }, 20 Value: 0, 21 Expected: "0;", 22 }, 23 24 { 25 Schema: &Schema{ 26 Type: TypeInt, 27 }, 28 Value: 200, 29 Expected: "200;", 30 }, 31 32 { 33 Schema: &Schema{ 34 Type: TypeBool, 35 }, 36 Value: true, 37 Expected: "1;", 38 }, 39 40 { 41 Schema: &Schema{ 42 Type: TypeBool, 43 }, 44 Value: false, 45 Expected: "0;", 46 }, 47 48 { 49 Schema: &Schema{ 50 Type: TypeFloat, 51 }, 52 Value: 1.0, 53 Expected: "1;", 54 }, 55 56 { 57 Schema: &Schema{ 58 Type: TypeFloat, 59 }, 60 Value: 1.54, 61 Expected: "1.54;", 62 }, 63 64 { 65 Schema: &Schema{ 66 Type: TypeFloat, 67 }, 68 Value: 0.1, 69 Expected: "0.1;", 70 }, 71 72 { 73 Schema: &Schema{ 74 Type: TypeString, 75 }, 76 Value: "hello", 77 Expected: "hello;", 78 }, 79 80 { 81 Schema: &Schema{ 82 Type: TypeString, 83 }, 84 Value: "1", 85 Expected: "1;", 86 }, 87 88 { 89 Schema: &Schema{ 90 Type: TypeList, 91 Elem: &Schema{ 92 Type: TypeString, 93 }, 94 }, 95 Value: []interface{}{}, 96 Expected: "();", 97 }, 98 99 { 100 Schema: &Schema{ 101 Type: TypeList, 102 Elem: &Schema{ 103 Type: TypeString, 104 }, 105 }, 106 Value: []interface{}{"hello", "world"}, 107 Expected: "(hello;world;);", 108 }, 109 110 { 111 Schema: &Schema{ 112 Type: TypeList, 113 Elem: &Resource{ 114 Schema: map[string]*Schema{ 115 "fo": { 116 Type: TypeString, 117 Required: true, 118 }, 119 "fum": { 120 Type: TypeString, 121 Required: true, 122 }, 123 }, 124 }, 125 }, 126 Value: []interface{}{ 127 map[string]interface{}{ 128 "fo": "bar", 129 }, 130 map[string]interface{}{ 131 "fo": "baz", 132 "fum": "boz", 133 }, 134 }, 135 Expected: "(<fo:bar;fum:;>;<fo:baz;fum:boz;>;);", 136 }, 137 138 { 139 Schema: &Schema{ 140 Type: TypeSet, 141 Elem: &Schema{ 142 Type: TypeString, 143 }, 144 }, 145 Value: NewSet(func(i interface{}) int { return len(i.(string)) }, []interface{}{ 146 "hello", 147 "woo", 148 }), 149 Expected: "{woo;hello;};", 150 }, 151 152 { 153 Schema: &Schema{ 154 Type: TypeMap, 155 Elem: &Schema{ 156 Type: TypeString, 157 }, 158 }, 159 Value: map[string]interface{}{ 160 "bool": true, 161 "float": 1.2, 162 "int": 1, 163 "string": "value", 164 }, 165 Expected: "[bool:true;float:1.2;int:1;string:value;];", 166 }, 167 168 { 169 Schema: &Resource{ 170 Schema: map[string]*Schema{ 171 "name": { 172 Type: TypeString, 173 Required: true, 174 }, 175 "size": { 176 Type: TypeInt, 177 Optional: true, 178 }, 179 "green": { 180 Type: TypeBool, 181 Optional: true, 182 Computed: true, 183 }, 184 "upside_down": { 185 Type: TypeBool, 186 Computed: true, 187 }, 188 }, 189 }, 190 Value: map[string]interface{}{ 191 "name": "my-fun-database", 192 "size": 12, 193 "green": true, 194 }, 195 Expected: "green:1;name:my-fun-database;size:12;", 196 }, 197 198 // test TypeMap nested in Schema: GH-7091 199 { 200 Schema: &Resource{ 201 Schema: map[string]*Schema{ 202 "outer": { 203 Type: TypeSet, 204 Required: true, 205 Elem: &Schema{ 206 Type: TypeMap, 207 Optional: true, 208 }, 209 }, 210 }, 211 }, 212 Value: map[string]interface{}{ 213 "outer": NewSet(func(i interface{}) int { return 42 }, []interface{}{ 214 map[string]interface{}{ 215 "foo": "bar", 216 "baz": "foo", 217 }, 218 }), 219 }, 220 Expected: "outer:{[baz:foo;foo:bar;];};", 221 }, 222 } 223 224 for _, test := range tests { 225 var gotBuf bytes.Buffer 226 schema := test.Schema 227 228 switch s := schema.(type) { 229 case *Schema: 230 SerializeValueForHash(&gotBuf, test.Value, s) 231 case *Resource: 232 SerializeResourceForHash(&gotBuf, test.Value, s) 233 } 234 235 got := gotBuf.String() 236 if got != test.Expected { 237 t.Errorf("hash(%#v) got %#v, but want %#v", test.Value, got, test.Expected) 238 } 239 } 240 }