tests: migrate assertions to testify's assert/require
Replaces raw t.Error/t.Errorf/t.Fatal/t.Fatalf across every test file that has any (domains/accounts, domains/authentication, domains/raw_events, domains/amazon, domains/reports x2) with testify's assert (non-halting) / require (halting) equivalents. The three Example-based tests (server/ui/svg, server/ui/charts) have no *testing.T at all - nothing to convert there. require.Eventually replaces several hand-rolled polling loops in domains/amazon/mock_test.go. Its condition function runs on a separate goroutine (confirmed in testify's source), so calling require.* from inside one - which two of the new Eventually calls initially did, via the isProcessed helper - is unsafe per Go's testing rules (t.FailNow must only be called from the test's own goroutine). Fixed by splitting a *testing.T-free queryIsProcessed(ctx, pool, shopID, eventID) out of isProcessed for use inside those closures specifically. github.com/stretchr/testify promoted from an indirect to a direct dependency (go.mod only - it was already present transitively, so go.sum is unchanged). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XEDaCB7C2NEBgyvqEtZuxY
This commit is contained in:
@@ -2,9 +2,11 @@ package accounts_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"ruben/inventory2/consts"
|
||||
"ruben/inventory2/domains/accounts"
|
||||
"ruben/inventory2/internal/testdb"
|
||||
@@ -21,30 +23,18 @@ func TestCreateAccount(t *testing.T) {
|
||||
email := userID + "@example.com"
|
||||
|
||||
acct, err := store.CreateAccount(ctx, userID, email)
|
||||
if err != nil {
|
||||
t.Fatalf("CreateAccount() error = %v", err)
|
||||
}
|
||||
require.NoError(t, err, "CreateAccount()")
|
||||
t.Cleanup(func() {
|
||||
pool.Exec(context.Background(), "DELETE FROM accounts WHERE account_id = $1", acct.AccountID)
|
||||
})
|
||||
|
||||
if acct.AccountID == 0 {
|
||||
t.Error("CreateAccount() returned a zero AccountID")
|
||||
}
|
||||
if acct.UserID != userID {
|
||||
t.Errorf("CreateAccount() UserID = %q, want %q", acct.UserID, userID)
|
||||
}
|
||||
if acct.Email != email {
|
||||
t.Errorf("CreateAccount() Email = %q, want %q", acct.Email, email)
|
||||
}
|
||||
assert.NotZero(t, acct.AccountID, "CreateAccount() returned a zero AccountID")
|
||||
assert.Equal(t, userID, acct.UserID, "CreateAccount() UserID")
|
||||
assert.Equal(t, email, acct.Email, "CreateAccount() Email")
|
||||
|
||||
got, err := store.GetAccount(ctx, acct.AccountID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetAccount() error = %v", err)
|
||||
}
|
||||
if got != acct {
|
||||
t.Errorf("GetAccount() = %+v, want %+v", got, acct)
|
||||
}
|
||||
require.NoError(t, err, "GetAccount()")
|
||||
assert.Equal(t, acct, got, "GetAccount()")
|
||||
}
|
||||
|
||||
func TestCreateAccount_DuplicateUserIsConflict(t *testing.T) {
|
||||
@@ -56,17 +46,13 @@ func TestCreateAccount_DuplicateUserIsConflict(t *testing.T) {
|
||||
testdb.SeedOAuthUser(t, pool, userID)
|
||||
|
||||
acct, err := store.CreateAccount(ctx, userID, userID+"@example.com")
|
||||
if err != nil {
|
||||
t.Fatalf("first CreateAccount() error = %v", err)
|
||||
}
|
||||
require.NoError(t, err, "first CreateAccount()")
|
||||
t.Cleanup(func() {
|
||||
pool.Exec(context.Background(), "DELETE FROM accounts WHERE account_id = $1", acct.AccountID)
|
||||
})
|
||||
|
||||
_, err = store.CreateAccount(ctx, userID, userID+"-other@example.com")
|
||||
if !errors.Is(err, consts.ErrConflict) {
|
||||
t.Fatalf("second CreateAccount() error = %v, want %v", err, consts.ErrConflict)
|
||||
}
|
||||
require.ErrorIs(t, err, consts.ErrConflict, "second CreateAccount()")
|
||||
}
|
||||
|
||||
func TestGetAccount_NotFound(t *testing.T) {
|
||||
@@ -75,9 +61,7 @@ func TestGetAccount_NotFound(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := store.GetAccount(ctx, -1)
|
||||
if !errors.Is(err, consts.ErrNotFound) {
|
||||
t.Fatalf("GetAccount() error = %v, want %v", err, consts.ErrNotFound)
|
||||
}
|
||||
require.ErrorIs(t, err, consts.ErrNotFound, "GetAccount()")
|
||||
}
|
||||
|
||||
func TestGetUserAndAccountByAccessToken(t *testing.T) {
|
||||
@@ -90,34 +74,22 @@ func TestGetUserAndAccountByAccessToken(t *testing.T) {
|
||||
|
||||
// before an account exists: user resolves, account does not.
|
||||
user, acct, err := store.GetUserAndAccountByAccessToken(ctx, accessToken)
|
||||
if err != nil {
|
||||
t.Fatalf("GetUserAndAccountByAccessToken() before account creation: error = %v", err)
|
||||
}
|
||||
if user.UserID != userID {
|
||||
t.Errorf("GetUserAndAccountByAccessToken() UserID = %q, want %q", user.UserID, userID)
|
||||
}
|
||||
if acct != nil {
|
||||
t.Errorf("GetUserAndAccountByAccessToken() Account = %+v, want nil before an account is created", acct)
|
||||
}
|
||||
require.NoError(t, err, "GetUserAndAccountByAccessToken() before account creation")
|
||||
assert.Equal(t, userID, user.UserID, "GetUserAndAccountByAccessToken() UserID")
|
||||
assert.Nil(t, acct, "GetUserAndAccountByAccessToken() Account should be nil before an account is created")
|
||||
|
||||
created, err := store.CreateAccount(ctx, userID, userID+"@example.com")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateAccount() error = %v", err)
|
||||
}
|
||||
require.NoError(t, err, "CreateAccount()")
|
||||
t.Cleanup(func() {
|
||||
pool.Exec(context.Background(), "DELETE FROM accounts WHERE account_id = $1", created.AccountID)
|
||||
})
|
||||
|
||||
// after an account exists: both resolve.
|
||||
user, acct, err = store.GetUserAndAccountByAccessToken(ctx, accessToken)
|
||||
if err != nil {
|
||||
t.Fatalf("GetUserAndAccountByAccessToken() after account creation: error = %v", err)
|
||||
}
|
||||
if user.UserID != userID {
|
||||
t.Errorf("GetUserAndAccountByAccessToken() UserID = %q, want %q", user.UserID, userID)
|
||||
}
|
||||
if acct == nil || acct.AccountID != created.AccountID {
|
||||
t.Errorf("GetUserAndAccountByAccessToken() Account = %+v, want AccountID %d", acct, created.AccountID)
|
||||
require.NoError(t, err, "GetUserAndAccountByAccessToken() after account creation")
|
||||
assert.Equal(t, userID, user.UserID, "GetUserAndAccountByAccessToken() UserID")
|
||||
if assert.NotNil(t, acct, "GetUserAndAccountByAccessToken() Account should be set after an account is created") {
|
||||
assert.Equal(t, created.AccountID, acct.AccountID, "GetUserAndAccountByAccessToken() Account.AccountID")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,7 +99,5 @@ func TestGetUserAndAccountByAccessToken_UnknownToken(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
_, _, err := store.GetUserAndAccountByAccessToken(ctx, "no-such-token-"+testdb.NewUserID(t))
|
||||
if !errors.Is(err, consts.ErrNotFound) {
|
||||
t.Fatalf("GetUserAndAccountByAccessToken() error = %v, want %v", err, consts.ErrNotFound)
|
||||
}
|
||||
require.ErrorIs(t, err, consts.ErrNotFound, "GetUserAndAccountByAccessToken()")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user