- dev auth flow (side-step OAuth) - db event processing integration tests - dev scripts (eg Makefile) - db / test db migration setup scripts.
84 lines
2.3 KiB
Go
84 lines
2.3 KiB
Go
package raw_events_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"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}`),
|
|
}
|
|
|
|
if err := store.Save(ctx, &older); err != nil {
|
|
t.Fatalf("Save() older event error = %v", err)
|
|
}
|
|
if err := store.Save(ctx, &newer); err != nil {
|
|
t.Fatalf("Save() newer event error = %v", err)
|
|
}
|
|
|
|
got, err := store.LoadEventsForStore(ctx, platform, storeID)
|
|
if err != nil {
|
|
t.Fatalf("LoadEventsForStore() error = %v", err)
|
|
}
|
|
|
|
if len(got) != 2 {
|
|
t.Fatalf("LoadEventsForStore() returned %d events, want 2: %+v", len(got), got)
|
|
}
|
|
|
|
// ordered event_timestamp DESC - newest first.
|
|
if got[0].EventID != "evt-2" || got[1].EventID != "evt-1" {
|
|
t.Errorf("LoadEventsForStore() order = [%s, %s], want [evt-2, evt-1]", got[0].EventID, got[1].EventID)
|
|
}
|
|
var payload struct{ N int }
|
|
if err := json.Unmarshal(got[0].Payload, &payload); err != nil {
|
|
t.Fatalf("failed to unmarshal LoadEventsForStore()[0].Payload = %s: %v", got[0].Payload, err)
|
|
}
|
|
if payload.N != 2 {
|
|
t.Errorf("LoadEventsForStore()[0].Payload n = %d, want 2", 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())
|
|
if err != nil {
|
|
t.Fatalf("LoadEventsForStore() error = %v", err)
|
|
}
|
|
if len(got) != 0 {
|
|
t.Fatalf("LoadEventsForStore() = %+v, want empty", got)
|
|
}
|
|
}
|