Files
go-admin/common/database/driver_test.go
T
zhangwenjian d4cf11d313 fix🐛: report an unknown database driver instead of panicking
opens is a map, so opens[c.Driver] on a driver this build does not carry
returns a nil function, and gorm.Open calls it. The operator saw a nil
dereference inside gorm with nothing naming the driver.

sqlite3 is the case that bites: it needs cgo and is only compiled in
under the sqlite3 build tag, so the same config file works on one binary
and dies on another. Resolve the driver first and say which ones this
build supports.
2026-08-23 13:19:15 +08:00

33 lines
869 B
Go

package database
import (
"strings"
"testing"
)
func TestOpenerForRejectsADriverThisBuildDoesNotCarry(t *testing.T) {
open, err := openerFor("sqlite")
if err == nil {
t.Fatalf("misspelled driver was accepted, opener is %v", open != nil)
}
// The message has to name the alternatives: the reader is looking at a
// config file and needs to know what to put there.
for _, want := range supportedDrivers() {
if !strings.Contains(err.Error(), want) {
t.Errorf("error does not mention the %s driver: %s", want, err)
}
}
}
func TestOpenerForResolvesTheBuiltInDrivers(t *testing.T) {
for _, driver := range []string{"mysql", "postgres", "sqlserver"} {
open, err := openerFor(driver)
if err != nil {
t.Fatalf("%s must be available in every build: %s", driver, err)
}
if open == nil {
t.Fatalf("%s resolved to a nil opener", driver)
}
}
}