github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/testing/mgo_test.go (about)

     1  // Copyright 2012, 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package testing_test
     5  
     6  import (
     7  	stdtesting "testing"
     8  
     9  	"labix.org/v2/mgo/bson"
    10  	gc "launchpad.net/gocheck"
    11  
    12  	"github.com/juju/juju/testing"
    13  )
    14  
    15  type mgoSuite struct {
    16  	testing.BaseSuite
    17  	testing.MgoSuite
    18  }
    19  
    20  var _ = gc.Suite(&mgoSuite{})
    21  
    22  func TestMgoSuite(t *stdtesting.T) {
    23  	testing.MgoTestPackage(t)
    24  }
    25  
    26  func (s *mgoSuite) SetUpSuite(c *gc.C) {
    27  	s.BaseSuite.SetUpSuite(c)
    28  	s.MgoSuite.SetUpSuite(c)
    29  }
    30  
    31  func (s *mgoSuite) TearDownSuite(c *gc.C) {
    32  	s.BaseSuite.TearDownSuite(c)
    33  	s.MgoSuite.TearDownSuite(c)
    34  }
    35  
    36  func (s *mgoSuite) SetUpTest(c *gc.C) {
    37  	s.BaseSuite.SetUpTest(c)
    38  	s.MgoSuite.SetUpTest(c)
    39  }
    40  
    41  func (s *mgoSuite) TearDownTest(c *gc.C) {
    42  	s.BaseSuite.TearDownTest(c)
    43  	s.MgoSuite.TearDownTest(c)
    44  }
    45  
    46  func (s *mgoSuite) TestResetWhenUnauthorized(c *gc.C) {
    47  	session := testing.MgoServer.MustDial()
    48  	defer session.Close()
    49  	err := session.DB("admin").AddUser("admin", "foo", false)
    50  	if err != nil && err.Error() != "need to login" {
    51  		c.Assert(err, gc.IsNil)
    52  	}
    53  	// The test will fail if the reset does not succeed
    54  }
    55  
    56  func (s *mgoSuite) TestStartAndClean(c *gc.C) {
    57  	c.Assert(testing.MgoServer.Addr(), gc.Not(gc.Equals), "")
    58  
    59  	session := testing.MgoServer.MustDial()
    60  	defer session.Close()
    61  	menu := session.DB("food").C("menu")
    62  	err := menu.Insert(
    63  		bson.D{{"spam", "lots"}},
    64  		bson.D{{"eggs", "fried"}},
    65  	)
    66  	c.Assert(err, gc.IsNil)
    67  	food := make([]map[string]string, 0)
    68  	err = menu.Find(nil).All(&food)
    69  	c.Assert(err, gc.IsNil)
    70  	c.Assert(food, gc.HasLen, 2)
    71  	c.Assert(food[0]["spam"], gc.Equals, "lots")
    72  	c.Assert(food[1]["eggs"], gc.Equals, "fried")
    73  
    74  	testing.MgoServer.Reset()
    75  	morefood := make([]map[string]string, 0)
    76  	err = menu.Find(nil).All(&morefood)
    77  	c.Assert(err, gc.IsNil)
    78  	c.Assert(morefood, gc.HasLen, 0)
    79  }