github.com/richardmarshall/terraform@v0.9.5-0.20170429023105-15704cc6ee35/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  		"string nil": {
   101  			[]string{"string"},
   102  			nil,
   103  			false,
   104  			map[string]string{
   105  				"string": "",
   106  			},
   107  		},
   108  
   109  		"list of resources": {
   110  			[]string{"listResource"},
   111  			[]interface{}{
   112  				map[string]interface{}{
   113  					"value": 80,
   114  				},
   115  			},
   116  			false,
   117  			map[string]string{
   118  				"listResource.#":       "1",
   119  				"listResource.0.value": "80",
   120  			},
   121  		},
   122  
   123  		"list of resources empty": {
   124  			[]string{"listResource"},
   125  			[]interface{}{},
   126  			false,
   127  			map[string]string{
   128  				"listResource.#": "0",
   129  			},
   130  		},
   131  
   132  		"list of resources nil": {
   133  			[]string{"listResource"},
   134  			nil,
   135  			false,
   136  			map[string]string{
   137  				"listResource.#": "0",
   138  			},
   139  		},
   140  
   141  		"list of strings": {
   142  			[]string{"list"},
   143  			[]interface{}{"foo", "bar"},
   144  			false,
   145  			map[string]string{
   146  				"list.#": "2",
   147  				"list.0": "foo",
   148  				"list.1": "bar",
   149  			},
   150  		},
   151  
   152  		"list element": {
   153  			[]string{"list", "0"},
   154  			"string",
   155  			true,
   156  			map[string]string{},
   157  		},
   158  
   159  		"map": {
   160  			[]string{"map"},
   161  			map[string]interface{}{"foo": "bar"},
   162  			false,
   163  			map[string]string{
   164  				"map.%":   "1",
   165  				"map.foo": "bar",
   166  			},
   167  		},
   168  
   169  		"map delete": {
   170  			[]string{"map"},
   171  			nil,
   172  			false,
   173  			map[string]string{
   174  				"map": "",
   175  			},
   176  		},
   177  
   178  		"map element": {
   179  			[]string{"map", "foo"},
   180  			"bar",
   181  			true,
   182  			map[string]string{},
   183  		},
   184  
   185  		"set": {
   186  			[]string{"set"},
   187  			[]interface{}{1, 2, 5},
   188  			false,
   189  			map[string]string{
   190  				"set.#": "3",
   191  				"set.1": "1",
   192  				"set.2": "2",
   193  				"set.5": "5",
   194  			},
   195  		},
   196  
   197  		"set nil": {
   198  			[]string{"set"},
   199  			nil,
   200  			false,
   201  			map[string]string{
   202  				"set.#": "0",
   203  			},
   204  		},
   205  
   206  		"set resource": {
   207  			[]string{"setDeep"},
   208  			[]interface{}{
   209  				map[string]interface{}{
   210  					"index": 10,
   211  					"value": "foo",
   212  				},
   213  				map[string]interface{}{
   214  					"index": 50,
   215  					"value": "bar",
   216  				},
   217  			},
   218  			false,
   219  			map[string]string{
   220  				"setDeep.#":        "2",
   221  				"setDeep.10.index": "10",
   222  				"setDeep.10.value": "foo",
   223  				"setDeep.50.index": "50",
   224  				"setDeep.50.value": "bar",
   225  			},
   226  		},
   227  
   228  		"set element": {
   229  			[]string{"set", "5"},
   230  			5,
   231  			true,
   232  			map[string]string{},
   233  		},
   234  
   235  		"full object": {
   236  			nil,
   237  			map[string]interface{}{
   238  				"string": "foo",
   239  				"list":   []interface{}{"foo", "bar"},
   240  			},
   241  			false,
   242  			map[string]string{
   243  				"string": "foo",
   244  				"list.#": "2",
   245  				"list.0": "foo",
   246  				"list.1": "bar",
   247  			},
   248  		},
   249  	}
   250  
   251  	for name, tc := range cases {
   252  		w := &MapFieldWriter{Schema: schema}
   253  		err := w.WriteField(tc.Addr, tc.Value)
   254  		if err != nil != tc.Err {
   255  			t.Fatalf("%s: err: %s", name, err)
   256  		}
   257  
   258  		actual := w.Map()
   259  		if !reflect.DeepEqual(actual, tc.Out) {
   260  			t.Fatalf("%s: bad: %#v", name, actual)
   261  		}
   262  	}
   263  }