Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2673cd4ab2 |
No files matched your search
@@ -61,10 +61,6 @@ linters:
|
|||||||
desc: >-
|
desc: >-
|
||||||
Test-support code belongs in test files and in packages whose
|
Test-support code belongs in test files and in packages whose
|
||||||
directory name ends in test, not in the shipped binary.
|
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
|
# Only decisions already recorded in the Go package defaults are
|
||||||
# listed here. Every entry matches the module path exactly.
|
# listed here. Every entry matches the module path exactly.
|
||||||
gomodguard_v2:
|
gomodguard_v2:
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import (
|
|||||||
"go.uber.org/fx/fxtest"
|
"go.uber.org/fx/fxtest"
|
||||||
"sneak.berlin/go/neoirc/internal/config"
|
"sneak.berlin/go/neoirc/internal/config"
|
||||||
"sneak.berlin/go/neoirc/internal/db"
|
"sneak.berlin/go/neoirc/internal/db"
|
||||||
"sneak.berlin/go/neoirc/internal/dbtest"
|
|
||||||
"sneak.berlin/go/neoirc/internal/globals"
|
"sneak.berlin/go/neoirc/internal/globals"
|
||||||
"sneak.berlin/go/neoirc/internal/logger"
|
"sneak.berlin/go/neoirc/internal/logger"
|
||||||
)
|
)
|
||||||
@@ -164,7 +163,7 @@ func TestForeignKeysOnAfterCancelledQuery(t *testing.T) {
|
|||||||
func TestNewTestDatabaseURLKeepsDataAfterCancelledQuery(t *testing.T) {
|
func TestNewTestDatabaseURLKeepsDataAfterCancelledQuery(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
database, err := startDatabase(t, dbtest.NewTestDatabaseURL(t))
|
database, err := startDatabase(t, db.NewTestDatabaseURL(t))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,15 +6,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"sneak.berlin/go/neoirc/internal/dbtest"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewTestDatabase creates an in-memory database for testing.
|
// NewTestDatabase creates an in-memory database for testing.
|
||||||
func NewTestDatabase(tb testing.TB) (*Database, error) {
|
func NewTestDatabase(tb testing.TB) (*Database, error) {
|
||||||
tb.Helper()
|
tb.Helper()
|
||||||
|
|
||||||
dsn := dbtest.NewTestDatabaseURL(tb) + "&_pragma=foreign_keys(1)"
|
dsn := NewTestDatabaseURL(tb) + "&_pragma=foreign_keys(1)"
|
||||||
|
|
||||||
conn, err := sql.Open("sqlite", dsn)
|
conn, err := sql.Open("sqlite", dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -2,10 +2,40 @@ package db
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"log/slog"
|
"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
|
// NewTestDatabaseFromConn creates a Database wrapping an
|
||||||
// existing *sql.DB connection. Intended for integration
|
// existing *sql.DB connection. Intended for integration
|
||||||
// tests in other packages.
|
// tests in other packages.
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
// 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,7 +27,6 @@ import (
|
|||||||
api "sneak.berlin/go/neoirc/internal/cli/api"
|
api "sneak.berlin/go/neoirc/internal/cli/api"
|
||||||
"sneak.berlin/go/neoirc/internal/config"
|
"sneak.berlin/go/neoirc/internal/config"
|
||||||
"sneak.berlin/go/neoirc/internal/db"
|
"sneak.berlin/go/neoirc/internal/db"
|
||||||
"sneak.berlin/go/neoirc/internal/dbtest"
|
|
||||||
"sneak.berlin/go/neoirc/internal/globals"
|
"sneak.berlin/go/neoirc/internal/globals"
|
||||||
"sneak.berlin/go/neoirc/internal/handlers"
|
"sneak.berlin/go/neoirc/internal/handlers"
|
||||||
"sneak.berlin/go/neoirc/internal/hashcash"
|
"sneak.berlin/go/neoirc/internal/hashcash"
|
||||||
@@ -164,7 +163,7 @@ func newTestServerWith(
|
|||||||
viperMu.Lock()
|
viperMu.Lock()
|
||||||
defer viperMu.Unlock()
|
defer viperMu.Unlock()
|
||||||
|
|
||||||
dbURL := dbtest.NewTestDatabaseURL(t)
|
dbURL := db.NewTestDatabaseURL(t)
|
||||||
copyTemplateDB(t, dbURL)
|
copyTemplateDB(t, dbURL)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import (
|
|||||||
"sneak.berlin/go/neoirc/internal/broker"
|
"sneak.berlin/go/neoirc/internal/broker"
|
||||||
"sneak.berlin/go/neoirc/internal/config"
|
"sneak.berlin/go/neoirc/internal/config"
|
||||||
"sneak.berlin/go/neoirc/internal/db"
|
"sneak.berlin/go/neoirc/internal/db"
|
||||||
"sneak.berlin/go/neoirc/internal/dbtest"
|
|
||||||
"sneak.berlin/go/neoirc/internal/globals"
|
"sneak.berlin/go/neoirc/internal/globals"
|
||||||
"sneak.berlin/go/neoirc/internal/ircserver"
|
"sneak.berlin/go/neoirc/internal/ircserver"
|
||||||
"sneak.berlin/go/neoirc/internal/logger"
|
"sneak.berlin/go/neoirc/internal/logger"
|
||||||
@@ -107,7 +106,7 @@ func newTestEnvWithResolver(
|
|||||||
) *testEnv {
|
) *testEnv {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
conn, err := sql.Open("sqlite", dbtest.NewTestDatabaseURL(t))
|
conn, err := sql.Open("sqlite", db.NewTestDatabaseURL(t))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("open db: %v", err)
|
t.Fatalf("open db: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import (
|
|||||||
"sneak.berlin/go/neoirc/internal/broker"
|
"sneak.berlin/go/neoirc/internal/broker"
|
||||||
"sneak.berlin/go/neoirc/internal/config"
|
"sneak.berlin/go/neoirc/internal/config"
|
||||||
"sneak.berlin/go/neoirc/internal/db"
|
"sneak.berlin/go/neoirc/internal/db"
|
||||||
"sneak.berlin/go/neoirc/internal/dbtest"
|
|
||||||
"sneak.berlin/go/neoirc/internal/globals"
|
"sneak.berlin/go/neoirc/internal/globals"
|
||||||
"sneak.berlin/go/neoirc/internal/handlers"
|
"sneak.berlin/go/neoirc/internal/handlers"
|
||||||
"sneak.berlin/go/neoirc/internal/healthcheck"
|
"sneak.berlin/go/neoirc/internal/healthcheck"
|
||||||
@@ -241,7 +240,7 @@ func newApp(
|
|||||||
stats.New,
|
stats.New,
|
||||||
),
|
),
|
||||||
fx.Decorate(func(cfg *config.Config) *config.Config {
|
fx.Decorate(func(cfg *config.Config) *config.Config {
|
||||||
cfg.DBURL = dbtest.NewTestDatabaseURL(t)
|
cfg.DBURL = db.NewTestDatabaseURL(t)
|
||||||
cfg.Port = port
|
cfg.Port = port
|
||||||
|
|
||||||
return cfg
|
return cfg
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ import (
|
|||||||
"sneak.berlin/go/neoirc/internal/broker"
|
"sneak.berlin/go/neoirc/internal/broker"
|
||||||
"sneak.berlin/go/neoirc/internal/config"
|
"sneak.berlin/go/neoirc/internal/config"
|
||||||
"sneak.berlin/go/neoirc/internal/db"
|
"sneak.berlin/go/neoirc/internal/db"
|
||||||
"sneak.berlin/go/neoirc/internal/dbtest"
|
|
||||||
"sneak.berlin/go/neoirc/internal/globals"
|
"sneak.berlin/go/neoirc/internal/globals"
|
||||||
"sneak.berlin/go/neoirc/internal/hashcash"
|
"sneak.berlin/go/neoirc/internal/hashcash"
|
||||||
"sneak.berlin/go/neoirc/internal/logger"
|
"sneak.berlin/go/neoirc/internal/logger"
|
||||||
@@ -46,7 +45,7 @@ type testEnv struct {
|
|||||||
func newTestEnv(t *testing.T) *testEnv {
|
func newTestEnv(t *testing.T) *testEnv {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
dbURL := dbtest.NewTestDatabaseURL(t)
|
dbURL := db.NewTestDatabaseURL(t)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
database *db.Database
|
database *db.Database
|
||||||
|
|||||||
Reference in new issue
Block a user