stardb/stardb_safe_test.go

128 lines
5.0 KiB
Go

package stardb
import (
"context"
"errors"
"testing"
)
func TestStarDB_NotInitialized(t *testing.T) {
db := NewStarDB()
if err := db.Close(); !errors.Is(err, ErrDBNotInitialized) {
t.Fatalf("expected ErrDBNotInitialized from Close, got %v", err)
}
if err := db.Ping(); !errors.Is(err, ErrDBNotInitialized) {
t.Fatalf("expected ErrDBNotInitialized from Ping, got %v", err)
}
if err := db.PingContext(context.Background()); !errors.Is(err, ErrDBNotInitialized) {
t.Fatalf("expected ErrDBNotInitialized from PingContext, got %v", err)
}
if _, err := db.Conn(context.Background()); !errors.Is(err, ErrDBNotInitialized) {
t.Fatalf("expected ErrDBNotInitialized from Conn, got %v", err)
}
if _, err := db.Query("SELECT 1"); !errors.Is(err, ErrDBNotInitialized) {
t.Fatalf("expected ErrDBNotInitialized from Query, got %v", err)
}
if err := db.ScanEach("SELECT 1", func(row *StarResult) error { return nil }); !errors.Is(err, ErrDBNotInitialized) {
t.Fatalf("expected ErrDBNotInitialized from ScanEach, got %v", err)
}
var model struct {
ID int `db:"id"`
}
if err := db.ScanEachORM("SELECT 1", &model, func(target interface{}) error { return nil }); !errors.Is(err, ErrDBNotInitialized) {
t.Fatalf("expected ErrDBNotInitialized from ScanEachORM, got %v", err)
}
if _, err := db.QueryRaw("SELECT 1"); !errors.Is(err, ErrDBNotInitialized) {
t.Fatalf("expected ErrDBNotInitialized from QueryRaw, got %v", err)
}
if _, err := db.Exec("SELECT 1"); !errors.Is(err, ErrDBNotInitialized) {
t.Fatalf("expected ErrDBNotInitialized from Exec, got %v", err)
}
if _, err := db.Prepare("SELECT 1"); !errors.Is(err, ErrDBNotInitialized) {
t.Fatalf("expected ErrDBNotInitialized from Prepare, got %v", err)
}
if _, err := db.Begin(); !errors.Is(err, ErrDBNotInitialized) {
t.Fatalf("expected ErrDBNotInitialized from Begin, got %v", err)
}
if err := db.WithTx(nil); !errors.Is(err, ErrTxFuncNil) {
t.Fatalf("expected ErrTxFuncNil from WithTx, got %v", err)
}
if err := db.WithTx(func(tx *StarTx) error { return nil }); !errors.Is(err, ErrDBNotInitialized) {
t.Fatalf("expected ErrDBNotInitialized from WithTx, got %v", err)
}
if _, err := db.QueryStmt(""); !errors.Is(err, ErrQueryEmpty) {
t.Fatalf("expected ErrQueryEmpty from QueryStmt, got %v", err)
}
if _, err := db.ExecStmt(""); !errors.Is(err, ErrQueryEmpty) {
t.Fatalf("expected ErrQueryEmpty from ExecStmt, got %v", err)
}
_ = db.Stats()
db.SetMaxOpenConns(5)
db.SetMaxIdleConns(2)
}
func TestStarTx_NotInitialized(t *testing.T) {
tx := &StarTx{}
var model struct {
ID int `db:"id"`
}
if _, err := tx.Query("SELECT 1"); !errors.Is(err, ErrTxNotInitialized) {
t.Fatalf("expected ErrTxNotInitialized from Query, got %v", err)
}
if err := tx.ScanEach("SELECT 1", func(row *StarResult) error { return nil }); !errors.Is(err, ErrTxNotInitialized) {
t.Fatalf("expected ErrTxNotInitialized from ScanEach, got %v", err)
}
if err := tx.ScanEachORM("SELECT 1", &model, func(target interface{}) error { return nil }); !errors.Is(err, ErrTxNotInitialized) {
t.Fatalf("expected ErrTxNotInitialized from ScanEachORM, got %v", err)
}
if _, err := tx.QueryRaw("SELECT 1"); !errors.Is(err, ErrTxNotInitialized) {
t.Fatalf("expected ErrTxNotInitialized from QueryRaw, got %v", err)
}
if _, err := tx.Exec("SELECT 1"); !errors.Is(err, ErrTxNotInitialized) {
t.Fatalf("expected ErrTxNotInitialized from Exec, got %v", err)
}
if _, err := tx.Prepare("SELECT 1"); !errors.Is(err, ErrTxNotInitialized) {
t.Fatalf("expected ErrTxNotInitialized from Prepare, got %v", err)
}
if err := tx.Commit(); !errors.Is(err, ErrTxNotInitialized) {
t.Fatalf("expected ErrTxNotInitialized from Commit, got %v", err)
}
if err := tx.Rollback(); !errors.Is(err, ErrTxNotInitialized) {
t.Fatalf("expected ErrTxNotInitialized from Rollback, got %v", err)
}
if _, err := tx.QueryStmt(""); !errors.Is(err, ErrQueryEmpty) {
t.Fatalf("expected ErrQueryEmpty from QueryStmt, got %v", err)
}
if _, err := tx.ExecStmt(""); !errors.Is(err, ErrQueryEmpty) {
t.Fatalf("expected ErrQueryEmpty from ExecStmt, got %v", err)
}
}
func TestStarStmt_NotInitialized(t *testing.T) {
stmt := &StarStmt{}
if _, err := stmt.Query(); !errors.Is(err, ErrStmtNotInitialized) {
t.Fatalf("expected ErrStmtNotInitialized from Query, got %v", err)
}
if err := stmt.ScanEach(func(row *StarResult) error { return nil }); !errors.Is(err, ErrStmtNotInitialized) {
t.Fatalf("expected ErrStmtNotInitialized from ScanEach, got %v", err)
}
if err := stmt.ScanEachORM(nil, func(target interface{}) error { return nil }); !errors.Is(err, ErrTargetNil) {
t.Fatalf("expected ErrTargetNil from ScanEachORM, got %v", err)
}
if _, err := stmt.QueryRaw(); !errors.Is(err, ErrStmtNotInitialized) {
t.Fatalf("expected ErrStmtNotInitialized from QueryRaw, got %v", err)
}
if _, err := stmt.Exec(); !errors.Is(err, ErrStmtNotInitialized) {
t.Fatalf("expected ErrStmtNotInitialized from Exec, got %v", err)
}
if err := stmt.Close(); !errors.Is(err, ErrStmtNotInitialized) {
t.Fatalf("expected ErrStmtNotInitialized from Close, got %v", err)
}
}