Rename goal to allowance

This commit is contained in:
Sebastiaan de Schaetzen 2025-05-14 15:31:56 +02:00
parent f210e7b4e6
commit e2428f6e9a

View File

@ -9,7 +9,7 @@ import (
) )
const ( const (
TestGoalName = "Test Goal" TestAllowanceName = "Test Allowance"
) )
func startServer(t *testing.T) *httpexpect.Expect { func startServer(t *testing.T) *httpexpect.Expect {
@ -49,56 +49,56 @@ func TestGetUserBadId(t *testing.T) {
e.GET("/user/bad-id").Expect().Status(400) e.GET("/user/bad-id").Expect().Status(400)
} }
func TestGetUserGoalsWhenNoGoalsPresent(t *testing.T) { func TestGetUserAllowanceWhenNoAllowancePresent(t *testing.T) {
e := startServer(t) e := startServer(t)
result := e.GET("/user/1/goals").Expect().Status(200).JSON().Array() result := e.GET("/user/1/allowance").Expect().Status(200).JSON().Array()
result.Length().IsEqual(0) result.Length().IsEqual(0)
} }
func TestGetUserGoals(t *testing.T) { func TestGetUserAllowance(t *testing.T) {
e := startServer(t) e := startServer(t)
// Create a new goal // Create a new allowance
requestBody := map[string]interface{}{ requestBody := map[string]interface{}{
"name": TestGoalName, "name": TestAllowanceName,
"target": 5000, "target": 5000,
"weight": 10, "weight": 10,
} }
e.POST("/user/1/goals").WithJSON(requestBody).Expect().Status(201) e.POST("/user/1/allowance").WithJSON(requestBody).Expect().Status(201)
// Validate goal // Validate allowance
result := e.GET("/user/1/goals").Expect().Status(200).JSON().Array() result := e.GET("/user/1/allowance").Expect().Status(200).JSON().Array()
result.Length().IsEqual(1) result.Length().IsEqual(1)
item := result.Value(0).Object() item := result.Value(0).Object()
item.Value("id").IsEqual(1) item.Value("id").IsEqual(1)
item.Value("name").IsEqual(TestGoalName) item.Value("name").IsEqual(TestAllowanceName)
item.Value("target").IsEqual(5000) item.Value("target").IsEqual(5000)
item.Value("weight").IsEqual(10) item.Value("weight").IsEqual(10)
item.Value("progress").IsEqual(0) item.Value("progress").IsEqual(0)
item.NotContainsKey("user_id") item.NotContainsKey("user_id")
} }
func TestGetUserGoalsNoUser(t *testing.T) { func TestGetUserAllowanceNoUser(t *testing.T) {
e := startServer(t) e := startServer(t)
e.GET("/user/999/goals").Expect().Status(404) e.GET("/user/999/allowance").Expect().Status(404)
} }
func TestGetUserGoalsBadId(t *testing.T) { func TestGetUserAllowanceBadId(t *testing.T) {
e := startServer(t) e := startServer(t)
e.GET("/user/bad-id/goals").Expect().Status(400) e.GET("/user/bad-id/allowance").Expect().Status(400)
} }
func TestCreateUserGoal(t *testing.T) { func TestCreateUserAllowance(t *testing.T) {
e := startServer(t) e := startServer(t)
// Create a new goal // Create a new allowance
requestBody := map[string]interface{}{ requestBody := map[string]interface{}{
"name": TestGoalName, "name": TestAllowanceName,
"target": 5000, "target": 5000,
"weight": 10, "weight": 10,
} }
response := e.POST("/user/1/goals"). response := e.POST("/user/1/allowance").
WithJSON(requestBody). WithJSON(requestBody).
Expect(). Expect().
Status(201). Status(201).
@ -106,40 +106,40 @@ func TestCreateUserGoal(t *testing.T) {
// Verify the response has an ID // Verify the response has an ID
response.ContainsKey("id") response.ContainsKey("id")
goalId := response.Value("id").Number().Raw() allowanceId := response.Value("id").Number().Raw()
// Verify the goal exists in the list of goals // Verify the allowance exists in the list of allowances
goals := e.GET("/user/1/goals"). allowances := e.GET("/user/1/allowance").
Expect(). Expect().
Status(200). Status(200).
JSON().Array() JSON().Array()
goals.Length().IsEqual(1) allowances.Length().IsEqual(1)
goal := goals.Value(0).Object() allowance := allowances.Value(0).Object()
goal.Value("id").IsEqual(goalId) allowance.Value("id").IsEqual(allowanceId)
goal.Value("name").IsEqual(TestGoalName) allowance.Value("name").IsEqual(TestAllowanceName)
goal.Value("target").IsEqual(5000) allowance.Value("target").IsEqual(5000)
goal.Value("weight").IsEqual(10) allowance.Value("weight").IsEqual(10)
goal.Value("progress").IsEqual(0) allowance.Value("progress").IsEqual(0)
} }
func TestCreateUserGoalNoUser(t *testing.T) { func TestCreateUserAllowanceNoUser(t *testing.T) {
e := startServer(t) e := startServer(t)
requestBody := map[string]interface{}{ requestBody := map[string]interface{}{
"name": TestGoalName, "name": TestAllowanceName,
"target": 5000, "target": 5000,
"weight": 10, "weight": 10,
} }
e.POST("/user/999/goals"). e.POST("/user/999/allowance").
WithJSON(requestBody). WithJSON(requestBody).
Expect(). Expect().
Status(404) Status(404)
} }
func TestCreateUserGoalInvalidInput(t *testing.T) { func TestCreateUserAllowanceInvalidInput(t *testing.T) {
e := startServer(t) e := startServer(t)
// Test with empty name // Test with empty name
@ -149,7 +149,7 @@ func TestCreateUserGoalInvalidInput(t *testing.T) {
"weight": 10, "weight": 10,
} }
e.POST("/user/1/goals"). e.POST("/user/1/allowance").
WithJSON(requestBody). WithJSON(requestBody).
Expect(). Expect().
Status(400) Status(400)
@ -159,76 +159,76 @@ func TestCreateUserGoalInvalidInput(t *testing.T) {
"target": 5000, "target": 5000,
} }
e.POST("/user/1/goals"). e.POST("/user/1/allowance").
WithJSON(invalidRequest). WithJSON(invalidRequest).
Expect(). Expect().
Status(400) Status(400)
} }
func TestCreateUserGoalBadId(t *testing.T) { func TestCreateUserAllowanceBadId(t *testing.T) {
e := startServer(t) e := startServer(t)
requestBody := map[string]interface{}{ requestBody := map[string]interface{}{
"name": TestGoalName, "name": TestAllowanceName,
"target": 5000, "target": 5000,
"weight": 10, "weight": 10,
} }
e.POST("/user/bad-id/goals"). e.POST("/user/bad-id/allowance").
WithJSON(requestBody). WithJSON(requestBody).
Expect(). Expect().
Status(400) Status(400)
} }
func TestDeleteUserGoal(t *testing.T) { func TestDeleteUserAllowance(t *testing.T) {
e := startServer(t) e := startServer(t)
// Create a new goal to delete // Create a new allowance to delete
createRequest := map[string]interface{}{ createRequest := map[string]interface{}{
"name": TestGoalName, "name": TestAllowanceName,
"target": 1000, "target": 1000,
"weight": 5, "weight": 5,
} }
response := e.POST("/user/1/goals"). response := e.POST("/user/1/allowance").
WithJSON(createRequest). WithJSON(createRequest).
Expect(). Expect().
Status(201). Status(201).
JSON().Object() JSON().Object()
goalId := response.Value("id").Number().Raw() allowanceId := response.Value("id").Number().Raw()
// Delete the goal // Delete the allowance
e.DELETE("/user/1/goal/" + strconv.Itoa(int(goalId))). e.DELETE("/user/1/allowance/" + strconv.Itoa(int(allowanceId))).
Expect(). Expect().
Status(200). Status(200).
JSON().Object().Value("message").IsEqual("Goal deleted successfully") JSON().Object().Value("message").IsEqual("Allowance deleted successfully")
// Verify the goal no longer exists // Verify the allowance no longer exists
goals := e.GET("/user/1/goals"). allowances := e.GET("/user/1/allowance").
Expect(). Expect().
Status(200). Status(200).
JSON().Array() JSON().Array()
goals.Length().IsEqual(0) allowances.Length().IsEqual(0)
} }
func TestDeleteUserGoalNotFound(t *testing.T) { func TestDeleteUserAllowanceNotFound(t *testing.T) {
e := startServer(t) e := startServer(t)
// Attempt to delete a non-existent goal // Attempt to delete a non-existent allowance
e.DELETE("/user/1/goal/999"). e.DELETE("/user/1/allowance/999").
Expect(). Expect().
Status(404). Status(404).
JSON().Object().Value("error").IsEqual("Goal not found") JSON().Object().Value("error").IsEqual("Allowance not found")
} }
func TestDeleteUserGoalInvalidId(t *testing.T) { func TestDeleteUserAllowanceInvalidId(t *testing.T) {
e := startServer(t) e := startServer(t)
// Attempt to delete a goal with an invalid ID // Attempt to delete an allowance with an invalid ID
e.DELETE("/user/1/goal/invalid-id"). e.DELETE("/user/1/allowance/invalid-id").
Expect(). Expect().
Status(400). Status(400).
JSON().Object().Value("error").IsEqual("Invalid goal ID") JSON().Object().Value("error").IsEqual("Invalid allowance ID")
} }
func TestCreateTask(t *testing.T) { func TestCreateTask(t *testing.T) {