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:
2026-08-06 20:39:30 -06:00
co-authored by Claude Sonnet 5
parent 56feb931bf
commit a9b51bcad9
8 changed files with 230 additions and 359 deletions
+17 -41
View File
@@ -3,12 +3,13 @@ package reports_test
import (
"context"
"encoding/json"
"errors"
"testing"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"ruben/inventory2/consts"
"ruben/inventory2/domains/accounts"
@@ -28,17 +29,13 @@ func setupAmazonMockShop(t *testing.T, pool *pgxpool.Pool, acctStore *accounts.S
testdb.SeedOAuthUser(t, pool, userID)
acct, err := acctStore.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", acct.AccountID)
})
id, err := acctStore.CreateMockShop(ctx, acct.AccountID, accounts.Amazon, "Test Shop")
if err != nil {
t.Fatalf("CreateMockShop() error = %v", err)
}
require.NoError(t, err, "CreateMockShop()")
shopID = id.String()
t.Cleanup(func() {
@@ -61,9 +58,7 @@ func insertRawShopEvent(t *testing.T, pool *pgxpool.Pool, shopID, eventID string
INSERT INTO mock.raw_shop_events (platform, shop_id, event_timestamp, event_id, raw_payload)
VALUES ('amazon', $1, $2, $3, $4::jsonb)
`, shopID, ts, eventID, payload)
if err != nil {
t.Fatalf("failed to insert raw shop event: %v", err)
}
require.NoError(t, err, "insert raw shop event")
}
func TestGetRawShopEvents(t *testing.T) {
@@ -80,30 +75,19 @@ func TestGetRawShopEvents(t *testing.T) {
insertRawShopEvent(t, pool, shopID, "evt-2", newer, `{"n":2}`)
got, err := reportsStore.GetRawShopEvents(ctx, acctID, accounts.Amazon, shopID)
if err != nil {
t.Fatalf("GetRawShopEvents() error = %v", err)
}
if len(got) != 2 {
t.Fatalf("GetRawShopEvents() returned %d events, want 2: %+v", len(got), got)
}
require.NoError(t, err, "GetRawShopEvents()")
require.Len(t, got, 2, "GetRawShopEvents()")
// ordered event_timestamp DESC, event_id ASC - newest first.
if got[0].EventID != "evt-2" || got[1].EventID != "evt-1" {
t.Errorf("GetRawShopEvents() order = [%s, %s], want [evt-2, evt-1]", got[0].EventID, got[1].EventID)
}
assert.Equal(t, "evt-2", got[0].EventID, "GetRawShopEvents()[0]")
assert.Equal(t, "evt-1", got[1].EventID, "GetRawShopEvents()[1]")
if got[0].Platform != accounts.Amazon || got[0].ShopID != shopID {
t.Errorf("got[0] Platform/ShopID = %s/%s, want %s/%s", got[0].Platform, got[0].ShopID, accounts.Amazon, shopID)
}
assert.Equal(t, accounts.Amazon, got[0].Platform, "got[0].Platform")
assert.Equal(t, shopID, got[0].ShopID, "got[0].ShopID")
var payload struct{ N int }
if err := json.Unmarshal(got[0].RawPayload, &payload); err != nil {
t.Fatalf("failed to unmarshal got[0].RawPayload = %s: %v", got[0].RawPayload, err)
}
if payload.N != 2 {
t.Errorf("got[0].RawPayload n = %d, want 2", payload.N)
}
require.NoError(t, json.Unmarshal(got[0].RawPayload, &payload), "unmarshal got[0].RawPayload")
assert.Equal(t, 2, payload.N, "got[0].RawPayload n")
}
func TestGetRawShopEvents_NoEvents(t *testing.T) {
@@ -115,12 +99,8 @@ func TestGetRawShopEvents_NoEvents(t *testing.T) {
acctID, shopID := setupAmazonMockShop(t, pool, acctStore)
got, err := reportsStore.GetRawShopEvents(ctx, acctID, accounts.Amazon, shopID)
if err != nil {
t.Fatalf("GetRawShopEvents() error = %v", err)
}
if len(got) != 0 {
t.Fatalf("GetRawShopEvents() = %+v, want empty", got)
}
require.NoError(t, err, "GetRawShopEvents()")
assert.Empty(t, got, "GetRawShopEvents()")
}
func TestGetRawShopEvents_UnknownShop(t *testing.T) {
@@ -133,15 +113,11 @@ func TestGetRawShopEvents_UnknownShop(t *testing.T) {
testdb.SeedOAuthUser(t, pool, userID)
acct, err := acctStore.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", acct.AccountID)
})
_, err = reportsStore.GetRawShopEvents(ctx, acct.AccountID, accounts.Amazon, "no-such-shop-"+uuid.NewString())
if !errors.Is(err, consts.ErrNotFound) {
t.Fatalf("GetRawShopEvents() error = %v, want %v", err, consts.ErrNotFound)
}
require.ErrorIs(t, err, consts.ErrNotFound, "GetRawShopEvents()")
}
+30 -50
View File
@@ -2,9 +2,11 @@ package reports_test
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"ruben/inventory2/consts"
"ruben/inventory2/domains/accounts"
"ruben/inventory2/domains/reports"
@@ -34,9 +36,7 @@ func createAmazonListing(t *testing.T, acctStore *accounts.Store, acctID int64,
Description: "a listing created for a test",
Count: baseCount,
})
if err != nil {
t.Fatalf("CreateMockListing() error = %v", err)
}
require.NoError(t, err, "CreateMockListing()")
t.Cleanup(func() {
pool.Exec(context.Background(), `
@@ -64,38 +64,28 @@ func TestGetListingCountsOverTime(t *testing.T) {
acctID, shopID := setupAmazonMockShop(t, pool, acctStore)
listingID := createAmazonListing(t, acctStore, acctID, shopID, 100)
if _, err := acctStore.SaveNewMockSale(ctx, acctID, accounts.Amazon, shopID, listingID, 10); err != nil {
t.Fatalf("SaveNewMockSale() error = %v", err)
}
if _, err := acctStore.SaveNewMockRefund(ctx, acctID, accounts.Amazon, shopID, listingID, 5); err != nil {
t.Fatalf("SaveNewMockRefund() error = %v", err)
}
if _, err := acctStore.SaveNewMockInventoryReset(ctx, acctID, accounts.Amazon, shopID, listingID, 50); err != nil {
t.Fatalf("SaveNewMockInventoryReset() error = %v", err)
}
_, err := acctStore.SaveNewMockSale(ctx, acctID, accounts.Amazon, shopID, listingID, 10)
require.NoError(t, err, "SaveNewMockSale()")
_, err = acctStore.SaveNewMockRefund(ctx, acctID, accounts.Amazon, shopID, listingID, 5)
require.NoError(t, err, "SaveNewMockRefund()")
_, err = acctStore.SaveNewMockInventoryReset(ctx, acctID, accounts.Amazon, shopID, listingID, 50)
require.NoError(t, err, "SaveNewMockInventoryReset()")
got, err := reportsStore.GetListingCountsOverTime(ctx, acctID, accounts.Amazon, shopID, listingID)
if err != nil {
t.Fatalf("GetListingCountsOverTime() error = %v", err)
}
require.NoError(t, err, "GetListingCountsOverTime()")
wantCounts := []int64{100, 110, 105, 50}
if len(got) != len(wantCounts) {
t.Fatalf("GetListingCountsOverTime() returned %d rows, want %d: %+v", len(got), len(wantCounts), got)
}
require.Len(t, got, len(wantCounts), "GetListingCountsOverTime()")
if got[0].EventTimestamp != nil {
t.Errorf("got[0].EventTimestamp = %v, want nil (the base count row)", got[0].EventTimestamp)
}
gotCounts := make([]int64, len(got))
for i, row := range got {
if row.Count != wantCounts[i] {
t.Errorf("got[%d].Count = %d, want %d (full sequence: %+v)", i, row.Count, wantCounts[i], got)
}
gotCounts[i] = row.Count
}
assert.Equal(t, wantCounts, gotCounts, "GetListingCountsOverTime() counts, full sequence: %+v", got)
assert.Nil(t, got[0].EventTimestamp, "got[0].EventTimestamp should be nil (the base count row)")
for i := 1; i < len(got); i++ {
if got[i].EventTimestamp == nil {
t.Errorf("got[%d].EventTimestamp = nil, want set (only the base row should be nil)", i)
}
assert.NotNil(t, got[i].EventTimestamp, "got[%d].EventTimestamp should be set (only the base row should be nil)", i)
}
}
@@ -108,9 +98,7 @@ func TestGetListingCountsOverTime_UnknownListing(t *testing.T) {
acctID, shopID := setupAmazonMockShop(t, pool, acctStore)
_, err := reportsStore.GetListingCountsOverTime(ctx, acctID, accounts.Amazon, shopID, "no-such-listing")
if !errors.Is(err, consts.ErrNotFound) {
t.Fatalf("GetListingCountsOverTime() error = %v, want %v", err, consts.ErrNotFound)
}
require.ErrorIs(t, err, consts.ErrNotFound, "GetListingCountsOverTime()")
}
func TestGetListingCountsReport(t *testing.T) {
@@ -122,28 +110,20 @@ func TestGetListingCountsReport(t *testing.T) {
acctID, shopID := setupAmazonMockShop(t, pool, acctStore)
listingID := createAmazonListing(t, acctStore, acctID, shopID, 100)
if _, err := acctStore.SaveNewMockSale(ctx, acctID, accounts.Amazon, shopID, listingID, 10); err != nil {
t.Fatalf("SaveNewMockSale() error = %v", err)
}
if _, err := acctStore.SaveNewMockInventoryReset(ctx, acctID, accounts.Amazon, shopID, listingID, 20); err != nil {
t.Fatalf("SaveNewMockInventoryReset() error = %v", err)
}
_, err := acctStore.SaveNewMockSale(ctx, acctID, accounts.Amazon, shopID, listingID, 10)
require.NoError(t, err, "SaveNewMockSale()")
_, err = acctStore.SaveNewMockInventoryReset(ctx, acctID, accounts.Amazon, shopID, listingID, 20)
require.NoError(t, err, "SaveNewMockInventoryReset()")
report, err := reportsStore.GetListingCountsReport(ctx, acctID, accounts.Amazon, shopID, listingID)
if err != nil {
t.Fatalf("GetListingCountsReport() error = %v", err)
}
require.NoError(t, err, "GetListingCountsReport()")
if report.AccountID != acctID || report.Platform != accounts.Amazon || report.ShopID != shopID || report.ListingID != listingID {
t.Errorf("report identity = %+v, want AccountID=%d Platform=%s ShopID=%s ListingID=%s",
report, acctID, accounts.Amazon, shopID, listingID)
}
assert.Equal(t, acctID, report.AccountID, "report.AccountID")
assert.Equal(t, accounts.Amazon, report.Platform, "report.Platform")
assert.Equal(t, shopID, report.ShopID, "report.ShopID")
assert.Equal(t, listingID, report.ListingID, "report.ListingID")
// counts over the sequence: 100 (base) -> 110 (sale +10) -> 20 (reset)
if got := report.MaxCount().Count; got != 110 {
t.Errorf("MaxCount().Count = %d, want 110", got)
}
if got := report.MinCount().Count; got != 20 {
t.Errorf("MinCount().Count = %d, want 20", got)
}
assert.Equal(t, int64(110), report.MaxCount().Count, "MaxCount().Count")
assert.Equal(t, int64(20), report.MinCount().Count, "MinCount().Count")
}