-
Notifications
You must be signed in to change notification settings - Fork 8
/
db_sqlite_test.go
52 lines (41 loc) · 1.18 KB
/
db_sqlite_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
package allsrv_test
import (
"database/sql"
"testing"
"github.com/golang-migrate/migrate/v4"
migsqlite "github.com/golang-migrate/migrate/v4/database/sqlite3"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/jsteenb2/mess/allsrv"
"github.com/jsteenb2/mess/allsrv/migrations"
)
func TestSQLite(t *testing.T) {
testDB(t, newSQLiteDB)
}
func newSQLiteDB(t *testing.T) allsrv.DB {
t.Helper()
db := newSQLiteInmem(t)
t.Cleanup(func() {
assert.NoError(t, db.Close())
})
return allsrv.NewSQLiteDB(db)
}
func newSQLiteInmem(t *testing.T) *sqlx.DB {
const driver = "sqlite3"
db, err := sql.Open(driver, ":memory:")
require.NoError(t, err)
const dbName = "testdb"
drvr, err := migsqlite.WithInstance(db, &migsqlite.Config{DatabaseName: dbName})
require.NoError(t, err)
iodrvr, err := iofs.New(migrations.SQLite, "sqlite")
require.NoError(t, err)
m, err := migrate.NewWithInstance("iofs", iodrvr, dbName, drvr)
require.NoError(t, err)
require.NoError(t, m.Up())
dbx := sqlx.NewDb(db, driver)
dbx.SetMaxIdleConns(1)
return dbx
}