This repository has been archived by the owner on Nov 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbutil_test.go
207 lines (167 loc) · 4.69 KB
/
dbutil_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package dbutil_test
import (
"database/sql"
"os"
"path"
"testing"
"github.com/minetest-go/dbutil"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
)
func setupDB(t assert.TestingT) *sql.DB {
tmpdir, err := os.MkdirTemp(os.TempDir(), "dbutil")
assert.NoError(t, err)
db_, err := sql.Open("sqlite3", path.Join(tmpdir, "dbutil.sqlite"))
assert.NoError(t, err)
db_.SetMaxOpenConns(1)
_, err = db_.Exec(`
create table mytable(
pk integer primary key autoincrement,
f1 int
);
`)
assert.NoError(t, err)
return db_
}
// testing below here
func Test(t *testing.T) {
// setup
db := setupDB(t)
provider := func() *MyTable { return &MyTable{} }
dbu := dbutil.New(db, dbutil.DialectSQLite, provider)
// insert
assert.NoError(t, dbu.Insert(&MyTable{F1: 1}))
// insert with return value (sqlite specific: "INSERT RETURNING")
var retVal int64 = -1
assert.NoError(t, dbu.InsertReturning(&MyTable{F1: 2}, "pk", &retVal))
assert.True(t, retVal >= 0)
// insert or replace (sqlite flavor)
assert.NoError(t, dbu.InsertOrReplace(&MyTable{F1: 2, PK: &retVal}))
// select single
res, err := dbu.Select("where f1 = %s", 1)
assert.NoError(t, err)
assert.NotNil(t, res)
assert.Equal(t, 1, res.F1)
// select single non-existent
_, err = dbu.Select("where f1 = %s", 0)
assert.Error(t, err)
assert.ErrorIs(t, sql.ErrNoRows, err)
// select multi
list, err := dbu.SelectMulti("where true=true")
assert.NoError(t, err)
assert.Equal(t, 2, len(list))
// select multi (no results)
list, err = dbu.SelectMulti("where f1 = %s", -1)
assert.NoError(t, err)
assert.Equal(t, 0, len(list))
// count all
count, err := dbu.Count("where true=true")
assert.NoError(t, err)
assert.Equal(t, 2, count)
// count all (in tx)
tx, err := db.Begin()
dbutx := dbutil.New(tx, dbutil.DialectSQLite, provider)
assert.NoError(t, err)
count, err = dbutx.Count("where true=true")
assert.NoError(t, err)
assert.Equal(t, 2, count)
assert.NoError(t, tx.Commit())
// update (where f2 = 2)
tbl, err := dbu.Select("where f1 = %s", 2)
assert.NoError(t, err)
assert.NotNil(t, tbl)
tbl.F1 = 3
err = dbu.Update(tbl, "where f1 = %s", 2)
assert.NoError(t, err)
// count specific
count, err = dbu.Count("where f1 = %s", 3)
assert.NoError(t, err)
assert.Equal(t, 1, count)
// delete
err = dbu.Delete("where f1 = %s", 3)
assert.NoError(t, err)
// count specific (after delete)
count, err = dbu.Count("where f1 = %s", 3)
assert.NoError(t, err)
assert.Equal(t, 0, count)
// prepare insert
ps, err := dbu.PrepareInsert()
assert.NoError(t, err)
assert.NotNil(t, ps)
assert.NoError(t, ps(&MyTable{F1: 66}))
// select prepared insert
res, err = dbu.Select("where f1 = %s", 66)
assert.NoError(t, err)
assert.NotNil(t, res)
assert.Equal(t, 66, res.F1)
}
func TestBindParams(t *testing.T) {
dbu := dbutil.New[*MyTable](nil, dbutil.DialectSQLite, nil)
str := dbu.FormatBindParams("where x = %s and y = %s", 2)
assert.Equal(t, "where x = ?1 and y = ?2", str)
dbu = dbutil.New[*MyTable](nil, dbutil.DialectPostgres, nil)
str = dbu.FormatBindParams("where x = %s and y = %s", 2)
assert.Equal(t, "where x = $1 and y = $2", str)
}
func BenchmarkInsert(b *testing.B) {
// setup
db := setupDB(b)
provider := func() *MyTable { return &MyTable{} }
dbu := dbutil.New(db, dbutil.DialectSQLite, provider)
// insert
b.ResetTimer()
for i := 0; i < b.N; i++ {
assert.NoError(b, dbu.Insert(&MyTable{F1: 1}))
}
}
func BenchmarkInsertTx(b *testing.B) {
// setup
db := setupDB(b)
provider := func() *MyTable { return &MyTable{} }
tx, err := db.Begin()
assert.NoError(b, err)
assert.NotNil(b, tx)
dbu := dbutil.New(tx, dbutil.DialectSQLite, provider)
// insert
b.ResetTimer()
for i := 0; i < b.N; i++ {
assert.NoError(b, dbu.Insert(&MyTable{F1: 1}))
}
assert.NoError(b, tx.Commit())
}
func BenchmarkInsertPrepared(b *testing.B) {
// setup
db := setupDB(b)
provider := func() *MyTable { return &MyTable{} }
dbu := dbutil.New(db, dbutil.DialectSQLite, provider)
// prepare insert
ps, err := dbu.PrepareInsert()
assert.NoError(b, err)
assert.NotNil(b, ps)
assert.NoError(b, ps(&MyTable{F1: 66}))
// insert
b.ResetTimer()
for i := 0; i < b.N; i++ {
assert.NoError(b, ps(&MyTable{F1: 1}))
}
}
func BenchmarkInsertPreparedTx(b *testing.B) {
// setup
db := setupDB(b)
provider := func() *MyTable { return &MyTable{} }
tx, err := db.Begin()
assert.NoError(b, err)
assert.NotNil(b, tx)
dbu := dbutil.New(tx, dbutil.DialectSQLite, provider)
// prepare insert
ps, err := dbu.PrepareInsert()
assert.NoError(b, err)
assert.NotNil(b, ps)
assert.NoError(b, ps(&MyTable{F1: 66}))
// insert
b.ResetTimer()
for i := 0; i < b.N; i++ {
assert.NoError(b, ps(&MyTable{F1: 1}))
}
assert.NoError(b, tx.Commit())
}