birthdaybot/utils/utils_test.go

48 lines
1.2 KiB
Go
Raw Permalink Normal View History

package utils
import (
"os"
"path/filepath"
"testing"
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
"github.com/stretchr/testify/require"
)
// ToDo: replace with os.CreateTemp
// ioutil.TempFile()
func TestGetPictureForBirthday(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "birthdaybot-pictures-")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
birthday := &model.Birthday{
Name: "John Doe",
Email: "john@doe.com",
Phone: "987654321",
YearOfBirth: 2022,
MonthOfBirth: 7,
DayOfBirth: 1,
}
expectedPath := filepath.Join(tmpDir, "2022_7_1_987654321.png")
t.Run("should return the right path when the file exists", func(t *testing.T) {
// we create the file
require.NoError(t, os.WriteFile(expectedPath, []byte("dummy"), 0755))
picture, err := GetPictureForBirthday(birthday, tmpDir)
require.NoError(t, err)
require.Equal(t, expectedPath, picture)
})
t.Run("should fail if the file doesn't exist", func(t *testing.T) {
// we ensure that the file doesn't exist
require.NoError(t, os.Remove(expectedPath))
picture, err := GetPictureForBirthday(birthday, tmpDir)
require.ErrorIs(t, err, os.ErrNotExist)
require.Empty(t, picture)
})
}