Files
apocapoc-api/internal/application/commands/update_habit_test.go
T
david 10d45fc34e Add COUNTER type with auto-increment and IsNegative field
COUNTER type:
- Only accepts integer values (rejects decimals)
- Auto-increment behavior: each mark adds to existing value
- Supports decrement via negative values (e.g., -2)
- Enforces minimum value of 0 (never negative)
- Default increment is 1 if no value provided

VALUE type:
- Accepts decimal values
- Replaces value (no auto-increment)

IsNegative field:
- New boolean field on Habit entity
- Persisted in database (is_negative column)
- Available in all DTOs and HTTP responses
- Marks habits as "negative" (e.g., candy consumption)

Examples:
- COUNTER: 5 + (-2) = 3, 2 + (-3) = 0, 0 + (-1) = 0
- VALUE: 5000 → 12000 = 12000 (replacement)
2025-11-27 08:58:39 +01:00

172 lines
4.3 KiB
Go

package commands
import (
"context"
"testing"
"apocapoc-api/internal/domain/entities"
"apocapoc-api/internal/domain/value_objects"
"apocapoc-api/internal/shared/errors"
)
type mockHabitRepoForUpdate struct {
mockHabitRepo
habitToReturn *entities.Habit
errorOnFind error
errorOnUpdate error
updatedHabit *entities.Habit
}
func (m *mockHabitRepoForUpdate) FindByID(ctx context.Context, id string) (*entities.Habit, error) {
if m.errorOnFind != nil {
return nil, m.errorOnFind
}
return m.habitToReturn, nil
}
func (m *mockHabitRepoForUpdate) Update(ctx context.Context, habit *entities.Habit) error {
if m.errorOnUpdate != nil {
return m.errorOnUpdate
}
m.updatedHabit = habit
return nil
}
func TestUpdateHabitHandler_UpdatesSuccessfully(t *testing.T) {
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
habit.ID = "habit-1"
habitRepo := &mockHabitRepoForUpdate{
habitToReturn: habit,
}
handler := NewUpdateHabitHandler(habitRepo)
newTargetValue := 5.0
cmd := UpdateHabitCommand{
HabitID: "habit-1",
UserID: "user-123",
Name: "Morning Exercise",
Description: "Updated description",
CarryOver: true,
TargetValue: &newTargetValue,
SpecificDays: []int{1, 3, 5},
}
err := handler.Handle(context.Background(), cmd)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if habitRepo.updatedHabit.Name != "Morning Exercise" {
t.Errorf("Expected name to be updated to 'Morning Exercise', got %s", habitRepo.updatedHabit.Name)
}
if habitRepo.updatedHabit.Description != "Updated description" {
t.Errorf("Expected description to be updated, got %s", habitRepo.updatedHabit.Description)
}
if !habitRepo.updatedHabit.CarryOver {
t.Error("Expected CarryOver to be true")
}
if habitRepo.updatedHabit.TargetValue == nil || *habitRepo.updatedHabit.TargetValue != 5.0 {
t.Errorf("Expected target value 5.0, got %v", habitRepo.updatedHabit.TargetValue)
}
if len(habitRepo.updatedHabit.SpecificDays) != 3 {
t.Errorf("Expected 3 specific days, got %d", len(habitRepo.updatedHabit.SpecificDays))
}
}
func TestUpdateHabitHandler_ReturnsErrorWhenHabitNotFound(t *testing.T) {
habitRepo := &mockHabitRepoForUpdate{
errorOnFind: errors.ErrNotFound,
}
handler := NewUpdateHabitHandler(habitRepo)
cmd := UpdateHabitCommand{
HabitID: "non-existent",
UserID: "user-123",
Name: "Exercise",
}
err := handler.Handle(context.Background(), cmd)
if err != errors.ErrNotFound {
t.Errorf("Expected ErrNotFound, got %v", err)
}
}
func TestUpdateHabitHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
habit.ID = "habit-1"
habitRepo := &mockHabitRepoForUpdate{
habitToReturn: habit,
}
handler := NewUpdateHabitHandler(habitRepo)
cmd := UpdateHabitCommand{
HabitID: "habit-1",
UserID: "user-456", // Different user
Name: "Exercise",
}
err := handler.Handle(context.Background(), cmd)
if err != errors.ErrUnauthorized {
t.Errorf("Expected ErrUnauthorized, got %v", err)
}
}
func TestUpdateHabitHandler_CannotUpdateArchivedHabit(t *testing.T) {
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
habit.ID = "habit-1"
habit.Archive()
habitRepo := &mockHabitRepoForUpdate{
habitToReturn: habit,
}
handler := NewUpdateHabitHandler(habitRepo)
cmd := UpdateHabitCommand{
HabitID: "habit-1",
UserID: "user-123",
Name: "Updated Exercise",
}
err := handler.Handle(context.Background(), cmd)
if err != errors.ErrInvalidInput {
t.Errorf("Expected ErrInvalidInput for archived habit, got %v", err)
}
}
func TestUpdateHabitHandler_ValidatesInput(t *testing.T) {
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
habit.ID = "habit-1"
habitRepo := &mockHabitRepoForUpdate{
habitToReturn: habit,
}
handler := NewUpdateHabitHandler(habitRepo)
cmd := UpdateHabitCommand{
HabitID: "habit-1",
UserID: "user-123",
Name: "", // Empty name
}
err := handler.Handle(context.Background(), cmd)
if err != errors.ErrInvalidInput {
t.Errorf("Expected ErrInvalidInput for empty name, got %v", err)
}
}