We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Stakwork Run
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) }) } }
The text was updated successfully, but these errors were encountered:
@tomsmith8 assign me?
Sorry, something went wrong.
@tomsmith8 I can help?
@tomsmith8 Please assign me?
@tomsmith8 I am available?
@tomsmith8 please assign me
aliraza556
Successfully merging a pull request may close this issue.
Unit Test Coverage for "CreateChat"
Stakwork Run
Unit Test Code
File: /tmp/stakwork/sphinx-tribes/handlers/chat.go
The text was updated successfully, but these errors were encountered: