Skip to content

Commit

Permalink
Unit Test GetChat
Browse files Browse the repository at this point in the history
  • Loading branch information
aliraza556 committed Dec 19, 2024
1 parent efdcbc9 commit 3b1f032
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
4 changes: 4 additions & 0 deletions db/test_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ func InitTestDB() {
func CleanDB() {
TestDB.db.Exec("DELETE FROM people")
}

func DeleteAllChats() {
TestDB.db.Exec("DELETE FROM chats")
}
166 changes: 166 additions & 0 deletions handlers/chat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/go-chi/chi"
Expand Down Expand Up @@ -149,3 +151,167 @@ func TestUpdateChat(t *testing.T) {
assert.Equal(t, "Chat not found", response.Message)
})
}

func TestGetChat(t *testing.T) {
teardownSuite := SetupSuite(t)
defer teardownSuite(t)

chatHandler := NewChatHandler(&http.Client{}, db.TestDB)

t.Run("should successfully get chats when valid workspace_id is provided", func(t *testing.T) {

db.DeleteAllChats()

chats := []*db.Chat{
{
ID: uuid.New().String(),
WorkspaceID: "workspace1",
Title: "Chat 1",
},
{
ID: uuid.New().String(),
WorkspaceID: "workspace1",
Title: "Chat 2",
},
}
for _, chat := range chats {
db.TestDB.AddChat(chat)
}

rr := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/hivechat?workspace_id=workspace1", nil)
assert.NoError(t, err)

handler := http.HandlerFunc(chatHandler.GetChat)
handler.ServeHTTP(rr, req)

var response ChatResponse
err = json.NewDecoder(rr.Body).Decode(&response)
assert.NoError(t, err)

assert.Equal(t, http.StatusOK, rr.Code)
assert.True(t, response.Success)

responseChats, ok := response.Data.([]interface{})
assert.True(t, ok)
assert.Equal(t, 2, len(responseChats))

firstChat := responseChats[0].(map[string]interface{})
assert.NotEmpty(t, firstChat["id"])
assert.Equal(t, "workspace1", firstChat["workspaceId"])
assert.Contains(t, []string{"Chat 1", "Chat 2"}, firstChat["title"])
})

t.Run("should return empty array when no chats exist for workspace", func(t *testing.T) {
rr := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/hivechat?workspace_id=nonexistent", nil)
assert.NoError(t, err)

handler := http.HandlerFunc(chatHandler.GetChat)
handler.ServeHTTP(rr, req)

var response ChatResponse
err = json.NewDecoder(rr.Body).Decode(&response)
assert.NoError(t, err)

assert.Equal(t, http.StatusOK, rr.Code)
assert.True(t, response.Success)

responseChats, ok := response.Data.([]interface{})
assert.True(t, ok)
assert.Empty(t, responseChats)
})

t.Run("should return bad request when workspace_id is missing", func(t *testing.T) {
rr := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/hivechat", nil)
assert.NoError(t, err)

handler := http.HandlerFunc(chatHandler.GetChat)
handler.ServeHTTP(rr, req)

var response ChatResponse
err = json.NewDecoder(rr.Body).Decode(&response)
assert.NoError(t, err)

assert.Equal(t, http.StatusBadRequest, rr.Code)
assert.False(t, response.Success)
assert.Equal(t, "workspace_id query parameter is required", response.Message)
})

t.Run("should return bad request when workspace_id is empty", func(t *testing.T) {
rr := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, "/hivechat?workspace_id=", nil)
assert.NoError(t, err)

handler := http.HandlerFunc(chatHandler.GetChat)
handler.ServeHTTP(rr, req)

var response ChatResponse
err = json.NewDecoder(rr.Body).Decode(&response)
assert.NoError(t, err)

assert.Equal(t, http.StatusBadRequest, rr.Code)
assert.False(t, response.Success)
assert.Equal(t, "workspace_id query parameter is required", response.Message)
})

t.Run("should handle special characters in workspace_id", func(t *testing.T) {
workspaceID := "workspace-123-special"
chat := &db.Chat{
ID: uuid.New().String(),
WorkspaceID: workspaceID,
Title: "Special Chat",
}
db.TestDB.AddChat(chat)

rr := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/hivechat?workspace_id=%s", url.QueryEscape(workspaceID)), nil)
assert.NoError(t, err)

handler := http.HandlerFunc(chatHandler.GetChat)
handler.ServeHTTP(rr, req)

var response ChatResponse
err = json.NewDecoder(rr.Body).Decode(&response)
assert.NoError(t, err)

assert.Equal(t, http.StatusOK, rr.Code)
assert.True(t, response.Success)

responseChats, ok := response.Data.([]interface{})
assert.True(t, ok)
assert.Equal(t, 1, len(responseChats))
})

t.Run("should handle large number of chats", func(t *testing.T) {
workspaceID := "workspace-large"

for i := 0; i < 100; i++ {
chat := &db.Chat{
ID: uuid.New().String(),
WorkspaceID: workspaceID,
Title: fmt.Sprintf("Chat %d", i),
}
db.TestDB.AddChat(chat)
}

rr := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("/hivechat?workspace_id=%s", workspaceID), nil)
assert.NoError(t, err)

handler := http.HandlerFunc(chatHandler.GetChat)
handler.ServeHTTP(rr, req)

var response ChatResponse
err = json.NewDecoder(rr.Body).Decode(&response)
assert.NoError(t, err)

assert.Equal(t, http.StatusOK, rr.Code)
assert.True(t, response.Success)

responseChats, ok := response.Data.([]interface{})
assert.True(t, ok)
assert.Equal(t, 100, len(responseChats))
})
}

0 comments on commit 3b1f032

Please sign in to comment.