Skip to content

Commit

Permalink
reconstructor code
Browse files Browse the repository at this point in the history
  • Loading branch information
junqiang.zhang committed Jun 20, 2022
1 parent 75266fc commit 9c1c67f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 29 deletions.
55 changes: 29 additions & 26 deletions example/exmaple.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ import (
)

type User struct {
Id int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
Id int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
Remark *string `json:"remark"`
}

func main() {
//testMockGORM()
testMockGORM()
testMockGORMV2()
//testMockXORM()
testMockXORM()
//testMockZORM()
//testMockRedis()
//testMockHttpServer()
Expand All @@ -41,22 +42,22 @@ func testMockZORM() {
fmt.Println(mock.GetDSN())
mock.RegisterModels(&User{})
//db=mock.GetGormDB()
//a:=mock.GetSqlDB()
//a:=mock.GetSqlDB()
//fmt.Println(a)
//return

// mock.InitSchemas(`CREATE TABLE user (
// id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
// age int(3) DEFAULT NULL,
// name varchar(255) DEFAULT NULL COMMENT '名称',
// PRIMARY KEY (id)
//) ENGINE=InnoDB ;`)
// mock.InitSchemas(`CREATE TABLE user (
// id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
// age int(3) DEFAULT NULL,
// name varchar(255) DEFAULT NULL COMMENT '名称',
// PRIMARY KEY (id)
//) ENGINE=InnoDB ;`)
mock.ResetAndInit()
mock.ResetAndInit()
var user User

finder:=zorm.NewSelectFinder("user").Append("where id=?",1)
_,err := zorm.QueryRow(context.Background(),finder,&user)
finder := zorm.NewSelectFinder("user").Append("where id=?", 1)
_, err := zorm.QueryRow(context.Background(), finder, &user)
if err != nil {
panic(err)
}
Expand All @@ -78,14 +79,12 @@ func testMockGORM() {
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
age int(3) DEFAULT NULL,
name varchar(255) DEFAULT NULL COMMENT '名称',
remark varchar(255) DEFAULT NULL COMMENT '名称',
PRIMARY KEY (id)
) ENGINE=InnoDB ;`)
mock.ResetAndInit()

db.Callback().Query().After("gorm:query").Register("ttt:xxxx", func(scope *gorm.Scope) {
mock.DoRecord(scope)
//fmt.Println(mock.DumpRecorderToSQL())
})
mock.DoRecord(mock.GetGormDB())

var user []User
err := db.Where("id=?", 1).Find(&user).Error
Expand All @@ -96,8 +95,7 @@ func testMockGORM() {
panic(fmt.Errorf("testMockGORM error"))
}


for _,sql:=range mock.GetDBUtil().DumpFromRecordInfo(mock.GetSqlDB(),mock.DumpRecorderInfo()) {
for _, sql := range mock.GetDBUtil().DumpFromRecordInfo(mock.GetSqlDB(), mock.DumpRecorderInfo()) {
fmt.Println(sql)
}

Expand All @@ -118,15 +116,15 @@ func testDBUtil() {
}

func testMockGORMV2() {
mockdb.DBType = "mysql"
//mockdb.DBType = "mysql"
var db *gormv2.DB
mock := gmock.NewMockGORMV2("example", func(orm *mockdb.MockGORMV2) {
db = orm.GetGormDB()
})
mock.GetGormDB().Callback().Query().After("gorm:query").Register("xxx:aaa", func(db *gormv2.DB) {
mock.DoRecord(db)
})

mock.DoRecord(mock.GetGormDB())
//注册模型

mock.RegisterModels(&User{})
//初始化数据库及表数据
mock.ResetAndInit()
Expand Down Expand Up @@ -169,8 +167,8 @@ func testMockRedis() {

func testMockHttpServer() {
// 只支持 http 不支持 https
for i:=0;i<10;i++ {
server := gmock.NewMockHttpServer(23435,"./example", []string{"www.baidu.com", "www.jenkins.org"})
for i := 0; i < 10; i++ {
server := gmock.NewMockHttpServer(23435, "./example", []string{"www.baidu.com", "www.jenkins.org"})
closeFunc := server.InitMockHttpServer()

//server.SetReqRspHandler(func(req *mockhttp.Request, rsp *mockhttp.Response) {
Expand Down Expand Up @@ -202,9 +200,13 @@ func testMockXORM() {
mockdb.DBType = "mysql"
mock := gmock.NewMockXORM("example", func(orm *mockdb.MockXORM) {
engine = orm.GetXORMEngine()

})
mock.DoRecord(mock.GetXORMEngine())
mock.RegisterModels(&User{})



mock.ResetAndInit()
db := mock.GetXORMEngine()
var user User
Expand All @@ -215,6 +217,7 @@ func testMockXORM() {
if user.Id != 1 {
panic(fmt.Errorf("testMockXORM error"))
}
mock.SaveRecordToFile("./example")
}

func testMockDocker() {
Expand Down
35 changes: 34 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,46 @@ go 1.16
require (
gitee.com/chunanyong/zorm v1.5.5
github.com/alicebob/miniredis/v2 v2.20.0
github.com/containerd/containerd v1.6.1
github.com/davecgh/go-spew v1.1.1
github.com/deckarep/golang-set v1.8.0
github.com/docker/docker v20.10.3-0.20220208084023-a5c757555091+incompatible
github.com/docker/go-connections v0.4.0
github.com/docker/go-units v0.4.0
github.com/dolthub/go-mysql-server v0.11.0
github.com/dolthub/vitess v0.0.0-20211013185428-a8845fb919c1
github.com/fsouza/go-dockerclient v1.7.10
github.com/go-kit/kit v0.10.0
github.com/go-redis/redis/v8 v8.11.5
github.com/go-sql-driver/mysql v1.6.0
github.com/gogo/protobuf v1.3.2
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/protobuf v1.5.2
github.com/golang/snappy v0.0.4
github.com/gomodule/redigo v1.8.8
github.com/jinzhu/gorm v1.9.16
github.com/mattn/go-sqlite3 v1.14.12
github.com/klauspost/compress v1.11.13
github.com/mattn/go-sqlite3 v1.14.13
github.com/moby/sys/mount v0.2.0
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6
github.com/morikuni/aec v1.0.0
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.0.2
github.com/opencontainers/runc v1.1.0
github.com/opentracing/opentracing-go v1.1.0
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0
github.com/sirupsen/logrus v1.8.1
github.com/sjqzhang/goutil v0.0.2
github.com/stretchr/testify v1.7.0
github.com/syndtr/goleveldb v1.0.0
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa
google.golang.org/grpc v1.43.0
google.golang.org/protobuf v1.27.1
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
gorm.io/driver/mysql v1.3.3
gorm.io/driver/sqlite v1.3.2
gorm.io/gorm v1.23.5
Expand Down
8 changes: 7 additions & 1 deletion mockdb/mock_gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,13 @@ func (m *MockGORM) SaveRecordToFile(dir string) {
m.util.SaveRecordToFile(dir, m.util.DumpFromRecordInfo(m.recorderSQLDB, m.DumpRecorderInfo()))
}

func (m *MockGORM) DoRecord(scope *gorm.Scope) {
func (m *MockGORM) DoRecord(db *gorm.DB) {
db.Callback().Query().After("gorm:query").Register("gmock:record", func(scope *gorm.Scope) {
m.doRecord(scope)
})
}

func (m *MockGORM) doRecord(scope *gorm.Scope) {
defer func() {
if err := recover(); err != nil {
logger.Error(fmt.Sprintf("%v", err))
Expand Down
8 changes: 7 additions & 1 deletion mockdb/mock_gormv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,13 @@ func (m *MockGORMV2) DumpRecorderInfo() map[string][]string {
return result
}

func (m *MockGORMV2) DoRecord(scope *gorm.DB) {
func (m *MockGORMV2) DoRecord(db *gorm.DB) {
db.Callback().Query().After("gorm:query").Register("gmock:record", func(scope *gorm.DB) {
m.doRecord(scope)
})
}

func (m *MockGORMV2) doRecord(scope *gorm.DB) {
defer func() {
if err := recover(); err != nil {
logger.Error(fmt.Sprintf("%v", err))
Expand Down

0 comments on commit 9c1c67f

Please sign in to comment.