Skip to content
Merged

Dev #27

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion internal/handlers/answers/answer.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ func (h *answerHandler) SelectCorrectAnswer(c *gin.Context) {
// @Tags Forum - Answer
// @Accept json
// @Param questionId path string true "Question ID"
// @Param answerId path string true "Answer ID"
// @Success 200 {object} dto.AnswerResponseDTO "Answer without answers"
// @Failure 400 {object} dto.ErrorResponse "Bad request"
// @Failure 403 {object} dto.ErrorResponse "If no jwt session is included"
Expand Down
7 changes: 7 additions & 0 deletions internal/handlers/answers/answer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@ func TestSelectCorrectAnswer_InvalidAnswerID(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, w.Code)
}

func TestSelectCorrectAnswer_Success(t *testing.T) {
req, _ := http.NewRequest("PUT", "/question/"+q_ids[0].Hex()+"/answer/"+a_ids[1].Hex()+"/correct", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)

assert.Equal(t, http.StatusOK, w.Code)
}

/* DELETE CORRECT ANSWER */

Expand Down
4 changes: 2 additions & 2 deletions internal/repository/forum/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func (r *forumRepository) SelectCorrectAnswer(ctx context.Context, questionId, a
}
result, err := r.collection.UpdateByID(ctx, questionId, update)
if err != nil {
return errors.NewInternalServerError("could not insert answer in database")
return errors.NewInternalServerError("could not select answer as correct in database")
}
if result.MatchedCount == 0 {
return errors.NewConflictError("question doesn't exist")
Expand All @@ -472,7 +472,7 @@ func (r *forumRepository) DeleteCorrectAnswer(ctx context.Context, questionId *p
}
result, err := r.collection.UpdateByID(ctx, questionId, update)
if err != nil {
return errors.NewInternalServerError("could not update question in database")
return errors.NewInternalServerError("could not delete answer as correct in database")
}
if result.MatchedCount == 0 {
return errors.NewConflictError("question doesn't exist")
Expand Down
120 changes: 114 additions & 6 deletions internal/repository/forum/mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ func TestAnswerVoting(t *testing.T) {

// Simulamos no existing vote (new_vote devuelve false)
mockCol.On("UpdateOne", mock.Anything, bson.M{
"_id": questionID,
"_id": questionID,
}, bson.M{
"$addToSet": bson.M{"answers.$[elem].user_upvotes": userID},
"$inc": bson.M{"answers.$[elem].votes": 1},
Expand All @@ -691,7 +691,7 @@ func TestAnswerVoting(t *testing.T) {

// Simulamos cambiar de downvote a upvote
mockCol.On("UpdateOne", mock.Anything, bson.M{
"_id": questionID,
"_id": questionID,
}, bson.M{
"$pull": bson.M{"answers.$[elem].user_downvotes": userID},
"$addToSet": bson.M{"answers.$[elem].user_upvotes": userID},
Expand All @@ -716,7 +716,7 @@ func TestAnswerVoting(t *testing.T) {

// Simulamos new vote
mockCol.On("UpdateOne", mock.Anything, bson.M{
"_id": questionID,
"_id": questionID,
}, bson.M{
"$addToSet": bson.M{"answers.$[elem].user_downvotes": userID},
"$inc": bson.M{"answers.$[elem].votes": -1},
Expand All @@ -740,7 +740,7 @@ func TestAnswerVoting(t *testing.T) {

// Simulamos no existing vote (new_vote devuelve false)
mockCol.On("UpdateOne", mock.Anything, bson.M{
"_id": questionID,
"_id": questionID,
}, bson.M{
"$addToSet": bson.M{"answers.$[elem].user_downvotes": userID},
"$inc": bson.M{"answers.$[elem].votes": -1},
Expand All @@ -752,7 +752,7 @@ func TestAnswerVoting(t *testing.T) {

// Simulamos cambiar de upvote a downvote
mockCol.On("UpdateOne", mock.Anything, bson.M{
"_id": questionID,
"_id": questionID,
}, bson.M{
"$pull": bson.M{"answers.$[elem].user_upvotes": userID},
"$addToSet": bson.M{"answers.$[elem].user_downvotes": userID},
Expand All @@ -779,7 +779,7 @@ func TestAnswerVoting(t *testing.T) {

// Simulamos removing upvote
mockCol.On("UpdateOne", mock.Anything, bson.M{
"_id": questionID,
"_id": questionID,
}, bson.M{
"$pull": bson.M{"answers.$[elem].user_upvotes": userID},
"$inc": bson.M{"answers.$[elem].votes": -1},
Expand Down Expand Up @@ -870,6 +870,114 @@ func TestAnswerVoting(t *testing.T) {
})
}

func TestAnswerSelectCorrect(t *testing.T) {
t.Run("Success", func(t *testing.T) {
mockCol := new(MockCollection)
repo := &forumRepository{collection: mockCol}
questionID := primitive.NewObjectID()
answerID := primitive.NewObjectID()
userID := primitive.Binary{Data: []byte("user123")}

mockCol.On("UpdateByID", mock.Anything, &questionID, bson.M{
"$set": bson.M{
"correct_answer": answerID,
},
}, mock.Anything).Return(
&mongo.UpdateResult{MatchedCount: 1}, nil,
)

err := repo.SelectCorrectAnswer(context.Background(), &questionID, &answerID, &userID)
assert.NoError(t, err)
mockCol.AssertExpectations(t)
})

t.Run("UpdateError", func(t *testing.T) {
mockCol := new(MockCollection)
repo := &forumRepository{collection: mockCol}
questionID := primitive.NewObjectID()
answerID := primitive.NewObjectID()
userID := primitive.Binary{Data: []byte("user123")}

mockCol.On("UpdateByID", mock.Anything, &questionID, mock.Anything, mock.Anything).Return(
&mongo.UpdateResult{}, errors.New("update error"),
)

err := repo.SelectCorrectAnswer(context.Background(), &questionID, &answerID, &userID)
assert.Error(t, err)
assert.Equal(t, "could not select answer as correct in database", err.Error())
mockCol.AssertExpectations(t)
})

t.Run("QuestionNotFound", func(t *testing.T) {
mockCol := new(MockCollection)
repo := &forumRepository{collection: mockCol}
questionID := primitive.NewObjectID()
answerID := primitive.NewObjectID()
userID := primitive.Binary{Data: []byte("user123")}

mockCol.On("UpdateByID", mock.Anything, &questionID, mock.Anything, mock.Anything).Return(
&mongo.UpdateResult{MatchedCount: 0}, nil,
)

err := repo.SelectCorrectAnswer(context.Background(), &questionID, &answerID, &userID)
assert.Error(t, err)
assert.Equal(t, "question doesn't exist", err.Error())
mockCol.AssertExpectations(t)
})
}

func TestAnswerDeleteCorrect(t *testing.T) {
t.Run("Success", func(t *testing.T) {
mockCol := new(MockCollection)
repo := &forumRepository{collection: mockCol}
questionID := primitive.NewObjectID()
userID := primitive.Binary{Data: []byte("user123")}

mockCol.On("UpdateByID", mock.Anything, &questionID, bson.M{
"$unset": bson.M{
"correct_answer": "",
},
}, mock.Anything).Return(
&mongo.UpdateResult{MatchedCount: 1}, nil,
)

err := repo.DeleteCorrectAnswer(context.Background(), &questionID, &userID)
assert.NoError(t, err)
mockCol.AssertExpectations(t)
})

t.Run("UpdateError", func(t *testing.T) {
mockCol := new(MockCollection)
repo := &forumRepository{collection: mockCol}
questionID := primitive.NewObjectID()
userID := primitive.Binary{Data: []byte("user123")}

mockCol.On("UpdateByID", mock.Anything, &questionID, mock.Anything, mock.Anything).Return(
&mongo.UpdateResult{}, errors.New("update error"),
)

err := repo.DeleteCorrectAnswer(context.Background(), &questionID, &userID)
assert.Error(t, err)
assert.Equal(t, "could not delete answer as correct in database", err.Error())
mockCol.AssertExpectations(t)
})

t.Run("QuestionNotFound", func(t *testing.T) {
mockCol := new(MockCollection)
repo := &forumRepository{collection: mockCol}
questionID := primitive.NewObjectID()
userID := primitive.Binary{Data: []byte("user123")}

mockCol.On("UpdateByID", mock.Anything, &questionID, mock.Anything, mock.Anything).Return(
&mongo.UpdateResult{MatchedCount: 0}, nil,
)

err := repo.DeleteCorrectAnswer(context.Background(), &questionID, &userID)
assert.Error(t, err)
assert.Equal(t, "question doesn't exist", err.Error())
mockCol.AssertExpectations(t)
})
}

// MockCursor es una implementación mock de mongo.Cursor
type MockCursor struct {
Expand Down
22 changes: 11 additions & 11 deletions internal/services/answers/answer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,21 +266,21 @@ func TestAnswer_DeleteVoteAnswer(t *testing.T) {

/* ANSWER SELECT CORRECT */
// TestAnswer_SelectCorrectAnswer tests the SelectCorrectAnswer method of the AnswerService.
// func TestAnswer_SelectCorrectAnswer(t *testing.T) {
// ctx := context.Background()
func TestAnswer_SelectCorrectAnswer(t *testing.T) {
ctx := context.Background()

// // Tomamos una de las preguntas creadas en TestMain
// q_id := q_ids[0]
// Tomamos una de las preguntas creadas en TestMain
q_id := q_ids[2]

// // Tomamos el Binary del primer usuario creado en TestMain
// userBinary := binaryUUIDs[0]
// Tomamos el Binary del primer usuario creado en TestMain
userBinary := binaryUUIDs[2]

// // Tomamos una de las respuestas hechas por este usuario en TestMain
// a_id := a_ids[0]
// Tomamos una de las respuestas hechas por este usuario en TestMain
a_id := a_ids[5]

// err := answerService.SelectCorrectAnswer(ctx, &q_id, &a_id, &userBinary)
// assert.NoError(t, err)
// }
err := answerService.SelectCorrectAnswer(ctx, &q_id, &a_id, &userBinary)
assert.NoError(t, err)
}

func TestAnswer_SelectCorrectAnswer_NotFound(t *testing.T) {
// Tomamos una de las preguntas creadas en TestMain
Expand Down