Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve unit tests around Hasresultset and WriteValue #1006

Merged
merged 7 commits into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mysql/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func (r *Result) Close() {
}

func (r *Result) HasResultset() bool {
if r == nil {
return false
}
if r.Resultset != nil && len(r.Resultset.Fields) > 0 {
return true
}
Expand Down
18 changes: 16 additions & 2 deletions mysql/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@ import (
func TestHasResultset_false(t *testing.T) {
r := NewResultReserveResultset(0)
b := r.HasResultset()
require.Equal(t, false, b)
require.False(t, b)
}

func TestHasResultset_true(t *testing.T) {
r := NewResultReserveResultset(1)
b := r.HasResultset()
require.Equal(t, true, b)
require.True(t, b)
}

// this shouldn't happen after d02e79a, but test just in case
func TestHasResultset_nilset(t *testing.T) {
r := NewResultReserveResultset(0)
r.Resultset = nil
b := r.HasResultset()
require.False(t, b)
}

func TestHasResultset_nil(t *testing.T) {
var r *Result
b := r.HasResultset()
require.False(t, b)
}
40 changes: 36 additions & 4 deletions server/resp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ func TestConnWriteOK(t *testing.T) {
clientConn := &mockconn.MockConn{}
conn := &Conn{Conn: packet.NewConn(clientConn)}

result := &mysql.Result{
AffectedRows: 1,
InsertId: 2,
}
result := mysql.NewResultReserveResultset(0)
result.AffectedRows = 1
result.InsertId = 2

// write ok with insertid and affectedrows set
err := conn.writeOK(result)
Expand Down Expand Up @@ -229,3 +228,36 @@ func TestConnWriteFieldValues(t *testing.T) {
// EOF
require.Equal(t, []byte{1, 0, 0, 4, mysql.EOF_HEADER}, clientConn.WriteBuffered[43:])
}

func TestWriteValue(t *testing.T) {
clientConn := &mockconn.MockConn{MultiWrite: true}
conn := &Conn{Conn: packet.NewConn(clientConn)}

// simple OK
err := conn.WriteValue(mysql.NewResultReserveResultset(0))
require.NoError(t, err)
expected := []byte{3, 0, 0, 0, mysql.OK_HEADER, 0, 0}
require.Equal(t, expected, clientConn.WriteBuffered)

// reset write buffer
clientConn.WriteBuffered = []byte{}

// resultset with no rows
rs := mysql.NewResultReserveResultset(1)
rs.Fields = []*mysql.Field{{Name: []byte("a")}}
err = conn.WriteValue(rs)
require.NoError(t, err)
expected = []byte{1, 0, 0, 1, mysql.MORE_DATE_HEADER}
require.Equal(t, expected, clientConn.WriteBuffered[:5])

// reset write buffer
clientConn.WriteBuffered = []byte{}

// resultset with rows
rs.Fields = []*mysql.Field{{Name: []byte("a")}}
rs.RowDatas = []mysql.RowData{[]byte{1, 2, 3}}
err = conn.WriteValue(rs)
require.NoError(t, err)
expected = []byte{1, 0, 0, 5, mysql.MORE_DATE_HEADER}
require.Equal(t, expected, clientConn.WriteBuffered[:5])
}
28 changes: 7 additions & 21 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,30 +241,16 @@ func (h *testHandler) handleQuery(query string, binary bool) (*mysql.Result, err
if err != nil {
return nil, errors.Trace(err)
} else {
return &mysql.Result{
Status: 0,
Warnings: 0,
InsertId: 0,
AffectedRows: 0,
Resultset: r,
}, nil
return mysql.NewResult(r), nil
}
case "insert":
return &mysql.Result{
Status: 0,
Warnings: 0,
InsertId: 1,
AffectedRows: 0,
Resultset: nil,
}, nil
res := mysql.NewResultReserveResultset(0)
res.InsertId = 1
return res, nil
case "delete", "update", "replace":
return &mysql.Result{
Status: 0,
Warnings: 0,
InsertId: 0,
AffectedRows: 1,
Resultset: nil,
}, nil
res := mysql.NewResultReserveResultset(0)
res.AffectedRows = 1
return res, nil
default:
return nil, fmt.Errorf("invalid query %s", query)
}
Expand Down
Loading