-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlean_test.go
180 lines (140 loc) · 4.05 KB
/
sqlean_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package sqlean_test
import (
"database/sql"
"github.com/mattn/go-sqlite3"
"github.com/riyaz-ali/sqlean.go"
"reflect"
"testing"
)
func init() {
sql.Register("sqlean", &sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
var db = reflect.Indirect(reflect.ValueOf(conn)).FieldByName("db").UnsafePointer()
return sqlean.Register(sqlean.DatabaseConnection(db))
},
})
}
func Open(t *testing.T, url string) (db *sql.DB) {
var err error
if db, err = sql.Open("sqlean", url); err != nil {
t.Fatalf("failed to open connection: %v", err)
}
return db
}
func TestSqleanCrypto_sha256(t *testing.T) {
var db = Open(t, ":memory:")
defer db.Close()
var hash string
if err := db.QueryRow("SELECT encode(sha256(?), 'hex')", "Hello World").Scan(&hash); err != nil {
t.Errorf("failed to generate sha256 hash: %v", err)
}
t.Logf("sha256(%q) => %s", "Hello World", hash)
}
func TestSqleanDefine_plusone(t *testing.T) {
var db = Open(t, ":memory:")
defer db.Close()
if _, err := db.Exec("select define('plus', ':x + 1')"); err != nil {
t.Fatalf("failed to define function: %v", err)
}
rows, err := db.Query("select value, plus(value) from generate_series(1, 10)")
if err != nil {
t.Errorf("query failed: %v", err)
}
defer rows.Close()
for rows.Next() {
var value, plusOne int
if err = rows.Scan(&value, &plusOne); err != nil {
t.Errorf("rows.Scan(): %v", err)
}
t.Logf("value: %2d\tplusOne: %2d", value, plusOne)
}
if err = rows.Err(); err != nil {
t.Errorf("rows.Err(): %v", err)
}
}
func TestSqleanFileIO_ls(t *testing.T) {
var db = Open(t, ":memory:")
defer db.Close()
const query = `select * from fileio_ls(?) order by name;`
//cwd, _ := os.Getwd()
rows, err := db.Query(query, ".")
if err != nil {
t.Errorf("query failed: %v", err)
}
defer rows.Close()
for rows.Next() {
var name string
var mode, mtime, size int
if err = rows.Scan(&name, &mode, &mtime, &size); err != nil {
t.Errorf("rows.Scan(): %v", err)
}
t.Logf("name: %-25s\tmode: %-5d\tmtime: %-4d\tsize: %8d", name, mode, mtime, size)
}
if err = rows.Err(); err != nil {
t.Errorf("rows.Err(): %v", err)
}
}
func TestSqleanIpAddr_ipnetwork(t *testing.T) {
var db = Open(t, ":memory:")
defer db.Close()
var network string
if err := db.QueryRow("select ipnetwork(?)", "192.168.16.12/24").Scan(&network); err != nil {
t.Errorf("query failed: %v", err)
}
t.Logf("network() => %s", network)
}
func TestSqleanMath_sqrt(t *testing.T) {
var db = Open(t, ":memory:")
defer db.Close()
var root int
if err := db.QueryRow("SELECT sqrt(?)", 9).Scan(&root); err != nil {
t.Errorf("query failed: %v", err)
}
t.Logf("sqrt(%d) => %d", 9, root)
}
func TestSqleanStats_median(t *testing.T) {
var db = Open(t, ":memory:")
defer db.Close()
const query = `SELECT median(value) FROM generate_series(1, 100)`
var median float64
if err := db.QueryRow(query).Scan(&median); err != nil {
t.Errorf("query failed: %v", err)
}
t.Logf("median() => %f", median)
}
func TestSqleanText_substr(t *testing.T) {
var db = Open(t, ":memory:")
defer db.Close()
var substr string
if err := db.QueryRow("SELECT text_substring(?, ?)", "Hello World", 7).Scan(&substr); err != nil {
t.Errorf("query failed: %v", err)
}
t.Logf("text_substring(%s, %d) => %s", "Hello World", 7, substr)
}
func TestSqleanUnicode_unaccent(t *testing.T) {
var db = Open(t, ":memory:")
defer db.Close()
var str string
if err := db.QueryRow("SELECT unaccent(?)", "hôtel").Scan(&str); err != nil {
t.Errorf("query failed: %v", err)
}
t.Logf("unaccent(%s) => %s", "hôtel", str)
}
func TestSqleanUuid_uuidv4(t *testing.T) {
var db = Open(t, ":memory:")
defer db.Close()
var id string
if err := db.QueryRow("SELECT uuid4()").Scan(&id); err != nil {
t.Errorf("query failed: %v", err)
}
t.Logf("uuid() => %s", id)
}
func TestSqlean_Version(t *testing.T) {
var db = Open(t, ":memory:")
defer db.Close()
var version string
if err := db.QueryRow("SELECT sqlean_version()").Scan(&version); err != nil {
t.Errorf("query failed: %v", err)
}
t.Logf("sqlean_version() => %s", version)
}