-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
dapeng
committed
Nov 20, 2024
1 parent
72b1cd1
commit fec6a37
Showing
5 changed files
with
187 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,146 @@ | ||
package xorm | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gone-io/gone" | ||
"github.com/gone-io/gone/goner/config" | ||
"github.com/stretchr/testify/assert" | ||
"go.uber.org/mock/gomock" | ||
"reflect" | ||
"testing" | ||
"xorm.io/xorm" | ||
) | ||
|
||
func Test_provider_Suck(t *testing.T) { | ||
controller := gomock.NewController(t) | ||
engineInterface := NewMockEngineInterface(controller) | ||
var defaultDb gone.XormEngine | ||
|
||
gone.RunTest(func() { | ||
println("ok") | ||
enginesMap := make(map[string]*xorm.Engine) | ||
for i := 0; i < 10; i++ { | ||
enginesMap[fmt.Sprintf("db%d", i)] = &xorm.Engine{} | ||
} | ||
|
||
gone.RunTest(func(i struct { | ||
p *provider `gone:"*"` | ||
}) { | ||
t.Run("get default gone.XormEngine", func(t *testing.T) { | ||
var X gone.XormEngine | ||
err := i.p.Suck("", reflect.ValueOf(&X).Elem()) | ||
assert.Nil(t, err) | ||
assert.Equal(t, X, defaultDb) | ||
}) | ||
|
||
t.Run("get default master gone.XormEngine", func(t *testing.T) { | ||
var X gone.XormEngine | ||
err := i.p.Suck("master", reflect.ValueOf(&X).Elem()) | ||
assert.Nil(t, err) | ||
assert.Equal(t, X.(*wrappedEngine).EngineInterface, enginesMap["db0"]) | ||
}) | ||
|
||
t.Run("get default slave gone.XormEngine", func(t *testing.T) { | ||
var X gone.XormEngine | ||
err := i.p.Suck("slave", reflect.ValueOf(&X).Elem()) | ||
assert.Nil(t, err) | ||
assert.Equal(t, X.(*wrappedEngine).EngineInterface, enginesMap["db1"]) | ||
|
||
err = i.p.Suck("slave=1", reflect.ValueOf(&X).Elem()) | ||
assert.Nil(t, err) | ||
assert.Equal(t, X.(*wrappedEngine).EngineInterface, enginesMap["db2"]) | ||
|
||
err = i.p.Suck("slave=0", reflect.ValueOf(&X).Elem()) | ||
assert.Nil(t, err) | ||
assert.Equal(t, X.(*wrappedEngine).EngineInterface, enginesMap["db1"]) | ||
}) | ||
|
||
t.Run("get default slave gone.XormEngine with error index", func(t *testing.T) { | ||
var X gone.XormEngine | ||
err := i.p.Suck("slave=-1", reflect.ValueOf(&X).Elem()) | ||
assert.Error(t, err) | ||
|
||
err = i.p.Suck("slave=2", reflect.ValueOf(&X).Elem()) | ||
assert.Error(t, err) | ||
}) | ||
|
||
t.Run("get default slaves with []gone.XormEngine", func(t *testing.T) { | ||
var X []gone.XormEngine | ||
err := i.p.Suck("", reflect.ValueOf(&X).Elem()) | ||
assert.Nil(t, err) | ||
assert.Equal(t, len(X), 2) | ||
assert.Equal(t, X[0].(*wrappedEngine).EngineInterface, enginesMap["db1"]) | ||
assert.Equal(t, X[1].(*wrappedEngine).EngineInterface, enginesMap["db2"]) | ||
}) | ||
|
||
t.Run("get user.database gone.XormEngine", func(t *testing.T) { | ||
var X gone.XormEngine | ||
err := i.p.Suck("db=user.database", reflect.ValueOf(&X).Elem()) | ||
assert.Nil(t, err) | ||
|
||
err = i.p.Suck("db=user.database,master", reflect.ValueOf(&X).Elem()) | ||
assert.Nil(t, err) | ||
assert.Equal(t, X.(*wrappedEngine).EngineInterface, enginesMap["db3"]) | ||
}) | ||
|
||
t.Run("get user.database slave gone.XormEngine", func(t *testing.T) { | ||
var X gone.XormEngine | ||
err := i.p.Suck("db=user.database,slave=1", reflect.ValueOf(&X).Elem()) | ||
assert.Nil(t, err) | ||
assert.Equal(t, X.(*wrappedEngine).EngineInterface, enginesMap["db5"]) | ||
|
||
err = i.p.Suck("db=user.database,slave=2", reflect.ValueOf(&X).Elem()) | ||
assert.Error(t, err) | ||
}) | ||
|
||
t.Run("get user.database slave with []gone.XormEngine", func(t *testing.T) { | ||
var X []gone.XormEngine | ||
err := i.p.Suck("db=user.database", reflect.ValueOf(&X).Elem()) | ||
assert.Nil(t, err) | ||
assert.Equal(t, len(X), 2) | ||
assert.Equal(t, X[0].(*wrappedEngine).EngineInterface, enginesMap["db4"]) | ||
assert.Equal(t, X[1].(*wrappedEngine).EngineInterface, enginesMap["db5"]) | ||
}) | ||
|
||
}, func(cemetery gone.Cemetery) error { | ||
e := wrappedEngine{ | ||
//Logger: in.logger, | ||
newFunc: func(driverName string, dataSourceName string) (xorm.EngineInterface, error) { | ||
engineInterface := NewMockEngineInterface(controller) | ||
engineInterface.EXPECT().Ping().Return(nil).AnyTimes() | ||
newFunc := func(driverName string, dataSourceName string) (xorm.EngineInterface, error) { | ||
return enginesMap[dataSourceName], nil | ||
} | ||
|
||
return engineInterface, nil | ||
}, | ||
e := wrappedEngine{ | ||
newFunc: newFunc, | ||
newSession: func(engineInterface xorm.EngineInterface) XInterface { | ||
session := NewMockXInterface(controller) | ||
//session.EXPECT().Begin().Return(nil) | ||
//session.EXPECT().Close().Return(nil) | ||
//session.EXPECT().Rollback().Return(errors.New("error")) | ||
|
||
return session | ||
}, | ||
conf: &Conf{ | ||
EnableCluster: true, | ||
MasterConf: &ClusterNodeConf{}, | ||
SlavesConf: []*ClusterNodeConf{ | ||
{ | ||
DriverName: "mysql", | ||
DSN: "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local", | ||
}, | ||
{ | ||
DriverName: "mysql", | ||
DSN: "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local", | ||
}, | ||
}, | ||
masterConf: &ClusterNodeConf{ | ||
DriverName: "mysql", | ||
DSN: "db0", | ||
}, | ||
slavesConf: []*ClusterNodeConf{ | ||
{ | ||
DriverName: "mysql", | ||
DSN: "db1", | ||
}, | ||
{ | ||
DriverName: "mysql", | ||
DSN: "db2", | ||
}, | ||
}, | ||
} | ||
err := e.Start(cemetery) | ||
|
||
err := e.create() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
e.EngineInterface = engineInterface | ||
e.unitTest = true | ||
defaultDb = &e | ||
|
||
cemetery.Bury(NewProvider(&e)) | ||
_ = config.Priest(cemetery) | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
|
||
|
||
user.database.cluster.enable=true | ||
user.database.cluster.master.driver-name=mysql | ||
user.database.cluster.master.dsn=db3 | ||
user.database.ping-before-start=false | ||
|
||
user.database.cluster.slaves[0].driver-name=mysql | ||
user.database.cluster.slaves[0].dsn=db4 | ||
|
||
user.database.cluster.slaves[1].driver-name=postgresql | ||
user.database.cluster.slaves[1].dsn=db5 |