forked from mtusi2026/mtusi2026lab2
feat: persist data in PostgreSQL and add Basic/Bearer sessions in Redis
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
"uuid"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"messenger/internal/domain"
|
||||
"messenger/internal/repository/postgres"
|
||||
)
|
||||
|
||||
func newStore(t *testing.T) (*postgres.Store, *pgxpool.Pool) {
|
||||
t.Helper()
|
||||
dbURL := os.Getenv("TEST_DATABASE_URL")
|
||||
if dbURL == "" {
|
||||
t.Skip("TEST_DATABASE_URL is required")
|
||||
}
|
||||
if err := postgres.Migrate(dbURL); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pool, err := postgres.NewPool(context.Background(), dbURL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
return postgres.New(pool), pool
|
||||
}
|
||||
|
||||
func newUser(username string) domain.User {
|
||||
return domain.User{ID: uuid.New(), Username: username, DisplayName: "Test", CreatedAt: time.Now().UTC()}
|
||||
}
|
||||
|
||||
func TestRegisterUsernameRace(t *testing.T) {
|
||||
store, pool := newStore(t)
|
||||
ctx := context.Background()
|
||||
username := "race_" + uuid.New().String()[:8]
|
||||
|
||||
const workers = 20
|
||||
var wg sync.WaitGroup
|
||||
errs := make([]error, workers)
|
||||
start := make(chan struct{})
|
||||
for i := range workers {
|
||||
wg.Go(func() {
|
||||
<-start
|
||||
errs[i] = store.CreateUser(ctx, newUser(username), "hash")
|
||||
})
|
||||
}
|
||||
close(start)
|
||||
wg.Wait()
|
||||
|
||||
created := 0
|
||||
for _, err := range errs {
|
||||
switch {
|
||||
case err == nil:
|
||||
created++
|
||||
case !errors.Is(err, domain.ErrConflict):
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
if created != 1 {
|
||||
t.Errorf("created %d users, want exactly 1", created)
|
||||
}
|
||||
var rows int
|
||||
if err := pool.QueryRow(ctx, `SELECT count(*) FROM users WHERE username = $1`, username).Scan(&rows); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if rows != 1 {
|
||||
t.Errorf("users rows = %d, want 1", rows)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateChatRollback(t *testing.T) {
|
||||
store, pool := newStore(t)
|
||||
ctx := context.Background()
|
||||
alice := newUser("rb_" + uuid.New().String()[:8])
|
||||
if err := store.CreateUser(ctx, alice, "hash"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
chat := domain.Chat{
|
||||
ID: uuid.New(),
|
||||
Title: "rollback",
|
||||
MemberIDs: []uuid.UUID{alice.ID, uuid.New()},
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
if err := store.CreateChat(ctx, chat); !errors.Is(err, domain.ErrNotFound) {
|
||||
t.Fatalf("CreateChat with unknown member: got %v, want ErrNotFound", err)
|
||||
}
|
||||
|
||||
var chats, members int
|
||||
if err := pool.QueryRow(ctx, `SELECT count(*) FROM chats WHERE id = $1`, chat.ID).Scan(&chats); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := pool.QueryRow(ctx, `SELECT count(*) FROM chat_members WHERE chat_id = $1`, chat.ID).Scan(&members); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if chats != 0 || members != 0 {
|
||||
t.Errorf("after rollback: chats=%d chat_members=%d, want 0 and 0", chats, members)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user