Files
apocapoc-api/internal/domain/entities/habit_test.go
T
david 664f7a0de0 Add domain layer tests (TDD - Red phase)
Following TDD principles, adding tests before implementation:

- Add HabitType value object tests (validation)
- Add Frequency value object tests (validation)
- Add User entity tests (creation, default timezone)
- Add Habit entity tests (creation, archive, active status)
- Add HabitEntry entity tests (creation, soft delete)

Tests cover core domain logic and business rules.
All tests will fail until implementation is added.
2025-11-25 10:12:04 +01:00

106 lines
2.6 KiB
Go

package entities
import (
"testing"
"time"
"habit-tracker-api/internal/domain/value_objects"
)
func TestNewHabit(t *testing.T) {
userID := "user-123"
name := "Morning Exercise"
habitType := value_objects.HabitTypeBoolean
frequency := value_objects.FrequencyDaily
carryOver := false
habit := NewHabit(userID, name, habitType, frequency, carryOver)
if habit.UserID != userID {
t.Errorf("Expected UserID %s, got %s", userID, habit.UserID)
}
if habit.Name != name {
t.Errorf("Expected Name %s, got %s", name, habit.Name)
}
if habit.Type != habitType {
t.Errorf("Expected Type %s, got %s", habitType, habit.Type)
}
if habit.Frequency != frequency {
t.Errorf("Expected Frequency %s, got %s", frequency, habit.Frequency)
}
if habit.CarryOver != carryOver {
t.Errorf("Expected CarryOver %v, got %v", carryOver, habit.CarryOver)
}
if habit.CreatedAt.IsZero() {
t.Error("CreatedAt should not be zero")
}
if habit.ArchivedAt != nil {
t.Error("ArchivedAt should be nil for new habit")
}
}
func TestHabit_Archive(t *testing.T) {
habit := NewHabit("user-123", "Test Habit", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
if habit.ArchivedAt != nil {
t.Error("New habit should not be archived")
}
habit.Archive()
if habit.ArchivedAt == nil {
t.Error("Habit should be archived after calling Archive()")
}
if habit.ArchivedAt.After(time.Now()) {
t.Error("ArchivedAt should not be in the future")
}
}
func TestHabit_IsActive(t *testing.T) {
habit := NewHabit("user-123", "Test Habit", value_objects.HabitTypeBoolean, value_objects.FrequencyDaily, false)
if !habit.IsActive() {
t.Error("New habit should be active")
}
habit.Archive()
if habit.IsActive() {
t.Error("Archived habit should not be active")
}
}
func TestHabit_WithSpecificDays(t *testing.T) {
habit := NewHabit("user-123", "Workout", value_objects.HabitTypeBoolean, value_objects.FrequencyWeekly, false)
habit.SpecificDays = []int{1, 3, 5} // Monday, Wednesday, Friday
if len(habit.SpecificDays) != 3 {
t.Errorf("Expected 3 specific days, got %d", len(habit.SpecificDays))
}
if habit.SpecificDays[0] != 1 || habit.SpecificDays[1] != 3 || habit.SpecificDays[2] != 5 {
t.Errorf("Expected days [1,3,5], got %v", habit.SpecificDays)
}
}
func TestHabit_WithTargetValue(t *testing.T) {
habit := NewHabit("user-123", "Drink Water", value_objects.HabitTypeCounter, value_objects.FrequencyDaily, false)
targetValue := 8.0
habit.TargetValue = &targetValue
if habit.TargetValue == nil {
t.Error("TargetValue should not be nil")
}
if *habit.TargetValue != 8.0 {
t.Errorf("Expected TargetValue 8.0, got %f", *habit.TargetValue)
}
}