test: expand handler tests to 53.4% coverage
Some checks failed
CI / lint-test (push) Failing after 1s
Some checks failed
CI / lint-test (push) Failing after 1s
This commit is contained in:
104
internal/service/task_test.go
Normal file
104
internal/service/task_test.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/daniil/homelab-api/internal/model"
|
||||
)
|
||||
|
||||
func TestNewTaskService(t *testing.T) {
|
||||
svc := NewTaskService(nil)
|
||||
if svc == nil {
|
||||
t.Fatal("expected non-nil TaskService")
|
||||
}
|
||||
}
|
||||
|
||||
// createNextRecurrence returns without DB call for unknown recurrence types.
|
||||
func TestCreateNextRecurrence_UnknownType(t *testing.T) {
|
||||
svc := &TaskService{} // nil taskRepo — should not be called
|
||||
task := &model.Task{
|
||||
UserID: 1,
|
||||
Title: "Test Task",
|
||||
IsRecurring: true,
|
||||
RecurrenceType: sql.NullString{String: "unknown_type", Valid: true},
|
||||
RecurrenceInterval: 1,
|
||||
DueDate: sql.NullTime{Time: time.Now().AddDate(0, 0, 1), Valid: true},
|
||||
}
|
||||
// Should return early — no panic from nil taskRepo
|
||||
svc.createNextRecurrence(task)
|
||||
}
|
||||
|
||||
// createNextRecurrence returns without DB call when next date is past end date.
|
||||
func TestCreateNextRecurrence_PastEndDate(t *testing.T) {
|
||||
svc := &TaskService{} // nil taskRepo — should not be called
|
||||
yesterday := time.Now().AddDate(0, 0, -1)
|
||||
twoDaysAgo := time.Now().AddDate(0, 0, -2)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
recurrenceType string
|
||||
currentDue time.Time
|
||||
endDate time.Time
|
||||
}{
|
||||
{
|
||||
name: "daily past end",
|
||||
recurrenceType: "daily",
|
||||
currentDue: twoDaysAgo,
|
||||
endDate: yesterday,
|
||||
},
|
||||
{
|
||||
name: "weekly past end",
|
||||
recurrenceType: "weekly",
|
||||
currentDue: time.Now().AddDate(0, 0, -14),
|
||||
endDate: yesterday,
|
||||
},
|
||||
{
|
||||
name: "monthly past end",
|
||||
recurrenceType: "monthly",
|
||||
currentDue: time.Now().AddDate(0, -2, 0),
|
||||
endDate: yesterday,
|
||||
},
|
||||
{
|
||||
name: "custom past end",
|
||||
recurrenceType: "custom",
|
||||
currentDue: twoDaysAgo,
|
||||
endDate: yesterday,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
task := &model.Task{
|
||||
UserID: 1,
|
||||
Title: "Recurring Task",
|
||||
IsRecurring: true,
|
||||
RecurrenceType: sql.NullString{String: tt.recurrenceType, Valid: true},
|
||||
RecurrenceInterval: 1,
|
||||
DueDate: sql.NullTime{Time: tt.currentDue, Valid: true},
|
||||
RecurrenceEndDate: sql.NullTime{Time: tt.endDate, Valid: true},
|
||||
}
|
||||
// Should return early — no panic from nil taskRepo
|
||||
svc.createNextRecurrence(task)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// interval < 1 gets normalized to 1
|
||||
func TestCreateNextRecurrence_IntervalNormalization(t *testing.T) {
|
||||
svc := &TaskService{}
|
||||
yesterday := time.Now().AddDate(0, 0, -1)
|
||||
|
||||
task := &model.Task{
|
||||
UserID: 1,
|
||||
Title: "Test",
|
||||
IsRecurring: true,
|
||||
RecurrenceType: sql.NullString{String: "daily", Valid: true},
|
||||
RecurrenceInterval: 0, // should be normalized to 1
|
||||
DueDate: sql.NullTime{Time: time.Now().AddDate(0, 0, -10), Valid: true},
|
||||
RecurrenceEndDate: sql.NullTime{Time: yesterday, Valid: true},
|
||||
}
|
||||
// Should return early due to end date — no panic from nil taskRepo
|
||||
svc.createNextRecurrence(task)
|
||||
}
|
||||
Reference in New Issue
Block a user