test: expand handler tests to 53.4% coverage
Some checks failed
CI / lint-test (push) Failing after 1s

This commit is contained in:
Cosmo
2026-03-26 19:21:30 +00:00
parent 3c8dd575c3
commit f3cdad1b80
12 changed files with 2031 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
package model
import (
"testing"
"time"
)
func TestEmailToken_Fields(t *testing.T) {
now := time.Now()
token := &EmailToken{
ID: 1,
UserID: 42,
Token: "abc123",
Type: "verification",
ExpiresAt: now.Add(24 * time.Hour),
CreatedAt: now,
}
if token.ID != 1 {
t.Errorf("expected ID 1, got %d", token.ID)
}
if token.UserID != 42 {
t.Errorf("expected UserID 42, got %d", token.UserID)
}
if token.Token != "abc123" {
t.Errorf("expected token abc123, got %s", token.Token)
}
if token.Type != "verification" {
t.Errorf("expected type verification, got %s", token.Type)
}
if token.UsedAt != nil {
t.Error("expected UsedAt nil initially")
}
}
func TestEmailToken_UsedAt(t *testing.T) {
now := time.Now()
token := &EmailToken{
UsedAt: &now,
}
if token.UsedAt == nil {
t.Error("expected non-nil UsedAt")
}
if !token.UsedAt.Equal(now) {
t.Errorf("expected %v, got %v", now, token.UsedAt)
}
}
func TestEmailToken_Types(t *testing.T) {
types := []string{"verification", "reset"}
for _, tp := range types {
token := &EmailToken{Type: tp}
if token.Type != tp {
t.Errorf("expected type %s, got %s", tp, token.Type)
}
}
}
func TestForgotPasswordRequest(t *testing.T) {
req := ForgotPasswordRequest{Email: "test@example.com"}
if req.Email != "test@example.com" {
t.Errorf("unexpected email: %s", req.Email)
}
}
func TestResetPasswordRequest(t *testing.T) {
req := ResetPasswordRequest{
Token: "reset-token",
NewPassword: "newpassword123",
}
if req.Token != "reset-token" {
t.Errorf("unexpected token: %s", req.Token)
}
if req.NewPassword != "newpassword123" {
t.Errorf("unexpected password: %s", req.NewPassword)
}
}
func TestVerifyEmailRequest(t *testing.T) {
req := VerifyEmailRequest{Token: "verify-token"}
if req.Token != "verify-token" {
t.Errorf("unexpected token: %s", req.Token)
}
}
func TestResendVerificationRequest(t *testing.T) {
req := ResendVerificationRequest{Email: "user@example.com"}
if req.Email != "user@example.com" {
t.Errorf("unexpected email: %s", req.Email)
}
}

View File

@@ -0,0 +1,58 @@
package model
import (
"database/sql"
"testing"
)
func TestFinanceCategory_ProcessForJSON(t *testing.T) {
tests := []struct {
name string
budget sql.NullFloat64
wantNil bool
wantValue float64
}{
{
name: "with valid budget",
budget: sql.NullFloat64{Float64: 5000.0, Valid: true},
wantNil: false,
wantValue: 5000.0,
},
{
name: "with null budget",
budget: sql.NullFloat64{Valid: false},
wantNil: true,
},
{
name: "with zero valid budget",
budget: sql.NullFloat64{Float64: 0, Valid: true},
wantNil: false,
wantValue: 0,
},
{
name: "with negative budget",
budget: sql.NullFloat64{Float64: -100.5, Valid: true},
wantNil: false,
wantValue: -100.5,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &FinanceCategory{Budget: tt.budget}
c.ProcessForJSON()
if tt.wantNil {
if c.BudgetVal != nil {
t.Errorf("expected nil BudgetVal, got %v", *c.BudgetVal)
}
} else {
if c.BudgetVal == nil {
t.Fatal("expected non-nil BudgetVal")
}
if *c.BudgetVal != tt.wantValue {
t.Errorf("expected %.2f, got %.2f", tt.wantValue, *c.BudgetVal)
}
}
})
}
}