birthdaybot/model/birthdays_test.go

124 lines
3.2 KiB
Go
Raw Normal View History

package model
import (
"testing"
2023-07-11 12:27:50 +01:00
"time"
"github.com/stretchr/testify/require"
)
func TestNewBirthdayFromRecord(t *testing.T) {
t.Run("should correctly parse different records", func(t *testing.T) {
testCases := []struct {
Record []string
ExpectedName string
ExpectedEmail string
ExpectedPhone string
ExpectedYearOfBirth int
ExpectedMonthOfBirth int
ExpectedDayOfBirth int
}{
{
Record: []string{"John Doe ", "john@doe.com", " 123456789", "17/04/2192"},
ExpectedName: "John Doe",
ExpectedEmail: "john@doe.com",
ExpectedPhone: "123456789",
ExpectedYearOfBirth: 2192,
ExpectedMonthOfBirth: 4,
ExpectedDayOfBirth: 17,
},
}
for _, tc := range testCases {
t.Run(tc.ExpectedName, func(t *testing.T) {
b, err := NewBirthdayFromRecord(tc.Record)
require.NoError(t, err)
require.Equal(t, tc.ExpectedName, b.Name)
require.Equal(t, tc.ExpectedEmail, b.Email)
require.Equal(t, tc.ExpectedPhone, b.Phone)
require.Equal(t, tc.ExpectedYearOfBirth, b.YearOfBirth)
require.Equal(t, tc.ExpectedMonthOfBirth, b.MonthOfBirth)
require.Equal(t, tc.ExpectedDayOfBirth, b.DayOfBirth)
})
}
})
}
2023-07-10 21:44:48 +01:00
func TestFilename(t *testing.T) {
birthday := &Birthday{
Name: "John Doe",
Email: "john@doe.com",
Phone: "123456789",
YearOfBirth: 2022,
MonthOfBirth: 4,
DayOfBirth: 6,
}
require.Equal(t, "2022_4_6_123456789.png", birthday.Filename())
}
2023-07-11 12:27:50 +01:00
func TestNextBirthdayDate(t *testing.T) {
firstBirthday := &Birthday{
YearOfBirth: 1900,
MonthOfBirth: 2,
DayOfBirth: 1,
}
secondBirthday := &Birthday{
YearOfBirth: 1900,
MonthOfBirth: 8,
DayOfBirth: 1,
}
birthdays := []*Birthday{firstBirthday, secondBirthday}
birthdaysReversed := []*Birthday{secondBirthday, firstBirthday}
testCases := []struct {
Name string
Now time.Time
Birthdays []*Birthday
ExpectedDay int
ExpectedMonth int
ExpectedYear int
}{
{
Name: "should find first birthday",
Now: time.Date(1900, time.Month(1), 1, 0, 0, 0, 0, time.Now().Location()),
ExpectedDay: 1,
ExpectedMonth: 2,
ExpectedYear: 1900,
},
{
Name: "should find second birthday",
Now: time.Date(1900, time.Month(4), 1, 0, 0, 0, 0, time.Now().Location()),
ExpectedDay: 1,
ExpectedMonth: 8,
ExpectedYear: 1900,
},
{
Name: "should find first birthday for next year",
Now: time.Date(1900, time.Month(10), 1, 0, 0, 0, 0, time.Now().Location()),
ExpectedDay: 1,
ExpectedMonth: 2,
ExpectedYear: 1900,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
t.Run("with birthdays sorted", func(t *testing.T) {
day, month, year := NextBirthdayDate(birthdays, tc.Now)
require.Equal(t, tc.ExpectedDay, day)
require.Equal(t, tc.ExpectedMonth, month)
require.Equal(t, tc.ExpectedYear, year)
})
t.Run("with birthdays reversed", func(t *testing.T) {
day, month, year := NextBirthdayDate(birthdaysReversed, tc.Now)
require.Equal(t, tc.ExpectedDay, day)
require.Equal(t, tc.ExpectedMonth, month)
require.Equal(t, tc.ExpectedYear, year)
})
})
}
}