Added sqltest testing util

This commit is contained in:
Maxim Lebedev 2022-01-08 22:42:58 +05:00
parent e2f33be718
commit 757c06842c
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 28 additions and 0 deletions

View File

@ -0,0 +1,28 @@
package sqltest
import (
"testing"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
_ "modernc.org/sqlite"
)
// Open creates a new InMemory sqlite3 database for testing.
func Open(tb testing.TB) (*sqlx.DB, func()) {
tb.Helper()
db, err := sqlx.Open("sqlite", ":memory:")
require.NoError(tb, err)
if !assert.NoError(tb, db.Ping()) {
_ = db.Close() //nolint: errcheck
tb.FailNow()
}
return db, func() {
_ = db.Close() //nolint: errcheck
}
}