Skip to content

Commit

Permalink
add redis port and dsn return
Browse files Browse the repository at this point in the history
  • Loading branch information
junqiang.zhang committed May 7, 2022
1 parent b3ee6d6 commit 79f6d67
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 17 deletions.
3 changes: 2 additions & 1 deletion example/exmaple.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func testMockGORM() {
mock := gmock.NewMockGORM("example", func(gorm *mockdb.MockGORM) {
db = gorm.GetGormDB()
})
fmt.Println(mock.GetDSN())
//mock.RegisterModels(&User{})
mock.InitSchemas(`CREATE TABLE user (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
Expand Down Expand Up @@ -97,7 +98,7 @@ func testMockGORMV2() {
}

func testMockRedis() {
server := gmock.NewMockRedisServer()
server := gmock.NewMockRedisServer(63790)
client := server.GetRedisClient()
ctx := context.Background()
key := "aa"
Expand Down
4 changes: 2 additions & 2 deletions gmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func NewMockGORMV2(pathToSqlFileName string, resetHandler func(orm *mockdb.MockG
return mockdb.NewMockGORMV2(pathToSqlFileName, resetHandler)
}

func NewMockRedisServer() *mockredis.MockRedisServer {
return mockredis.NewMockRedisServer()
func NewMockRedisServer(port int) *mockredis.MockRedisServer {
return mockredis.NewMockRedisServer(port)
}

func NewMockXORM(pathToSqlFileName string, resetHandler func(orm *mockdb.MockXORM)) *mockdb.MockXORM {
Expand Down
18 changes: 12 additions & 6 deletions mockdb/mock_gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type MockGORM struct {
db *gorm.DB
dbType string
dsn string
schema string // just fix:github.com/jinzhu/gorm "fix tables"
schema string // just fix:github.com/jinzhu/gorm "fix tables"
util *util.DBUtil
models []interface{}
resetHandler func(orm *MockGORM)
Expand Down Expand Up @@ -88,7 +88,7 @@ func (m *MockGORM) ResetAndInit() {
//m.db = renew()

m.dropTables()
if DBType != "mysql" {
if m.dbType != "mysql" {
m.initModels()
}
m.initSQL()
Expand All @@ -114,7 +114,7 @@ func (m *MockGORM) ResetAndInit() {
//}

func (m *MockGORM) dropTables() {
if DBType == "mysql" {
if m.dbType == "mysql" {
rows, err := m.db.Raw("show tables").Rows()
if err == nil {
for rows.Next() {
Expand Down Expand Up @@ -149,7 +149,13 @@ func (m *MockGORM) GetSqlDB() *sql.DB {

// InitSchemas 为了兼容github.com/jinzhu/gorm mysql的bug 特殊处理的
func (m *MockGORM) InitSchemas(sqlSchema string) {
m.schema=sqlSchema
m.schema = sqlSchema
}

func (m *MockGORM) GetDSN() (dbType string, dsn string) {
dbType = m.dbType
dsn = m.dsn
return
}

// RegisterModels 注册模型
Expand Down Expand Up @@ -180,8 +186,8 @@ func (m *MockGORM) initModels() {
}
}
func (m *MockGORM) initSQL() {
if m.schema!="" {
sqls:=m.parseMockSQL(m.schema)
if m.schema != "" {
sqls := m.parseMockSQL(m.schema)
for _, sql := range sqls {
err := m.db.Exec(sql).Error
if err != nil {
Expand Down
20 changes: 14 additions & 6 deletions mockdb/mock_gormv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ func NewMockGORMV2(pathToSqlFileName string, resetHandler func(orm *MockGORMV2))
}
mock.util.RunMySQLServer("mock", i, false)
time.Sleep(time.Second)
db, err = gorm.Open(mysql.Open(fmt.Sprintf("root:root@tcp(127.0.0.1:%v)/mock?charset=utf8&parseTime=True&loc=Local",i)), &gorm.Config{NamingStrategy: ns})
mock.dsn= fmt.Sprintf("root:root@tcp(127.0.0.1:%v)/mock?charset=utf8&parseTime=True&loc=Local", i)
mock.dbType="mysql"
db, err = gorm.Open(mysql.Open(mock.dsn), &gorm.Config{NamingStrategy: ns})
break
}

} else {
db, err = gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{
mock.dbType = "sqlite3"
mock.dsn = "file::memory:?cache=shared"
db, err = gorm.Open(sqlite.Open(mock.dsn), &gorm.Config{
NamingStrategy: ns,
})
}
Expand Down Expand Up @@ -101,12 +105,16 @@ func (m *MockGORMV2) dropTables() {
m.db.Migrator().DropTable(model)
}
}
func (m *MockGORMV2) GetDSN() (dbType string, dsn string) {
dbType = m.dbType
dsn = m.dsn
return
}

func (m *MockGORMV2) InitSchemas(sqlSchema string) {
m.schema=sqlSchema
m.schema = sqlSchema
}


// GetSqlDB 获取*sql.DB实例
func (m *MockGORMV2) GetSqlDB() *sql.DB {
db, err := m.db.DB()
Expand Down Expand Up @@ -144,8 +152,8 @@ func (m *MockGORMV2) initModels() {
}
}
func (m *MockGORMV2) initSQL() {
if m.schema!="" {
sqls:=m.parseMockSQL(m.schema)
if m.schema != "" {
sqls := m.parseMockSQL(m.schema)
for _, sql := range sqls {
err := m.db.Exec(sql).Error
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions mockdb/mock_xorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ func (m *MockXORM) GetSqlDB() *sql.DB {
return m.engine.DB().DB
}

func (m *MockXORM) GetDSN() (dbType string, dsn string) {
dbType = m.dbType
dsn = m.dsn
return
}

// RegisterModels 注册模型
func (m *MockXORM) RegisterModels(models ...interface{}) {
if len(models) > 0 {
Expand Down
4 changes: 2 additions & 2 deletions mockredis/mock_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ type MockRedisServer struct {
redisServer *miniredis.Miniredis
}

func NewMockRedisServer() *MockRedisServer {
func NewMockRedisServer(port int) *MockRedisServer {

svc := MockRedisServer{
port: 23436,
port: port,
redisServer: miniredis.NewMiniRedis(),
}
err := svc.redisServer.Start()
Expand Down

0 comments on commit 79f6d67

Please sign in to comment.