From e9c615197ad575c087af0a5e8d34e042a21e5d9a Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Fri, 30 Jun 2023 09:51:34 +0200 Subject: [PATCH] Add a test for the CSV parser and check errors on the read config --- model/config_test.go | 5 +++-- parser/csv_parser_test.go | 30 ++++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/model/config_test.go b/model/config_test.go index ca541bc..3d9433a 100644 --- a/model/config_test.go +++ b/model/config_test.go @@ -15,8 +15,9 @@ func TestReadConfig(t *testing.T) { require.NoError(t, err) defer os.Remove(f.Name()) - io.WriteString(f, "---\nbirthday_file: birthday.csv") - f.Close() + _, werr := io.WriteString(f, "---\nbirthday_file: birthday.csv") + require.NoError(t, werr) + require.NoError(t, f.Close()) config, err := ReadConfig(f.Name()) require.NoError(t, err) diff --git a/parser/csv_parser_test.go b/parser/csv_parser_test.go index 40b1516..d0b977b 100644 --- a/parser/csv_parser_test.go +++ b/parser/csv_parser_test.go @@ -1,6 +1,8 @@ package parser import ( + "io" + "io/ioutil" "testing" "github.com/stretchr/testify/require" @@ -8,9 +10,29 @@ import ( func TestParseCsv(t *testing.T) { t.Run("should correctly parse a valid CSV file", func(t *testing.T) { - // create a temp CSV file - // call parse command - // check birthdays and contents - require.FailNow(t, "ToBeImplemented") + 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) }) }