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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| package main
import ( "io/ioutil" "os" "reflect" "testing"
"github.com/globalsign/mgo/bson" "github.com/golang/mock/gomock" "github.com/gunsluo/pmgo" "github.com/gunsluo/pmgo/pmgomock" )
func TestGetUser(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish()
user := User{ ID: 1, Name: "Zapp Brannigan", }
// Mock up a database, session, collection and a query and set // expected/returned values for each type query := pmgomock.NewMockQueryManager(ctrl) query.EXPECT().One(gomock.Any()).SetArg(0, user).Return(nil)
collection := pmgomock.NewMockCollectionManager(ctrl) collection.EXPECT().Find(bson.M{"id": 1}).Return(query)
database := pmgomock.NewMockDatabaseManager(ctrl) database.EXPECT().C("testc").Return(collection)
session := pmgomock.NewMockSessionManager(ctrl) session.EXPECT().DB("test").Return(database)
// Call the function we want to test. It will use the mocked interfaces readUser, err := getUser(session, 1)
if err != nil { t.Errorf("getUser returned an error: %s\n", err.Error()) }
if !reflect.DeepEqual(*readUser, user) { t.Errorf("Users don't match. Got %+v, want %+v\n", readUser, user) } }
var Server pmgo.DBTestServer
func TestIntegration(t *testing.T) { setup()
readUser, err := getUser(Server.Session(), 1) if err != nil { t.Errorf("getUser returned an error: %s\n", err.Error()) }
if !reflect.DeepEqual(*readUser, mockUser()) { t.Errorf("Users don't match. Got %+v, want %+v\n", readUser, mockUser()) }
tearDown() }
func setup() { os.Setenv("CHECK_SESSIONS", "0") tempDir, err := ioutil.TempDir("", "testing") if err != nil { panic(err) }
Server = pmgo.NewDBServer() Server.SetPath(tempDir)
session := Server.Session() // load some fake data into the db session.DB("test").C("testc").Insert(mockUser()) }
func mockUser() User { return User{ ID: 1, Name: "Zapp Brannigan", }
}
func tearDown() { Server.Session().Close() Server.Session().DB("samples").DropDatabase() Server.Stop() }
|