Files
pulse-api/internal/handler/savings_test.go
2026-03-26 19:03:22 +00:00

98 lines
2.8 KiB
Go

package handler
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
)
func TestSavingsHandler_CreateCategory_InvalidBody(t *testing.T) {
req := httptest.NewRequest("POST", "/savings/categories", bytes.NewBufferString("not json"))
rr := httptest.NewRecorder()
h := &SavingsHandler{repo: nil}
h.CreateCategory(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestSavingsHandler_CreateCategory_EmptyName(t *testing.T) {
body := `{"name":""}`
req := httptest.NewRequest("POST", "/savings/categories", bytes.NewBufferString(body))
rr := httptest.NewRecorder()
h := &SavingsHandler{repo: nil}
h.CreateCategory(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestSavingsHandler_CreateTransaction_InvalidBody(t *testing.T) {
req := httptest.NewRequest("POST", "/savings/transactions", bytes.NewBufferString("bad"))
rr := httptest.NewRecorder()
h := &SavingsHandler{repo: nil}
h.CreateTransaction(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestSavingsHandler_CreateTransaction_Validation(t *testing.T) {
tests := []struct {
name string
body string
}{
{"missing category_id", `{"amount": 100, "type": "deposit", "date": "2026-01-01"}`},
{"zero amount", `{"category_id": 1, "amount": 0, "type": "deposit", "date": "2026-01-01"}`},
{"negative amount", `{"category_id": 1, "amount": -10, "type": "deposit", "date": "2026-01-01"}`},
{"invalid type", `{"category_id": 1, "amount": 100, "type": "invalid", "date": "2026-01-01"}`},
{"missing date", `{"category_id": 1, "amount": 100, "type": "deposit", "date": ""}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest("POST", "/savings/transactions", bytes.NewBufferString(tt.body))
rr := httptest.NewRecorder()
h := &SavingsHandler{repo: nil}
h.CreateTransaction(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
})
}
}
func TestSavingsHandler_UpdateCategory_InvalidBody(t *testing.T) {
req := httptest.NewRequest("PUT", "/savings/categories/1", bytes.NewBufferString("bad"))
rr := httptest.NewRecorder()
h := &SavingsHandler{repo: nil}
h.UpdateCategory(rr, req)
// No chi route params → will fail on ParseInt → 400
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}
func TestSavingsHandler_CreateRecurringPlan_InvalidBody(t *testing.T) {
req := httptest.NewRequest("POST", "/savings/categories/1/plans", bytes.NewBufferString("bad"))
rr := httptest.NewRecorder()
h := &SavingsHandler{repo: nil}
h.CreateRecurringPlan(rr, req)
if rr.Code != http.StatusBadRequest {
t.Errorf("expected 400, got %d", rr.Code)
}
}