Files
pulse-api/internal/service/habit_test.go
Cosmo f3cdad1b80
Some checks failed
CI / lint-test (push) Failing after 1s
test: expand handler tests to 53.4% coverage
2026-03-26 19:21:30 +00:00

42 lines
1018 B
Go

package service
import (
"database/sql"
"testing"
"time"
"github.com/daniil/homelab-api/internal/model"
)
func TestNewHabitService(t *testing.T) {
svc := NewHabitService(nil, nil)
if svc == nil {
t.Fatal("expected non-nil HabitService")
}
}
func TestErrFutureDate(t *testing.T) {
if ErrFutureDate.Error() != "cannot log habit for future date" {
t.Errorf("unexpected: %s", ErrFutureDate.Error())
}
}
func TestErrAlreadyLogged(t *testing.T) {
if ErrAlreadyLogged.Error() != "habit already logged for this date" {
t.Errorf("unexpected: %s", ErrAlreadyLogged.Error())
}
}
// When totalLogs==0 the function returns immediately without any DB access.
func TestCalculateCompletionPctWithFreezes_ZeroLogs(t *testing.T) {
svc := &HabitService{}
habit := &model.Habit{
Frequency: "daily",
StartDate: sql.NullTime{Time: time.Now().AddDate(0, 0, -30), Valid: true},
}
pct := svc.calculateCompletionPctWithFreezes(habit, 0)
if pct != 0 {
t.Errorf("expected 0%% for zero logs, got %.2f", pct)
}
}