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] - CreateChat #2271

Closed
tomsmith8 opened this issue Dec 23, 2024 · 5 comments · Fixed by #2277
Closed

[Unit Tests] - CreateChat #2271

tomsmith8 opened this issue Dec 23, 2024 · 5 comments · Fixed by #2277
Assignees
Labels

Comments

@tomsmith8
Copy link

Unit Test Coverage for "CreateChat"


Stakwork Run


Unit Test Code


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


package chat

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

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

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

func (m *MockDB) AddChat(chat *db.Chat) (*db.Chat, error) {
  args := m.Called(chat)
  return args.Get(0).(*db.Chat), args.Error(1)
}

// Test function for CreateChat
func TestCreateChat(t *testing.T) {
  mockDB := new(MockDB)
  handler := &ChatHandler{db: mockDB}

  tests := []struct {
  	name           string
  	input          string
  	mockSetup      func()
  	expectedStatus int
  	expectedBody   string
  }{
  	{
  		name:  "Valid Input",
  		input: `{"workspaceId": "workspace123", "title": "Chat Title"}`,
  		mockSetup: func() {
  			mockDB.On("AddChat", mock.Anything).Return(&db.Chat{ID: "1", WorkspaceID: "workspace123", Title: "Chat Title"}, nil)
  		},
  		expectedStatus: http.StatusOK,
  		expectedBody:   `{"Success":true,"Message":"Chat created successfully","Data":{"ID":"1","WorkspaceID":"workspace123","Title":"Chat Title"}}`,
  	},
  	{
  		name:  "Empty Title",
  		input: `{"workspaceId": "workspace123", "title": ""}`,
  		mockSetup: func() {
  			mockDB.On("AddChat", mock.Anything).Return(&db.Chat{ID: "1", WorkspaceID: "workspace123", Title: ""}, nil)
  		},
  		expectedStatus: http.StatusOK,
  		expectedBody:   `{"Success":true,"Message":"Chat created successfully","Data":{"ID":"1","WorkspaceID":"workspace123","Title":""}}`,
  	},
  	{
  		name:  "Empty WorkspaceID",
  		input: `{"workspaceId": "", "title": "Chat Title"}`,
  		mockSetup: func() {
  			mockDB.On("AddChat", mock.Anything).Return(&db.Chat{ID: "1", WorkspaceID: "", Title: "Chat Title"}, nil)
  		},
  		expectedStatus: http.StatusOK,
  		expectedBody:   `{"Success":true,"Message":"Chat created successfully","Data":{"ID":"1","WorkspaceID":"","Title":"Chat Title"}}`,
  	},
  	{
  		name:  "Invalid JSON Format",
  		input: `{"workspaceId": "workspace123", "title": "Chat Title"`,
  		mockSetup: func() {},
  		expectedStatus: http.StatusBadRequest,
  		expectedBody:   `{"Success":false,"Message":"Invalid request body"}`,
  	},
  	{
  		name:  "Missing WorkspaceID",
  		input: `{"title": "Chat Title"}`,
  		mockSetup: func() {},
  		expectedStatus: http.StatusBadRequest,
  		expectedBody:   `{"Success":false,"Message":"Invalid request body"}`,
  	},
  	{
  		name:  "Missing Title",
  		input: `{"workspaceId": "workspace123"}`,
  		mockSetup: func() {},
  		expectedStatus: http.StatusBadRequest,
  		expectedBody:   `{"Success":false,"Message":"Invalid request body"}`,
  	},
  	{
  		name:  "Database Error",
  		input: `{"workspaceId": "workspace123", "title": "Chat Title"}`,
  		mockSetup: func() {
  			mockDB.On("AddChat", mock.Anything).Return(nil, errors.New("database error"))
  		},
  		expectedStatus: http.StatusInternalServerError,
  		expectedBody:   `{"Success":false,"Message":"Failed to create chat: database error"}`,
  	},
  	{
  		name:  "Non-String WorkspaceID",
  		input: `{"workspaceId": 12345, "title": "Chat Title"}`,
  		mockSetup: func() {},
  		expectedStatus: http.StatusBadRequest,
  		expectedBody:   `{"Success":false,"Message":"Invalid request body"}`,
  	},
  	{
  		name:  "Non-String Title",
  		input: `{"workspaceId": "workspace123", "title": 12345}`,
  		mockSetup: func() {},
  		expectedStatus: http.StatusBadRequest,
  		expectedBody:   `{"Success":false,"Message":"Invalid request body"}`,
  	},
  	{
  		name:  "Whitespace Only Title",
  		input: `{"workspaceId": "workspace123", "title": "   "}`,
  		mockSetup: func() {
  			mockDB.On("AddChat", mock.Anything).Return(&db.Chat{ID: "1", WorkspaceID: "workspace123", Title: "   "}, nil)
  		},
  		expectedStatus: http.StatusOK,
  		expectedBody:   `{"Success":true,"Message":"Chat created successfully","Data":{"ID":"1","WorkspaceID":"workspace123","Title":"   "}}`,
  	},
  	{
  		name:  "Whitespace Only WorkspaceID",
  		input: `{"workspaceId": "   ", "title": "Chat Title"}`,
  		mockSetup: func() {
  			mockDB.On("AddChat", mock.Anything).Return(&db.Chat{ID: "1", WorkspaceID: "   ", Title: "Chat Title"}, nil)
  		},
  		expectedStatus: http.StatusOK,
  		expectedBody:   `{"Success":true,"Message":"Chat created successfully","Data":{"ID":"1","WorkspaceID":"   ","Title":"Chat Title"}}`,
  	},
  }

  for _, tt := range tests {
  	t.Run(tt.name, func(t *testing.T) {
  		tt.mockSetup()

  		req, err := http.NewRequest("POST", "/create-chat", bytes.NewBuffer([]byte(tt.input)))
  		assert.NoError(t, err)

  		rr := httptest.NewRecorder()
  		handler.CreateChat(rr, req)

  		assert.Equal(t, tt.expectedStatus, rr.Code)
  		assert.JSONEq(t, tt.expectedBody, rr.Body.String())

  		mockDB.AssertExpectations(t)
  	})
  }
}
@aliraza556
Copy link
Contributor

aliraza556 commented Dec 23, 2024

@tomsmith8 assign me?

@sophieturner0
Copy link
Contributor

@tomsmith8 I can help?

@MuhammadUmer44
Copy link
Contributor

@tomsmith8 Please assign me?

@MirzaHanan
Copy link
Contributor

@tomsmith8 I am available?

@MahtabBukhari
Copy link
Contributor

@tomsmith8 please assign me

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.

6 participants