package postgres_test import ( "context" "errors" "fmt" "os" "testing" "messenger/internal/platform/postgres" "messenger/internal/testenv" ) var db *testenv.DB func TestMain(m *testing.M) { var err error db, err = testenv.StartPostgres(context.Background()) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } code := m.Run() db.Close() os.Exit(code) } func TestMigrateIsRepeatable(t *testing.T) { ctx := context.Background() if err := postgres.Migrate(ctx, db.Pool); err != nil { t.Fatalf("second migrate: %v", err) } var version int64 if err := db.Pool.QueryRow(ctx, `SELECT max(version_id) FROM goose_db_version`).Scan(&version); err != nil { t.Fatal(err) } if version != 1 { t.Fatalf("schema version = %d, want 1", version) } for _, table := range []string{"users", "chats", "chat_members", "messages"} { var exists bool if err := db.Pool.QueryRow(ctx, `SELECT to_regclass('public.' || $1) IS NOT NULL`, table).Scan(&exists); err != nil { t.Fatal(err) } if !exists { t.Fatalf("table %s is missing", table) } } } func TestPoolAppliesLimitsAndTimeouts(t *testing.T) { if got := db.Pool.Config().MaxConns; got != 10 { t.Fatalf("MaxConns = %d, want 10", got) } ctx := context.Background() for setting, want := range map[string]string{ "statement_timeout": "5s", "idle_in_transaction_session_timeout": "10s", } { var got string if err := db.Pool.QueryRow(ctx, "SHOW "+setting).Scan(&got); err != nil { t.Fatal(err) } if got != want { t.Fatalf("%s = %q, want %q", setting, got, want) } } } func TestResetEmptiesEveryTableButKeepsSchemaVersion(t *testing.T) { ctx := context.Background() _, err := db.Pool.Exec(ctx, ` WITH u AS ( INSERT INTO users (id, username, display_name, password_hash, created_at) VALUES ('0190f1d2-0000-7000-8000-000000000001', 'reset_check', 'Reset', 'x', now()) RETURNING id ), c AS ( INSERT INTO chats (id, title, created_at) VALUES ('0190f1d2-0000-7000-8000-000000000002', 'Reset', now()) RETURNING id ), m AS ( INSERT INTO chat_members (chat_id, user_id) SELECT c.id, u.id FROM c, u ) INSERT INTO messages (id, chat_id, sender_id, text, created_at) SELECT '0190f1d2-0000-7000-8000-000000000003', c.id, u.id, 'hi', now() FROM c, u`) if err != nil { t.Fatal(err) } db.Reset(t) for _, table := range []string{"users", "chats", "chat_members", "messages"} { var n int if err := db.Pool.QueryRow(ctx, "SELECT count(*) FROM "+table).Scan(&n); err != nil { t.Fatal(err) } if n != 0 { t.Fatalf("%s has %d rows after Reset", table, n) } } var versions int if err := db.Pool.QueryRow(ctx, `SELECT count(*) FROM goose_db_version`).Scan(&versions); err != nil { t.Fatal(err) } if versions == 0 { t.Fatal("Reset wiped the schema version table") } } func TestIsUniqueViolation(t *testing.T) { db.Reset(t) ctx := context.Background() insert := `INSERT INTO users (id, username, display_name, password_hash, created_at) VALUES ($1, 'taken', 'Taken', 'x', now())` if _, err := db.Pool.Exec(ctx, insert, "0190f1d2-0000-7000-8000-000000000011"); err != nil { t.Fatal(err) } _, dup := db.Pool.Exec(ctx, insert, "0190f1d2-0000-7000-8000-000000000012") if dup == nil { t.Fatal("duplicate username was accepted") } if !postgres.IsUniqueViolation(dup, "users_username_key") { t.Fatalf("not recognised as a username violation: %v", dup) } if postgres.IsUniqueViolation(dup, "some_other_key") { t.Fatal("matched a different constraint") } if postgres.IsUniqueViolation(fmt.Errorf("wrapped: %w", dup), "users_username_key") == false { t.Fatal("wrapped error not recognised") } if postgres.IsUniqueViolation(errors.New("plain"), "users_username_key") || postgres.IsUniqueViolation(nil, "users_username_key") { t.Fatal("non-database error recognised as a violation") } }