reports: add test coverage for GetRawShopEvents
Covers the happy path (ordering by event_timestamp DESC/event_id ASC, Platform/ShopID fields, RawPayload round-tripping through jsonb), the empty-shop case, and the unknown-shop ErrNotFound case (via the accts.GetMockShop check GetRawShopEvents does before querying). setupAmazonMockShop creates a real account + Amazon mock shop via the same Store methods the app uses (CreateAccount, CreateMockShop) and registers cleanup in FK-safe order. GetListingCountsOverTime/GetListingCountsReport coverage is a separate, larger task (needs a listing plus count-changing history feeding a DB view) - not done here. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XEDaCB7C2NEBgyvqEtZuxY
This commit is contained in:
@@ -0,0 +1,147 @@
|
|||||||
|
package reports_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
|
|
||||||
|
"ruben/inventory2/consts"
|
||||||
|
"ruben/inventory2/domains/accounts"
|
||||||
|
"ruben/inventory2/domains/reports"
|
||||||
|
"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) {
|
||||||
|
t.Helper()
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
userID := testdb.NewUserID(t)
|
||||||
|
testdb.SeedOAuthUser(t, pool, userID)
|
||||||
|
|
||||||
|
acct, err := acctStore.CreateAccount(ctx, userID, userID+"@example.com")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateAccount() error = %v", err)
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
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.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.accounts WHERE account_id = $1", acct.AccountID)
|
||||||
|
})
|
||||||
|
|
||||||
|
return acct.AccountID, shopID
|
||||||
|
}
|
||||||
|
|
||||||
|
func insertRawShopEvent(t *testing.T, pool *pgxpool.Pool, shopID, eventID string, ts time.Time, payload 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)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to insert raw shop event: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetRawShopEvents(t *testing.T) {
|
||||||
|
pool := testdb.Pool(t)
|
||||||
|
acctStore := accounts.NewStore(testdb.Logger(), pool)
|
||||||
|
reportsStore := reports.NewStore(testdb.Logger(), pool, acctStore)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
acctID, shopID := setupAmazonMockShop(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)
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetRawShopEvents_NoEvents(t *testing.T) {
|
||||||
|
pool := testdb.Pool(t)
|
||||||
|
acctStore := accounts.NewStore(testdb.Logger(), pool)
|
||||||
|
reportsStore := reports.NewStore(testdb.Logger(), pool, acctStore)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetRawShopEvents_UnknownShop(t *testing.T) {
|
||||||
|
pool := testdb.Pool(t)
|
||||||
|
acctStore := accounts.NewStore(testdb.Logger(), pool)
|
||||||
|
reportsStore := reports.NewStore(testdb.Logger(), pool, acctStore)
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
userID := testdb.NewUserID(t)
|
||||||
|
testdb.SeedOAuthUser(t, pool, userID)
|
||||||
|
|
||||||
|
acct, err := acctStore.CreateAccount(ctx, userID, userID+"@example.com")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateAccount() error = %v", err)
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
# Work Summary — 2026-08-05 22:18
|
||||||
|
|
||||||
|
## Task
|
||||||
|
First half of `domains/reports` test coverage (agreed to split into two separate tasks): `GetRawShopEvents`. The other method, `GetListingCountsOverTime`/`GetListingCountsReport`, needs a deeper fixture (a listing plus count-changing history feeding a DB view) and was deliberately left for a separate pass.
|
||||||
|
|
||||||
|
## Changes
|
||||||
|
New `domains/reports/events_test.go`:
|
||||||
|
- `setupAmazonMockShop` helper: creates a fresh account (via `testdb.SeedOAuthUser` + `accounts.Store.CreateAccount`, matching the pattern from `domains/accounts/accounts_test.go`) and an Amazon mock shop via `accounts.Store.CreateMockShop`, registering cleanup for every row it creates in FK-safe order (`mock.shop_amazon_events` / `mock.raw_shop_events` → `mock.shop_amazon` → `mock.accounts` → `accounts`; `oauth_users` cleanup comes from `SeedOAuthUser` itself). Cleans up `shop_amazon_events` too even though these tests don't touch the Amazon event processor, since any `raw_shop_events` insert for `platform='amazon'` fires the same DB trigger that populates it.
|
||||||
|
- `TestGetRawShopEvents`: inserts two raw events at different timestamps, confirms both come back, in the right order (`event_timestamp DESC, event_id ASC`), with the right `Platform`/`ShopID`, and that `RawPayload` round-trips correctly through the `jsonb` column.
|
||||||
|
- `TestGetRawShopEvents_NoEvents`: a valid shop with zero events returns an empty slice, not an error.
|
||||||
|
- `TestGetRawShopEvents_UnknownShop`: an unrecognized shop ID returns `consts.ErrNotFound` (via the `accts.GetMockShop` check `GetRawShopEvents` does before querying events).
|
||||||
|
|
||||||
|
## Verification
|
||||||
|
- `go build ./...` / `go vet ./...` clean.
|
||||||
|
- `go test ./domains/reports/... -v -race`: all 3 pass.
|
||||||
|
- 10x repeated runs (`-count=1 -race`) with no flakes, ~1.1-1.2s each.
|
||||||
|
- `make test`: full suite green.
|
||||||
|
- Confirmed zero leftover rows after the run across `accounts`, `mock.accounts`, and `mock.raw_shop_events` on the test DB.
|
||||||
|
- Skipped `make test-against-dev-db`: same live `go run .` process from the previous session was still running, and this suite inserts real `mock.raw_shop_events` rows for `platform='amazon'`, which fires the same trigger/NOTIFY that live process's Amazon background handler listens on. Almost certainly harmless (unique per-test IDs, no connection manipulation involved this time, unlike the insight #4 test), but no strong need to interact with a running session's live processing loop just to re-confirm what the test-DB run already showed cleanly.
|
||||||
|
|
||||||
|
## Follow-ups / not done here
|
||||||
|
- `GetListingCountsOverTime`/`GetListingCountsReport` coverage remains a separate, open task - needs an account + mock shop + `CreateMockListing` + something that actually generates count history (a simulated sale/refund/inventory change, or a direct insert) so the `listingCountsView` these methods read from has real data to aggregate. That view is real per-platform logic, not just a passthrough, so this would be the first test exercising it directly rather than just the Go code around it.
|
||||||
Reference in New Issue
Block a user