1package filesystem 2 3import ( 4 "os" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8) 9 10func TestGenerateCSVLines(t *testing.T) { 11 lineCount := 0 12 err := GenerateCSVLines( 13 "testdata/project.csv", 14 func(columns []string) { 15 lineCount++ 16 assert.Equal(t, 9, len(columns), "Fixture CSV Column Count") 17 }, 18 ) 19 assert.Equal(t, 670, lineCount, "Fixture CSV line count") 20 assert.Equal(t, nil, err, "Read CSV Error output") 21} 22 23func TestNonExistentFile(t *testing.T) { 24 25 err := GenerateCSVLines( 26 "this_file_totally_does_not_exist.csv", 27 func(columns []string) {}, 28 ) 29 assert.NotEqual(t, err, nil, "CSV Error should be generated") 30} 31 32func TestWriteCSVToFile(t *testing.T) { 33 outputPath := "testdata/generate.csv" 34 defer os.Remove(outputPath) 35 36 header := []string{ 37 "col1", 38 "col2", 39 } 40 rowsOfCols := [][]string{ 41 []string{ 42 "line1_val1", 43 "line1_val2", 44 }, 45 []string{ 46 "line2_val1", 47 "line2_val2", 48 }, 49 } 50 err := WriteCSVToFile(header, rowsOfCols, outputPath) 51 assert.Equal(t, err, nil, "Error should not be generated") 52 53 var reconstituted [][]string 54 err = GenerateCSVLines( 55 outputPath, 56 func(columns []string) { 57 assert.Equal(t, 2, len(columns), "Initial CSV ColumnCount") 58 reconstituted = append(reconstituted, columns) 59 }, 60 ) 61 assert.Equal(t, nil, err, "No error should exist reading created CSV") 62 assert.Equal(t, rowsOfCols, reconstituted, "Reconstituted should equal original") 63} 64