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,41 @@
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)
}
}