152 lines
3.1 KiB
Go
152 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/gavv/httpexpect/v2"
|
|
)
|
|
|
|
func startServer(t *testing.T) *httpexpect.Expect {
|
|
config := ServerConfig{
|
|
Datasource: ":memory:",
|
|
Port: "8181",
|
|
Started: make(chan bool),
|
|
}
|
|
go start(t.Context(), &config)
|
|
<-config.Started
|
|
return httpexpect.Default(t, "http://localhost:8181/api")
|
|
}
|
|
|
|
func TestGetUsers(t *testing.T) {
|
|
e := startServer(t)
|
|
result := e.GET("/users").Expect().Status(200).JSON()
|
|
result.Array().Length().IsEqual(2)
|
|
result.Path("$[0].name").InList("Seeseemelk", "Huffle")
|
|
result.Path("$[1].name").InList("Seeseemelk", "Huffle")
|
|
}
|
|
|
|
func TestGetUser(t *testing.T) {
|
|
e := startServer(t)
|
|
result := e.GET("/user/1").Expect().Status(200).JSON().Object()
|
|
result.Value("name").IsEqual("Seeseemelk")
|
|
result.Value("id").IsEqual(1)
|
|
}
|
|
|
|
func TestGetUserUnknown(t *testing.T) {
|
|
e := startServer(t)
|
|
e.GET("/user/999").Expect().Status(404)
|
|
}
|
|
|
|
func TestGetUserBadId(t *testing.T) {
|
|
e := startServer(t)
|
|
e.GET("/user/bad-id").Expect().Status(400)
|
|
}
|
|
|
|
func TestGetUserGoalsWhenNoGoalsPresent(t *testing.T) {
|
|
e := startServer(t)
|
|
result := e.GET("/user/1/goals").Expect().Status(200).JSON().Array()
|
|
result.Length().IsEqual(0)
|
|
}
|
|
|
|
func TestGetUserGoalsNoUser(t *testing.T) {
|
|
e := startServer(t)
|
|
e.GET("/user/999/goals").Expect().Status(404)
|
|
}
|
|
|
|
func TestGetUserGoalsBadId(t *testing.T) {
|
|
e := startServer(t)
|
|
e.GET("/user/bad-id/goals").Expect().Status(400)
|
|
}
|
|
|
|
func TestCreateUserGoal(t *testing.T) {
|
|
e := startServer(t)
|
|
|
|
// Create a new goal
|
|
requestBody := map[string]interface{}{
|
|
"name": "Test Goal",
|
|
"target": 5000,
|
|
"weight": 10,
|
|
}
|
|
|
|
response := e.POST("/user/1/goals").
|
|
WithJSON(requestBody).
|
|
Expect().
|
|
Status(201).
|
|
JSON().Object()
|
|
|
|
// Verify the response has an ID
|
|
response.ContainsKey("id")
|
|
goalId := response.Value("id").Number().Raw()
|
|
|
|
// Verify the goal exists in the list of goals
|
|
goals := e.GET("/user/1/goals").
|
|
Expect().
|
|
Status(200).
|
|
JSON().Array()
|
|
|
|
goals.Length().IsEqual(1)
|
|
|
|
goal := goals.Value(0).Object()
|
|
goal.Value("id").IsEqual(goalId)
|
|
goal.Value("name").IsEqual("Test Goal")
|
|
goal.Value("target").IsEqual(5000)
|
|
goal.Value("weight").IsEqual(10)
|
|
goal.Value("progress").IsEqual(0)
|
|
}
|
|
|
|
func TestCreateUserGoalNoUser(t *testing.T) {
|
|
e := startServer(t)
|
|
|
|
requestBody := map[string]interface{}{
|
|
"name": "Test Goal",
|
|
"target": 5000,
|
|
"weight": 10,
|
|
}
|
|
|
|
e.POST("/user/999/goals").
|
|
WithJSON(requestBody).
|
|
Expect().
|
|
Status(404)
|
|
}
|
|
|
|
func TestCreateUserGoalInvalidInput(t *testing.T) {
|
|
e := startServer(t)
|
|
|
|
// Test with empty name
|
|
requestBody := map[string]interface{}{
|
|
"name": "",
|
|
"target": 5000,
|
|
"weight": 10,
|
|
}
|
|
|
|
e.POST("/user/1/goals").
|
|
WithJSON(requestBody).
|
|
Expect().
|
|
Status(400)
|
|
|
|
// Test with missing fields
|
|
invalidRequest := map[string]interface{}{
|
|
"target": 5000,
|
|
}
|
|
|
|
e.POST("/user/1/goals").
|
|
WithJSON(invalidRequest).
|
|
Expect().
|
|
Status(400)
|
|
}
|
|
|
|
func TestCreateUserGoalBadId(t *testing.T) {
|
|
e := startServer(t)
|
|
|
|
requestBody := map[string]interface{}{
|
|
"name": "Test Goal",
|
|
"target": 5000,
|
|
"weight": 10,
|
|
}
|
|
|
|
e.POST("/user/bad-id/goals").
|
|
WithJSON(requestBody).
|
|
Expect().
|
|
Status(400)
|
|
}
|