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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package description
     5  
     6  import (
     7  	gc "gopkg.in/check.v1"
     8  
     9  	"github.com/juju/juju/testing"
    10  )
    11  
    12  type SerializationSuite struct {
    13  	testing.BaseSuite
    14  	importName string
    15  	importFunc func(map[string]interface{}) (interface{}, error)
    16  	testFields func(map[string]interface{})
    17  }
    18  
    19  func (s *SerializationSuite) makeMap(version interface{}) map[string]interface{} {
    20  	result := make(map[string]interface{})
    21  	if s.testFields != nil {
    22  		s.testFields(result)
    23  	}
    24  	if version != nil {
    25  		result["version"] = version
    26  	}
    27  	return result
    28  }
    29  
    30  func (s *SerializationSuite) TestNil(c *gc.C) {
    31  	_, err := s.importFunc(nil)
    32  	c.Check(err, gc.ErrorMatches, s.importName+" version schema check failed: .*")
    33  }
    34  
    35  func (s *SerializationSuite) TestMissingVersion(c *gc.C) {
    36  	_, err := s.importFunc(s.makeMap(nil))
    37  	c.Check(err.Error(), gc.Equals, s.importName+" version schema check failed: version: expected int, got nothing")
    38  }
    39  
    40  func (s *SerializationSuite) TestNonIntVersion(c *gc.C) {
    41  	_, err := s.importFunc(s.makeMap("hello"))
    42  	c.Check(err.Error(), gc.Equals, s.importName+` version schema check failed: version: expected int, got string("hello")`)
    43  }
    44  
    45  func (s *SerializationSuite) TestUnknownVersion(c *gc.C) {
    46  	_, err := s.importFunc(s.makeMap("42"))
    47  	c.Check(err.Error(), gc.Equals, `version 42 not valid`)
    48  }
    49  
    50  type SliceSerializationSuite struct {
    51  	SerializationSuite
    52  	sliceName string
    53  }
    54  
    55  func (s *SliceSerializationSuite) TestMissingSlice(c *gc.C) {
    56  	_, err := s.importFunc(map[string]interface{}{
    57  		"version": 1,
    58  	})
    59  	c.Check(err.Error(), gc.Equals, s.importName+" version schema check failed: "+s.sliceName+": expected list, got nothing")
    60  }
    61  
    62  func (s *SliceSerializationSuite) TestSliceNameIsMap(c *gc.C) {
    63  	_, err := s.importFunc(map[string]interface{}{
    64  		"version":   1,
    65  		s.sliceName: []interface{}{"hello"},
    66  	})
    67  	c.Check(err.Error(), gc.Equals, s.importName+" version schema check failed: "+s.sliceName+`[0]: expected map, got string("hello")`)
    68  }