Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
flaneur2020 committed Jan 20, 2025
1 parent 08e284d commit e928caf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
10 changes: 5 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,6 @@ func (c *APIClient) startQueryRequest(ctx context.Context, request *QueryRequest
return c.doRequest(ctx, "POST", path, request, c.NeedSticky(), &resp, &respHeaders)
}, Query,
)
if resp.Error != nil {
return nil, errors.Wrap(resp.Error, "query error")
} else if err != nil {
return nil, errors.Wrap(err, "failed to do query request")
}

if len(resp.NodeID) != 0 {
c.NodeID = resp.NodeID
Expand All @@ -528,6 +523,11 @@ func (c *APIClient) startQueryRequest(ctx context.Context, request *QueryRequest
if len(respHeaders) > 0 && len(respHeaders.Get(DatabendRouteHintHeader)) > 0 {
c.routeHint = respHeaders.Get(DatabendRouteHintHeader)
}
if resp.Error != nil {
return nil, errors.Wrap(resp.Error, "query error")
} else if err != nil {
return nil, errors.Wrap(err, "failed to do query request")
}
return &resp, nil
}

Expand Down
48 changes: 22 additions & 26 deletions tests/txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,58 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/test-go/testify/require"
)

func TestTnx(t *testing.T) {
selectT := "SELECT * FROM t ORDER BY c;"
db1, err := sql.Open("databend", dsn)
require.NoError(t, err)
assert.NoError(t, err)
db2, err := sql.Open("databend", dsn)
require.NoError(t, err)
assert.NoError(t, err)

// test commit
_, err = db1.Exec("CREATE OR REPLACE TABLE t(c int);")
require.NoError(t, err)
assert.NoError(t, err)
tx1, err := db1.Begin()
require.NoError(t, err)
assert.NoError(t, err)
_, err = tx1.Exec("INSERT INTO t(c) VALUES(1);")
require.NoError(t, err)
assert.NoError(t, err)
rows, err := tx1.Query("select 1")
require.NoError(t, err)
require.True(t, rows.Next())
assert.NoError(t, err)
assert.Equal(t, true, rows.Next())
rows.Close()

rows2, err := db2.Query(selectT)
require.NoError(t, err)
require.False(t, rows2.Next())
assert.NoError(t, err)
assert.False(t, rows2.Next())

tx2, err := db2.Begin()
assert.NoError(t, err)

_, err = tx2.Exec("INSERT INTO t(c) VALUES(2);")
require.NoError(t, err)
assert.NoError(t, err)
rows2, err = tx2.Query(selectT)
require.NoError(t, err)
require.True(t, rows2.Next())
assert.NoError(t, err)
assert.True(t, rows2.Next())

rows1, err := tx1.Query("select 2")
require.NoError(t, err)
require.True(t, rows1.Next())
assert.NoError(t, err)
assert.True(t, rows1.Next())
rows1.Close()

err = tx2.Commit()
require.NoError(t, err)
assert.NoError(t, err)
err = tx1.Commit()
require.Error(t, err)
assert.Error(t, err)

rows1, err = db1.Query(selectT)
require.Error(t, err)
assert.NoError(t, err)
if rows1 != nil {
res1, _ := scanValues(rows1)
assert.Equal(t, [][]interface{}{{int32(2)}}, res1)
}
rows2, err = db2.Query(selectT)
require.NoError(t, err)
assert.NoError(t, err)
if rows2 != nil {
res2, _ := scanValues(rows2)
assert.Equal(t, [][]interface{}{{int32(2)}}, res2)
Expand All @@ -66,16 +65,13 @@ func TestTnx(t *testing.T) {
// test rollback
db1.Exec("DROP table t;")
_, err = db1.Exec("CREATE OR REPLACE TABLE t(c int);")
require.Error(t, err)

assert.NoError(t, err)
tx1, err = db1.Begin()
require.NoError(t, err)
_, err = db1.Exec("CREATE OR REPLACE TABLE t(c int);")
require.NoError(t, err)
assert.NoError(t, err)
_, err = tx1.Exec("INSERT INTO t(c) VALUES(1);")
require.NoError(t, err)
assert.NoError(t, err)
rows, err = tx1.Query(selectT)
require.NoError(t, err)
assert.NoError(t, err)
assert.True(t, rows.Next())
rows.Close()
tx1.Rollback()
Expand Down

0 comments on commit e928caf

Please sign in to comment.