-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_test.go
56 lines (51 loc) · 1.3 KB
/
sql_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
package decimal
import (
"database/sql"
"testing"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/require"
)
func TestSQLScan(t *testing.T) {
t.Run("Read Decimal from SQL", func(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
require.NoError(t, err)
defer db.Close()
rows, err := db.Query(`SELECT 1`)
require.NoError(t, err)
defer rows.Close()
decimals := make([]Decimal, 0)
for rows.Next() {
var d Decimal
require.NoError(t, rows.Scan(&d))
decimals = append(decimals, d)
}
require.NoError(t, rows.Err())
require.Len(t, decimals, 1)
require.Equal(t, "1", decimals[0].String())
})
t.Run("Read Invalid from SQL", func(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
require.NoError(t, err)
defer db.Close()
rows, err := db.Query(`SELECT "AB"`)
require.NoError(t, err)
defer rows.Close()
for rows.Next() {
var d Decimal
require.Error(t, rows.Scan(&d))
}
})
t.Run("Write to SQL", func(t *testing.T) {
db, err := sql.Open("sqlite3", ":memory:")
require.NoError(t, err)
defer db.Close()
rows, err := db.Query(`SELECT 1 as Foo WHERE Foo = $1`, NewFromInt(1))
require.NoError(t, err)
defer rows.Close()
for rows.Next() {
var d Decimal
require.NoError(t, rows.Scan(&d))
require.Equal(t, "1", d.String())
}
})
}