Skip to content

Commit

Permalink
Merge pull request arana-db#16 from baerwang/master
Browse files Browse the repository at this point in the history
style:optimizing test cases, add conn close
  • Loading branch information
dk-lockdown authored Dec 23, 2021
2 parents e9ab8de + e55fba8 commit 8b69663
Showing 1 changed file with 20 additions and 54 deletions.
74 changes: 20 additions & 54 deletions test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,25 @@ import (

func TestInsert(t *testing.T) {
db, err := sql.Open("mysql", "dksl:123456@tcp(127.0.0.1:13306)/employees?timeout=1s&readTimeout=1s&writeTimeout=1s&parseTime=true&loc=Local&charset=utf8mb4,utf8")
if err != nil {
t.Errorf("connection error: %v", err)
return
}
assert.NoErrorf(t, err, "connection error: %v", err)
defer db.Close()

result, err := db.Exec(`INSERT INTO employees ( emp_no, birth_date, first_name, last_name, gender, hire_date )
VALUES (?, ?, ?, ?, ?, ?)`, 100001, "1949-10-01", "共和国", "中华人民", "M", "1949-10-01")
if err != nil {
t.Errorf("insert row error: %v", err)
return
}
assert.NoErrorf(t, err, "insert row error: %v", err)
affected, err := result.RowsAffected()
if err != nil {
t.Errorf("insert row error: %v", err)
return
}
assert.NoErrorf(t, err, "insert row error: %v", err)
assert.Equal(t, int64(1), affected)
}

func TestSelect(t *testing.T) {
db, err := sql.Open("mysql", "dksl:123456@tcp(127.0.0.1:13306)/employees?timeout=1s&readTimeout=1s&writeTimeout=1s&parseTime=true&loc=Local&charset=utf8mb4,utf8")
if err != nil {
t.Errorf("connection error: %v", err)
return
}
assert.NoErrorf(t, err, "connection error: %v", err)
defer db.Close()

rows, err := db.Query(`SELECT emp_no, birth_date, first_name, last_name, gender, hire_date FROM employees
WHERE emp_no = ?`, 100001)
if err != nil {
t.Errorf("select row error: %v", err)
return
}
assert.NoErrorf(t, err, "select row error: %v", err)

var empNo string
var birthDate time.Time
Expand All @@ -73,7 +60,7 @@ func TestSelect(t *testing.T) {
var gender string
var hireDate time.Time
if rows.Next() {
err := rows.Scan(&empNo, &birthDate, &firstName, &lastName, &gender, &hireDate)
err = rows.Scan(&empNo, &birthDate, &firstName, &lastName, &gender, &hireDate)
if err != nil {
t.Error(err)
}
Expand All @@ -83,16 +70,11 @@ func TestSelect(t *testing.T) {

func TestSelectLimit1(t *testing.T) {
db, err := sql.Open("mysql", "dksl:123456@tcp(127.0.0.1:13306)/employees?timeout=1s&readTimeout=1s&writeTimeout=1s&parseTime=true&loc=Local&charset=utf8mb4,utf8")
if err != nil {
t.Errorf("connection error: %v", err)
return
}
assert.NoErrorf(t, err, "connection error: %v", err)
defer db.Close()

rows, err := db.Query(`SELECT emp_no, birth_date, first_name, last_name, gender, hire_date FROM employees LIMIT 1`)
if err != nil {
t.Errorf("select row error: %v", err)
return
}
assert.NoErrorf(t, err, "select row error: %v", err)

var empNo string
var birthDate time.Time
Expand All @@ -101,7 +83,7 @@ func TestSelectLimit1(t *testing.T) {
var gender string
var hireDate time.Time
if rows.Next() {
err := rows.Scan(&empNo, &birthDate, &firstName, &lastName, &gender, &hireDate)
err = rows.Scan(&empNo, &birthDate, &firstName, &lastName, &gender, &hireDate)
if err != nil {
t.Error(err)
}
Expand All @@ -111,41 +93,25 @@ func TestSelectLimit1(t *testing.T) {

func TestUpdate(t *testing.T) {
db, err := sql.Open("mysql", "dksl:123456@tcp(127.0.0.1:13306)/employees?timeout=1s&readTimeout=1s&writeTimeout=1s&parseTime=true&loc=Local&charset=utf8mb4,utf8")
if err != nil {
t.Errorf("connection error: %v", err)
return
}
assert.NoErrorf(t, err, "connection error: %v", err)
defer db.Close()

result, err := db.Exec(`UPDATE employees set last_name = ? where emp_no = ?`, "伟大的中华人民", 100001)
if err != nil {
t.Errorf("update row error: %v", err)
return
}
assert.NoErrorf(t, err, "update row error: %v", err)
affected, err := result.RowsAffected()
if err != nil {
t.Errorf("update row error: %v", err)
return
}
assert.NoErrorf(t, err, "update row error: %v", err)

assert.Equal(t, int64(1), affected)
}

func TestDelete(t *testing.T) {
db, err := sql.Open("mysql", "dksl:123456@tcp(127.0.0.1:13306)/employees?timeout=1s&readTimeout=1s&writeTimeout=1s&parseTime=true&loc=Local&charset=utf8mb4,utf8")
if err != nil {
t.Errorf("connection error: %v", err)
return
}
assert.NoErrorf(t, err, "connection error: %v", err)
defer db.Close()

result, err := db.Exec(`DELETE FROM employees WHERE emp_no = ?`, 100001)
if err != nil {
t.Errorf("delete row error: %v", err)
return
}
assert.NoErrorf(t, err, "delete row error: %v", err)
affected, err := result.RowsAffected()
if err != nil {
t.Errorf("delete row error: %v", err)
return
}
assert.NoErrorf(t, err, "delete row error: %v", err)
assert.Equal(t, int64(1), affected)
}

0 comments on commit 8b69663

Please sign in to comment.