Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unit Tests] - GetChat #2238

Closed
tomsmith8 opened this issue Dec 19, 2024 · 3 comments
Closed

[Unit Tests] - GetChat #2238

tomsmith8 opened this issue Dec 19, 2024 · 3 comments

Comments

@tomsmith8
Copy link

Unit Test Coverage for "GetChat"


Stakwork Run


Unit Test Code


File: /tmp/stakwork/sphinx-tribes/handlers/chat.go


package chat

import (
  "encoding/json"
  "fmt"
  "net/http"
  "net/http/httptest"
  "testing"

  "github.com/stretchr/testify/assert"
  "github.com/stretchr/testify/mock"
)

// Mock database for testing
type MockDB struct {
  mock.Mock
}

func (mdb *MockDB) GetChatsForWorkspace(workspaceID string) ([]Chat, error) {
  args := mdb.Called(workspaceID)
  return args.Get(0).([]Chat), args.Error(1)
}

// ChatHandler with a mock database
type ChatHandler struct {
  db *MockDB
}

// ChatResponse represents the response structure
type ChatResponse struct {
  Success bool        `json:"success"`
  Message string      `json:"message,omitempty"`
  Data    interface{} `json:"data,omitempty"`
}

// Chat represents a chat structure
type Chat struct {
  ID   string `json:"id"`
  Name string `json:"name"`
}

func TestGetChat(t *testing.T) {
  mockDB := new(MockDB)
  handler := &ChatHandler{db: mockDB}

  tests := []struct {
  	name           string
  	workspaceID    string
  	mockChats      []Chat
  	mockError      error
  	expectedStatus int
  	expectedBody   ChatResponse
  }{
  	{
  		name:           "Valid Workspace ID",
  		workspaceID:    "123",
  		mockChats:      []Chat{{ID: "1", Name: "Chat1"}, {ID: "2", Name: "Chat2"}},
  		mockError:      nil,
  		expectedStatus: http.StatusOK,
  		expectedBody:   ChatResponse{Success: true, Data: []Chat{{ID: "1", Name: "Chat1"}, {ID: "2", Name: "Chat2"}}},
  	},
  	{
  		name:           "Empty Workspace ID",
  		workspaceID:    "",
  		expectedStatus: http.StatusBadRequest,
  		expectedBody:   ChatResponse{Success: false, Message: "workspace_id query parameter is required"},
  	},
  	{
  		name:           "Non-Existent Workspace ID",
  		workspaceID:    "999",
  		mockChats:      []Chat{},
  		mockError:      nil,
  		expectedStatus: http.StatusOK,
  		expectedBody:   ChatResponse{Success: true, Data: []Chat{}},
  	},
  	{
  		name:           "Database Error",
  		workspaceID:    "123",
  		mockError:      fmt.Errorf("database error"),
  		expectedStatus: http.StatusInternalServerError,
  		expectedBody:   ChatResponse{Success: false, Message: "Failed to fetch chats: database error"},
  	},
  	{
  		name:           "Invalid Workspace ID Format",
  		workspaceID:    "abc!@#",
  		expectedStatus: http.StatusBadRequest,
  		expectedBody:   ChatResponse{Success: false, Message: "Invalid workspace_id format"},
  	},
  	{
  		name:           "Large Number of Chats",
  		workspaceID:    "123",
  		mockChats:      make([]Chat, 1000), // Simulate a large number of chats
  		mockError:      nil,
  		expectedStatus: http.StatusOK,
  		expectedBody:   ChatResponse{Success: true, Data: make([]Chat, 1000)},
  	},
  	{
  		name:           "Multiple Workspace IDs",
  		workspaceID:    "123&workspace_id=456",
  		expectedStatus: http.StatusBadRequest,
  		expectedBody:   ChatResponse{Success: false, Message: "Multiple workspace_id parameters are not allowed"},
  	},
  	{
  		name:           "Case Sensitivity",
  		workspaceID:    "ABC",
  		mockChats:      []Chat{},
  		mockError:      nil,
  		expectedStatus: http.StatusOK,
  		expectedBody:   ChatResponse{Success: true, Data: []Chat{}},
  	},
  	{
  		name:           "Missing Query Parameter",
  		workspaceID:    "",
  		expectedStatus: http.StatusBadRequest,
  		expectedBody:   ChatResponse{Success: false, Message: "workspace_id query parameter is required"},
  	},
  	{
  		name:           "Whitespace in Workspace ID",
  		workspaceID:    " 123 ",
  		expectedStatus: http.StatusBadRequest,
  		expectedBody:   ChatResponse{Success: false, Message: "Invalid workspace_id format"},
  	},
  }

  for _, tt := range tests {
  	t.Run(tt.name, func(t *testing.T) {
  		// Setup mock expectations
  		if tt.workspaceID != "" && tt.workspaceID != "abc!@#" && tt.workspaceID != " 123 " {
  			mockDB.On("GetChatsForWorkspace", tt.workspaceID).Return(tt.mockChats, tt.mockError)
  		}

  		// Create a new HTTP request
  		req, err := http.NewRequest("GET", fmt.Sprintf("/chat?workspace_id=%s", tt.workspaceID), nil)
  		assert.NoError(t, err)

  		// Create a ResponseRecorder to capture the response
  		rr := httptest.NewRecorder()

  		// Call the GetChat handler
  		handler.GetChat(rr, req)

  		// Check the status code
  		assert.Equal(t, tt.expectedStatus, rr.Code)

  		// Check the response body
  		var responseBody ChatResponse
  		err = json.NewDecoder(rr.Body).Decode(&responseBody)
  		assert.NoError(t, err)
  		assert.Equal(t, tt.expectedBody, responseBody)

  		// Assert mock expectations
  		mockDB.AssertExpectations(t)
  	})
  }
}
@aliraza556
Copy link
Contributor

@tomsmith8 assign me?

@MahtabBukhari
Copy link
Contributor

@tomsmith8 assign

@AhsanFarooqDev
Copy link
Contributor

@tomsmith8 can I help

@tomsmith8 tomsmith8 closed this as not planned Won't fix, can't repro, duplicate, stale Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants