Add update test
All checks were successful
Build / build (push) Successful in 1m13s

This commit is contained in:
Sebastiaan de Schaetzen 2025-03-16 11:04:21 +01:00
parent a377448de3
commit 187ed5987d
2 changed files with 30 additions and 0 deletions

View File

@ -66,3 +66,18 @@ func TestQueryWithRange(t *testing.T) {
}
require.NoError(t, err)
}
func TestUpdateQuery(t *testing.T) {
db := openTestDb(t)
func() {
tx := db.MustBegin()
defer tx.MustRollback()
tx.Query("insert into mytable(key, value) values ('lorem', 'bar')").MustExec()
tx.Query("update mytable set value = 'ipsum' where key = 'lorem'").MustExec()
tx.MustCommit()
}()
var value string
db.Query("select value from mytable where value = 'ipsum'").MustScanSingle(&value)
require.Equal(t, "ipsum", value)
}

View File

@ -15,11 +15,26 @@ func (d *Db) Begin() (*Tx, error) {
return &Tx{db: d}, nil
}
func (d *Db) MustBegin() *Tx {
tx, err := d.Begin()
if err != nil {
panic(err)
}
return tx
}
func (tx *Tx) Commit() error {
defer tx.unlock()
return tx.Query("COMMIT").Exec()
}
func (tx *Tx) MustCommit() {
err := tx.Commit()
if err != nil {
panic(err)
}
}
func (tx *Tx) Rollback() error {
if tx.db == nil {
// The transaction was already commited