forked from mtusi2026/mtusi2026lab5
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
//go:build contract
|
|
|
|
package studenttests_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
func TestCreateChatRollback(t *testing.T) {
|
|
data, err := os.ReadFile("../secrets/test_database_url.txt")
|
|
if err != nil {
|
|
t.Fatalf("read secrets/test_database_url: %v", err)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
conn, err := pgx.Connect(ctx, strings.TrimSpace(string(data)))
|
|
if err != nil {
|
|
t.Fatalf("connect to database: %v", err)
|
|
}
|
|
defer conn.Close(ctx)
|
|
|
|
creator := createUser(t)
|
|
title := "rollback-" + uuid.NewString()
|
|
|
|
body, err := json.Marshal(map[string]any{
|
|
"title": title,
|
|
"member_ids": []uuid.UUID{creator.ID, uuid.New()},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("marshal body: %v", err)
|
|
}
|
|
|
|
resp := doRequest(t, http.MethodPost, "/api/v1/chats", creator.Token, body)
|
|
resp.Body.Close()
|
|
if resp.StatusCode < 400 || resp.StatusCode >= 500 {
|
|
t.Fatalf("expected 4xx, got %d", resp.StatusCode)
|
|
}
|
|
|
|
var n int
|
|
err = conn.QueryRow(ctx, "SELECT count(*) FROM chats WHERE title = $1", title).Scan(&n)
|
|
if err != nil {
|
|
t.Fatalf("count chats: %v", err)
|
|
}
|
|
if n != 0 {
|
|
t.Fatalf("expected no chat rows after failed creation, found %d", n)
|
|
}
|
|
}
|