launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/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  	"launchpad.net/juju-core/testing"
    13  	"launchpad.net/juju-core/testing/testbase"
    14  )
    15  
    16  type mgoSuite struct {
    17  	testbase.LoggingSuite
    18  	testing.MgoSuite
    19  }
    20  
    21  var _ = gc.Suite(&mgoSuite{})
    22  
    23  func TestMgoSuite(t *stdtesting.T) {
    24  	testing.MgoTestPackage(t)
    25  }
    26  
    27  func (s *mgoSuite) SetUpSuite(c *gc.C) {
    28  	s.LoggingSuite.SetUpSuite(c)
    29  	s.MgoSuite.SetUpSuite(c)
    30  }
    31  
    32  func (s *mgoSuite) TearDownSuite(c *gc.C) {
    33  	s.LoggingSuite.TearDownSuite(c)
    34  	s.MgoSuite.TearDownSuite(c)
    35  }
    36  
    37  func (s *mgoSuite) SetUpTest(c *gc.C) {
    38  	s.LoggingSuite.SetUpTest(c)
    39  	s.MgoSuite.SetUpTest(c)
    40  }
    41  
    42  func (s *mgoSuite) TearDownTest(c *gc.C) {
    43  	s.LoggingSuite.TearDownTest(c)
    44  	s.MgoSuite.TearDownTest(c)
    45  }
    46  
    47  func (s *mgoSuite) TestResetWhenUnauthorized(c *gc.C) {
    48  	session := testing.MgoServer.MustDial()
    49  	defer session.Close()
    50  	err := session.DB("admin").AddUser("admin", "foo", false)
    51  	if err != nil && err.Error() != "need to login" {
    52  		c.Assert(err, gc.IsNil)
    53  	}
    54  	// The test will fail if the reset does not succeed
    55  }
    56  
    57  func (s *mgoSuite) TestStartAndClean(c *gc.C) {
    58  	c.Assert(testing.MgoServer.Addr(), gc.Not(gc.Equals), "")
    59  
    60  	session := testing.MgoServer.MustDial()
    61  	defer session.Close()
    62  	menu := session.DB("food").C("menu")
    63  	err := menu.Insert(
    64  		bson.D{{"spam", "lots"}},
    65  		bson.D{{"eggs", "fried"}},
    66  	)
    67  	c.Assert(err, gc.IsNil)
    68  	food := make([]map[string]string, 0)
    69  	err = menu.Find(nil).All(&food)
    70  	c.Assert(err, gc.IsNil)
    71  	c.Assert(food, gc.HasLen, 2)
    72  	c.Assert(food[0]["spam"], gc.Equals, "lots")
    73  	c.Assert(food[1]["eggs"], gc.Equals, "fried")
    74  
    75  	testing.MgoServer.Reset()
    76  	morefood := make([]map[string]string, 0)
    77  	err = menu.Find(nil).All(&morefood)
    78  	c.Assert(err, gc.IsNil)
    79  	c.Assert(morefood, gc.HasLen, 0)
    80  }