Skip to content

Commit

Permalink
add test for commit, rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
hantmac committed Mar 4, 2024
1 parent 2fe6a29 commit 474b972
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,46 @@ func (s *DatabendTestSuite) TestQueryNull() {
s.r.NoError(rows.Close())
}

func (s *DatabendTestSuite) TestTransactionCommit() {
tx, err := s.db.Begin()
s.r.Nil(err)

_, err = tx.Exec(fmt.Sprintf("INSERT INTO %s (i64) VALUES (?)", s.table), int64(1))
s.r.Nil(err)

err = tx.Commit()
s.r.Nil(err)

rows, err := s.db.Query(fmt.Sprintf("SELECT * FROM %s", s.table))
s.r.Nil(err)

result, err := scanValues(rows)
s.r.Nil(err)
s.r.Equal([][]interface{}{{int64(1), nil, nil, nil, nil, nil, nil, nil, nil}}, result)

s.r.NoError(rows.Close())
}

func (s *DatabendTestSuite) TestTransactionRollback() {
tx, err := s.db.Begin()
s.r.Nil(err)

_, err = tx.Exec(fmt.Sprintf("INSERT INTO %s (i64) VALUES (?)", s.table), int64(1))
s.r.Nil(err)

err = tx.Rollback()
s.r.Nil(err)

rows, err := s.db.Query(fmt.Sprintf("SELECT * FROM %s", s.table))
s.r.Nil(err)

result, err := scanValues(rows)
s.r.Nil(err)
s.r.Empty(result)

s.r.NoError(rows.Close())
}

func scanValues(rows *sql.Rows) (interface{}, error) {
var err error
var result [][]interface{}
Expand Down

0 comments on commit 474b972

Please sign in to comment.