reports: switch test fixtures from Amazon to Etsy to fix test isolation

domains/reports' fixtures used Amazon as their example platform - same
as domains/amazon's own tests. Both packages' test binaries run
concurrently under go test ./... by default, both wrote to the shared
mock.raw_shop_events table with platform='amazon', and the DB trigger
routed those into mock.shop_amazon_events - the exact table
domains/amazon's background-processing tests poll and assert on.
Confirmed directly: running the two packages together, domains/amazon's
TestProcessUnprocessedEvents_RetriesUnackedNotification picked up 4
events instead of 1, three of them from domains/reports' fixture shop.

Switched to Etsy instead (test-only change, no production code
touched). Verified the casing first since it mattered here:
accounts.Etsy's Go value is "Etsy" (capital), and separately Etsy's
raw_shop_events trigger checks for lowercase 'etsy' - but the view
these tests actually depend on (mock.shop_etsy_listing_event_sequence)
filters on 'Etsy', matching the Go constant, confirmed via
pg_get_viewdef. So the fixtures work correctly and, as a side effect,
never fire the lowercase-gated trigger at all - keeping
mock.shop_etsy_events untouched by these tests regardless.

Combined with the previous commit's goroutine-leak fix, go test ./...
and make test are both reliably green as single commands again.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XEDaCB7C2NEBgyvqEtZuxY
This commit is contained in:
2026-08-20 00:24:04 -06:00
co-authored by Claude Sonnet 5
parent 30296ee221
commit 7dcfc41188
3 changed files with 69 additions and 42 deletions
+25 -19
View File
@@ -17,11 +17,20 @@ import (
"ruben/inventory2/internal/testdb"
)
// setupAmazonMockShop creates a fresh account and an Amazon mock shop for
// it, and registers cleanup for every row it creates, in FK-safe order
// (shop_amazon[_events] -> mock.accounts -> accounts; oauth_users cleanup
// is handled by testdb.SeedOAuthUser itself).
func setupAmazonMockShop(t *testing.T, pool *pgxpool.Pool, acctStore *accounts.Store) (acctID int64, shopID string) {
// setupEtsyMockShop creates a fresh account and an Etsy mock shop for it,
// and registers cleanup for every row it creates, in FK-safe order
// (shop_etsy[_events] -> mock.accounts -> accounts; oauth_users cleanup is
// handled by testdb.SeedOAuthUser itself).
//
// Etsy (not Amazon) is deliberately used here: domains/amazon's tests
// process every unprocessed row in mock.shop_amazon_events system-wide
// (that's correct production behavior for a background worker, not a
// bug), so any test that writes Amazon-platform mock events risks being
// picked up by domains/amazon's tests when the two packages' test
// binaries run concurrently (go test ./... does this by default). Using
// a different platform here keeps this package's fixtures completely off
// domains/amazon's tables and NOTIFY channel.
func setupEtsyMockShop(t *testing.T, pool *pgxpool.Pool, acctStore *accounts.Store) (acctID int64, shopID string) {
t.Helper()
ctx := context.Background()
@@ -34,18 +43,15 @@ func setupAmazonMockShop(t *testing.T, pool *pgxpool.Pool, acctStore *accounts.S
pool.Exec(context.Background(), "DELETE FROM accounts WHERE account_id = $1", acct.AccountID)
})
id, err := acctStore.CreateMockShop(ctx, acct.AccountID, accounts.Amazon, "Test Shop")
id, err := acctStore.CreateMockShop(ctx, acct.AccountID, accounts.Etsy, "Test Shop")
require.NoError(t, err, "CreateMockShop()")
shopID = id.String()
t.Cleanup(func() {
ctx := context.Background()
// a raw_shop_events insert for platform='amazon' fires a trigger
// that also populates shop_amazon_events - clean that up too even
// though these tests don't touch the amazon event processor.
pool.Exec(ctx, "DELETE FROM mock.shop_amazon_events WHERE shop_id = $1", shopID)
pool.Exec(ctx, "DELETE FROM mock.shop_etsy_events WHERE shop_id = $1", shopID)
pool.Exec(ctx, "DELETE FROM mock.raw_shop_events WHERE shop_id = $1", shopID)
pool.Exec(ctx, "DELETE FROM mock.shop_amazon WHERE account_id = $1", acct.AccountID)
pool.Exec(ctx, "DELETE FROM mock.shop_etsy WHERE account_id = $1", acct.AccountID)
pool.Exec(ctx, "DELETE FROM mock.accounts WHERE account_id = $1", acct.AccountID)
})
@@ -56,8 +62,8 @@ func insertRawShopEvent(t *testing.T, pool *pgxpool.Pool, shopID, eventID string
t.Helper()
_, err := pool.Exec(context.Background(), `
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)
VALUES ($1, $2, $3, $4, $5::jsonb)
`, string(accounts.Etsy), shopID, ts, eventID, payload)
require.NoError(t, err, "insert raw shop event")
}
@@ -67,14 +73,14 @@ func TestGetRawShopEvents(t *testing.T) {
reportsStore := reports.NewStore(testdb.Logger(), pool, acctStore)
ctx := context.Background()
acctID, shopID := setupAmazonMockShop(t, pool, acctStore)
acctID, shopID := setupEtsyMockShop(t, pool, acctStore)
older := time.Now().Add(-time.Hour).UTC()
newer := time.Now().UTC()
insertRawShopEvent(t, pool, shopID, "evt-1", older, `{"n":1}`)
insertRawShopEvent(t, pool, shopID, "evt-2", newer, `{"n":2}`)
got, err := reportsStore.GetRawShopEvents(ctx, acctID, accounts.Amazon, shopID)
got, err := reportsStore.GetRawShopEvents(ctx, acctID, accounts.Etsy, shopID)
require.NoError(t, err, "GetRawShopEvents()")
require.Len(t, got, 2, "GetRawShopEvents()")
@@ -82,7 +88,7 @@ func TestGetRawShopEvents(t *testing.T) {
assert.Equal(t, "evt-2", got[0].EventID, "GetRawShopEvents()[0]")
assert.Equal(t, "evt-1", got[1].EventID, "GetRawShopEvents()[1]")
assert.Equal(t, accounts.Amazon, got[0].Platform, "got[0].Platform")
assert.Equal(t, accounts.Etsy, got[0].Platform, "got[0].Platform")
assert.Equal(t, shopID, got[0].ShopID, "got[0].ShopID")
var payload struct{ N int }
@@ -96,9 +102,9 @@ func TestGetRawShopEvents_NoEvents(t *testing.T) {
reportsStore := reports.NewStore(testdb.Logger(), pool, acctStore)
ctx := context.Background()
acctID, shopID := setupAmazonMockShop(t, pool, acctStore)
acctID, shopID := setupEtsyMockShop(t, pool, acctStore)
got, err := reportsStore.GetRawShopEvents(ctx, acctID, accounts.Amazon, shopID)
got, err := reportsStore.GetRawShopEvents(ctx, acctID, accounts.Etsy, shopID)
require.NoError(t, err, "GetRawShopEvents()")
assert.Empty(t, got, "GetRawShopEvents()")
}
@@ -118,6 +124,6 @@ func TestGetRawShopEvents_UnknownShop(t *testing.T) {
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())
_, err = reportsStore.GetRawShopEvents(ctx, acct.AccountID, accounts.Etsy, "no-such-shop-"+uuid.NewString())
require.ErrorIs(t, err, consts.ErrNotFound, "GetRawShopEvents()")
}