Skip to content

Commit

Permalink
Add test on error at second writing to file
Browse files Browse the repository at this point in the history
  • Loading branch information
rnixik committed Jul 29, 2018
1 parent 6896136 commit e4590ea
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
15 changes: 13 additions & 2 deletions csv_writer_test.go
Expand Up @@ -83,7 +83,18 @@ func TestCsvWriterWriteRowsFileWriteError(t *testing.T) {
rows = append(rows, &map[string]interface{}{"name": "one"})
err := writer.WriteRows("some_table", []string{"name"}, rows)
if err == nil {
t.Errorf("Expected file writer error, but got nil ")
t.Errorf("Expected file writer error, but got nil")
}
}

func TestCsvWriterWriteRowsFileWriteErrorAtSecondAttempt(t *testing.T) {
fw := &TestFileWhichFailsAtSecondAttemptWriter{}
writer := NewCsvWriter(fw, "result.csv", "", ",")
rows := make([]*map[string]interface{}, 0)
rows = append(rows, &map[string]interface{}{"name": "one"})
err := writer.WriteRows("some_table", []string{"name"}, rows)
if err == nil {
t.Errorf("Expected file writer error, but got nil")
}
}

Expand All @@ -94,6 +105,6 @@ func TestCsvWriterWriteRowsFileHandlerError(t *testing.T) {
rows = append(rows, &map[string]interface{}{"name": "one"})
err := writer.WriteRows("some_table", []string{"name"}, rows)
if err == nil {
t.Errorf("Expected write error, but got nil ")
t.Errorf("Expected write error, but got nil")
}
}
25 changes: 25 additions & 0 deletions filewriter_test.go
Expand Up @@ -66,6 +66,31 @@ func (fw *TestFileHandlerErrorWriter) getFileHandler(filename string) (f File, e
return nil, fmt.Errorf("Some testing error at getFileHandler")
}

// TestFileWhichFailsAtSecondAttempt

type TestFileWhichFailsAtSecondAttempt struct {
attempt int
}

func (f *TestFileWhichFailsAtSecondAttempt) WriteString(s string) (n int, err error) {
f.attempt += 1
if f.attempt > 1 {
return 0, fmt.Errorf("Error at second attempt to write file")
}
return 1, nil
}
func (f *TestFileWhichFailsAtSecondAttempt) Close() error {
return nil
}

type TestFileWhichFailsAtSecondAttemptWriter struct {
}

func (fw *TestFileWhichFailsAtSecondAttemptWriter) getFileHandler(filename string) (f File, err error) {
testFile := &TestFileWhichFailsAtSecondAttempt{}
return testFile, nil
}

// Tests on NewOsFileWriter

func TestGetFileHandlerFileExists(t *testing.T) {
Expand Down

0 comments on commit e4590ea

Please sign in to comment.