Skip to content

Commit 6419d94

Browse files
authored
Merge pull request #41 from igh9410/dev
Removed Mock tests. Will replace with integration test
2 parents fc24ab5 + 4a8a008 commit 6419d94

10 files changed

+34
-308
lines changed

backend/Dockerfile.multistage

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ RUN go mod download
1212
# Copy the entire application
1313
COPY . .
1414

15+
# Run the tests
16+
RUN CGO_ENABLED=1 GOOS=linux go test -v ./...
17+
18+
# Build the application
1519
RUN CGO_ENABLED=1 GOOS=linux go build -o blabber-hive ./cmd/main.go
1620

17-
# Run the tests in the container
18-
FROM build-stage AS run-test-stage
19-
RUN go test -v ./...
2021

2122
# Deploy the application binary into a lean image
2223
FROM gcr.io/distroless/cc-debian12 AS build-release-stage

backend/internal/chat/handler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (h *Handler) GetChatRoomList(c *gin.Context) {
8686

8787
chatRoomList, err := h.Service.GetChatRoomList(c)
8888
if err != nil {
89-
slog.Error("Error occured with getting chat room list %v: %v", chatRoomList, err.Error())
89+
log.Printf("Error occured with getting chat room list: %v", err.Error())
9090
}
9191
c.JSON(http.StatusOK, chatRoomList)
9292
}

backend/internal/chat/repository.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ func (r *repository) CreateChatRoom(ctx context.Context, chatRoom *ChatRoom) (*C
6060
return nil, errors.New("failed to create chat room")
6161
}
6262

63+
// Commit the transaction on success
64+
if err = tx.Commit(); err != nil {
65+
slog.Error("Transaction commit failed: ", err)
66+
return nil, err
67+
}
68+
6369
return chatRoom, nil
6470
}
6571

@@ -119,19 +125,10 @@ func (r *repository) FindChatRoomInfoByID(ctx context.Context, chatRoomID uuid.U
119125
}
120126

121127
func (r *repository) JoinChatRoomByID(ctx context.Context, chatRoomID uuid.UUID, userID uuid.UUID) (*ChatRoom, error) {
122-
// Find the existing chat room first
123-
chatRoom, err := r.FindChatRoomByID(ctx, chatRoomID)
124-
if err != nil {
125-
return nil, err
126-
}
127-
128-
// INSERT into chat rooms, UNIQUE constraint will prevent duplicates
129-
query := `INSERT INTO users_in_chat_rooms (user_id, chat_room_id) VALUES ($1, $2)`
130-
131128
tx, err := r.db.BeginTx(ctx, nil)
132129
if err != nil {
133130
slog.Error("Joining chat room transaction failed")
134-
return nil, err // handle error appropriately
131+
return nil, err
135132
}
136133

137134
// Ensure rollback in case of error
@@ -143,7 +140,9 @@ func (r *repository) JoinChatRoomByID(ctx context.Context, chatRoomID uuid.UUID,
143140
}
144141
}()
145142

146-
_, err = r.db.ExecContext(ctx, query, userID, chatRoomID)
143+
// INSERT into chat rooms, UNIQUE and FOREIGN KEY constraints will handle duplicates and non-existing chat room
144+
query := `INSERT INTO users_in_chat_rooms (user_id, chat_room_id) VALUES ($1, $2)`
145+
_, err = tx.ExecContext(ctx, query, userID, chatRoomID)
147146
if err != nil {
148147
slog.Error("Error joining chat room, db execcontext: ", err)
149148
return nil, err
@@ -154,6 +153,9 @@ func (r *repository) JoinChatRoomByID(ctx context.Context, chatRoomID uuid.UUID,
154153
slog.Error("Transaction commit failed: ", err)
155154
return nil, err
156155
}
156+
chatRoom := &ChatRoom{
157+
ID: chatRoomID,
158+
}
157159

158160
return chatRoom, nil
159161

backend/internal/chat/repository_test.go

-40
This file was deleted.

backend/internal/match/handler.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package match
33
import (
44
"backend/internal/common"
55
"log"
6-
"log/slog"
76
"net/http"
87

98
"github.com/gin-gonic/gin"
@@ -29,7 +28,9 @@ func (h *Handler) EnqueueUser(c *gin.Context) {
2928
res, err := h.Service.EnqueueUser(c.Request.Context(), userID)
3029

3130
if err != nil {
32-
slog.Error("Error occured with enqueuing user ID %v: %v", userID, err.Error())
31+
log.Printf("Error occurred with user ID %v: %v", userID, err.Error())
32+
c.JSON((http.StatusBadRequest), gin.H{"error": err.Error()})
33+
return
3334
}
3435

3536
c.JSON(http.StatusCreated, res)
@@ -47,7 +48,7 @@ func (h *Handler) DequeueUser(c *gin.Context) {
4748

4849
err := h.Service.DequeueUser(c.Request.Context(), userID)
4950
if err != nil {
50-
slog.Error("Error occurred with dequeuing user ID %v: %v", userID, err.Error())
51+
log.Printf("Error occurred with dequeuing user ID %v: %v", userID, err.Error())
5152
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error removing user from queue"})
5253
return
5354
}

backend/internal/match/service.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"backend/infra/redis"
55
"backend/internal/chat"
66
"context"
7+
"log"
78
"log/slog"
9+
"strings"
810
"time"
911

1012
"github.com/google/uuid"
@@ -83,7 +85,6 @@ func (s *service) StartMatchmakingSubscriber(ctx context.Context) {
8385
if msg == nil {
8486
continue // or handle the nil message scenario
8587
}
86-
slog.Info("Received matchmaking request for user:", msg.Payload)
8788

8889
// Trigger matchmaking logic
8990
if err := s.performMatchmaking(msg.Payload); err != nil {
@@ -111,7 +112,7 @@ func (s *service) performMatchmaking(userID string) error {
111112
}
112113

113114
if matchID == "" {
114-
slog.Info("No suitable match found for user:", userID)
115+
slog.Error("EnqueueUser error", "userID", userID, "error", err.Error())
115116
return nil // or custom error indicating no match found
116117
}
117118

@@ -129,8 +130,8 @@ func (s *service) handleMatch(userID, matchID string) error {
129130
ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
130131
defer cancel()
131132

132-
removedIDs := []string{userID, matchID} // Remove two users from the queue to form a match
133-
slog.Info("Match found for users:", removedIDs)
133+
removedIDs := []string{userID, matchID} // Remove two users from the queue to form a match
134+
log.Printf("Match found for users: %s", strings.Join(removedIDs, ", ")) // Fix the format specifier from %d to %s
134135
err := s.Repository.DequeueUsers(ctx, removedIDs...)
135136
// Code goes here
136137
if err != nil {
@@ -141,7 +142,8 @@ func (s *service) handleMatch(userID, matchID string) error {
141142

142143
_, err = s.ChatRepository.CreateChatRoom(ctx, chatRoom)
143144
if err != nil {
144-
slog.Error("Error creating chat room:", err)
145+
log.Print("Error creating chat room:", err)
146+
return err
145147
}
146148
return nil
147149
}

backend/internal/user/handler_test.go

-77
This file was deleted.

backend/internal/user/repository.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package user
33
import (
44
"context"
55
"database/sql"
6-
"log/slog"
6+
"log"
77
"time"
88
)
99

@@ -18,8 +18,6 @@ type DBTX interface {
1818
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
1919
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
2020
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
21-
Commit() error
22-
Rollback() error
2321
}
2422

2523
type repository struct {
@@ -34,15 +32,15 @@ func (r *repository) CreateUser(ctx context.Context, user *User) (*User, error)
3432

3533
tx, err := r.db.BeginTx(ctx, nil)
3634
if err != nil {
37-
slog.Error("Creating User transaction failed")
35+
log.Println("Creating User transaction failed")
3836
return nil, err // handle error appropriately
3937
}
4038

4139
// Ensure rollback in case of error
4240
defer func() {
4341
if err != nil {
4442
if rbErr := tx.Rollback(); rbErr != nil {
45-
slog.Error("Transaction rollback failed: %v", rbErr)
43+
log.Printf("Transaction rollback failed: %v", rbErr)
4644
}
4745
}
4846
}()
@@ -55,13 +53,13 @@ func (r *repository) CreateUser(ctx context.Context, user *User) (*User, error)
5553
err = r.db.QueryRowContext(ctx, query, user.ID, user.Username, user.Email, user.ProfileImageURL, user.CreatedAt).Scan(&user.ID)
5654

5755
if err != nil {
58-
slog.Error("Error creating user, db execcontext: ", err)
56+
log.Printf("Error creating user, db execcontext: %d", err)
5957
return nil, err
6058
}
6159

6260
// Commit transaction
6361
if err = tx.Commit(); err != nil {
64-
slog.Error("Transaction commit failed: ", err)
62+
log.Printf("Transaction commit failed: %d", err)
6563
return nil, err
6664
}
6765

0 commit comments

Comments
 (0)