Add a test for the CSV parser and check errors on the read config

This commit is contained in:
Miguel de la Cruz 2023-06-30 09:51:34 +02:00
parent ea6ba550b1
commit e9c615197a
2 changed files with 29 additions and 6 deletions

View file

@ -15,8 +15,9 @@ func TestReadConfig(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
defer os.Remove(f.Name()) defer os.Remove(f.Name())
io.WriteString(f, "---\nbirthday_file: birthday.csv") _, werr := io.WriteString(f, "---\nbirthday_file: birthday.csv")
f.Close() require.NoError(t, werr)
require.NoError(t, f.Close())
config, err := ReadConfig(f.Name()) config, err := ReadConfig(f.Name())
require.NoError(t, err) require.NoError(t, err)

View file

@ -1,6 +1,8 @@
package parser package parser
import ( import (
"io"
"io/ioutil"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -8,9 +10,29 @@ import (
func TestParseCsv(t *testing.T) { func TestParseCsv(t *testing.T) {
t.Run("should correctly parse a valid CSV file", func(t *testing.T) { t.Run("should correctly parse a valid CSV file", func(t *testing.T) {
// create a temp CSV file f, err := ioutil.TempFile("", "birthdaybot-")
// call parse command require.NoError(t, err)
// check birthdays and contents
require.FailNow(t, "ToBeImplemented") _, 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)
}) })
} }