From 757c06842ceb32a4a8c9b1b24f676d2559f357c2 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Sat, 8 Jan 2022 22:42:58 +0500 Subject: [PATCH] :sparkles: Added sqltest testing util --- internal/testing/sqltest/sqltest.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 internal/testing/sqltest/sqltest.go diff --git a/internal/testing/sqltest/sqltest.go b/internal/testing/sqltest/sqltest.go new file mode 100644 index 0000000..e5def5d --- /dev/null +++ b/internal/testing/sqltest/sqltest.go @@ -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 + } +}