Add delete goal endpoint (#27)

Closes #18

Reviewed-on: #27
This commit was merged in pull request #27.
This commit is contained in:
2025-05-08 15:46:23 +02:00
parent d251d41650
commit 0668228139
3 changed files with 148 additions and 19 deletions

View File

@@ -1,11 +1,16 @@
package main
import (
"strconv"
"testing"
"github.com/gavv/httpexpect/v2"
)
const (
TestGoalName = "Test Goal"
)
func startServer(t *testing.T) *httpexpect.Expect {
config := ServerConfig{
Datasource: ":memory:",
@@ -63,7 +68,7 @@ func TestCreateUserGoal(t *testing.T) {
// Create a new goal
requestBody := map[string]interface{}{
"name": "Test Goal",
"name": TestGoalName,
"target": 5000,
"weight": 10,
}
@@ -88,7 +93,7 @@ func TestCreateUserGoal(t *testing.T) {
goal := goals.Value(0).Object()
goal.Value("id").IsEqual(goalId)
goal.Value("name").IsEqual("Test Goal")
goal.Value("name").IsEqual(TestGoalName)
goal.Value("target").IsEqual(5000)
goal.Value("weight").IsEqual(10)
goal.Value("progress").IsEqual(0)
@@ -98,7 +103,7 @@ func TestCreateUserGoalNoUser(t *testing.T) {
e := startServer(t)
requestBody := map[string]interface{}{
"name": "Test Goal",
"name": TestGoalName,
"target": 5000,
"weight": 10,
}
@@ -139,7 +144,7 @@ func TestCreateUserGoalBadId(t *testing.T) {
e := startServer(t)
requestBody := map[string]interface{}{
"name": "Test Goal",
"name": TestGoalName,
"target": 5000,
"weight": 10,
}
@@ -149,3 +154,54 @@ func TestCreateUserGoalBadId(t *testing.T) {
Expect().
Status(400)
}
func TestDeleteUserGoal(t *testing.T) {
e := startServer(t)
// Create a new goal to delete
createRequest := map[string]interface{}{
"name": TestGoalName,
"target": 1000,
"weight": 5,
}
response := e.POST("/user/1/goals").
WithJSON(createRequest).
Expect().
Status(201).
JSON().Object()
goalId := response.Value("id").Number().Raw()
// Delete the goal
e.DELETE("/user/1/goal/" + strconv.Itoa(int(goalId))).
Expect().
Status(200).
JSON().Object().Value("message").IsEqual("Goal deleted successfully")
// Verify the goal no longer exists
goals := e.GET("/user/1/goals").
Expect().
Status(200).
JSON().Array()
goals.Length().IsEqual(0)
}
func TestDeleteUserGoalNotFound(t *testing.T) {
e := startServer(t)
// Attempt to delete a non-existent goal
e.DELETE("/user/1/goal/999").
Expect().
Status(404).
JSON().Object().Value("error").IsEqual("Goal not found")
}
func TestDeleteUserGoalInvalidId(t *testing.T) {
e := startServer(t)
// Attempt to delete a goal with an invalid ID
e.DELETE("/user/1/goal/invalid-id").
Expect().
Status(400).
JSON().Object().Value("error").IsEqual("Invalid goal ID")
}