Add get tasks endpoint (#29)

Closes #20

Reviewed-on: #29
This commit was merged in pull request #29.
This commit is contained in:
2025-05-13 10:36:24 +02:00
parent ec635ba8ff
commit 2c133e0d85
3 changed files with 101 additions and 12 deletions

View File

@@ -1,10 +1,10 @@
package main
import (
"fmt"
"github.com/gavv/httpexpect/v2"
"strconv"
"testing"
"github.com/gavv/httpexpect/v2"
)
const (
@@ -14,12 +14,12 @@ const (
func startServer(t *testing.T) *httpexpect.Expect {
config := ServerConfig{
Datasource: ":memory:",
Port: "8181",
Addr: ":0",
Started: make(chan bool),
}
go start(t.Context(), &config)
<-config.Started
return httpexpect.Default(t, "http://localhost:8181/api")
return httpexpect.Default(t, fmt.Sprintf("http://localhost:%d/api", config.Port))
}
func TestGetUsers(t *testing.T) {
@@ -53,6 +53,29 @@ func TestGetUserGoalsWhenNoGoalsPresent(t *testing.T) {
result.Length().IsEqual(0)
}
func TestGetUserGoals(t *testing.T) {
e := startServer(t)
// Create a new goal
requestBody := map[string]interface{}{
"name": TestGoalName,
"target": 5000,
"weight": 10,
}
e.POST("/user/1/goals").WithJSON(requestBody).Expect().Status(201)
// Validate goal
result := e.GET("/user/1/goals").Expect().Status(200).JSON().Array()
result.Length().IsEqual(1)
item := result.Value(0).Object()
item.Value("id").IsEqual(1)
item.Value("name").IsEqual(TestGoalName)
item.Value("target").IsEqual(5000)
item.Value("weight").IsEqual(10)
item.Value("progress").IsEqual(0)
item.NotContainsKey("user_id")
}
func TestGetUserGoalsNoUser(t *testing.T) {
e := startServer(t)
e.GET("/user/999/goals").Expect().Status(404)
@@ -282,3 +305,30 @@ func TestCreateTaskInvalidRequestBody(t *testing.T) {
Expect().
Status(400)
}
func TestGetTaskWhenNoTasks(t *testing.T) {
e := startServer(t)
result := e.GET("/tasks").Expect().Status(200).JSON().Array()
result.Length().IsEqual(0)
}
func TestGetTaskWhenTasks(t *testing.T) {
e := startServer(t)
// Create a new task
requestBody := map[string]interface{}{
"name": "Test Task",
"reward": 100,
}
e.POST("/tasks").WithJSON(requestBody).Expect().Status(201)
// Get the task
result := e.GET("/tasks").Expect().Status(200).JSON().Array()
result.Length().IsEqual(1)
item := result.Value(0).Object()
item.Value("id").IsEqual(1)
item.Value("name").IsEqual("Test Task")
item.Value("reward").IsEqual(100)
item.Value("assigned").IsNull()
}