Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
Fs02 committed Aug 20, 2022
1 parent 37ba6cb commit 9816ff1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
8 changes: 6 additions & 2 deletions mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ func New(database *db.DB) rel.Adapter {

// Open mysql connection using dsn.
func Open(dsn string) (rel.Adapter, error) {
var database, err = db.Open("mysql", rewriteDsn(dsn))
return New(database), err
}

func rewriteDsn(dsn string) string {
// force clientFoundRows=true
// this allows not found record check when updating a record.
if strings.ContainsRune(dsn, '?') {
Expand All @@ -64,8 +69,7 @@ func Open(dsn string) (rel.Adapter, error) {
dsn += "?clientFoundRows=true"
}

var database, err = db.Open("mysql", dsn)
return New(database), err
return dsn
}

// MustOpen mysql connection using dsn.
Expand Down
27 changes: 8 additions & 19 deletions mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,6 @@ func TestAdapter_PrimaryReplica_specs(t *testing.T) {
AdapterSpecs(t, repo)
}

func TestAdapter_Open(t *testing.T) {
dsn := "root@tcp(localhost:3306)"
if os.Getenv("MYSQL_DATABASE") != "" {
dsn = os.Getenv("MYSQL_DATABASE")
}

// with parameter
assert.NotPanics(t, func() {
adapter, _ := Open(dsn + "/rel_test?charset=utf8")
defer adapter.Close()
})

// without paremeter
assert.NotPanics(t, func() {
adapter, _ := Open(dsn + "/rel_test")
defer adapter.Close()
})
}

func TestAdapter_Transaction_commitError(t *testing.T) {
adapter, err := Open(dsn())
assert.Nil(t, err)
Expand All @@ -169,6 +150,14 @@ func TestAdapter_Exec_error(t *testing.T) {
assert.NotNil(t, err)
}

func TestRewriteDsn(t *testing.T) {
// with parameter
assert.Contains(t, rewriteDsn("root@tcp(localhost:3306)/rel_test?charset=utf8"), "&clientFoundRows=true")

// without paremeter
assert.Contains(t, rewriteDsn("root@tcp(localhost:3306)/rel_test"), "?clientFoundRows=true")
}

func TestCheck(t *testing.T) {
assert.Panics(t, func() {
check(errors.New("error"))
Expand Down

0 comments on commit 9816ff1

Please sign in to comment.