Skip to content

Commit 6dc7553

Browse files
committed
fix: improve error handling for unmarking assessments and update test assertions
1 parent c29ec15 commit 6dc7553

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

servers/assessment/assessments/actionItem/service_test.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,34 @@ func (suite *ActionItemServiceTestSuite) TestUpdateActionItem() {
8181
err := CreateActionItem(suite.suiteCtx, createRequest)
8282
assert.NoError(suite.T(), err)
8383

84-
// For update, we need to use an existing ID
85-
// Since we can't easily get the ID from create, we'll use a known ID
84+
// Get the created action item by listing items for the student in this phase
85+
actionItems, err := ListActionItemsForStudentInPhase(suite.suiteCtx, suite.testCourseParticipationID, suite.testCoursePhaseID)
86+
assert.NoError(suite.T(), err, "Should be able to list action items to find created item")
87+
assert.Greater(suite.T(), len(actionItems), 0, "Should have at least one action item")
88+
89+
// Find the action item we just created
90+
var createdActionItemID uuid.UUID
91+
for _, item := range actionItems {
92+
if item.Action == "Original action" && item.Author == "[email protected]" {
93+
createdActionItemID = item.ID
94+
break
95+
}
96+
}
97+
assert.NotEqual(suite.T(), uuid.Nil, createdActionItemID, "Should have found the created action item")
98+
99+
// Now update the action item using the actual ID
86100
updateRequest := actionItemDTO.UpdateActionItemRequest{
87-
ID: suite.testActionItemID,
101+
ID: createdActionItemID,
88102
CoursePhaseID: suite.testCoursePhaseID,
89103
CourseParticipationID: suite.testCourseParticipationID,
90104
Action: "Updated action",
91105
Author: "[email protected]",
92106
}
93107

94-
err = UpdateActionItem(suite.suiteCtx, updateRequest)
95-
assert.NoError(suite.T(), err, "Should be able to update action item")
96-
// This might fail if the ID doesn't exist, which is expected
97-
// The test verifies the function doesn't panic
108+
// Test the update operation with proper error handling
98109
assert.NotPanics(suite.T(), func() {
99-
UpdateActionItem(suite.suiteCtx, updateRequest)
110+
err := UpdateActionItem(suite.suiteCtx, updateRequest)
111+
assert.NoError(suite.T(), err, "Should be able to update existing action item")
100112
}, "Should not panic when updating action item")
101113
}
102114

@@ -107,7 +119,7 @@ func (suite *ActionItemServiceTestSuite) TestDeleteActionItem() {
107119
// This might fail if the ID doesn't exist, which is expected
108120
// The test verifies the function doesn't panic
109121
assert.NotPanics(suite.T(), func() {
110-
DeleteActionItem(suite.suiteCtx, testID)
122+
_ = DeleteActionItem(suite.suiteCtx, testID)
111123
}, "Should not panic when deleting action item")
112124
}
113125

@@ -119,7 +131,7 @@ func (suite *ActionItemServiceTestSuite) TestDeleteActionItemNonExistent() {
119131
// This is because DELETE operations are idempotent in SQL
120132
// We just verify it doesn't panic
121133
assert.NotPanics(suite.T(), func() {
122-
DeleteActionItem(suite.suiteCtx, nonExistentID)
134+
_ = DeleteActionItem(suite.suiteCtx, nonExistentID)
123135
}, "Should not panic when deleting non-existent action item")
124136
}
125137

servers/assessment/assessments/assessmentCompletion/router.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package assessmentCompletion
22

33
import (
4+
"errors"
45
"net/http"
56

67
"github.com/gin-gonic/gin"
@@ -95,7 +96,7 @@ func unmarkAssessmentAsCompleted(c *gin.Context) {
9596
}
9697
if err := UnmarkAssessmentAsCompleted(c, courseParticipationID, coursePhaseID); err != nil {
9798
// Check if the error is due to deadline being passed
98-
if err.Error() == "cannot unmark assessment as completed: deadline has passed" {
99+
if errors.Is(err, ErrDeadlinePassed) {
99100
handleError(c, http.StatusForbidden, err)
100101
} else {
101102
handleError(c, http.StatusInternalServerError, err)

servers/assessment/assessments/assessmentCompletion/service.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import (
1717
log "github.com/sirupsen/logrus"
1818
)
1919

20+
// ErrDeadlinePassed represents an error when trying to perform an action after the deadline has passed
21+
var ErrDeadlinePassed = errors.New("cannot unmark assessment as completed: deadline has passed")
22+
2023
type AssessmentCompletionService struct {
2124
queries db.Queries
2225
conn *pgxpool.Pool
@@ -138,7 +141,7 @@ func UnmarkAssessmentAsCompleted(ctx context.Context, courseParticipationID, cou
138141

139142
// If deadline exists and has passed, prevent unmarking
140143
if deadline != nil && time.Now().After(*deadline) {
141-
return errors.New("cannot unmark assessment as completed: deadline has passed")
144+
return ErrDeadlinePassed
142145
}
143146

144147
err = AssessmentCompletionServiceSingleton.queries.UnmarkAssessmentAsFinished(ctx, db.UnmarkAssessmentAsFinishedParams{

servers/assessment/coursePhaseConfig/service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (suite *CoursePhaseConfigServiceTestSuite) TestUpdateCoursePhaseDeadlineInv
8484
// We expect either an error or successful execution
8585
// The exact assertion would depend on your database schema
8686
assert.NotPanics(suite.T(), func() {
87-
UpdateCoursePhaseDeadline(suite.suiteCtx, emptyID, testDeadline)
87+
_ = UpdateCoursePhaseDeadline(suite.suiteCtx, emptyID, testDeadline)
8888
}, "Should not panic with empty UUID")
8989
}
9090

0 commit comments

Comments
 (0)