-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarshal_test.go
62 lines (52 loc) · 1.47 KB
/
marshal_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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package tools_test
import (
"github.com/juju/mgo/v3/bson"
jc "github.com/juju/testing/checkers"
"github.com/juju/version/v2"
gc "gopkg.in/check.v1"
"github.com/juju/juju/tools"
)
var _ = gc.Suite(&marshalSuite{})
type marshalSuite struct {
}
func newTools(vers, url string) *tools.Tools {
return &tools.Tools{
Version: version.MustParseBinary(vers),
URL: url,
Size: 10,
SHA256: "1234",
}
}
func (s *marshalSuite) TestMarshalUnmarshal(c *gc.C) {
testTools := newTools("7.8.9-ubuntu-amd64", "http://arble.tgz")
data, err := bson.Marshal(&testTools)
c.Assert(err, jc.ErrorIsNil)
// Check the exact document.
want := bson.M{
"version": testTools.Version.String(),
"url": testTools.URL,
"size": testTools.Size,
"sha256": testTools.SHA256,
}
got := bson.M{}
err = bson.Unmarshal(data, &got)
c.Assert(err, jc.ErrorIsNil)
c.Assert(got, gc.DeepEquals, want)
// Check that it unpacks properly too.
var t tools.Tools
err = bson.Unmarshal(data, &t)
c.Assert(err, jc.ErrorIsNil)
c.Assert(t, gc.Equals, *testTools)
}
func (s *marshalSuite) TestUnmarshalNilRoundtrip(c *gc.C) {
// We have a custom unmarshaller that should keep
// the field unset when it finds a nil value.
var v struct{ Tools *tools.Tools }
data, err := bson.Marshal(&v)
c.Assert(err, jc.ErrorIsNil)
err = bson.Unmarshal(data, &v)
c.Assert(err, jc.ErrorIsNil)
c.Assert(v.Tools, gc.IsNil)
}