square_webhook_events is now CREATE TABLE IF NOT EXISTS, registered as the 24th maintenance job (sweep-square-webhook-events, daily 2:30am) pruning rows older than 90 days, and testdb gives a clear docker compose hint when the admin DB connection fails.
90 lines
2.5 KiB
Go
90 lines
2.5 KiB
Go
//go:build test
|
|
|
|
package jobs
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"testing"
|
|
|
|
"crussell/db"
|
|
"crussell/testutils/testdb"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
pool := testdb.CreateTestDatabase("crussell_test_jobs")
|
|
db.Conn = db.NewPoolProxy(pool)
|
|
code := m.Run()
|
|
testdb.DestroyTestDatabase(pool, "crussell_test_jobs")
|
|
os.Exit(code)
|
|
}
|
|
|
|
// ============================================================
|
|
// SweepSquareWebhookEvents Tests
|
|
// ============================================================
|
|
|
|
func TestSweepSquareWebhookEvents_DeletesOldRows(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
// Recent event (NOW() — must be preserved)
|
|
if _, err := db.Conn.Exec(ctx,
|
|
"INSERT INTO square_webhook_events (event_id, received_at) VALUES ($1, NOW())",
|
|
"evt_recent"); err != nil {
|
|
t.Fatalf("failed to insert recent webhook event: %v", err)
|
|
}
|
|
|
|
// Old event (100 days ago — must be swept)
|
|
if _, err := db.Conn.Exec(ctx,
|
|
"INSERT INTO square_webhook_events (event_id, received_at) VALUES ($1, NOW() - INTERVAL '100 days')",
|
|
"evt_old"); err != nil {
|
|
t.Fatalf("failed to insert old webhook event: %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
_, _ = db.Conn.Exec(ctx, "DELETE FROM square_webhook_events WHERE event_id IN ('evt_recent', 'evt_old')")
|
|
})
|
|
|
|
n, err := SweepSquareWebhookEvents(ctx)
|
|
if err != nil {
|
|
t.Fatalf("SweepSquareWebhookEvents failed: %v", err)
|
|
}
|
|
if n != 1 {
|
|
t.Errorf("expected 1 row deleted, got %d", n)
|
|
}
|
|
|
|
// Old row must be gone
|
|
var oldCount int
|
|
if err := db.Conn.QueryRow(ctx,
|
|
"SELECT COUNT(*) FROM square_webhook_events WHERE event_id = 'evt_old'").Scan(&oldCount); err != nil {
|
|
t.Fatalf("failed to count old event: %v", err)
|
|
}
|
|
if oldCount != 0 {
|
|
t.Errorf("expected old event to be deleted, got %d rows", oldCount)
|
|
}
|
|
|
|
// Recent row must remain
|
|
var recentCount int
|
|
if err := db.Conn.QueryRow(ctx,
|
|
"SELECT COUNT(*) FROM square_webhook_events WHERE event_id = 'evt_recent'").Scan(&recentCount); err != nil {
|
|
t.Fatalf("failed to count recent event: %v", err)
|
|
}
|
|
if recentCount != 1 {
|
|
t.Errorf("expected recent event to be preserved, got %d rows", recentCount)
|
|
}
|
|
}
|
|
|
|
func TestSweepSquareWebhookEvents_EmptyTable(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
if _, err := db.Conn.Exec(ctx, "DELETE FROM square_webhook_events"); err != nil {
|
|
t.Fatalf("failed to clear square_webhook_events: %v", err)
|
|
}
|
|
|
|
n, err := SweepSquareWebhookEvents(ctx)
|
|
if err != nil {
|
|
t.Fatalf("SweepSquareWebhookEvents failed on empty table: %v", err)
|
|
}
|
|
if n != 0 {
|
|
t.Errorf("expected 0 rows deleted on empty table, got %d", n)
|
|
}
|
|
}
|