github.com/richardmarshall/terraform@v0.9.5-0.20170429023105-15704cc6ee35/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 testCase{ 17 Schema: &Schema{ 18 Type: TypeInt, 19 }, 20 Value: 0, 21 Expected: "0;", 22 }, 23 24 testCase{ 25 Schema: &Schema{ 26 Type: TypeInt, 27 }, 28 Value: 200, 29 Expected: "200;", 30 }, 31 32 testCase{ 33 Schema: &Schema{ 34 Type: TypeBool, 35 }, 36 Value: true, 37 Expected: "1;", 38 }, 39 40 testCase{ 41 Schema: &Schema{ 42 Type: TypeBool, 43 }, 44 Value: false, 45 Expected: "0;", 46 }, 47 48 testCase{ 49 Schema: &Schema{ 50 Type: TypeFloat, 51 }, 52 Value: 1.0, 53 Expected: "1;", 54 }, 55 56 testCase{ 57 Schema: &Schema{ 58 Type: TypeFloat, 59 }, 60 Value: 1.54, 61 Expected: "1.54;", 62 }, 63 64 testCase{ 65 Schema: &Schema{ 66 Type: TypeFloat, 67 }, 68 Value: 0.1, 69 Expected: "0.1;", 70 }, 71 72 testCase{ 73 Schema: &Schema{ 74 Type: TypeString, 75 }, 76 Value: "hello", 77 Expected: "hello;", 78 }, 79 80 testCase{ 81 Schema: &Schema{ 82 Type: TypeString, 83 }, 84 Value: "1", 85 Expected: "1;", 86 }, 87 88 testCase{ 89 Schema: &Schema{ 90 Type: TypeList, 91 Elem: &Schema{ 92 Type: TypeString, 93 }, 94 }, 95 Value: []interface{}{}, 96 Expected: "();", 97 }, 98 99 testCase{ 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 testCase{ 111 Schema: &Schema{ 112 Type: TypeList, 113 Elem: &Resource{ 114 Schema: map[string]*Schema{ 115 "fo": &Schema{ 116 Type: TypeString, 117 Required: true, 118 }, 119 "fum": &Schema{ 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 testCase{ 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 testCase{ 153 Schema: &Schema{ 154 Type: TypeMap, 155 Elem: &Schema{ 156 Type: TypeString, 157 }, 158 }, 159 Value: map[string]interface{}{ 160 "foo": "bar", 161 "baz": "foo", 162 }, 163 Expected: "[baz:foo;foo:bar;];", 164 }, 165 166 testCase{ 167 Schema: &Resource{ 168 Schema: map[string]*Schema{ 169 "name": &Schema{ 170 Type: TypeString, 171 Required: true, 172 }, 173 "size": &Schema{ 174 Type: TypeInt, 175 Optional: true, 176 }, 177 "green": &Schema{ 178 Type: TypeBool, 179 Optional: true, 180 Computed: true, 181 }, 182 "upside_down": &Schema{ 183 Type: TypeBool, 184 Computed: true, 185 }, 186 }, 187 }, 188 Value: map[string]interface{}{ 189 "name": "my-fun-database", 190 "size": 12, 191 "green": true, 192 }, 193 Expected: "green:1;name:my-fun-database;size:12;", 194 }, 195 196 // test TypeMap nested in Schema: GH-7091 197 testCase{ 198 Schema: &Resource{ 199 Schema: map[string]*Schema{ 200 "outer": &Schema{ 201 Type: TypeSet, 202 Required: true, 203 Elem: &Schema{ 204 Type: TypeMap, 205 Optional: true, 206 }, 207 }, 208 }, 209 }, 210 Value: map[string]interface{}{ 211 "outer": NewSet(func(i interface{}) int { return 42 }, []interface{}{ 212 map[string]interface{}{ 213 "foo": "bar", 214 "baz": "foo", 215 }, 216 }), 217 }, 218 Expected: "outer:{[baz:foo;foo:bar;];};", 219 }, 220 } 221 222 for _, test := range tests { 223 var gotBuf bytes.Buffer 224 schema := test.Schema 225 226 switch s := schema.(type) { 227 case *Schema: 228 SerializeValueForHash(&gotBuf, test.Value, s) 229 case *Resource: 230 SerializeResourceForHash(&gotBuf, test.Value, s) 231 } 232 233 got := gotBuf.String() 234 if got != test.Expected { 235 t.Errorf("hash(%#v) got %#v, but want %#v", test.Value, got, test.Expected) 236 } 237 } 238 }