forked from moov-io/watchman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader_test.go
304 lines (253 loc) · 7.31 KB
/
reader_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright 2018 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package ofac
import (
"encoding/json"
"os"
"strings"
"testing"
)
// TestAddressCSVFileRead validates reading an OFAC Address CSV File
func TestAddressCSVFileRead(t *testing.T) {
r := Reader{}
r.FileName = "test/testdata/add.csv"
if err := r.Read(); err != nil {
t.Errorf("%T: %s", err, err)
}
}
// TestAlternateIDCSVFileRead validates reading an OFAC Alternate ID CSV File
func TestAlternateIDCSVFileRead(t *testing.T) {
r := Reader{}
r.FileName = "test/testdata/alt.csv"
if err := r.Read(); err != nil {
t.Errorf("%T: %s", err, err)
}
}
// TestSDNCSVFileRead validates reading an OFAC Specially Designated National CSV File
func TestSDNCSVFileRead(t *testing.T) {
r := Reader{}
r.FileName = "test/testdata/sdn.csv"
if err := r.Read(); err != nil {
t.Errorf("%T: %s", err, err)
}
}
// TestSDNCommentsCSVFileRead validates reading an OFAC Specially Designated National Comments CSV File
func TestSDNCommentsCSVFileRead(t *testing.T) {
r := Reader{}
r.FileName = "test/testdata/sdn_comments.csv"
if err := r.Read(); err != nil {
t.Errorf("%T: %s", err, err)
}
}
// TestDPLTXTFileRead validates reading a BIS Denied Persons List file
func TestDPLTXTFileRead(t *testing.T) {
r := Reader{}
r.FileName = "test/testdata/dpl.txt"
if err := r.Read(); err != nil {
t.Errorf("%T: %s", err, err)
}
}
func TestConsolidatedListCSVFileRead(t *testing.T) {
r := Reader{}
r.FileName = "test/testdata/csl.csv"
if err := r.Read(); err != nil {
t.Errorf("%T: %s", err, err)
}
}
// TestInvalidFileExtension validates the file extension is csv
func TestInvalidFileExtension(t *testing.T) {
r := Reader{}
r.FileName = "test/testdata/add.csb"
err := r.Read()
if !strings.Contains(err.Error(), "file type") {
t.Errorf("%T: %s", err, err)
}
}
// TestInvalidFileName validates the file name is valid
func TestInvalidFileName(t *testing.T) {
r := Reader{}
r.FileName = "test/testdata/xyz.csv"
err := r.Read()
if !strings.Contains(err.Error(), "file name") {
t.Errorf("%T: %s", err, err)
}
}
// TestSDNCSVFileHit validates reading an OFAC Specially Designated National CSV File Hit
func TestSDNCSVFileHit(t *testing.T) {
r := Reader{}
r.FileName = "test/testdata/sdn.csv"
err := r.Read()
if err != nil {
t.Errorf("%T: %s", err, err)
}
hit := false
for _, sdn := range r.SDNs {
if sdn.SDNName == "HAWATMA, Nayif" {
hit = true
}
}
if !hit {
t.Errorf("%s", "the check missed a specially designated name")
}
}
// TestDPLTXTFileHit validates reading a BIS Denied Person TXT File Hit
func TestDPLTXTFileHit(t *testing.T) {
r := Reader{}
r.FileName = "test/testdata/dpl.txt"
err := r.Read()
if err != nil {
t.Errorf("%T: %s", err, err)
}
hit := false
for _, sdn := range r.DeniedPersons {
if sdn.Name == "HAYDEE HERRERA" {
hit = true
}
}
if !hit {
t.Error("the check missed a denied person")
}
}
// TestSDNCSVFileJSON tests reading an OFAC Specially Designated National CSV File and formatting to JSON
func TestSDNCSVFileJSON(t *testing.T) {
r := Reader{}
r.FileName = "test/testdata/sdn.csv"
if err := r.Read(); err != nil {
t.Errorf("%T: %s", err, err)
}
if _, err := json.Marshal(r.SDNs); err != nil {
t.Errorf("%T: %s", err, err)
}
}
// TestDPLTXTFileJSON tests reading a BIS Denied Persons TXT File and formatting to JSON
func TestDPLTXTFileJSON(t *testing.T) {
r := Reader{}
r.FileName = "test/testdata/dpl.txt"
if err := r.Read(); err != nil {
t.Errorf("%T: %s", err, err)
}
if _, err := json.Marshal(r.DeniedPersons); err != nil {
t.Errorf("%T: %s", err, err)
}
}
func TestCSLCSVFileJSON(t *testing.T) {
r := Reader{FileName: "test/testdata/csl.csv"}
if err := r.Read(); err != nil {
t.Errorf("%T: %s", err, err)
}
if _, err := json.Marshal(r.SectoralSanctions); err != nil {
t.Errorf("%T: %s", err, err)
}
}
// TestInvalidAddressCSVFile validates validates an invalid Address CSV File returns an error
func TestInvalidAddressCSVFile(t *testing.T) {
r := Reader{}
r.FileName = "invalid/add.csv"
if err := r.Read(); err != nil {
if _, ok := err.(*os.PathError); ok {
} else {
t.Errorf("%T: %s", err, err)
}
}
}
// TestInvalidAlternateIDCSVFile validates validates an invalid Alternate ID CSV File returns an error
func TestInvalidAlternateIDCSVFile(t *testing.T) {
r := Reader{}
r.FileName = "invalid/alt.csv"
if err := r.Read(); err != nil {
if _, ok := err.(*os.PathError); ok {
} else {
t.Errorf("%T: %s", err, err)
}
}
}
// TestInvalidSDNCSVFile validates an invalid SDN CSV File returns an error
func TestInvalidSDNCSVFile(t *testing.T) {
r := Reader{}
r.FileName = "invalid/sdn.csv"
if err := r.Read(); err != nil {
if _, ok := err.(*os.PathError); ok {
} else {
t.Errorf("%T: %s", err, err)
}
}
}
// TestInvalidSDNCommentsCSVFile validates an invalid SDNComments CSV File returns an error
func TestInvalidSDNCommentsCSVFile(t *testing.T) {
r := Reader{}
r.FileName = "invalid/sdn_comments.csv"
if err := r.Read(); err != nil {
if _, ok := err.(*os.PathError); ok {
} else {
t.Errorf("%T: %s", err, err)
}
}
}
// TestAddressCSVInvalidLineRead validates an error is returned when reading an OFAC Specially Designated National
// Address CSV File with an invalid line
func TestAddressCSVInvalidLineRead(t *testing.T) {
r := Reader{}
r.FileName = "test/testdata/invalidFiles/add.csv"
if err := r.Read(); err != nil {
if !strings.Contains(err.Error(), "wrong number of fields") {
t.Errorf("%T: %s", err, err)
}
}
}
// TestAlternateIDCSVInvalidLineRead validates an error is returned when reading an OFAC Specially Designated National
// Alternate ID CSV File with an invalid line
func TestAlternateIDCSVInvalidLineRead(t *testing.T) {
r := Reader{}
r.FileName = "test/testdata/invalidFiles/alt.csv"
if err := r.Read(); err != nil {
if !strings.Contains(err.Error(), "wrong number of fields") {
t.Errorf("%T: %s", err, err)
}
}
}
// TestSDNCSVInvalidLineRead validates an error is returned when reading an OFAC Specially Designated National
// CSV File with an invalid line
func TestSDNCSVInvalidLineRead(t *testing.T) {
r := Reader{}
r.FileName = "test/testdata/invalidFiles/sdn.csv"
if err := r.Read(); err != nil {
if !strings.Contains(err.Error(), "wrong number of fields") {
t.Errorf("%T: %s", err, err)
}
}
}
// TestSDNCommentsCSVInvalidLineRead validates an error is returned when reading an OFAC Specially Designated National
// Comments CSV File with an invalid line
func TestSDNCommentsCSVInvalidLineRead(t *testing.T) {
r := Reader{}
r.FileName = "test/testdata/invalidFiles/sdn_comments.csv"
if err := r.Read(); err != nil {
if !strings.Contains(err.Error(), "wrong number of fields") {
t.Errorf("%T: %s", err, err)
}
}
}
// TestParseError validates parseError
func TestParseError(t *testing.T) {
r := Reader{}
r.FileName = "test/testdata/sdn_comments.csv"
err := r.Read()
if e := r.parseError(err); e != nil {
t.Errorf("%T: %s", e, e)
}
}
func TestReplaceNull(t *testing.T) {
ans := replaceNull(nil)
if ans != nil {
t.Errorf("Got %v", ans)
}
ans = replaceNull([]string{" -0-"})
if len(ans) != 1 || ans[0] != "" {
t.Errorf("Got %v", ans)
}
ans = replaceNull([]string{"foo", " -0-"})
if len(ans) != 2 || ans[0] != "foo" || ans[1] != "" {
t.Errorf("Got %v", ans)
}
}