holidayspl/holidays_test.go
2024-09-30 22:14:16 +02:00

70 lines
2.0 KiB
Go

package holidayspl_test
import (
"testing"
"time"
"git.ma-al.com/goc_marek/holidayspl"
)
func TestGetEasterDate(t *testing.T) {
holidays := holidayspl.New()
tests := []struct {
year int
expected time.Time
}{
{2024, time.Date(2024, time.March, 31, 0, 0, 0, 0, time.UTC)},
{2025, time.Date(2025, time.April, 20, 0, 0, 0, 0, time.UTC)},
{2026, time.Date(2026, time.April, 5, 0, 0, 0, 0, time.UTC)},
}
for _, test := range tests {
result := holidays.GetEasterDate(test.year)
if !result.Equal(test.expected) {
t.Errorf("GetEasterDate(%d) = %v; want %v", test.year, result, test.expected)
}
}
}
func TestGetByDate(t *testing.T) {
holidays := holidayspl.New()
tests := []struct {
date time.Time
expected string
found bool
}{
{time.Date(2024, time.January, 1, 0, 0, 0, 0, time.UTC), "New Year", true},
{time.Date(2024, time.March, 31, 0, 0, 0, 0, time.UTC), "Easter Sunday", true},
{time.Date(2024, time.December, 25, 0, 0, 0, 0, time.UTC), "Christmas", true},
{time.Date(2024, time.August, 15, 0, 0, 0, 0, time.UTC), "Assumption of Mary", true},
{time.Date(2024, time.July, 4, 0, 0, 0, 0, time.UTC), "", false}, // No holiday on this date
}
for _, test := range tests {
result, found := holidays.GetByDate(test.date)
if found != test.found {
t.Errorf("GetByDate(%v) = %v; want %v", test.date, found, test.found)
}
if found && result.Name != test.expected {
t.Errorf("GetByDate(%v) = %v; want %v", test.date, result.Name, test.expected)
}
}
}
func TestCaching(t *testing.T) {
holidays := holidayspl.New()
year := 2024
// First call should calculate and cache holidays for the year
holidays.GetHolidaysList(year)
// Modify the cache and see if the function returns the cached value
holidays.GetCache()[year][0].Name = "Modified New Year"
result := holidays.GetHolidaysList(year)
if result[0].Name != "Modified New Year" {
t.Errorf("Caching not working properly. Got %v, expected %v", result[0].Name, "Modified New Year")
}
}