Add unit tests for middleware, models, services, handlers, and repository helpers
All checks were successful
CI / ci (push) Successful in 35s

This commit is contained in:
Cosmo
2026-03-01 02:32:59 +00:00
parent 2b4a6ce4c8
commit 8d9fe818f4
11 changed files with 800 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
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")
}
})
}

View File

@@ -0,0 +1,61 @@
package model
import (
"database/sql"
"testing"
"time"
)
func TestSavingsCategory_ProcessForJSON(t *testing.T) {
t.Run("with deposit dates", func(t *testing.T) {
c := &SavingsCategory{
DepositStartDate: sql.NullTime{Time: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true},
DepositEndDate: sql.NullTime{Time: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC), Valid: true},
CreditStartDate: sql.NullTime{Time: time.Date(2025, 6, 1, 0, 0, 0, 0, time.UTC), Valid: true},
}
c.ProcessForJSON()
if c.DepositStartStr == nil || *c.DepositStartStr != "2025-01-01" {
t.Errorf("expected 2025-01-01, got %v", c.DepositStartStr)
}
if c.DepositEndStr == nil || *c.DepositEndStr != "2026-01-01" {
t.Errorf("expected 2026-01-01, got %v", c.DepositEndStr)
}
if c.CreditStartStr == nil || *c.CreditStartStr != "2025-06-01" {
t.Errorf("expected 2025-06-01, got %v", c.CreditStartStr)
}
})
t.Run("without dates", func(t *testing.T) {
c := &SavingsCategory{}
c.ProcessForJSON()
if c.DepositStartStr != nil {
t.Error("expected nil deposit_start_date")
}
})
}
func TestSavingsRecurringPlan_ProcessForJSON(t *testing.T) {
t.Run("with user_id", func(t *testing.T) {
p := &SavingsRecurringPlan{
UserID: sql.NullInt64{Int64: 42, Valid: true},
}
p.ProcessForJSON()
if p.UserIDPtr == nil || *p.UserIDPtr != 42 {
t.Errorf("expected 42, got %v", p.UserIDPtr)
}
})
t.Run("without user_id", func(t *testing.T) {
p := &SavingsRecurringPlan{
UserID: sql.NullInt64{Valid: false},
}
p.ProcessForJSON()
if p.UserIDPtr != nil {
t.Error("expected nil user_id")
}
})
}

View File

@@ -0,0 +1,66 @@
package model
import (
"database/sql"
"testing"
"time"
)
func TestTask_ProcessForJSON(t *testing.T) {
t.Run("task with HH:MM:SS reminder and all fields", func(t *testing.T) {
task := &Task{
DueDate: sql.NullTime{Time: time.Date(2025, 3, 15, 0, 0, 0, 0, time.UTC), Valid: true},
ReminderTime: sql.NullString{String: "14:30:00", Valid: true},
CompletedAt: sql.NullTime{Time: time.Now(), Valid: true},
RecurrenceType: sql.NullString{String: "weekly", Valid: true},
RecurrenceEndDate: sql.NullTime{Time: time.Date(2025, 12, 31, 0, 0, 0, 0, time.UTC), Valid: true},
ParentTaskID: sql.NullInt64{Int64: 5, Valid: true},
}
task.ProcessForJSON()
if task.DueDateStr == nil || *task.DueDateStr != "2025-03-15" {
t.Errorf("expected due_date 2025-03-15, got %v", task.DueDateStr)
}
if task.ReminderTimeStr == nil || *task.ReminderTimeStr != "14:30" {
t.Errorf("expected reminder 14:30, got %v", task.ReminderTimeStr)
}
if !task.Completed {
t.Error("expected completed to be true")
}
if task.RecurrenceTypeStr == nil || *task.RecurrenceTypeStr != "weekly" {
t.Errorf("expected recurrence_type weekly, got %v", task.RecurrenceTypeStr)
}
if task.RecurrenceEndStr == nil || *task.RecurrenceEndStr != "2025-12-31" {
t.Errorf("expected recurrence_end 2025-12-31, got %v", task.RecurrenceEndStr)
}
if task.ParentTaskIDPtr == nil || *task.ParentTaskIDPtr != 5 {
t.Errorf("expected parent_task_id 5, got %v", task.ParentTaskIDPtr)
}
})
t.Run("task with RFC3339 reminder", func(t *testing.T) {
task := &Task{
ReminderTime: sql.NullString{String: "0000-01-01T09:00:00Z", Valid: true},
}
task.ProcessForJSON()
if task.ReminderTimeStr == nil || *task.ReminderTimeStr != "09:00" {
t.Errorf("expected 09:00, got %v", task.ReminderTimeStr)
}
})
t.Run("incomplete task with null fields", func(t *testing.T) {
task := &Task{
DueDate: sql.NullTime{Valid: false},
CompletedAt: sql.NullTime{Valid: false},
}
task.ProcessForJSON()
if task.DueDateStr != nil {
t.Error("expected due_date nil")
}
if task.Completed {
t.Error("expected completed to be false")
}
})
}

View File

@@ -0,0 +1,46 @@
package model
import (
"database/sql"
"testing"
)
func TestUser_ProcessForJSON(t *testing.T) {
t.Run("with telegram chat id", func(t *testing.T) {
u := &User{
TelegramChatID: sql.NullInt64{Int64: 123456, Valid: true},
MorningReminderTime: sql.NullString{String: "09:00:00", Valid: true},
EveningReminderTime: sql.NullString{String: "21:30:00", Valid: true},
}
u.ProcessForJSON()
if u.TelegramChatIDValue == nil || *u.TelegramChatIDValue != 123456 {
t.Error("telegram_chat_id not set correctly")
}
if u.MorningTime != "09:00" {
t.Errorf("expected 09:00, got %s", u.MorningTime)
}
if u.EveningTime != "21:30" {
t.Errorf("expected 21:30, got %s", u.EveningTime)
}
})
t.Run("without telegram chat id", func(t *testing.T) {
u := &User{
TelegramChatID: sql.NullInt64{Valid: false},
MorningReminderTime: sql.NullString{Valid: false},
EveningReminderTime: sql.NullString{Valid: false},
}
u.ProcessForJSON()
if u.TelegramChatIDValue != nil {
t.Error("telegram_chat_id should be nil")
}
if u.MorningTime != "09:00" {
t.Errorf("expected default 09:00, got %s", u.MorningTime)
}
if u.EveningTime != "21:00" {
t.Errorf("expected default 21:00, got %s", u.EveningTime)
}
})
}