package expense import ( "slices" "strings" "testing" ) func TestNew(t *testing.T) { tests := []struct { name string date string category string note string cents int64 wantErr bool }{ {"good", "2026-11-03", "food", "lunch", 1250, false}, {"no note", "2026-11-03", "food", "", 1250, false}, {"bad date", "2026-11-31", "food", "", 1250, true}, {"date in another layout", "03/11/2026", "food", "", 1250, true}, {"empty category", "2026-11-03", " ", "", 1250, true}, {"long category", "2026-11-03", strings.Repeat("x", 31), "", 1250, true}, {"long note", "2026-11-03", "food", strings.Repeat("x", 101), 1250, true}, {"zero", "2026-11-03", "food", "", 0, true}, {"negative", "2026-11-03", "food", "", -5, true}, {"too much", "2026-11-03", "food", "", MaxCents + 1, true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { _, err := New(tt.date, tt.category, tt.note, tt.cents) if tt.wantErr && err == nil { t.Errorf("New gave no error, want one") } if !tt.wantErr && err != nil { t.Errorf("New gave error %v, want none", err) } }) } } func TestNewTrimsAndLowers(t *testing.T) { got, err := New(" 2026-11-03 ", " Food ", " lunch ", 1250) if err != nil { t.Fatalf("New gave error %v", err) } want := Expense{Date: "2026-11-03", Category: "food", Note: "lunch", Cents: 1250} if got != want { t.Errorf("New gave %+v, want %+v", got, want) } } func TestParseAmount(t *testing.T) { tests := []struct { text string want int64 wantErr bool }{ {"12", 1200, false}, {"12.5", 1250, false}, {"12.50", 1250, false}, {"0.99", 99, false}, {" 3.05 ", 305, false}, {"", 0, true}, {"abc", 0, true}, {"-3", 0, true}, {"1.234", 0, true}, {"12.", 0, true}, {".5", 0, true}, {"1,50", 0, true}, {"99999999999999999999", 0, true}, } for _, tt := range tests { t.Run(tt.text, func(t *testing.T) { got, err := ParseAmount(tt.text) if tt.wantErr { if err == nil { t.Errorf("ParseAmount(%q) gave %d and no error, want an error", tt.text, got) } return } if err != nil { t.Fatalf("ParseAmount(%q) gave error %v", tt.text, err) } if got != tt.want { t.Errorf("ParseAmount(%q) = %d, want %d", tt.text, got, tt.want) } }) } } func TestFormatCents(t *testing.T) { tests := []struct { cents int64 want string }{ {1250, "12.50"}, {5, "0.05"}, {0, "0.00"}, {100000, "1000.00"}, {-250, "-2.50"}, } for _, tt := range tests { got := FormatCents(tt.cents) if got != tt.want { t.Errorf("FormatCents(%d) = %q, want %q", tt.cents, got, tt.want) } } } func TestCheckMonth(t *testing.T) { if err := CheckMonth("2026-11"); err != nil { t.Errorf("CheckMonth(2026-11) gave error %v", err) } for _, bad := range []string{"", "2026-13", "11-2026", "2026-11-03"} { if CheckMonth(bad) == nil { t.Errorf("CheckMonth(%q) gave no error", bad) } } } // sample is a small list used by the tests below. var sample = []Expense{ {ID: 1, Date: "2026-10-30", Category: "food", Cents: 800}, {ID: 2, Date: "2026-11-03", Category: "food", Cents: 1250}, {ID: 3, Date: "2026-11-03", Category: "transport", Cents: 500}, {ID: 4, Date: "2026-11-10", Category: "rent", Cents: 50000}, } func TestInMonth(t *testing.T) { got := InMonth(sample, "2026-11") ids := []int{} for _, e := range got { ids = append(ids, e.ID) } if !slices.Equal(ids, []int{2, 3, 4}) { t.Errorf("InMonth gave IDs %v, want [2 3 4]", ids) } if len(InMonth(sample, "")) != len(sample) { t.Errorf("InMonth with an empty month should return everything") } if len(InMonth(sample, "2025-11")) != 0 { t.Errorf("InMonth for a month with no expenses should return nothing") } } func TestSummarize(t *testing.T) { got := Summarize(sample) if got.Count != 4 { t.Errorf("Count = %d, want 4", got.Count) } if got.TotalCents != 52550 { t.Errorf("TotalCents = %d, want 52550", got.TotalCents) } wantCategories := []Sum{{"rent", 50000}, {"food", 2050}, {"transport", 500}} if !slices.Equal(got.ByCategory, wantCategories) { t.Errorf("ByCategory = %v, want %v", got.ByCategory, wantCategories) } wantMonths := []Sum{{"2026-10", 800}, {"2026-11", 51750}} if !slices.Equal(got.ByMonth, wantMonths) { t.Errorf("ByMonth = %v, want %v", got.ByMonth, wantMonths) } } func TestSummarizeEmpty(t *testing.T) { got := Summarize(nil) if got.Count != 0 || got.TotalCents != 0 || len(got.ByCategory) != 0 || len(got.ByMonth) != 0 { t.Errorf("Summarize(nil) = %+v, want all zero", got) } }