Skip to content

Commit

Permalink
fix bug that multi db use the dafult database, and add related test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ififi committed Jul 27, 2012
1 parent 520ff28 commit 27b26d3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
14 changes: 7 additions & 7 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ func (db *Database) CollectionsDo(f func(c ...*mgo.Collection), names ...string)
}

func (db *Database) Save(collectionName string, obj Id) (err error) {
CollectionDo(collectionName, func(rc *mgo.Collection) {
db.CollectionDo(collectionName, func(rc *mgo.Collection) {
_, err = rc.Upsert(bson.M{"_id": obj.MakeId()}, obj)
})
return
}

func (db *Database) DropCollection(collectionName string) (err error) {
CollectionDo(collectionName, func(rc *mgo.Collection) {
db.CollectionDo(collectionName, func(rc *mgo.Collection) {
err = rc.DropCollection()
})
return
Expand All @@ -92,14 +92,14 @@ func (db *Database) DropCollections(collectionNames ...string) (err error) {
}

func (db *Database) Delete(collectionName string, id interface{}) (err error) {
CollectionDo(collectionName, func(rc *mgo.Collection) {
db.CollectionDo(collectionName, func(rc *mgo.Collection) {
_, err = rc.RemoveAll(bson.M{"_id": id})
})
return
}

func (db *Database) Update(collectionName string, obj Id) (err error) {
CollectionDo(collectionName, func(rc *mgo.Collection) {
db.CollectionDo(collectionName, func(rc *mgo.Collection) {
v := reflect.ValueOf(obj)
if v.Kind() == reflect.Ptr {
v = v.Elem()
Expand Down Expand Up @@ -128,21 +128,21 @@ func (db *Database) Update(collectionName string, obj Id) (err error) {
}

func (db *Database) FindAll(collectionName string, query interface{}, result interface{}) (err error) {
CollectionDo(collectionName, func(c *mgo.Collection) {
db.CollectionDo(collectionName, func(c *mgo.Collection) {
err = c.Find(query).All(result)
})
return
}

func (db *Database) FindOne(collectionName string, query interface{}, result interface{}) (err error) {
CollectionDo(collectionName, func(c *mgo.Collection) {
db.CollectionDo(collectionName, func(c *mgo.Collection) {
err = c.Find(query).One(result)
})
return
}

func (db *Database) FindById(collectionName string, id interface{}, result interface{}) (err error) {
CollectionDo(collectionName, func(c *mgo.Collection) {
db.CollectionDo(collectionName, func(c *mgo.Collection) {
err = c.Find(bson.M{"_id": id}).One(result)
})
return
Expand Down
40 changes: 40 additions & 0 deletions database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package mgodb

import (
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"testing"
)

const (
ALLUSERS = "allusers"
DB1 = "mogdb_test_1"
DB2 = "mogdb_test_2"
)

func TestSave(t *testing.T) {
db := NewDatabase("localhost", DB1)

db.Save(ALLUSERS, &User{Email: "sunfmin@gmail.com", Name: "Felix Sun"})

var found *User
db.CollectionDo(ALLUSERS, func(uc *mgo.Collection) {
uc.Find(bson.M{"email": "sunfmin@gmail.com"}).One(&found)
})
if found == nil {
t.Error("Can not find user after saved")
}

db = NewDatabase("localhost", DB2)

db.Save(ALLUSERS, &User{Email: "sunfmin@gmail.com", Name: "Felix Sun"})

var u2 *User
db.CollectionDo(ALLUSERS, func(uc *mgo.Collection) {
uc.Find(bson.M{"email": "sunfmin@gmail.com"}).One(&u2)
})
if u2 == nil {
t.Error("Can not find user after saved")
}

}

0 comments on commit 27b26d3

Please sign in to comment.