test: expand handler tests to 53.4% coverage
Some checks failed
CI / lint-test (push) Failing after 1s
Some checks failed
CI / lint-test (push) Failing after 1s
This commit is contained in:
41
internal/service/habit_test.go
Normal file
41
internal/service/habit_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user