parseFlow / internal /models /models_test.go
Soumik-404's picture
Deploy 684217f676e8b0a2126452d47c10ddc3dc7aea74
84ffb7a verified
Raw History Blame Contribute Delete
2.15 kB
package models
import (
"strings"
"testing"
"time"
)
func TestValidStatus(t *testing.T) {
for _, s := range []string{StatusToRead, StatusReading, StatusFinished} {
if !ValidStatus(s) {
t.Errorf("expected %q to be valid", s)
}
}
for _, s := range []string{"", "read", "DONE", "finished "} {
if ValidStatus(s) {
t.Errorf("expected %q to be invalid", s)
}
}
}
func TestStatsProgress(t *testing.T) {
cases := []struct {
goal, finished int
want int
}{
{10, 0, 0},
{10, 5, 50},
{10, 10, 100},
{10, 15, 100}, // over-achievement caps at 100
{0, 5, 0}, // no goal β†’ no progress
}
for _, c := range cases {
s := Stats{Goal: c.goal, FinishedYear: c.finished}
if got := s.Progress(); got != c.want {
t.Errorf("Progress(goal=%d, finished=%d) = %d, want %d", c.goal, c.finished, got, c.want)
}
}
}
func TestBookStars(t *testing.T) {
r := func(n int) *int { return &n }
cases := []struct {
rating *int
want string
}{
{nil, ""},
{r(5), "β˜…β˜…β˜…β˜…β˜…"},
{r(3), "β˜…β˜…β˜…β˜†β˜†"},
{r(1), "β˜…β˜†β˜†β˜†β˜†"},
{r(0), "β˜…β˜†β˜†β˜†β˜†"}, // clamped up
{r(9), "β˜…β˜…β˜…β˜…β˜…"}, // clamped down
}
for _, c := range cases {
b := Book{Rating: c.rating}
if got := b.Stars(); got != c.want {
t.Errorf("Stars(%v) = %q, want %q", c.rating, got, c.want)
}
}
}
func TestBookRatingValueAndText(t *testing.T) {
r := 4
b := Book{Rating: &r}
if b.RatingValue() != 4 {
t.Errorf("RatingValue() = %d, want 4", b.RatingValue())
}
if (Book{}).RatingValue() != 0 {
t.Error("unrated book must report 0")
}
review := "loved it"
b2 := Book{Review: &review}
if b2.ReviewText() != "loved it" {
t.Errorf("ReviewText() = %q", b2.ReviewText())
}
if (Book{}).ReviewText() != "" {
t.Error("nil review must be empty string")
}
}
func TestBookFinishedDate(t *testing.T) {
if (Book{}).FinishedDate() != "" {
t.Error("unfinished book must have no date")
}
when := time.Date(2026, 3, 15, 0, 0, 0, 0, time.UTC)
b := Book{FinishedAt: &when}
if !strings.Contains(b.FinishedDate(), "2026") {
t.Errorf("FinishedDate() = %q, want a 2026 date", b.FinishedDate())
}
}