forked from moov-io/ach
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddenda_internal_test.go
147 lines (133 loc) · 4.19 KB
/
addenda_internal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2017 The ACH Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package ach
import (
"strings"
"testing"
)
func mockAddenda() Addenda {
addenda := NewAddenda()
addenda.EntryDetailSequenceNumber = 1234567
return addenda
}
func TestMockAddenda(t *testing.T) {
addenda := mockAddenda()
if err := addenda.Validate(); err != nil {
t.Error("mockAddenda does not validate and will break other tests")
}
if addenda.EntryDetailSequenceNumber != 1234567 {
t.Error("EntryDetailSequenceNumber dependent default value has changed")
}
}
func TestParseAddenda(t *testing.T) {
var line = "710WEB DIEGO MAY 00010000001"
r := NewReader(strings.NewReader(line))
r.addCurrentBatch(NewBatchPPD())
r.currentBatch.GetHeader().StandardEntryClassCode = "PPD"
r.currentBatch.AddEntry(&EntryDetail{TransactionCode: 22, AddendaRecordIndicator: 1})
r.line = line
err := r.parseAddenda()
if err != nil {
t.Errorf("%T: %s", err, err)
}
record := r.currentBatch.GetEntries()[0].Addendum[0]
if record.recordType != "7" {
t.Errorf("RecordType Expected '7' got: %v", record.recordType)
}
if record.TypeCode != "10" {
t.Errorf("TypeCode Expected 10 got: %v", record.TypeCode)
}
if record.PaymentRelatedInformationField() != "WEB DIEGO MAY " {
t.Errorf("PaymentRelatedInformation Expected 'WEB DIEGO MAY ' got: %v", record.PaymentRelatedInformationField())
}
if record.SequenceNumberField() != "0001" {
t.Errorf("SequenceNumber Expected '0001' got: %v", record.SequenceNumberField())
}
if record.EntryDetailSequenceNumberField() != "0000001" {
t.Errorf("EntryDetailSequenceNumber Expected '0000001' got: %v", record.EntryDetailSequenceNumberField())
}
}
// TestAddendaString validats that a known parsed file can be return to a string of the same value
func TestAddendaString(t *testing.T) {
var line = "710WEB DIEGO MAY 00010000001"
r := NewReader(strings.NewReader(line))
r.addCurrentBatch(NewBatchPPD())
r.currentBatch.GetHeader().StandardEntryClassCode = "PPD"
r.currentBatch.AddEntry(&EntryDetail{AddendaRecordIndicator: 1})
r.line = line
err := r.parseAddenda()
if err != nil {
t.Errorf("%T: %s", err, err)
}
record := r.currentBatch.GetEntries()[0].Addendum[0]
if record.String() != line {
t.Errorf("Strings do not match")
}
}
func TestValidateAddendaRecordType(t *testing.T) {
addenda := mockAddenda()
addenda.recordType = "2"
if err := addenda.Validate(); err != nil {
if e, ok := err.(*FieldError); ok {
if e.FieldName != "recordType" {
t.Errorf("%T: %s", err, err)
}
}
}
}
func TestValidateAddendaTypeCode(t *testing.T) {
addenda := mockAddenda()
addenda.TypeCode = "23"
if err := addenda.Validate(); err != nil {
if e, ok := err.(*FieldError); ok {
if e.FieldName != "TypeCode" {
t.Errorf("%T: %s", err, err)
}
}
}
}
func TestAddendaFieldInclusion(t *testing.T) {
addenda := mockAddenda()
addenda.EntryDetailSequenceNumber = 0
if err := addenda.Validate(); err != nil {
if e, ok := err.(*FieldError); ok {
if e.FieldName != "EntryDetailSequenceNumber" {
t.Errorf("%T: %s", err, err)
}
}
}
}
func TestAddendaFieldInclusionRecordType(t *testing.T) {
addenda := mockAddenda()
addenda.recordType = ""
if err := addenda.Validate(); err != nil {
if e, ok := err.(*FieldError); ok {
if e.Msg != msgFieldInclusion {
t.Errorf("%T: %s", err, err)
}
}
}
}
func TestAddendaPaymentRelatedInformationAlphaNumeric(t *testing.T) {
addenda := mockAddenda()
addenda.PaymentRelatedInformation = "®©"
if err := addenda.Validate(); err != nil {
if e, ok := err.(*FieldError); ok {
if e.FieldName != "PaymentRelatedInformation" {
t.Errorf("%T: %s", err, err)
}
}
}
}
func TestAddendaTyeCodeNil(t *testing.T) {
addenda := mockAddenda()
addenda.TypeCode = ""
if err := addenda.Validate(); err != nil {
if e, ok := err.(*FieldError); ok {
if e.FieldName != "TypeCode" {
t.Errorf("%T: %s", err, err)
}
}
}
}