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
129 lines
5.1 KiB
Go
129 lines
5.1 KiB
Go
package reports_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"ruben/inventory2/consts"
|
|
"ruben/inventory2/domains/accounts"
|
|
"ruben/inventory2/domains/reports"
|
|
"ruben/inventory2/internal/testdb"
|
|
)
|
|
|
|
// createEtsyListing creates a listing with the given base count in shopID
|
|
// and registers its cleanup. Must be called after setupEtsyMockShop, so
|
|
// cleanup order (LIFO) deletes the listing before the shop it belongs to.
|
|
func createEtsyListing(t *testing.T, acctStore *accounts.Store, acctID int64, shopID string, baseCount int64) (listingID string) {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
|
|
pool := testdb.Pool(t)
|
|
|
|
listing, err := acctStore.CreateMockListing(ctx, accounts.MockListing{
|
|
AccountShopListingIDs: accounts.AccountShopListingIDs{
|
|
AccountShopIDs: accounts.AccountShopIDs{
|
|
AccountIDs: accounts.AccountIDs{AccountID: acctID},
|
|
Platform: accounts.Etsy,
|
|
ShopID: shopID,
|
|
},
|
|
},
|
|
SKU: "test-sku",
|
|
Name: "Test Listing",
|
|
Description: "a listing created for a test",
|
|
Count: baseCount,
|
|
})
|
|
require.NoError(t, err, "CreateMockListing()")
|
|
|
|
t.Cleanup(func() {
|
|
pool.Exec(context.Background(), `
|
|
DELETE FROM mock.shop_etsy_listings WHERE shop_id = $1 AND listing_id = $2
|
|
`, shopID, listing.ListingID)
|
|
})
|
|
|
|
return listing.ListingID
|
|
}
|
|
|
|
// TestGetListingCountsOverTime exercises the actual running-count logic,
|
|
// which lives in a recursive SQL view (mock.shop_etsy_listing_counts,
|
|
// built on mock.shop_etsy_listing_event_sequence) rather than in Go: it
|
|
// starts from the listing's base count and walks mock.raw_shop_events in
|
|
// order, applying each as a delta (sale/refund) or an absolute reset
|
|
// (inventory-reset). Uses the real SaveNewMock* methods to write those
|
|
// events, the same entry points the simulate-sale/refund/inventory UI
|
|
// uses, rather than hand-rolling the JSON payload shape.
|
|
func TestGetListingCountsOverTime(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 := setupEtsyMockShop(t, pool, acctStore)
|
|
listingID := createEtsyListing(t, acctStore, acctID, shopID, 100)
|
|
|
|
_, err := acctStore.SaveNewMockSale(ctx, acctID, accounts.Etsy, shopID, listingID, 10)
|
|
require.NoError(t, err, "SaveNewMockSale()")
|
|
_, err = acctStore.SaveNewMockRefund(ctx, acctID, accounts.Etsy, shopID, listingID, 5)
|
|
require.NoError(t, err, "SaveNewMockRefund()")
|
|
_, err = acctStore.SaveNewMockInventoryReset(ctx, acctID, accounts.Etsy, shopID, listingID, 50)
|
|
require.NoError(t, err, "SaveNewMockInventoryReset()")
|
|
|
|
got, err := reportsStore.GetListingCountsOverTime(ctx, acctID, accounts.Etsy, shopID, listingID)
|
|
require.NoError(t, err, "GetListingCountsOverTime()")
|
|
|
|
wantCounts := []int64{100, 110, 105, 50}
|
|
require.Len(t, got, len(wantCounts), "GetListingCountsOverTime()")
|
|
|
|
gotCounts := make([]int64, len(got))
|
|
for i, row := range 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++ {
|
|
assert.NotNil(t, got[i].EventTimestamp, "got[%d].EventTimestamp should be set (only the base row should be nil)", i)
|
|
}
|
|
}
|
|
|
|
func TestGetListingCountsOverTime_UnknownListing(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 := setupEtsyMockShop(t, pool, acctStore)
|
|
|
|
_, err := reportsStore.GetListingCountsOverTime(ctx, acctID, accounts.Etsy, shopID, "no-such-listing")
|
|
require.ErrorIs(t, err, consts.ErrNotFound, "GetListingCountsOverTime()")
|
|
}
|
|
|
|
func TestGetListingCountsReport(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 := setupEtsyMockShop(t, pool, acctStore)
|
|
listingID := createEtsyListing(t, acctStore, acctID, shopID, 100)
|
|
|
|
_, err := acctStore.SaveNewMockSale(ctx, acctID, accounts.Etsy, shopID, listingID, 10)
|
|
require.NoError(t, err, "SaveNewMockSale()")
|
|
_, err = acctStore.SaveNewMockInventoryReset(ctx, acctID, accounts.Etsy, shopID, listingID, 20)
|
|
require.NoError(t, err, "SaveNewMockInventoryReset()")
|
|
|
|
report, err := reportsStore.GetListingCountsReport(ctx, acctID, accounts.Etsy, shopID, listingID)
|
|
require.NoError(t, err, "GetListingCountsReport()")
|
|
|
|
assert.Equal(t, acctID, report.AccountID, "report.AccountID")
|
|
assert.Equal(t, accounts.Etsy, 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)
|
|
assert.Equal(t, int64(110), report.MaxCount().Count, "MaxCount().Count")
|
|
assert.Equal(t, int64(20), report.MinCount().Count, "MinCount().Count")
|
|
}
|