59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
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)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|