Add endpoint to get allowance history (#37)

Closes #12

Reviewed-on: #37
This commit was merged in pull request #37.
This commit is contained in:
2025-05-14 15:27:29 +02:00
parent 2486bbf1ec
commit 4355e1b1b7
8 changed files with 106 additions and 39 deletions

View File

@@ -5,6 +5,7 @@ import (
"github.com/gavv/httpexpect/v2"
"strconv"
"testing"
"time"
)
const (
@@ -35,6 +36,7 @@ func TestGetUser(t *testing.T) {
result := e.GET("/user/1").Expect().Status(200).JSON().Object()
result.Value("name").IsEqual("Seeseemelk")
result.Value("id").IsEqual(1)
result.Value("allowance").IsEqual(0)
}
func TestGetUserUnknown(t *testing.T) {
@@ -402,5 +404,25 @@ func TestPostAllowanceInvalidUserId(t *testing.T) {
e.POST("/user/999/allowance").WithJSON(PostAllowance{Allowance: 100}).Expect().
Status(404)
}
func TestGetHistory(t *testing.T) {
e := startServer(t)
e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: 100}).Expect().Status(200)
e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: 20}).Expect().Status(200)
e.POST("/user/1/allowance").WithJSON(PostAllowance{Allowance: -10}).Expect().Status(200)
response := e.GET("/user/1/history").Expect().Status(200).JSON().Array()
response.Length().IsEqual(3)
response.Value(0).Object().Value("allowance").Number().IsEqual(100)
response.Value(0).Object().Value("timestamp").String().AsDateTime().InRange(getDelta(time.Now(), 2.0))
response.Value(1).Object().Value("allowance").Number().IsEqual(20)
response.Value(2).Object().Value("allowance").Number().IsEqual(-10)
}
func getDelta(base time.Time, delta float64) (time.Time, time.Time) {
start := base.Add(-time.Duration(delta) * time.Second)
end := base.Add(time.Duration(delta) * time.Second)
return start, end
}