Move get picture to a utils package so it can be reused
This commit is contained in:
parent
5fdf7df7df
commit
cdd29a7ae8
3 changed files with 69 additions and 14 deletions
|
@ -4,11 +4,11 @@ import (
|
|||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"text/template"
|
||||
|
||||
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
|
||||
"git.ctrlz.es/mgdelacroix/birthdaybot/utils"
|
||||
"github.com/charmbracelet/log"
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
@ -39,18 +39,6 @@ func NewTelegramNotificationService(logger *log.Logger, config *model.Config) (*
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (tns *TelegramNotificationService) getPictureFileForBirthday(birthday *model.Birthday) (string, error) {
|
||||
filename := birthday.Filename()
|
||||
path := filepath.Join(tns.config.Birthdays.Pictures, filename)
|
||||
|
||||
_, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func (tns *TelegramNotificationService) Notify(birthday *model.Birthday, template *template.Template) error {
|
||||
var msgText string
|
||||
if template != nil {
|
||||
|
@ -69,7 +57,7 @@ func (tns *TelegramNotificationService) Notify(birthday *model.Birthday, templat
|
|||
return fmt.Errorf("cannot parse ChannelID: %w", err)
|
||||
}
|
||||
|
||||
picture, err := tns.getPictureFileForBirthday(birthday)
|
||||
picture, err := utils.GetPictureForBirthday(birthday, tns.config.Birthdays.Pictures)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("cannot get picture for birthday: %w", err)
|
||||
}
|
||||
|
|
20
utils/utils.go
Normal file
20
utils/utils.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"git.ctrlz.es/mgdelacroix/birthdaybot/model"
|
||||
)
|
||||
|
||||
func GetPictureForBirthday(birthday *model.Birthday, picturesDir string) (string, error) {
|
||||
filename := birthday.Filename()
|
||||
path := filepath.Join(picturesDir, filename)
|
||||
|
||||
_, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return path, nil
|
||||
}
|
47
utils/utils_test.go
Normal file
47
utils/utils_test.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
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)
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue