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