2023-06-06 12:37:54 +01:00
|
|
|
package gormlite
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
2023-09-20 02:41:09 +01:00
|
|
|
"github.com/ncruces/go-sqlite3"
|
|
|
|
|
"github.com/ncruces/go-sqlite3/driver"
|
2023-06-06 12:37:54 +01:00
|
|
|
_ "github.com/ncruces/go-sqlite3/embed"
|
2024-06-02 10:33:20 +01:00
|
|
|
_ "github.com/ncruces/go-sqlite3/internal/testcfg"
|
2024-08-05 21:25:47 +01:00
|
|
|
"github.com/ncruces/go-sqlite3/vfs/memdb"
|
2023-06-06 12:37:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestDialector(t *testing.T) {
|
2024-08-05 21:25:47 +01:00
|
|
|
tmp := memdb.TestDB(t)
|
2023-06-06 12:37:54 +01:00
|
|
|
|
2023-09-20 02:41:09 +01:00
|
|
|
// Custom connection with a custom function called "my_custom_function".
|
2024-08-05 21:25:47 +01:00
|
|
|
db, err := driver.Open(tmp, func(conn *sqlite3.Conn) error {
|
2023-09-20 02:41:09 +01:00
|
|
|
return conn.CreateFunction("my_custom_function", 0, sqlite3.DETERMINISTIC,
|
|
|
|
|
func(ctx sqlite3.Context, arg ...sqlite3.Value) {
|
|
|
|
|
ctx.ResultText("my-result")
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-06 12:37:54 +01:00
|
|
|
rows := []struct {
|
|
|
|
|
description string
|
2023-10-13 00:42:06 +01:00
|
|
|
dialector gorm.Dialector
|
2023-06-06 12:37:54 +01:00
|
|
|
openSuccess bool
|
|
|
|
|
query string
|
|
|
|
|
querySuccess bool
|
|
|
|
|
}{
|
|
|
|
|
{
|
2023-10-13 00:42:06 +01:00
|
|
|
description: "Default driver",
|
2024-08-05 21:25:47 +01:00
|
|
|
dialector: Open(tmp),
|
2023-06-06 12:37:54 +01:00
|
|
|
openSuccess: true,
|
|
|
|
|
query: "SELECT 1",
|
|
|
|
|
querySuccess: true,
|
|
|
|
|
},
|
2023-09-20 02:41:09 +01:00
|
|
|
{
|
2023-10-13 00:42:06 +01:00
|
|
|
description: "Custom function",
|
2024-08-05 21:25:47 +01:00
|
|
|
dialector: Open(tmp),
|
2023-09-20 02:41:09 +01:00
|
|
|
openSuccess: true,
|
|
|
|
|
query: "SELECT my_custom_function()",
|
|
|
|
|
querySuccess: false,
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-10-13 00:42:06 +01:00
|
|
|
description: "Custom connection",
|
|
|
|
|
dialector: OpenDB(db),
|
2023-09-20 02:41:09 +01:00
|
|
|
openSuccess: true,
|
|
|
|
|
query: "SELECT 1",
|
|
|
|
|
querySuccess: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
2023-10-13 00:42:06 +01:00
|
|
|
description: "Custom connection, custom function",
|
|
|
|
|
dialector: OpenDB(db),
|
2023-09-20 02:41:09 +01:00
|
|
|
openSuccess: true,
|
|
|
|
|
query: "SELECT my_custom_function()",
|
|
|
|
|
querySuccess: true,
|
|
|
|
|
},
|
2023-06-06 12:37:54 +01:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|