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