github.com/tonyhb/nomad@v0.11.8/helper/fields/data_test.go (about)

     1  package fields
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestFieldDataGet(t *testing.T) {
     9  	cases := map[string]struct {
    10  		Schema map[string]*FieldSchema
    11  		Raw    map[string]interface{}
    12  		Key    string
    13  		Value  interface{}
    14  	}{
    15  		"string type, string value": {
    16  			map[string]*FieldSchema{
    17  				"foo": {Type: TypeString},
    18  			},
    19  			map[string]interface{}{
    20  				"foo": "bar",
    21  			},
    22  			"foo",
    23  			"bar",
    24  		},
    25  
    26  		"string type, int value": {
    27  			map[string]*FieldSchema{
    28  				"foo": {Type: TypeInt},
    29  			},
    30  			map[string]interface{}{
    31  				"foo": 42,
    32  			},
    33  			"foo",
    34  			42,
    35  		},
    36  
    37  		"string type, unset value": {
    38  			map[string]*FieldSchema{
    39  				"foo": {Type: TypeString},
    40  			},
    41  			map[string]interface{}{},
    42  			"foo",
    43  			"",
    44  		},
    45  
    46  		"string type, unset value with default": {
    47  			map[string]*FieldSchema{
    48  				"foo": {
    49  					Type:    TypeString,
    50  					Default: "bar",
    51  				},
    52  			},
    53  			map[string]interface{}{},
    54  			"foo",
    55  			"bar",
    56  		},
    57  
    58  		"int type, int value": {
    59  			map[string]*FieldSchema{
    60  				"foo": {Type: TypeInt},
    61  			},
    62  			map[string]interface{}{
    63  				"foo": 42,
    64  			},
    65  			"foo",
    66  			42,
    67  		},
    68  
    69  		"bool type, bool value": {
    70  			map[string]*FieldSchema{
    71  				"foo": {Type: TypeBool},
    72  			},
    73  			map[string]interface{}{
    74  				"foo": false,
    75  			},
    76  			"foo",
    77  			false,
    78  		},
    79  
    80  		"map type, map value": {
    81  			map[string]*FieldSchema{
    82  				"foo": {Type: TypeMap},
    83  			},
    84  			map[string]interface{}{
    85  				"foo": map[string]interface{}{
    86  					"child": true,
    87  				},
    88  			},
    89  			"foo",
    90  			map[string]interface{}{
    91  				"child": true,
    92  			},
    93  		},
    94  
    95  		"array type, array value": {
    96  			map[string]*FieldSchema{
    97  				"foo": {Type: TypeArray},
    98  			},
    99  			map[string]interface{}{
   100  				"foo": []interface{}{},
   101  			},
   102  			"foo",
   103  			[]interface{}{},
   104  		},
   105  	}
   106  
   107  	for name, tc := range cases {
   108  		data := &FieldData{
   109  			Raw:    tc.Raw,
   110  			Schema: tc.Schema,
   111  		}
   112  
   113  		actual := data.Get(tc.Key)
   114  		if !reflect.DeepEqual(actual, tc.Value) {
   115  			t.Fatalf(
   116  				"bad: %s\n\nExpected: %#v\nGot: %#v",
   117  				name, tc.Value, actual)
   118  		}
   119  	}
   120  }