53 lines
1.5 KiB
Go
53 lines
1.5 KiB
Go
package model
|
|
|
|
import (
|
|
"database/sql"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestHabit_ProcessForJSON(t *testing.T) {
|
|
t.Run("with reminder time RFC3339 format", func(t *testing.T) {
|
|
h := &Habit{
|
|
ReminderTime: sql.NullString{String: "0000-01-01T19:00:00Z", Valid: true},
|
|
StartDate: sql.NullTime{Time: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC), Valid: true},
|
|
}
|
|
h.ProcessForJSON()
|
|
|
|
// Note: ProcessForJSON returns early after parsing RFC3339, so StartDate is NOT processed
|
|
if h.ReminderTimeStr == nil || *h.ReminderTimeStr != "19:00" {
|
|
t.Errorf("expected 19:00, got %v", h.ReminderTimeStr)
|
|
}
|
|
})
|
|
|
|
t.Run("with reminder time HH:MM:SS format and start date", func(t *testing.T) {
|
|
h := &Habit{
|
|
ReminderTime: sql.NullString{String: "08:30:00", Valid: true},
|
|
StartDate: sql.NullTime{Time: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC), Valid: true},
|
|
}
|
|
h.ProcessForJSON()
|
|
|
|
if h.ReminderTimeStr == nil || *h.ReminderTimeStr != "08:30" {
|
|
t.Errorf("expected 08:30, got %v", h.ReminderTimeStr)
|
|
}
|
|
if h.StartDateStr == nil || *h.StartDateStr != "2025-01-15" {
|
|
t.Errorf("expected 2025-01-15, got %v", h.StartDateStr)
|
|
}
|
|
})
|
|
|
|
t.Run("without reminder time", func(t *testing.T) {
|
|
h := &Habit{
|
|
ReminderTime: sql.NullString{Valid: false},
|
|
StartDate: sql.NullTime{Valid: false},
|
|
}
|
|
h.ProcessForJSON()
|
|
|
|
if h.ReminderTimeStr != nil {
|
|
t.Error("reminder_time should be nil")
|
|
}
|
|
if h.StartDateStr != nil {
|
|
t.Error("start_date should be nil")
|
|
}
|
|
})
|
|
}
|