mirror of
https://github.com/ncruces/go-sqlite3.git
synced 2026-01-12 22:19:14 +00:00
65 lines
1.3 KiB
Go
65 lines
1.3 KiB
Go
package gormlite
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
_ "github.com/ncruces/go-sqlite3/embed"
|
|
)
|
|
|
|
func TestDialector(t *testing.T) {
|
|
// This is the DSN of the in-memory SQLite database for these tests.
|
|
const InMemoryDSN = "file:testdatabase?mode=memory&cache=shared"
|
|
|
|
rows := []struct {
|
|
description string
|
|
dialector *Dialector
|
|
openSuccess bool
|
|
query string
|
|
querySuccess bool
|
|
}{
|
|
{
|
|
description: "Default driver",
|
|
dialector: &Dialector{
|
|
DSN: InMemoryDSN,
|
|
},
|
|
openSuccess: true,
|
|
query: "SELECT 1",
|
|
querySuccess: true,
|
|
},
|
|
}
|
|
for rowIndex, row := range rows {
|
|
t.Run(fmt.Sprintf("%d/%s", rowIndex, row.description), func(t *testing.T) {
|
|
db, err := gorm.Open(row.dialector, &gorm.Config{})
|
|
if !row.openSuccess {
|
|
if err == nil {
|
|
t.Errorf("Expected Open to fail.")
|
|
}
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
t.Errorf("Expected Open to succeed; got error: %v", err)
|
|
}
|
|
if db == nil {
|
|
t.Errorf("Expected db to be non-nil.")
|
|
}
|
|
if row.query != "" {
|
|
err = db.Exec(row.query).Error
|
|
if !row.querySuccess {
|
|
if err == nil {
|
|
t.Errorf("Expected query to fail.")
|
|
}
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
t.Errorf("Expected query to succeed; got error: %v", err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|