chore: use idiomatic test setup in notification tests (#14416)

This commit is contained in:
Danny Kopping
2024-08-23 16:29:27 +02:00
committed by GitHub
parent d4adfa3902
commit a4d785dec5
6 changed files with 187 additions and 185 deletions

View File

@ -2,63 +2,23 @@ package notifications_test
import (
"context"
"database/sql"
"sync/atomic"
"testing"
"time"
"github.com/google/uuid"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/serpent"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
"github.com/coder/coder/v2/coderd/database/dbgen"
"github.com/coder/coder/v2/coderd/database/dbmem"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/notifications"
"github.com/coder/coder/v2/coderd/notifications/dispatch"
"github.com/coder/coder/v2/coderd/notifications/types"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
)
func setup(t *testing.T) (context.Context, slog.Logger, database.Store) {
t.Helper()
connectionURL, closeFunc, err := dbtestutil.Open()
require.NoError(t, err)
t.Cleanup(closeFunc)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitSuperLong)
t.Cleanup(cancel)
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true, IgnoredErrorIs: []error{}}).Leveled(slog.LevelDebug)
sqlDB, err := sql.Open("postgres", connectionURL)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, sqlDB.Close())
})
// nolint:gocritic // unit tests.
return dbauthz.AsSystemRestricted(ctx), logger, database.New(sqlDB)
}
func setupInMemory(t *testing.T) (context.Context, slog.Logger, database.Store) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
t.Cleanup(cancel)
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true, IgnoredErrorIs: []error{}}).Leveled(slog.LevelDebug)
// nolint:gocritic // unit tests.
return dbauthz.AsSystemRestricted(ctx), logger, dbmem.New()
}
func defaultNotificationsConfig(method database.NotificationMethod) codersdk.NotificationsConfig {
return codersdk.NotificationsConfig{
Method: serpent.String(method),