2023-06-29 23:37:56 +01:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
2023-06-30 08:51:34 +01:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2023-06-29 23:37:56 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseCsv(t *testing.T) {
|
|
|
|
t.Run("should correctly parse a valid CSV file", func(t *testing.T) {
|
2023-06-30 08:51:34 +01:00
|
|
|
f, err := ioutil.TempFile("", "birthdaybot-")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, werr := io.WriteString(f, "John Doe , john@doe.com, 1234, 17/04/2192\nJane Doe,jane@doe.com,4321,15/01/2020\n")
|
|
|
|
require.NoError(t, werr)
|
|
|
|
require.NoError(t, f.Close())
|
|
|
|
|
|
|
|
birthdays, err := ParseCSV(f.Name())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, birthdays, 2)
|
|
|
|
|
|
|
|
require.Equal(t, "John Doe", birthdays[0].Name)
|
|
|
|
require.Equal(t, "john@doe.com", birthdays[0].Email)
|
|
|
|
require.Equal(t, "1234", birthdays[0].Phone)
|
|
|
|
require.Equal(t, 17, birthdays[0].DayOfBirth)
|
|
|
|
require.Equal(t, 4, birthdays[0].MonthOfBirth)
|
|
|
|
require.Equal(t, 2192, birthdays[0].YearOfBirth)
|
|
|
|
|
|
|
|
require.Equal(t, "Jane Doe", birthdays[1].Name)
|
|
|
|
require.Equal(t, "jane@doe.com", birthdays[1].Email)
|
|
|
|
require.Equal(t, "4321", birthdays[1].Phone)
|
|
|
|
require.Equal(t, 15, birthdays[1].DayOfBirth)
|
|
|
|
require.Equal(t, 1, birthdays[1].MonthOfBirth)
|
|
|
|
require.Equal(t, 2020, birthdays[1].YearOfBirth)
|
2023-06-29 23:37:56 +01:00
|
|
|
})
|
|
|
|
}
|