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)
This commit is contained in:
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func TestArchiveHabitHandler_ArchivesSuccessfully(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForUpdate{
|
||||
@@ -59,7 +59,7 @@ func TestArchiveHabitHandler_ReturnsErrorWhenHabitNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestArchiveHabitHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForUpdate{
|
||||
@@ -81,7 +81,7 @@ func TestArchiveHabitHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestArchiveHabitHandler_CanArchiveAlreadyArchivedHabit(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.Archive()
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ type CreateHabitCommand struct {
|
||||
SpecificDays []int
|
||||
SpecificDates []int
|
||||
CarryOver bool
|
||||
IsNegative bool
|
||||
TargetValue *float64
|
||||
}
|
||||
|
||||
@@ -46,7 +47,7 @@ func (h *CreateHabitHandler) Handle(ctx context.Context, cmd CreateHabitCommand)
|
||||
return "", errors.ErrInvalidInput
|
||||
}
|
||||
|
||||
habit := entities.NewHabit(cmd.UserID, cmd.Name, cmd.Type, cmd.Frequency, cmd.CarryOver)
|
||||
habit := entities.NewHabit(cmd.UserID, cmd.Name, cmd.Type, cmd.Frequency, cmd.CarryOver, cmd.IsNegative)
|
||||
habit.Description = cmd.Description
|
||||
habit.SpecificDays = cmd.SpecificDays
|
||||
habit.SpecificDates = cmd.SpecificDates
|
||||
|
||||
@@ -195,3 +195,37 @@ func TestCreateHabitHandler_MonthlyWithSpecificDates(t *testing.T) {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateHabitHandler_NegativeHabit(t *testing.T) {
|
||||
mock := &mockHabitRepo{
|
||||
createFunc: func(ctx context.Context, habit *entities.Habit) error {
|
||||
habit.ID = "habit-123"
|
||||
if !habit.IsNegative {
|
||||
t.Error("Expected IsNegative to be true")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewCreateHabitHandler(mock)
|
||||
|
||||
cmd := CreateHabitCommand{
|
||||
UserID: "user-123",
|
||||
Name: "Eat Candy",
|
||||
Description: "Track bad habit",
|
||||
Type: "COUNTER",
|
||||
Frequency: "DAILY",
|
||||
CarryOver: false,
|
||||
IsNegative: true,
|
||||
}
|
||||
|
||||
habitID, err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
|
||||
if habitID == "" {
|
||||
t.Error("Expected habit ID to be returned")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,13 @@ package commands
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"apocapoc-api/internal/domain/entities"
|
||||
"apocapoc-api/internal/domain/repositories"
|
||||
"apocapoc-api/internal/domain/value_objects"
|
||||
"apocapoc-api/internal/shared/errors"
|
||||
)
|
||||
|
||||
type MarkHabitCommand struct {
|
||||
@@ -44,7 +47,61 @@ func (h *MarkHabitHandler) Handle(ctx context.Context, cmd MarkHabitCommand) err
|
||||
return fmt.Errorf("habit is archived")
|
||||
}
|
||||
|
||||
entry := entities.NewHabitEntry(cmd.HabitID, cmd.ScheduledDate, cmd.Value)
|
||||
if habit.Type == value_objects.HabitTypeCounter && cmd.Value != nil {
|
||||
if *cmd.Value != math.Floor(*cmd.Value) {
|
||||
return errors.ErrInvalidInput
|
||||
}
|
||||
}
|
||||
|
||||
var finalValue *float64
|
||||
|
||||
if habit.Type == value_objects.HabitTypeCounter {
|
||||
startOfDay := time.Date(cmd.ScheduledDate.Year(), cmd.ScheduledDate.Month(), cmd.ScheduledDate.Day(), 0, 0, 0, 0, cmd.ScheduledDate.Location())
|
||||
endOfDay := startOfDay.Add(24 * time.Hour).Add(-time.Nanosecond)
|
||||
|
||||
existingEntries, _ := h.entryRepo.FindByHabitIDAndDateRange(ctx, cmd.HabitID, startOfDay, endOfDay)
|
||||
|
||||
if len(existingEntries) > 0 {
|
||||
existingEntry := existingEntries[0]
|
||||
|
||||
var increment float64 = 1.0
|
||||
if cmd.Value != nil {
|
||||
increment = *cmd.Value
|
||||
}
|
||||
|
||||
var newValue float64
|
||||
if existingEntry.Value != nil {
|
||||
newValue = *existingEntry.Value + increment
|
||||
} else {
|
||||
newValue = increment
|
||||
}
|
||||
|
||||
if newValue < 0 {
|
||||
newValue = 0
|
||||
}
|
||||
|
||||
finalValue = &newValue
|
||||
existingEntry.Value = finalValue
|
||||
existingEntry.CompletedAt = time.Now()
|
||||
|
||||
return h.entryRepo.Update(ctx, existingEntry)
|
||||
} else {
|
||||
if cmd.Value != nil {
|
||||
value := *cmd.Value
|
||||
if value < 0 {
|
||||
value = 0
|
||||
}
|
||||
finalValue = &value
|
||||
} else {
|
||||
defaultValue := 1.0
|
||||
finalValue = &defaultValue
|
||||
}
|
||||
}
|
||||
} else {
|
||||
finalValue = cmd.Value
|
||||
}
|
||||
|
||||
entry := entities.NewHabitEntry(cmd.HabitID, cmd.ScheduledDate, finalValue)
|
||||
|
||||
return h.entryRepo.Create(ctx, entry)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,9 @@ import (
|
||||
)
|
||||
|
||||
type mockEntryRepo struct {
|
||||
createFunc func(ctx context.Context, entry *entities.HabitEntry) error
|
||||
createFunc func(ctx context.Context, entry *entities.HabitEntry) error
|
||||
findByDateRangeFunc func(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error)
|
||||
updateFunc func(ctx context.Context, entry *entities.HabitEntry) error
|
||||
}
|
||||
|
||||
func (m *mockEntryRepo) Create(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
@@ -30,6 +32,9 @@ func (m *mockEntryRepo) FindByHabitID(ctx context.Context, habitID string) ([]*e
|
||||
}
|
||||
|
||||
func (m *mockEntryRepo) FindByHabitIDAndDateRange(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
|
||||
if m.findByDateRangeFunc != nil {
|
||||
return m.findByDateRangeFunc(ctx, habitID, from, to)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -38,6 +43,9 @@ func (m *mockEntryRepo) FindPendingByHabitID(ctx context.Context, habitID string
|
||||
}
|
||||
|
||||
func (m *mockEntryRepo) Update(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if m.updateFunc != nil {
|
||||
return m.updateFunc(ctx, entry)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -74,7 +82,7 @@ func (m *mockHabitRepoForMark) Delete(ctx context.Context, id string) error {
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_Success(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
@@ -122,7 +130,7 @@ func TestMarkHabitHandler_HabitNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_ArchivedHabit(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.Archive()
|
||||
|
||||
@@ -144,7 +152,7 @@ func TestMarkHabitHandler_ArchivedHabit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_WithValue(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Steps", value_objects.HabitTypeValue, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Steps", value_objects.HabitTypeValue, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
@@ -179,7 +187,7 @@ func TestMarkHabitHandler_WithValue(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_DuplicateEntry(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
@@ -202,3 +210,332 @@ func TestMarkHabitHandler_DuplicateEntry(t *testing.T) {
|
||||
t.Errorf("Expected ErrAlreadyExists, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterOnlyAcceptsIntegers(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Water Glasses", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
entryRepo := &mockEntryRepo{}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
decimalValue := 2.5
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &decimalValue,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != errors.ErrInvalidInput {
|
||||
t.Errorf("Expected ErrInvalidInput for decimal value on COUNTER, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterAcceptsIntegers(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Water Glasses", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
entryRepo := &mockEntryRepo{
|
||||
createFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if entry.Value == nil || *entry.Value != 3.0 {
|
||||
t.Errorf("Expected value 3.0, got %v", entry.Value)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
intValue := 3.0
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &intValue,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterAutoIncrementFirstMark(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Water Glasses", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
entryRepo := &mockEntryRepo{
|
||||
findByDateRangeFunc: func(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
|
||||
return []*entities.HabitEntry{}, nil
|
||||
},
|
||||
createFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if entry.Value == nil || *entry.Value != 1.0 {
|
||||
t.Errorf("Expected default value 1.0, got %v", entry.Value)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: nil,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterAutoIncrementSubsequentMarks(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Water Glasses", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
|
||||
existingValue := 3.0
|
||||
existingEntry := &entities.HabitEntry{
|
||||
ID: "entry-1",
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &existingValue,
|
||||
}
|
||||
|
||||
entryRepo := &mockEntryRepo{
|
||||
findByDateRangeFunc: func(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
|
||||
return []*entities.HabitEntry{existingEntry}, nil
|
||||
},
|
||||
updateFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if entry.Value == nil || *entry.Value != 4.0 {
|
||||
t.Errorf("Expected value 4.0, got %v", entry.Value)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: nil,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterAutoIncrementWithCustomValue(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Water Glasses", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
|
||||
existingValue := 3.0
|
||||
existingEntry := &entities.HabitEntry{
|
||||
ID: "entry-1",
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &existingValue,
|
||||
}
|
||||
|
||||
entryRepo := &mockEntryRepo{
|
||||
findByDateRangeFunc: func(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
|
||||
return []*entities.HabitEntry{existingEntry}, nil
|
||||
},
|
||||
updateFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if entry.Value == nil || *entry.Value != 5.0 {
|
||||
t.Errorf("Expected value 5.0 (3+2), got %v", entry.Value)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
increment := 2.0
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &increment,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterCanDecrement(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Cigarettes", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
|
||||
existingValue := 5.0
|
||||
existingEntry := &entities.HabitEntry{
|
||||
ID: "entry-1",
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &existingValue,
|
||||
}
|
||||
|
||||
entryRepo := &mockEntryRepo{
|
||||
findByDateRangeFunc: func(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
|
||||
return []*entities.HabitEntry{existingEntry}, nil
|
||||
},
|
||||
updateFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if entry.Value == nil || *entry.Value != 3.0 {
|
||||
t.Errorf("Expected value 3.0 (5-2), got %v", entry.Value)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
decrement := -2.0
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &decrement,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterMinimumZero(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Water Glasses", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
|
||||
existingValue := 2.0
|
||||
existingEntry := &entities.HabitEntry{
|
||||
ID: "entry-1",
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &existingValue,
|
||||
}
|
||||
|
||||
entryRepo := &mockEntryRepo{
|
||||
findByDateRangeFunc: func(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
|
||||
return []*entities.HabitEntry{existingEntry}, nil
|
||||
},
|
||||
updateFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if entry.Value == nil || *entry.Value != 0.0 {
|
||||
t.Errorf("Expected value 0.0 (2-3 clamped to 0), got %v", entry.Value)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
decrement := -3.0
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &decrement,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterStaysAtZero(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Water Glasses", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
|
||||
existingValue := 0.0
|
||||
existingEntry := &entities.HabitEntry{
|
||||
ID: "entry-1",
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &existingValue,
|
||||
}
|
||||
|
||||
entryRepo := &mockEntryRepo{
|
||||
findByDateRangeFunc: func(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
|
||||
return []*entities.HabitEntry{existingEntry}, nil
|
||||
},
|
||||
updateFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if entry.Value == nil || *entry.Value != 0.0 {
|
||||
t.Errorf("Expected value to stay at 0.0, got %v", entry.Value)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
decrement := -1.0
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &decrement,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkHabitHandler_CounterFirstMarkWithNegative(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Water Glasses", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForMark{habit: habit}
|
||||
entryRepo := &mockEntryRepo{
|
||||
findByDateRangeFunc: func(ctx context.Context, habitID string, from, to time.Time) ([]*entities.HabitEntry, error) {
|
||||
return []*entities.HabitEntry{}, nil
|
||||
},
|
||||
createFunc: func(ctx context.Context, entry *entities.HabitEntry) error {
|
||||
if entry.Value == nil || *entry.Value != 0.0 {
|
||||
t.Errorf("Expected value 0.0 (negative clamped), got %v", entry.Value)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
handler := NewMarkHabitHandler(entryRepo, habitRepo)
|
||||
|
||||
negativeValue := -5.0
|
||||
cmd := MarkHabitCommand{
|
||||
HabitID: "habit-1",
|
||||
ScheduledDate: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC),
|
||||
Value: &negativeValue,
|
||||
}
|
||||
|
||||
err := handler.Handle(context.Background(), cmd)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ func (m *mockEntryRepoForUnmark) Delete(ctx context.Context, id string) error {
|
||||
}
|
||||
|
||||
func TestUnmarkHabitHandler_UnmarksSuccessfully(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
scheduledDate := time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC)
|
||||
@@ -87,7 +87,7 @@ func TestUnmarkHabitHandler_ReturnsErrorWhenHabitNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUnmarkHabitHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForUpdate{
|
||||
@@ -112,7 +112,7 @@ func TestUnmarkHabitHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUnmarkHabitHandler_ReturnsErrorWhenEntryNotFound(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForUpdate{
|
||||
|
||||
@@ -33,7 +33,7 @@ func (m *mockHabitRepoForUpdate) Update(ctx context.Context, habit *entities.Hab
|
||||
}
|
||||
|
||||
func TestUpdateHabitHandler_UpdatesSuccessfully(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForUpdate{
|
||||
@@ -101,7 +101,7 @@ func TestUpdateHabitHandler_ReturnsErrorWhenHabitNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdateHabitHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForUpdate{
|
||||
@@ -124,7 +124,7 @@ func TestUpdateHabitHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdateHabitHandler_CannotUpdateArchivedHabit(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.Archive()
|
||||
|
||||
@@ -148,7 +148,7 @@ func TestUpdateHabitHandler_CannotUpdateArchivedHabit(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdateHabitHandler_ValidatesInput(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoForUpdate{
|
||||
|
||||
@@ -39,6 +39,7 @@ func (h *GetHabitByIDHandler) Handle(ctx context.Context, query GetHabitByIDQuer
|
||||
Frequency: habit.Frequency,
|
||||
TargetValue: habit.TargetValue,
|
||||
CarryOver: habit.CarryOver,
|
||||
IsNegative: habit.IsNegative,
|
||||
SpecificDays: habit.SpecificDays,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ func (m *mockHabitRepoWithFindByID) FindByID(ctx context.Context, id string) (*e
|
||||
|
||||
func TestGetHabitByIDHandler_ReturnsHabitSuccessfully(t *testing.T) {
|
||||
targetValue := 5.0
|
||||
habit := entities.NewHabit("user-123", "Drink Water", value_objects.HabitTypeValue, value_objects.FrequencyDaily, true)
|
||||
habit := entities.NewHabit("user-123", "Drink Water", value_objects.HabitTypeValue, value_objects.FrequencyDaily, true, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.TargetValue = &targetValue
|
||||
|
||||
@@ -82,7 +82,7 @@ func TestGetHabitByIDHandler_ReturnsErrorWhenHabitNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHabitByIDHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoWithFindByID{
|
||||
|
||||
@@ -25,7 +25,7 @@ func (m *mockEntryRepoWithFindByHabitID) FindByHabitIDAndDateRange(ctx context.C
|
||||
}
|
||||
|
||||
func TestGetHabitEntriesHandler_ReturnsEntriesSuccessfully(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
date1 := time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC)
|
||||
@@ -101,7 +101,7 @@ func TestGetHabitEntriesHandler_ReturnsErrorWhenHabitNotFound(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHabitEntriesHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoWithFindByID{
|
||||
@@ -127,7 +127,7 @@ func TestGetHabitEntriesHandler_ReturnsErrorWhenUserDoesNotOwnHabit(t *testing.T
|
||||
}
|
||||
|
||||
func TestGetHabitEntriesHandler_WithPagination(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
entries := make([]*entities.HabitEntry, 10)
|
||||
@@ -179,7 +179,7 @@ func TestGetHabitEntriesHandler_WithPagination(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetHabitEntriesHandler_RequiresPaginationWithoutDateRange(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepoWithFindByID{
|
||||
@@ -207,7 +207,7 @@ func TestGetHabitEntriesHandler_RequiresPaginationWithoutDateRange(t *testing.T)
|
||||
}
|
||||
|
||||
func TestGetHabitEntriesHandler_AllowsNoPaginationWithShortDateRange(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
from := time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
@@ -247,7 +247,7 @@ func TestGetHabitEntriesHandler_AllowsNoPaginationWithShortDateRange(t *testing.
|
||||
}
|
||||
|
||||
func TestGetHabitEntriesHandler_RequiresPaginationWithLongDateRange(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
from := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
@@ -14,6 +14,7 @@ type TodaysHabitDTO struct {
|
||||
Name string
|
||||
Type value_objects.HabitType
|
||||
TargetValue *float64
|
||||
IsNegative bool
|
||||
ScheduledDate time.Time
|
||||
IsCarriedOver bool
|
||||
}
|
||||
@@ -83,6 +84,7 @@ func (h *GetTodaysHabitsHandler) Handle(
|
||||
Name: habit.Name,
|
||||
Type: habit.Type,
|
||||
TargetValue: habit.TargetValue,
|
||||
IsNegative: habit.IsNegative,
|
||||
ScheduledDate: query.Date,
|
||||
IsCarriedOver: !shouldAppear && habit.CarryOver,
|
||||
})
|
||||
|
||||
@@ -76,7 +76,7 @@ func (m *mockEntryRepo) Delete(ctx context.Context, id string) error {
|
||||
}
|
||||
|
||||
func TestGetTodaysHabitsHandler_DailyHabitNoEntries(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
habitRepo := &mockHabitRepo{habits: []*entities.Habit{habit}}
|
||||
@@ -110,7 +110,7 @@ func TestGetTodaysHabitsHandler_DailyHabitNoEntries(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetTodaysHabitsHandler_DailyHabitAlreadyCompleted(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit.ID = "habit-1"
|
||||
|
||||
targetDate := time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC)
|
||||
@@ -139,7 +139,7 @@ func TestGetTodaysHabitsHandler_DailyHabitAlreadyCompleted(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetTodaysHabitsHandler_WeeklyHabitOnCorrectDay(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Gym", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false)
|
||||
habit := entities.NewHabit("user-123", "Gym", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.SpecificDays = []int{1, 3, 5}
|
||||
|
||||
@@ -168,7 +168,7 @@ func TestGetTodaysHabitsHandler_WeeklyHabitOnCorrectDay(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetTodaysHabitsHandler_WeeklyHabitOnWrongDay(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Gym", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false)
|
||||
habit := entities.NewHabit("user-123", "Gym", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.SpecificDays = []int{1, 3, 5}
|
||||
|
||||
@@ -197,7 +197,7 @@ func TestGetTodaysHabitsHandler_WeeklyHabitOnWrongDay(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetTodaysHabitsHandler_CarryOverEnabled(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Gym", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, true)
|
||||
habit := entities.NewHabit("user-123", "Gym", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, true, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.SpecificDays = []int{1}
|
||||
|
||||
@@ -230,7 +230,7 @@ func TestGetTodaysHabitsHandler_CarryOverEnabled(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetTodaysHabitsHandler_CarryOverDisabled(t *testing.T) {
|
||||
habit := entities.NewHabit("user-123", "Gym", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false)
|
||||
habit := entities.NewHabit("user-123", "Gym", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.SpecificDays = []int{1}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ type HabitDTO struct {
|
||||
Frequency value_objects.Frequency
|
||||
TargetValue *float64
|
||||
CarryOver bool
|
||||
IsNegative bool
|
||||
SpecificDays []int
|
||||
}
|
||||
|
||||
@@ -46,6 +47,7 @@ func (h *GetUserHabitsHandler) Handle(ctx context.Context, query GetUserHabitsQu
|
||||
Frequency: habit.Frequency,
|
||||
TargetValue: habit.TargetValue,
|
||||
CarryOver: habit.CarryOver,
|
||||
IsNegative: habit.IsNegative,
|
||||
SpecificDays: habit.SpecificDays,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,10 +9,10 @@ import (
|
||||
)
|
||||
|
||||
func TestGetUserHabitsHandler_ReturnsAllActiveHabits(t *testing.T) {
|
||||
habit1 := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
|
||||
habit1 := entities.NewHabit("user-123", "Exercise", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false, false)
|
||||
habit1.ID = "habit-1"
|
||||
|
||||
habit2 := entities.NewHabit("user-123", "Read", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false)
|
||||
habit2 := entities.NewHabit("user-123", "Read", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false, false)
|
||||
habit2.ID = "habit-2"
|
||||
|
||||
habitRepo := &mockHabitRepo{habits: []*entities.Habit{habit1, habit2}}
|
||||
@@ -64,7 +64,7 @@ func TestGetUserHabitsHandler_ReturnsEmptyListForUserWithNoHabits(t *testing.T)
|
||||
|
||||
func TestGetUserHabitsHandler_IncludesAllHabitFields(t *testing.T) {
|
||||
targetValue := 5.0
|
||||
habit := entities.NewHabit("user-123", "Drink Water", value_objects.HabitTypeValue, value_objects.FrequencyDaily, true)
|
||||
habit := entities.NewHabit("user-123", "Drink Water", value_objects.HabitTypeValue, value_objects.FrequencyDaily, true, false)
|
||||
habit.ID = "habit-1"
|
||||
habit.TargetValue = &targetValue
|
||||
|
||||
|
||||
Reference in New Issue
Block a user