93 lines
2.4 KiB
Go
93 lines
2.4 KiB
Go
package cif
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestIsSameDay(t *testing.T) {
|
|
today := time.Now()
|
|
notToday := time.Date(2024, 01, 23, 23, 01, 3, 421, time.Local)
|
|
|
|
if !isSameToday(today) {
|
|
t.Errorf("Error in isSameDay(today). Expected true, got false.")
|
|
}
|
|
|
|
if isSameToday(notToday) {
|
|
t.Errorf("Error in isSameDay(notToday). Expected false, got true.")
|
|
}
|
|
}
|
|
|
|
func TestHowManyDaysAgo(t *testing.T) {
|
|
testCases := []struct {
|
|
input time.Time
|
|
expected int
|
|
}{
|
|
{time.Now(), 0}, // Today
|
|
{time.Now().Add(-24 * time.Hour), 1}, // Yesterday
|
|
{time.Now().Add(-48 * time.Hour), 2}, // Ereyesterday
|
|
{time.Now().Add(24 * time.Hour), -1}, // Tomorrow
|
|
{time.Now().Add(48 * time.Hour), -2}, // Overmorrow
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
result := howManyDaysAgo(tc.input)
|
|
if result != tc.expected {
|
|
t.Errorf("For input %v, expected %d but got %d", tc.input, tc.expected, result)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGetDayString(t *testing.T) {
|
|
testCases := []struct {
|
|
input time.Time
|
|
expected string
|
|
}{ // Note that the test times are in UTC, but days are checked in Europe/London
|
|
{time.Date(2024, time.April, 7, 0, 0, 0, 0, time.UTC), "sun"},
|
|
{time.Date(2024, time.April, 4, 21, 0, 0, 0, time.UTC), "thu"},
|
|
{time.Date(2001, time.September, 11, 12, 46, 0, 0, time.UTC), "tue"},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
result := getDayString(tc.input)
|
|
if result != tc.expected {
|
|
t.Errorf("For input %v, expected %s, but got %s", tc.input, tc.expected, result)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGenerateUpdateDays(t *testing.T) {
|
|
testCases := []struct {
|
|
days int
|
|
expected []time.Time
|
|
}{
|
|
{1, []time.Time{time.Now()}},
|
|
{2, []time.Time{time.Now().Add(-24 * time.Hour), time.Now()}},
|
|
{4, []time.Time{time.Now().Add(-72 * time.Hour),
|
|
time.Now().Add(-48 * time.Hour),
|
|
time.Now().Add(-24 * time.Hour),
|
|
time.Now(),
|
|
}},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
result := generateUpdateDays(tc.days)
|
|
|
|
if len(result) != len(tc.expected) {
|
|
t.Errorf("For %d days, expected %v, but got %v", tc.days, tc.expected, result)
|
|
continue
|
|
}
|
|
|
|
for i := range result {
|
|
if !isSameDate(result[i], tc.expected[i]) {
|
|
t.Errorf("For %d days, expected %v, but got %v", tc.days, tc.expected, result)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Checks if two time values have the same year, month and day.
|
|
func isSameDate(t1, t2 time.Time) bool {
|
|
return t1.Year() == t2.Year() && t1.Month() == t2.Month() && t1.Day() == t2.Day()
|
|
}
|