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
69 lines
2.0 KiB
Go
69 lines
2.0 KiB
Go
package raw_events_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"ruben/inventory2/domains/raw_events"
|
|
"ruben/inventory2/internal/testdb"
|
|
)
|
|
|
|
func TestSaveAndLoadEventsForStore(t *testing.T) {
|
|
pool := testdb.Pool(t)
|
|
store := raw_events.NewStore(testdb.Logger(), pool)
|
|
ctx := context.Background()
|
|
|
|
platform := "test-platform"
|
|
storeID := "test-store-" + uuid.NewString()
|
|
|
|
t.Cleanup(func() {
|
|
pool.Exec(context.Background(), "DELETE FROM raw_store_events WHERE platform = $1 AND store_id = $2", platform, storeID)
|
|
})
|
|
|
|
older := raw_events.Event{
|
|
Platform: platform,
|
|
StoreID: storeID,
|
|
EventID: "evt-1",
|
|
EventTimestamp: time.Now().Add(-time.Hour).UTC(),
|
|
Payload: json.RawMessage(`{"n":1}`),
|
|
}
|
|
newer := raw_events.Event{
|
|
Platform: platform,
|
|
StoreID: storeID,
|
|
EventID: "evt-2",
|
|
EventTimestamp: time.Now().UTC(),
|
|
Payload: json.RawMessage(`{"n":2}`),
|
|
}
|
|
|
|
require.NoError(t, store.Save(ctx, &older), "Save() older event")
|
|
require.NoError(t, store.Save(ctx, &newer), "Save() newer event")
|
|
|
|
got, err := store.LoadEventsForStore(ctx, platform, storeID)
|
|
require.NoError(t, err, "LoadEventsForStore()")
|
|
require.Len(t, got, 2, "LoadEventsForStore()")
|
|
|
|
// ordered event_timestamp DESC - newest first.
|
|
assert.Equal(t, "evt-2", got[0].EventID, "LoadEventsForStore()[0]")
|
|
assert.Equal(t, "evt-1", got[1].EventID, "LoadEventsForStore()[1]")
|
|
|
|
var payload struct{ N int }
|
|
require.NoError(t, json.Unmarshal(got[0].Payload, &payload), "unmarshal LoadEventsForStore()[0].Payload")
|
|
assert.Equal(t, 2, payload.N, "LoadEventsForStore()[0].Payload n")
|
|
}
|
|
|
|
func TestLoadEventsForStore_NoEvents(t *testing.T) {
|
|
pool := testdb.Pool(t)
|
|
store := raw_events.NewStore(testdb.Logger(), pool)
|
|
ctx := context.Background()
|
|
|
|
got, err := store.LoadEventsForStore(ctx, "test-platform", "no-such-store-"+uuid.NewString())
|
|
require.NoError(t, err, "LoadEventsForStore()")
|
|
assert.Empty(t, got, "LoadEventsForStore()")
|
|
}
|