github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/helper/schema/field_writer_map_test.go (about)

     1  package schema
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestMapFieldWriter_impl(t *testing.T) {
     9  	var _ FieldWriter = new(MapFieldWriter)
    10  }
    11  
    12  func TestMapFieldWriter(t *testing.T) {
    13  	schema := map[string]*Schema{
    14  		"bool":   &Schema{Type: TypeBool},
    15  		"int":    &Schema{Type: TypeInt},
    16  		"string": &Schema{Type: TypeString},
    17  		"list": &Schema{
    18  			Type: TypeList,
    19  			Elem: &Schema{Type: TypeString},
    20  		},
    21  		"listInt": &Schema{
    22  			Type: TypeList,
    23  			Elem: &Schema{Type: TypeInt},
    24  		},
    25  		"listResource": &Schema{
    26  			Type:     TypeList,
    27  			Optional: true,
    28  			Computed: true,
    29  			Elem: &Resource{
    30  				Schema: map[string]*Schema{
    31  					"value": &Schema{
    32  						Type:     TypeInt,
    33  						Optional: true,
    34  					},
    35  				},
    36  			},
    37  		},
    38  		"map": &Schema{Type: TypeMap},
    39  		"set": &Schema{
    40  			Type: TypeSet,
    41  			Elem: &Schema{Type: TypeInt},
    42  			Set: func(a interface{}) int {
    43  				return a.(int)
    44  			},
    45  		},
    46  		"setDeep": &Schema{
    47  			Type: TypeSet,
    48  			Elem: &Resource{
    49  				Schema: map[string]*Schema{
    50  					"index": &Schema{Type: TypeInt},
    51  					"value": &Schema{Type: TypeString},
    52  				},
    53  			},
    54  			Set: func(a interface{}) int {
    55  				return a.(map[string]interface{})["index"].(int)
    56  			},
    57  		},
    58  	}
    59  
    60  	cases := map[string]struct {
    61  		Addr  []string
    62  		Value interface{}
    63  		Err   bool
    64  		Out   map[string]string
    65  	}{
    66  		"noexist": {
    67  			[]string{"noexist"},
    68  			42,
    69  			true,
    70  			map[string]string{},
    71  		},
    72  
    73  		"bool": {
    74  			[]string{"bool"},
    75  			false,
    76  			false,
    77  			map[string]string{
    78  				"bool": "false",
    79  			},
    80  		},
    81  
    82  		"int": {
    83  			[]string{"int"},
    84  			42,
    85  			false,
    86  			map[string]string{
    87  				"int": "42",
    88  			},
    89  		},
    90  
    91  		"string": {
    92  			[]string{"string"},
    93  			"42",
    94  			false,
    95  			map[string]string{
    96  				"string": "42",
    97  			},
    98  		},
    99  
   100  		"list of resources": {
   101  			[]string{"listResource"},
   102  			[]interface{}{
   103  				map[string]interface{}{
   104  					"value": 80,
   105  				},
   106  			},
   107  			false,
   108  			map[string]string{
   109  				"listResource.#":       "1",
   110  				"listResource.0.value": "80",
   111  			},
   112  		},
   113  
   114  		"list of resources empty": {
   115  			[]string{"listResource"},
   116  			[]interface{}{},
   117  			false,
   118  			map[string]string{
   119  				"listResource.#": "0",
   120  			},
   121  		},
   122  
   123  		"list of resources nil": {
   124  			[]string{"listResource"},
   125  			nil,
   126  			false,
   127  			map[string]string{
   128  				"listResource.#": "0",
   129  			},
   130  		},
   131  
   132  		"list of strings": {
   133  			[]string{"list"},
   134  			[]interface{}{"foo", "bar"},
   135  			false,
   136  			map[string]string{
   137  				"list.#": "2",
   138  				"list.0": "foo",
   139  				"list.1": "bar",
   140  			},
   141  		},
   142  
   143  		"list element": {
   144  			[]string{"list", "0"},
   145  			"string",
   146  			true,
   147  			map[string]string{},
   148  		},
   149  
   150  		"map": {
   151  			[]string{"map"},
   152  			map[string]interface{}{"foo": "bar"},
   153  			false,
   154  			map[string]string{
   155  				"map.#":   "1",
   156  				"map.foo": "bar",
   157  			},
   158  		},
   159  
   160  		"map delete": {
   161  			[]string{"map"},
   162  			nil,
   163  			false,
   164  			map[string]string{
   165  				"map": "",
   166  			},
   167  		},
   168  
   169  		"map element": {
   170  			[]string{"map", "foo"},
   171  			"bar",
   172  			true,
   173  			map[string]string{},
   174  		},
   175  
   176  		"set": {
   177  			[]string{"set"},
   178  			[]interface{}{1, 2, 5},
   179  			false,
   180  			map[string]string{
   181  				"set.#": "3",
   182  				"set.1": "1",
   183  				"set.2": "2",
   184  				"set.5": "5",
   185  			},
   186  		},
   187  
   188  		"set nil": {
   189  			[]string{"set"},
   190  			nil,
   191  			false,
   192  			map[string]string{
   193  				"set.#": "0",
   194  			},
   195  		},
   196  
   197  		"set resource": {
   198  			[]string{"setDeep"},
   199  			[]interface{}{
   200  				map[string]interface{}{
   201  					"index": 10,
   202  					"value": "foo",
   203  				},
   204  				map[string]interface{}{
   205  					"index": 50,
   206  					"value": "bar",
   207  				},
   208  			},
   209  			false,
   210  			map[string]string{
   211  				"setDeep.#":        "2",
   212  				"setDeep.10.index": "10",
   213  				"setDeep.10.value": "foo",
   214  				"setDeep.50.index": "50",
   215  				"setDeep.50.value": "bar",
   216  			},
   217  		},
   218  
   219  		"set element": {
   220  			[]string{"set", "5"},
   221  			5,
   222  			true,
   223  			map[string]string{},
   224  		},
   225  
   226  		"full object": {
   227  			nil,
   228  			map[string]interface{}{
   229  				"string": "foo",
   230  				"list":   []interface{}{"foo", "bar"},
   231  			},
   232  			false,
   233  			map[string]string{
   234  				"string": "foo",
   235  				"list.#": "2",
   236  				"list.0": "foo",
   237  				"list.1": "bar",
   238  			},
   239  		},
   240  	}
   241  
   242  	for name, tc := range cases {
   243  		w := &MapFieldWriter{Schema: schema}
   244  		err := w.WriteField(tc.Addr, tc.Value)
   245  		if (err != nil) != tc.Err {
   246  			t.Fatalf("%s: err: %s", name, err)
   247  		}
   248  
   249  		actual := w.Map()
   250  		if !reflect.DeepEqual(actual, tc.Out) {
   251  			t.Fatalf("%s: bad: %#v", name, actual)
   252  		}
   253  	}
   254  }