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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user