diff options
Diffstat (limited to 'dbstore/tx_test.go')
-rw-r--r-- | dbstore/tx_test.go | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/dbstore/tx_test.go b/dbstore/tx_test.go new file mode 100644 index 0000000..df15f9e --- /dev/null +++ b/dbstore/tx_test.go @@ -0,0 +1,140 @@ +package dbstore + +import ( + "context" + "os" + "path/filepath" + // "reflect" + "testing" + // "time" +) + +func TestNewTxDupTx(t *testing.T) { + // cache context, create temp dir + ctx := context.Background() + dir, err := os.MkdirTemp("", "") + if err != nil { + t.Error(err) + return + } + defer os.RemoveAll(dir) + + // open db + db, err := Open(filepath.Join(dir, "newtxduptx.db")) + if err != nil { + t.Error(err) + return + } + + // init db + if err := db.Init(ctx); err != nil { + t.Error(err) + return + } + + // create first transaction + if _, err = db.Tx(ctx, []string{}); err != nil { + t.Error(err) + return + } + + // create second transaction + got, err := db.Tx(ctx, []string{}) + if err != nil { // FIXME + t.Errorf("got %v, exp error", got) + return + } +} + +func TestNewTxBadQueries(t *testing.T) { + badQueryIds := []string { "query/does/not/exist" } + + // cache context, create temp dir + ctx := context.Background() + dir, err := os.MkdirTemp("", "") + if err != nil { + t.Error(err) + return + } + defer os.RemoveAll(dir) + + // open db + db, err := Open(filepath.Join(dir, "newtx-bad-queries.db")) + if err != nil { + t.Error(err) + return + } + + // init db + if err := db.Init(ctx); err != nil { + t.Error(err) + return + } + + // create first transaction + if got, err := db.Tx(ctx, badQueryIds); err == nil { + t.Errorf("got %v, exp error", got) + return + } +} + +func TestNewTxPrepareFail(t *testing.T) { + // cache context, create temp dir + ctx := context.Background() + dir, err := os.MkdirTemp("", "") + if err != nil { + t.Error(err) + return + } + defer os.RemoveAll(dir) + + // open db + db, err := Open(filepath.Join(dir, "newtx-prepare-fail.db")) + if err != nil { + t.Error(err) + return + } + + // create transaction + if got, err := db.Tx(ctx, []string { "test/junk" }); err == nil { + t.Errorf("got %v, exp error", got) + } +} + +func TestTxExecFail(t *testing.T) { + // cache context, create temp dir + ctx := context.Background() + dir, err := os.MkdirTemp("", "") + if err != nil { + t.Error(err) + return + } + defer os.RemoveAll(dir) + + // open db + db, err := Open(filepath.Join(dir, "txexecfail.db")) + if err != nil { + t.Error(err) + return + } + + // init db + if err := db.Init(ctx); err != nil { + t.Error(err) + return + } + + // create transaction + tx, err := db.Tx(ctx, []string { "init" }) + if err != nil { + t.Error(err) + return + } + + // close transaction (this will clear statements) + tx.Close() + + if got, err := tx.Exec(ctx, "init"); err == nil { + t.Errorf("got %v, exp error", got) + } +} |