42 lines
1018 B
Go
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)
|
|
}
|
|
}
|