mirror of
https://github.com/chaisql/chai.git
synced 2025-10-05 23:57:01 +08:00

All new error handling code now rely on internal/errors package which provides a compilation time toggle that enables to capture stacktraces for easier debugging while developing. It also comes with a new testutil/assert package which replaces the require package when it comes to checking or comparing errors and printing the stack traces if needed. Finally, the test target of the Makefile uses the debug build tag by default. A testnodebug target is also provided for convenience and to make sure no tests are broken due to not having used the internal/errors or testutil/assert package. See #431 for more details
49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package query_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/genjidb/genji"
|
|
"github.com/genjidb/genji/internal/testutil/assert"
|
|
)
|
|
|
|
func TestTransactionRun(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
queries []string
|
|
fails bool
|
|
}{
|
|
{"Same exec/ Basic", []string{`BEGIN`}, false},
|
|
{"Same exec/ Nested transaction", []string{`BEGIN;BEGIN`}, true},
|
|
{"Same exec/ Begin then commit", []string{`BEGIN;COMMIT`}, false},
|
|
{"Same exec/ Begin then rollback", []string{`BEGIN;ROLLBACK`}, false},
|
|
{"Same exec/ Begin, select, then rollback", []string{`BEGIN;SELECT 1;ROLLBACK`}, false},
|
|
{"Multiple execs/ Begin then rollback", []string{`BEGIN`, `ROLLBACK`}, false},
|
|
{"Multiple execs/ Begin then commit", []string{`BEGIN`, `COMMIT`}, false},
|
|
{"Multiple execs/ Double", []string{`BEGIN`, `COMMIT`, `BEGIN`, `COMMIT`}, false},
|
|
{"Multiple execs/ Begin then begin", []string{`BEGIN`, `BEGIN`}, true},
|
|
{"Multiple execs/ Nested", []string{`BEGIN`, `BEGIN`, `COMMIT`, `COMMIT`}, true},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
db, err := genji.Open(":memory:")
|
|
assert.NoError(t, err)
|
|
defer db.Close()
|
|
defer db.Exec("ROLLBACK")
|
|
|
|
for _, q := range test.queries {
|
|
err = db.Exec(q)
|
|
if err != nil {
|
|
break
|
|
}
|
|
}
|
|
if test.fails {
|
|
assert.Error(t, err)
|
|
return
|
|
}
|
|
assert.NoError(t, err)
|
|
})
|
|
}
|
|
}
|