-
-
Notifications
You must be signed in to change notification settings - Fork 160
/
sqlite_cgo_test.go
65 lines (58 loc) · 1.46 KB
/
sqlite_cgo_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//go:build cgo && !pure
// +build cgo,!pure
package sqlite
import (
"database/sql"
"os"
"testing"
"github.com/mattn/go-sqlite3"
)
const (
// This is the DSN of the in-memory SQLite database for these tests.
InMemoryDSN = "file:testdatabase?mode=memory&cache=shared"
// This is the custom SQLite driver name.
CustomDriverName = "my_custom_driver"
)
func TestMain(m *testing.M) {
// Register the custom SQlite3 driver.
// It will have one custom function called "my_custom_function".
sql.Register(CustomDriverName,
// This is the custom SQLite driver.
&sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
// Define the `concat` function, since we use this elsewhere.
err := conn.RegisterFunc(
"my_custom_function",
func(arguments ...interface{}) (string, error) {
return "my-result", nil // Return a string value.
},
true,
)
return err
},
},
)
rows = append(rows, []testRow{
{
description: "Explicit default driver, custom function",
dialector: &Dialector{
DriverName: DriverName,
DSN: InMemoryDSN,
},
openSuccess: true,
query: "SELECT my_custom_function()",
querySuccess: false,
},
{
description: "Custom driver, custom function",
dialector: &Dialector{
DriverName: CustomDriverName,
DSN: InMemoryDSN,
},
openSuccess: true,
query: "SELECT my_custom_function()",
querySuccess: true,
},
}...)
os.Exit(m.Run())
}