launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/tools/marshal_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package tools_test 5 6 import ( 7 "labix.org/v2/mgo/bson" 8 gc "launchpad.net/gocheck" 9 10 "launchpad.net/juju-core/tools" 11 "launchpad.net/juju-core/version" 12 ) 13 14 var _ = gc.Suite(&marshalSuite{}) 15 16 type marshalSuite struct { 17 } 18 19 func newTools(vers, url string) *tools.Tools { 20 return &tools.Tools{ 21 Version: version.MustParseBinary(vers), 22 URL: url, 23 Size: 10, 24 SHA256: "1234", 25 } 26 } 27 28 func (s *marshalSuite) TestMarshalUnmarshal(c *gc.C) { 29 testTools := newTools("7.8.9-foo-bar", "http://arble.tgz") 30 data, err := bson.Marshal(&testTools) 31 c.Assert(err, gc.IsNil) 32 33 // Check the exact document. 34 want := bson.M{ 35 "version": testTools.Version.String(), 36 "url": testTools.URL, 37 "size": testTools.Size, 38 "sha256": testTools.SHA256, 39 } 40 got := bson.M{} 41 err = bson.Unmarshal(data, &got) 42 c.Assert(err, gc.IsNil) 43 c.Assert(got, gc.DeepEquals, want) 44 45 // Check that it unpacks properly too. 46 var t tools.Tools 47 err = bson.Unmarshal(data, &t) 48 c.Assert(err, gc.IsNil) 49 c.Assert(t, gc.Equals, *testTools) 50 } 51 52 func (s *marshalSuite) TestUnmarshalNilRoundtrip(c *gc.C) { 53 // We have a custom unmarshaller that should keep 54 // the field unset when it finds a nil value. 55 var v struct{ Tools *tools.Tools } 56 data, err := bson.Marshal(&v) 57 c.Assert(err, gc.IsNil) 58 err = bson.Unmarshal(data, &v) 59 c.Assert(err, gc.IsNil) 60 c.Assert(v.Tools, gc.IsNil) 61 }