mydb/adapter/mongo/helper_test.go

78 lines
1.4 KiB
Go
Raw Normal View History

2023-09-18 15:15:42 +08:00
package mongo
import (
"fmt"
"os"
"git.hexq.cn/tiglog/mydb"
"git.hexq.cn/tiglog/mydb/internal/testsuite"
mgo "gopkg.in/mgo.v2"
)
var settings = ConnectionURL{
Database: os.Getenv("DB_NAME"),
User: os.Getenv("DB_USERNAME"),
Password: os.Getenv("DB_PASSWORD"),
Host: os.Getenv("DB_HOST") + ":" + os.Getenv("DB_PORT"),
}
type Helper struct {
sess mydb.Session
}
func (h *Helper) Session() mydb.Session {
return h.sess
}
func (h *Helper) Adapter() string {
return "mongo"
}
func (h *Helper) TearDown() error {
return h.sess.Close()
}
func (h *Helper) TearUp() error {
var err error
h.sess, err = Open(settings)
if err != nil {
return err
}
mgod, ok := h.sess.Driver().(*mgo.Session)
if !ok {
panic("expecting mgo.Session")
}
var col *mgo.Collection
col = mgod.DB(settings.Database).C("birthdays")
_ = col.DropCollection()
col = mgod.DB(settings.Database).C("fibonacci")
_ = col.DropCollection()
col = mgod.DB(settings.Database).C("is_even")
_ = col.DropCollection()
col = mgod.DB(settings.Database).C("CaSe_TesT")
_ = col.DropCollection()
// Getting a pointer to the "artist" collection.
artist := h.sess.Collection("artist")
_ = artist.Truncate()
for i := 0; i < 999; i++ {
_, err = artist.Insert(artistType{
Name: fmt.Sprintf("artist-%d", i),
})
if err != nil {
return err
}
}
return nil
}
var _ testsuite.Helper = &Helper{}