Repository: moov-io/ach
Pull request #221 (2018-06-20 16:00:16)

Writer updates
Writer Updates
Additional test coverage


Patch:
diff --git a/entryDetail.go b/entryDetail.go
index 8ff8a4ed..3a3c3bca 100644
--- a/entryDetail.go
+++ b/entryDetail.go
@@ -415,6 +415,7 @@ func (ed *EntryDetail) CreditOrDebit() string {
 		return "C"
 	case "5", "6", "7", "8", "9":
 		return "D"
+	default:
 	}
 	return ""
 }
diff --git a/writer.go b/writer.go
index d8346961..beaf46f3 100644
--- a/writer.go
+++ b/writer.go
@@ -13,7 +13,7 @@ import (
 // A Writer writes an ach.file to a NACHA encoded file.
 //
 // As returned by NewWriter, a Writer writes ach.file structs into
-// NACHA formted files.
+// NACHA formatted files.
 //
 type Writer struct {
 	w       *bufio.Writer
@@ -35,46 +35,32 @@ func (w *Writer) Write(file *File) error {
 
 	w.lineNum = 0
 	// Iterate over all records in the file
-	if _, err := w.w.WriteString(file.Header.String() + "\n"); err != nil {
-		return err
-	}
+	w.w.WriteString(file.Header.String() + "\n")
 	w.lineNum++
 
 	for _, batch := range file.Batches {
-		if _, err := w.w.WriteString(batch.GetHeader().String() + "\n"); err != nil {
-			return err
-		}
+		w.w.WriteString(batch.GetHeader().String() + "\n")
 		w.lineNum++
 		for _, entry := range batch.GetEntries() {
-			if _, err := w.w.WriteString(entry.String() + "\n"); err != nil {
-				return err
-			}
+			w.w.WriteString(entry.String() + "\n")
 			w.lineNum++
 			for _, addenda := range entry.Addendum {
-				if _, err := w.w.WriteString(addenda.String() + "\n"); err != nil {
-					return err
-				}
+				w.w.WriteString(addenda.String() + "\n")
 				w.lineNum++
 			}
 		}
-		if _, err := w.w.WriteString(batch.GetControl().String() + "\n"); err != nil {
-			return err
-		}
+		w.w.WriteString(batch.GetControl().String() + "\n")
 		w.lineNum++
 	}
-	if _, err := w.w.WriteString(file.Control.String() + "\n"); err != nil {
-		return err
-	}
+	w.w.WriteString(file.Control.String() + "\n")
 	w.lineNum++
 
 	// pad the final block
 	for i := 0; i < (10-(w.lineNum%10)) && w.lineNum%10 != 0; i++ {
-		if _, err := w.w.WriteString(strings.Repeat("9", 94) + "\n"); err != nil {
-			return err
-		}
+		w.w.WriteString(strings.Repeat("9", 94) + "\n")
 	}
 
-	return nil
+	return w.w.Flush()
 }
 
 // Flush writes any buffered data to the underlying io.Writer.
@@ -82,22 +68,3 @@ func (w *Writer) Write(file *File) error {
 func (w *Writer) Flush() {
 	w.w.Flush()
 }
-
-// Error reports any error that has occurred during a previous Write or Flush.
-func (w *Writer) Error() error {
-	_, err := w.w.Write(nil)
-	return err
-}
-
-// WriteAll writes multiple ach.files to w using Write and then calls Flush.
-func (w *Writer) WriteAll(files []*File) error {
-	for _, file := range files {
-		err := w.Write(file)
-		// TODO if one of the files errors at a Writer struct flag to decide if
-		// the other files should still be written
-		if err != nil {
-			return err
-		}
-	}
-	return w.w.Flush()
-}


Test patch:
diff --git a/batchCCD_test.go b/batchCCD_test.go
index cd778aa2..604aa6c9 100644
--- a/batchCCD_test.go
+++ b/batchCCD_test.go
@@ -88,7 +88,7 @@ func TestBatchCCDAddendumCount(t *testing.T) {
 	testBatchCCDAddendumCount(t)
 }
 
-// BenchmarkBatchCCDAddendumCoun benchmarks batch control CCD can only have one addendum per entry detail
+// BenchmarkBatchCCDAddendumCount benchmarks batch control CCD can only have one addendum per entry detail
 func BenchmarkBatchCCDAddendumCount(b *testing.B) {
 	b.ReportAllocs()
 	for i := 0; i < b.N; i++ {
@@ -239,3 +239,28 @@ func BenchmarkBatchCCDCreate(b *testing.B) {
 		testBatchCCDCreate(b)
 	}
 }
+
+// testBatchCCDReceivingCompanyField validates CCDReceivingCompanyField
+// underlying IndividualName
+func testBatchCCDReceivingCompanyField(t testing.TB) {
+	mockBatch := mockBatchCCD()
+	ts := mockBatch.Entries[0].ReceivingCompanyField()
+	if ts != "Best Co. #23          " {
+		t.Error("Receiving Company Field is invalid")
+	}
+}
+
+// TestBatchCCDReceivingCompanyField tests validating CCDReceivingCompanyField
+// underlying IndividualName
+func TestBatchCCDReceivingCompanyFieldField(t *testing.T) {
+	testBatchCCDReceivingCompanyField(t)
+}
+
+// BenchmarkBatchCCDReceivingCompanyField benchmarks validating CCDReceivingCompanyField
+// underlying IndividualName
+func BenchmarkBatchCCDReceivingCompanyField(b *testing.B) {
+	b.ReportAllocs()
+	for i := 0; i < b.N; i++ {
+		testBatchCCDReceivingCompanyField(b)
+	}
+}
diff --git a/batchWEB_test.go b/batchWEB_test.go
index 1bb6c355..cf0fb946 100644
--- a/batchWEB_test.go
+++ b/batchWEB_test.go
@@ -214,3 +214,37 @@ func BenchmarkBatchWebCreate(b *testing.B) {
 		testBatchWebCreate(b)
 	}
 }
+
+// testBatchWebPaymentTypeR validates that the entry detail
+// payment type / discretionary data is reoccurring
+func testBatchWebPaymentTypeR(t testing.TB) {
+	mockBatch := mockBatchWEB()
+	mockBatch.GetEntries()[0].DiscretionaryData = "R"
+	if err := mockBatch.Validate(); err != nil {
+		if e, ok := err.(*BatchError); ok {
+			if e.FieldName != "PaymentType" {
+				t.Errorf("%T: %s", err, err)
+			}
+		} else {
+			t.Errorf("%T: %s", err, err)
+		}
+	}
+	if mockBatch.GetEntries()[0].PaymentTypeField() != "R" {
+		t.Errorf("PaymentTypeField %v was expecting R", mockBatch.GetEntries()[0].PaymentTypeField())
+	}
+}
+
+// TestBatchWebPaymentTypeR tests validating that the entry detail
+// payment type / discretionary data is reoccurring
+func TestBatchWebPaymentTypeR(t *testing.T) {
+	testBatchWebPaymentTypeR(t)
+}
+
+// BenchmarkBatchWebPaymentTypeR benchmarks validating that the entry detail
+// payment type / discretionary data is reoccurring
+func BenchmarkBatchWebPaymentTypeR(b *testing.B) {
+	b.ReportAllocs()
+	for i := 0; i < b.N; i++ {
+		testBatchWebPaymentTypeR(b)
+	}
+}
diff --git a/entryDetail_test.go b/entryDetail_test.go
index 96ffb8e6..40228666 100644
--- a/entryDetail_test.go
+++ b/entryDetail_test.go
@@ -606,6 +606,10 @@ func testEDCreditOrDebit(t testing.TB) {
 	if entry.CreditOrDebit() != "D" {
 		t.Errorf("TransactionCode %v expected a Debit(D) got %v", entry.TransactionCode, entry.CreditOrDebit())
 	}
+	entry.TransactionCode = 10
+	if entry.CreditOrDebit() != "" {
+		t.Errorf("TransactionCode %v is invalid", entry.TransactionCode)
+	}
 }
 
 // TestEDCreditOrDebit tests validating debit and credit transaction code
diff --git a/record_test.go b/record_test.go
index 393a50f3..5718983a 100644
--- a/record_test.go
+++ b/record_test.go
@@ -222,7 +222,7 @@ func testBuildFile(t testing.TB) {
 	// Finally we write the file to an io.Writer
 	var b bytes.Buffer
 	w := NewWriter(&b)
-	if err := w.WriteAll([]*File{file}); err != nil {
+	if err := w.Write(file); err != nil {
 		t.Errorf("%T: %s", err, err)
 	}
 	w.Flush()
diff --git a/test/ach-arc-write/main.go b/test/ach-arc-write/main.go
index a9a680da..08c03697 100644
--- a/test/ach-arc-write/main.go
+++ b/test/ach-arc-write/main.go
@@ -57,7 +57,7 @@ func main() {
 
 	// write the file to std out. Anything io.Writer
 	w := ach.NewWriter(os.Stdout)
-	if err := w.WriteAll([]*ach.File{file}); err != nil {
+	if err := w.Write(file); err != nil {
 		log.Fatalf("Unexpected error: %s\n", err)
 	}
 	w.Flush()
diff --git a/test/ach-boc-write/main.go b/test/ach-boc-write/main.go
index a8794521..c6085e42 100644
--- a/test/ach-boc-write/main.go
+++ b/test/ach-boc-write/main.go
@@ -57,7 +57,7 @@ func main() {
 
 	// write the file to std out. Anything io.Writer
 	w := ach.NewWriter(os.Stdout)
-	if err := w.WriteAll([]*ach.File{file}); err != nil {
+	if err := w.Write(file); err != nil {
 		log.Fatalf("Unexpected error: %s\n", err)
 	}
 	w.Flush()
diff --git a/test/ach-cie-write/main.go b/test/ach-cie-write/main.go
index 2be2b5fb..fc9f8ffc 100644
--- a/test/ach-cie-write/main.go
+++ b/test/ach-cie-write/main.go
@@ -65,7 +65,7 @@ func main() {
 
 	// write the file to std out. Anything io.Writer
 	w := ach.NewWriter(os.Stdout)
-	if err := w.WriteAll([]*ach.File{file}); err != nil {
+	if err := w.Write(file); err != nil {
 		log.Fatalf("Unexpected error: %s\n", err)
 	}
 	w.Flush()
diff --git a/test/ach-pop-write/main.go b/test/ach-pop-write/main.go
index 654f77d7..fa5f63b7 100644
--- a/test/ach-pop-write/main.go
+++ b/test/ach-pop-write/main.go
@@ -59,7 +59,7 @@ func main() {
 
 	// write the file to std out. Anything io.Writer
 	w := ach.NewWriter(os.Stdout)
-	if err := w.WriteAll([]*ach.File{file}); err != nil {
+	if err := w.Write(file); err != nil {
 		log.Fatalf("Unexpected error: %s\n", err)
 	}
 	w.Flush()
diff --git a/test/ach-pos-write/main.go b/test/ach-pos-write/main.go
index 51b00478..c792a8b9 100644
--- a/test/ach-pos-write/main.go
+++ b/test/ach-pos-write/main.go
@@ -72,7 +72,7 @@ func main() {
 
 	// write the file to std out. Anything io.Writer
 	w := ach.NewWriter(os.Stdout)
-	if err := w.WriteAll([]*ach.File{file}); err != nil {
+	if err := w.Write(file); err != nil {
 		log.Fatalf("Unexpected error: %s\n", err)
 	}
 	w.Flush()
diff --git a/test/ach-ppd-write/main.go b/test/ach-ppd-write/main.go
index a320bfea..411a9ca2 100644
--- a/test/ach-ppd-write/main.go
+++ b/test/ach-ppd-write/main.go
@@ -59,7 +59,7 @@ func main() {
 
 	// write the file to std out. Anything io.Writer
 	w := ach.NewWriter(os.Stdout)
-	if err := w.WriteAll([]*ach.File{file}); err != nil {
+	if err := w.Write(file); err != nil {
 		log.Fatalf("Unexpected error: %s\n", err)
 	}
 	w.Flush()
diff --git a/test/ach-rck-write/main.go b/test/ach-rck-write/main.go
index fa81c70f..adf2bcbe 100644
--- a/test/ach-rck-write/main.go
+++ b/test/ach-rck-write/main.go
@@ -57,7 +57,7 @@ func main() {
 
 	// write the file to std out. Anything io.Writer
 	w := ach.NewWriter(os.Stdout)
-	if err := w.WriteAll([]*ach.File{file}); err != nil {
+	if err := w.Write(file); err != nil {
 		log.Fatalf("Unexpected error: %s\n", err)
 	}
 	w.Flush()
diff --git a/test/ach-shr-write/main.go b/test/ach-shr-write/main.go
index 2e42bc9a..4451b968 100644
--- a/test/ach-shr-write/main.go
+++ b/test/ach-shr-write/main.go
@@ -74,7 +74,7 @@ func main() {
 
 	// write the file to std out. Anything io.Writer
 	w := ach.NewWriter(os.Stdout)
-	if err := w.WriteAll([]*ach.File{file}); err != nil {
+	if err := w.Write(file); err != nil {
 		log.Fatalf("Unexpected error: %s\n", err)
 	}
 	w.Flush()
diff --git a/writer_test.go b/writer_test.go
index fd08657e..2de2e945 100644
--- a/writer_test.go
+++ b/writer_test.go
@@ -31,9 +31,10 @@ func testPPDWrite(t testing.TB) {
 	b := &bytes.Buffer{}
 	f := NewWriter(b)
 
-	if err := f.WriteAll([]*File{file}); err != nil {
+	if err := f.Write(file); err != nil {
 		t.Errorf("%T: %s", err, err)
 	}
+
 	r := NewReader(strings.NewReader(b.String()))
 	_, err := r.Read()
 	if err != nil {
@@ -80,7 +81,7 @@ func testFileWriteErr(t testing.TB) {
 	b := &bytes.Buffer{}
 	f := NewWriter(b)
 
-	if err := f.WriteAll([]*File{file}); err != nil {
+	if err := f.Write(file); err != nil {
 		if e, ok := err.(*FileError); ok {
 			if e.FieldName != "EntryAddendaCount" {
 				t.Errorf("%T: %s", err, err)