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] - GetChatHistory #2272

Closed
tomsmith8 opened this issue Dec 23, 2024 · 4 comments · Fixed by #2286
Closed

[Unit Tests] - GetChatHistory #2272

tomsmith8 opened this issue Dec 23, 2024 · 4 comments · Fixed by #2286
Assignees
Labels

Comments

@tomsmith8
Copy link

Unit Test Coverage for "GetChatHistory"


Stakwork Run


Unit Test Code


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


package chat

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

  "github.com/go-chi/chi/v5"
  "github.com/stretchr/testify/assert"
  "github.com/stretchr/testify/mock"
)

// Mocking the database interface
type MockDB struct {
  mock.Mock
}

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

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

// ChatResponse and HistoryChatResponse structs
type ChatResponse struct {
  Success bool   `json:"success"`
  Message string `json:"message"`
}

type HistoryChatResponse struct {
  Success bool     `json:"success"`
  Data    []string `json:"data"`
}

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

  tests := []struct {
  	name           string
  	chatID         string
  	mockMessages   []string
  	mockError      error
  	expectedStatus int
  	expectedBody   interface{}
  }{
  	{
  		name:           "Valid Chat ID with Messages",
  		chatID:         "valid-chat-id",
  		mockMessages:   []string{"message1", "message2"},
  		mockError:      nil,
  		expectedStatus: http.StatusOK,
  		expectedBody: HistoryChatResponse{
  			Success: true,
  			Data:    []string{"message1", "message2"},
  		},
  	},
  	{
  		name:           "Valid Chat ID with No Messages",
  		chatID:         "valid-chat-id-no-messages",
  		mockMessages:   []string{},
  		mockError:      nil,
  		expectedStatus: http.StatusOK,
  		expectedBody: HistoryChatResponse{
  			Success: true,
  			Data:    []string{},
  		},
  	},
  	{
  		name:           "Empty Chat ID",
  		chatID:         "",
  		expectedStatus: http.StatusBadRequest,
  		expectedBody: ChatResponse{
  			Success: false,
  			Message: "Chat ID is required",
  		},
  	},
  	{
  		name:           "Non-Existent Chat ID",
  		chatID:         "non-existent-chat-id",
  		mockMessages:   []string{},
  		mockError:      nil,
  		expectedStatus: http.StatusOK,
  		expectedBody: HistoryChatResponse{
  			Success: true,
  			Data:    []string{},
  		},
  	},
  	{
  		name:           "Database Error",
  		chatID:         "valid-chat-id",
  		mockMessages:   nil,
  		mockError:      errors.New("database error"),
  		expectedStatus: http.StatusInternalServerError,
  		expectedBody: ChatResponse{
  			Success: false,
  			Message: "Failed to fetch chat history: database error",
  		},
  	},
  	{
  		name:           "Large Number of Messages",
  		chatID:         "large-message-chat-id",
  		mockMessages:   make([]string, 1000), // Simulating large data set
  		mockError:      nil,
  		expectedStatus: http.StatusOK,
  		expectedBody: HistoryChatResponse{
  			Success: true,
  			Data:    make([]string, 1000),
  		},
  	},
  	{
  		name:           "Chat ID with Special Characters",
  		chatID:         "special-!@#-chat-id",
  		mockMessages:   []string{"specialMessage"},
  		mockError:      nil,
  		expectedStatus: http.StatusOK,
  		expectedBody: HistoryChatResponse{
  			Success: true,
  			Data:    []string{"specialMessage"},
  		},
  	},
  	{
  		name:           "Chat ID with Maximum Length",
  		chatID:         string(make([]byte, 255)),
  		mockMessages:   []string{"maxLengthMessage"},
  		mockError:      nil,
  		expectedStatus: http.StatusOK,
  		expectedBody: HistoryChatResponse{
  			Success: true,
  			Data:    []string{"maxLengthMessage"},
  		},
  	},
  	{
  		name:           "Malformed Chat ID",
  		chatID:         "malformed-chat-id-###",
  		mockMessages:   []string{},
  		mockError:      nil,
  		expectedStatus: http.StatusOK,
  		expectedBody: HistoryChatResponse{
  			Success: true,
  			Data:    []string{},
  		},
  	},
  }

  for _, tt := range tests {
  	t.Run(tt.name, func(t *testing.T) {
  		if tt.chatID != "" {
  			mockDB.On("GetChatMessagesForChatID", tt.chatID).Return(tt.mockMessages, tt.mockError)
  		}

  		req := httptest.NewRequest("GET", fmt.Sprintf("/history/%s", tt.chatID), nil)
  		rr := httptest.NewRecorder()

  		r := chi.NewRouter()
  		r.Get("/history/{uuid}", handler.GetChatHistory)
  		r.ServeHTTP(rr, req)

  		assert.Equal(t, tt.expectedStatus, rr.Code)

  		var actualBody interface{}
  		if tt.expectedStatus == http.StatusOK {
  			actualBody = &HistoryChatResponse{}
  		} else {
  			actualBody = &ChatResponse{}
  		}
  		json.NewDecoder(rr.Body).Decode(actualBody)

  		assert.Equal(t, tt.expectedBody, actualBody)
  	})
  }
}
@MirzaHanan
Copy link
Contributor

@tomsmith8 assign me?

@sophieturner0
Copy link
Contributor

@tomsmith8 I can help?

@MuhammadUmer44
Copy link
Contributor

@tomsmith8 Please assign me?

@MahtabBukhari
Copy link
Contributor

@tomsmith8 please assign

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants