Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9116bc7446 |
@@ -61,6 +61,10 @@ linters:
|
||||
desc: >-
|
||||
Test-support code belongs in test files and in packages whose
|
||||
directory name ends in test, not in the shipped binary.
|
||||
- pkg: sneak.berlin/go/neoirc/internal/dbtest
|
||||
desc: >-
|
||||
Test-support code belongs in test files and in packages whose
|
||||
directory name ends in test, not in the shipped binary.
|
||||
# Only decisions already recorded in the Go package defaults are
|
||||
# listed here. Every entry matches the module path exactly.
|
||||
gomodguard_v2:
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"go.uber.org/fx/fxtest"
|
||||
"sneak.berlin/go/neoirc/internal/config"
|
||||
"sneak.berlin/go/neoirc/internal/db"
|
||||
"sneak.berlin/go/neoirc/internal/dbtest"
|
||||
"sneak.berlin/go/neoirc/internal/globals"
|
||||
"sneak.berlin/go/neoirc/internal/logger"
|
||||
)
|
||||
@@ -163,7 +164,7 @@ func TestForeignKeysOnAfterCancelledQuery(t *testing.T) {
|
||||
func TestNewTestDatabaseURLKeepsDataAfterCancelledQuery(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
database, err := startDatabase(t, db.NewTestDatabaseURL(t))
|
||||
database, err := startDatabase(t, dbtest.NewTestDatabaseURL(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -6,13 +6,15 @@ import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"testing"
|
||||
|
||||
"sneak.berlin/go/neoirc/internal/dbtest"
|
||||
)
|
||||
|
||||
// NewTestDatabase creates an in-memory database for testing.
|
||||
func NewTestDatabase(tb testing.TB) (*Database, error) {
|
||||
tb.Helper()
|
||||
|
||||
dsn := NewTestDatabaseURL(tb) + "&_pragma=foreign_keys(1)"
|
||||
dsn := dbtest.NewTestDatabaseURL(tb) + "&_pragma=foreign_keys(1)"
|
||||
|
||||
conn, err := sql.Open("sqlite", dsn)
|
||||
if err != nil {
|
||||
|
||||
@@ -2,40 +2,10 @@ package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"log/slog"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// NewTestDatabaseURL returns the URL of a new, empty in-memory
|
||||
// database for one test. SQLite deletes an in-memory database when
|
||||
// its last connection closes. A pool closes a connection when a query
|
||||
// on it is cancelled while it runs. So this function holds a
|
||||
// connection of its own open until the test ends.
|
||||
func NewTestDatabaseURL(tb testing.TB) string {
|
||||
tb.Helper()
|
||||
|
||||
// A random name, so that no other test can share the database.
|
||||
dbURL := "file:" + rand.Text() + "?mode=memory&cache=shared"
|
||||
|
||||
pool, err := sql.Open("sqlite", dbURL)
|
||||
if err != nil {
|
||||
tb.Fatalf("open test database: %v", err)
|
||||
}
|
||||
|
||||
tb.Cleanup(func() { _ = pool.Close() })
|
||||
|
||||
conn, err := pool.Conn(tb.Context())
|
||||
if err != nil {
|
||||
tb.Fatalf("connect to test database: %v", err)
|
||||
}
|
||||
|
||||
tb.Cleanup(func() { _ = conn.Close() })
|
||||
|
||||
return dbURL
|
||||
}
|
||||
|
||||
// NewTestDatabaseFromConn creates a Database wrapping an
|
||||
// existing *sql.DB connection. Intended for integration
|
||||
// tests in other packages.
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
// Package dbtest gives each test a database of its own. Only tests
|
||||
// may import it.
|
||||
package dbtest
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"testing"
|
||||
|
||||
_ "modernc.org/sqlite" // registers the sqlite driver
|
||||
)
|
||||
|
||||
// NewTestDatabaseURL returns the URL of a new, empty in-memory
|
||||
// database for one test. SQLite deletes an in-memory database when
|
||||
// its last connection closes. A pool closes a connection when a query
|
||||
// on it is cancelled while it runs. So this function holds a
|
||||
// connection of its own open until the test ends.
|
||||
func NewTestDatabaseURL(tb testing.TB) string {
|
||||
tb.Helper()
|
||||
|
||||
// A random name, so that no other test can share the database.
|
||||
dbURL := "file:" + rand.Text() + "?mode=memory&cache=shared"
|
||||
|
||||
pool, err := sql.Open("sqlite", dbURL)
|
||||
if err != nil {
|
||||
tb.Fatalf("open test database: %v", err)
|
||||
}
|
||||
|
||||
tb.Cleanup(func() { _ = pool.Close() })
|
||||
|
||||
conn, err := pool.Conn(tb.Context())
|
||||
if err != nil {
|
||||
tb.Fatalf("connect to test database: %v", err)
|
||||
}
|
||||
|
||||
tb.Cleanup(func() { _ = conn.Close() })
|
||||
|
||||
return dbURL
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
api "sneak.berlin/go/neoirc/internal/cli/api"
|
||||
"sneak.berlin/go/neoirc/internal/config"
|
||||
"sneak.berlin/go/neoirc/internal/db"
|
||||
"sneak.berlin/go/neoirc/internal/dbtest"
|
||||
"sneak.berlin/go/neoirc/internal/globals"
|
||||
"sneak.berlin/go/neoirc/internal/handlers"
|
||||
"sneak.berlin/go/neoirc/internal/hashcash"
|
||||
@@ -163,7 +164,7 @@ func newTestServerWith(
|
||||
viperMu.Lock()
|
||||
defer viperMu.Unlock()
|
||||
|
||||
dbURL := db.NewTestDatabaseURL(t)
|
||||
dbURL := dbtest.NewTestDatabaseURL(t)
|
||||
copyTemplateDB(t, dbURL)
|
||||
|
||||
var (
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"sneak.berlin/go/neoirc/internal/broker"
|
||||
"sneak.berlin/go/neoirc/internal/config"
|
||||
"sneak.berlin/go/neoirc/internal/db"
|
||||
"sneak.berlin/go/neoirc/internal/dbtest"
|
||||
"sneak.berlin/go/neoirc/internal/globals"
|
||||
"sneak.berlin/go/neoirc/internal/ircserver"
|
||||
"sneak.berlin/go/neoirc/internal/logger"
|
||||
@@ -106,7 +107,7 @@ func newTestEnvWithResolver(
|
||||
) *testEnv {
|
||||
t.Helper()
|
||||
|
||||
conn, err := sql.Open("sqlite", db.NewTestDatabaseURL(t))
|
||||
conn, err := sql.Open("sqlite", dbtest.NewTestDatabaseURL(t))
|
||||
if err != nil {
|
||||
t.Fatalf("open db: %v", err)
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
"sneak.berlin/go/neoirc/internal/broker"
|
||||
"sneak.berlin/go/neoirc/internal/config"
|
||||
"sneak.berlin/go/neoirc/internal/db"
|
||||
"sneak.berlin/go/neoirc/internal/dbtest"
|
||||
"sneak.berlin/go/neoirc/internal/globals"
|
||||
"sneak.berlin/go/neoirc/internal/handlers"
|
||||
"sneak.berlin/go/neoirc/internal/healthcheck"
|
||||
@@ -240,7 +241,7 @@ func newApp(
|
||||
stats.New,
|
||||
),
|
||||
fx.Decorate(func(cfg *config.Config) *config.Config {
|
||||
cfg.DBURL = db.NewTestDatabaseURL(t)
|
||||
cfg.DBURL = dbtest.NewTestDatabaseURL(t)
|
||||
cfg.Port = port
|
||||
|
||||
return cfg
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
"sneak.berlin/go/neoirc/internal/broker"
|
||||
"sneak.berlin/go/neoirc/internal/config"
|
||||
"sneak.berlin/go/neoirc/internal/db"
|
||||
"sneak.berlin/go/neoirc/internal/dbtest"
|
||||
"sneak.berlin/go/neoirc/internal/globals"
|
||||
"sneak.berlin/go/neoirc/internal/hashcash"
|
||||
"sneak.berlin/go/neoirc/internal/logger"
|
||||
@@ -45,7 +46,7 @@ type testEnv struct {
|
||||
func newTestEnv(t *testing.T) *testEnv {
|
||||
t.Helper()
|
||||
|
||||
dbURL := db.NewTestDatabaseURL(t)
|
||||
dbURL := dbtest.NewTestDatabaseURL(t)
|
||||
|
||||
var (
|
||||
database *db.Database
|
||||
|
||||
Reference in New Issue
Block a user