github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/core/description/address_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package description
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  	"gopkg.in/yaml.v2"
    10  )
    11  
    12  type AddressSerializationSuite struct {
    13  	SerializationSuite
    14  }
    15  
    16  var _ = gc.Suite(&AddressSerializationSuite{})
    17  
    18  func (s *AddressSerializationSuite) SetUpTest(c *gc.C) {
    19  	s.SerializationSuite.SetUpTest(c)
    20  	s.importName = "address"
    21  	s.importFunc = func(m map[string]interface{}) (interface{}, error) {
    22  		return importAddress(m)
    23  	}
    24  	s.testFields = func(m map[string]interface{}) {
    25  		m["value"] = ""
    26  		m["type"] = ""
    27  	}
    28  }
    29  
    30  func (s *AddressSerializationSuite) TestMissingValue(c *gc.C) {
    31  	testMap := s.makeMap(1)
    32  	delete(testMap, "value")
    33  	_, err := importAddress(testMap)
    34  	c.Check(err.Error(), gc.Equals, "address v1 schema check failed: value: expected string, got nothing")
    35  }
    36  
    37  func (s *AddressSerializationSuite) TestMissingType(c *gc.C) {
    38  	testMap := s.makeMap(1)
    39  	delete(testMap, "type")
    40  	_, err := importAddress(testMap)
    41  	c.Check(err.Error(), gc.Equals, "address v1 schema check failed: type: expected string, got nothing")
    42  }
    43  
    44  func (*AddressSerializationSuite) TestParsing(c *gc.C) {
    45  	addr, err := importAddress(map[string]interface{}{
    46  		"version": 1,
    47  		"value":   "no",
    48  		"type":    "content",
    49  		"scope":   "done",
    50  		"origin":  "here",
    51  	})
    52  	c.Assert(err, jc.ErrorIsNil)
    53  	expected := &address{
    54  		Version: 1,
    55  		Value_:  "no",
    56  		Type_:   "content",
    57  		Scope_:  "done",
    58  		Origin_: "here",
    59  	}
    60  	c.Assert(addr, jc.DeepEquals, expected)
    61  }
    62  
    63  func (*AddressSerializationSuite) TestOptionalValues(c *gc.C) {
    64  	addr, err := importAddress(map[string]interface{}{
    65  		"version": 1,
    66  		"value":   "no",
    67  		"type":    "content",
    68  	})
    69  	c.Assert(err, jc.ErrorIsNil)
    70  	expected := &address{
    71  		Version: 1,
    72  		Value_:  "no",
    73  		Type_:   "content",
    74  	}
    75  	c.Assert(addr, jc.DeepEquals, expected)
    76  }
    77  
    78  func (*AddressSerializationSuite) TestParsingSerializedData(c *gc.C) {
    79  	initial := &address{
    80  		Version: 1,
    81  		Value_:  "no",
    82  		Type_:   "content",
    83  		Scope_:  "done",
    84  		Origin_: "here",
    85  	}
    86  
    87  	bytes, err := yaml.Marshal(initial)
    88  	c.Assert(err, jc.ErrorIsNil)
    89  
    90  	var source map[string]interface{}
    91  	err = yaml.Unmarshal(bytes, &source)
    92  	c.Assert(err, jc.ErrorIsNil)
    93  
    94  	addresss, err := importAddress(source)
    95  	c.Assert(err, jc.ErrorIsNil)
    96  
    97  	c.Assert(addresss, jc.DeepEquals, initial)
    98  }