-
Notifications
You must be signed in to change notification settings - Fork 4
/
iif_test.go
395 lines (357 loc) · 8.75 KB
/
iif_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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package iif
import (
"io/ioutil"
"testing"
"github.com/stretchr/testify/assert"
)
func TestExport(t *testing.T) {
data := []DataLine{
accntData{
Name: "Accounts",
AccntType: "Payable",
Desc: "AP",
Accnum: "2000",
},
vendData{
Name: "Vendor",
Refnum: "1",
PrintAs: "",
Addr1: "Jon Vendor",
Addr2: "555",
Addr3: "Street St",
Addr4: `"Anywhere, AZ 85730"`,
Addr5: "USA",
Cont1: "Jon Vendor",
Phone1: "5555555555",
FirstName: "Jon",
LastName: "Vendor",
},
trnsData{
TrnsID: "",
TrnsType: "BILLPMT",
Date: "7/16/1998",
Accnt: "Checking",
Name: "Vendor",
Amount: "-35",
Docnum: "",
Memo: "Test Memo",
Clear: "N",
ToPrint: "Y",
},
trnsData{
TrnsID: "",
TrnsType: "BILLPMT",
Date: "7/16/1999",
Accnt: "Checking",
Name: "Joe",
Amount: "-35",
Docnum: "",
Memo: "Test Memo",
Clear: "N",
ToPrint: "Y",
},
splData{
SplID: "",
TrnsType: "DEPOSIT",
Date: "7/1/1998",
Accnt: "Income",
Name: "Customer",
Class: "",
Amount: "-10000",
Docnum: "",
Memo: "",
Clear: "N",
},
}
err := Export(data, "example")
assert.NoError(t, err)
dat, err := ioutil.ReadFile("example.iif")
assert.NoError(t, err)
expected := "!ACCNT\tNAME\tACCNTTYPE\tDESC\tACCNUM\tEXTRA\nACCNT\tAccounts\tPayable\tAP\t2000\t\n!VEND\tNAME\tREFNUM\tPRINTAS\tADDR1\tADDR2\tADDR3\tADDR4\tADDR5\tVTYPE\tCONT1\tCONT2\tPHONE1\tPHONE2\tFAXNUM\tEMAIL\tNOTE\tTAXID\tLIMIT\tTERMS\tNOTEPAD\tSALUTATION\tCOMPANYNAME\tFIRSTNAME\tMIDINIT\tLASTNAME\nVEND\tVendor\t1\t\tJon Vendor\t555\tStreet St\t\"Anywhere, AZ 85730\"\tUSA\t\tJon Vendor\t\t5555555555\t\t\t\t\t\t\t\t\t\t\tJon\t\tVendor\n!TRNS\tTRNSID\tTRNSTYPE\tDATE\tACCNT\tNAME\tAMOUNT\tDOCNUM\tMEMO\tCLEAR\tTOPRINT\n!SPL\tSPLID\tTRNSTYPE\tDATE\tACCNT\tNAME\tCLASS\tAMOUNT\tDOCNUM\tMEMP\tCLEAR\n!ENDTRNS\nTRNS\t\tBILLPMT\t7/16/1998\tChecking\tVendor\t-35\t\tTest Memo\tN\tY\nTRNS\t\tBILLPMT\t7/16/1999\tChecking\tJoe\t-35\t\tTest Memo\tN\tY\nSPL\t\tDEPOSIT\t7/1/1998\tIncome\tCustomer\t\t-10000\t\t\tN\nENDTRNS"
assert.Equal(t, expected, string(dat))
}
func TestSorting(t *testing.T) {
var wrapper = []Wrapper{
{
Type: Vend,
Header: "test_header",
Line: "test_line",
},
{
Type: Accnt,
Header: "test_header",
Line: "test_line",
},
{
Type: Trns,
Header: "test_header",
Line: "test_line",
},
{
Type: Class,
Header: "test_header",
Line: "test_line",
},
}
wrapper = sorting(wrapper)
assert.Equal(t, Accnt, wrapper[0].Type)
assert.Equal(t, Class, wrapper[1].Type)
assert.Equal(t, Vend, wrapper[2].Type)
assert.Equal(t, Trns, wrapper[3].Type)
}
func TestGrouping(t *testing.T) {
var wrapper = []Wrapper{
{
Type: Vend,
Header: "test_header",
Line: "test_line",
},
{
Type: Accnt,
Header: "test_header",
Line: "test_line",
},
{
Type: Trns,
Header: "test_header",
Line: "test_line",
},
{
Type: Trns,
Header: "test_header_2",
Line: "test_line_2",
},
{
Type: Class,
Header: "test_header",
Line: "test_line",
},
}
wrapper = sorting(wrapper)
gw := grouping(wrapper)
assert.Equal(t, Accnt, gw[0].Type)
assert.Equal(t, Trns, gw[3].Type)
assert.Equal(t, "test_line", gw[3].Lines[0])
assert.Equal(t, "test_line_2", gw[3].Lines[1])
}
func TestBuild(t *testing.T) {
var wrapper = []Wrapper{
{
Type: Vend,
Header: "test_header",
Line: "test_line",
},
{
Type: Accnt,
Header: "test_header",
Line: "test_line",
},
{
Type: Trns,
Header: "test_header",
Line: "test_line",
},
{
Type: Trns,
Header: "test_header_2",
Line: "test_line_2",
},
{
Type: Class,
Header: "test_header",
Line: "test_line",
},
}
wrapper = sorting(wrapper)
gw := grouping(wrapper)
result := build(gw)
var expected = "test_header\ntest_line\ntest_header\ntest_line\ntest_header\ntest_line\ntest_header\n!ENDTRNS\ntest_line\ntest_line_2\nENDTRNS"
assert.Equal(t, expected, string(result))
}
func TestGetFilename(t *testing.T) {
type testCase struct {
Value string
Expected string
}
cases := []testCase{
{
Value: "test",
Expected: "test.iif",
},
{
Value: "test.iif",
Expected: "test.iif",
},
{
Value: "test.ii",
Expected: "test.iif",
},
}
for _, v := range cases {
assert.Equal(t, v.Expected, getFilename(v.Value))
}
}
func TestGetHeader(t *testing.T) {
vend := vendData{
Name: "Vendor",
Refnum: "1",
PrintAs: "",
Addr1: "Jon Vendor",
Addr2: "555",
Addr3: "Street St",
Addr4: `"Anywhere, AZ 85730"`,
Addr5: "USA",
Cont1: "Jon Vendor",
Phone1: "5555555555",
FirstName: "Jon",
LastName: "Vendor",
}
header, err := getHeader(vend)
assert.NoError(t, err)
expected := "!VEND\tNAME\tREFNUM\tPRINTAS\tADDR1\tADDR2\tADDR3\tADDR4\tADDR5\tVTYPE\tCONT1\tCONT2\tPHONE1\tPHONE2\tFAXNUM\tEMAIL\tNOTE\tTAXID\tLIMIT\tTERMS\tNOTEPAD\tSALUTATION\tCOMPANYNAME\tFIRSTNAME\tMIDINIT\tLASTNAME"
assert.Equal(t, expected, header)
}
func TestDataLineToString(t *testing.T) {
vend := vendData{
Name: "Vendor",
Refnum: "1",
PrintAs: "",
Addr1: "Jon Vendor",
Addr2: "555",
Addr3: "Street St",
Addr4: `Anywhere, AZ 85730`,
Addr5: "USA",
Cont1: "Jon Vendor",
Phone1: "5555555555",
FirstName: "Jon",
LastName: "Vendor",
}
result, err := dataLineToString(vend)
assert.NoError(t, err)
expected := "VEND\tVendor\t1\t\tJon Vendor\t555\tStreet St\tAnywhere, AZ 85730\tUSA\t\tJon Vendor\t\t5555555555\t\t\t\t\t\t\t\t\t\t\tJon\t\tVendor"
assert.Equal(t, expected, result)
}
func TestGetEndTrns(t *testing.T) {
endtrns := getEndTrns(true)
assert.Equal(t, "!"+endTrns, endtrns)
endtrns = getEndTrns(false)
assert.Equal(t, endTrns, endtrns)
}
func TestOrderLocation(t *testing.T) {
loc := orderOfTypes.Location(Accnt)
assert.Equal(t, loc, 0)
loc = orderOfTypes.Location(Trns)
assert.Equal(t, loc, 5)
}
type accntData struct {
Name string `iif:"NAME"`
AccntType string `iif:"ACCNTTYPE"`
Desc string `iif:"DESC"`
Accnum string `iif:"ACCNUM"`
Extra string `iif:"EXTRA"`
}
func (a accntData) GetType() Type {
return Accnt
}
type vendData struct {
Name string `iif:"NAME"`
Refnum string `iif:"REFNUM"`
PrintAs string `iif:"PRINTAS"`
Addr1 string `iif:"ADDR1"`
Addr2 string `iif:"ADDR2"`
Addr3 string `iif:"ADDR3"`
Addr4 string `iif:"ADDR4"`
Addr5 string `iif:"ADDR5"`
Vtype string `iif:"VTYPE"`
Cont1 string `iif:"CONT1"`
Cont2 string `iif:"CONT2"`
Phone1 string `iif:"PHONE1"`
Phone2 string `iif:"PHONE2"`
Faxnum string `iif:"FAXNUM"`
Email string `iif:"EMAIL"`
Note string `iif:"NOTE"`
TaxID string `iif:"TAXID"`
Limit string `iif:"LIMIT"`
Terms string `iif:"TERMS"`
Notepad string `iif:"NOTEPAD"`
Salutation string `iif:"SALUTATION"`
CompanyName string `iif:"COMPANYNAME"`
FirstName string `iif:"FIRSTNAME"`
Midinit string `iif:"MIDINIT"`
LastName string `iif:"LASTNAME"`
}
func (v vendData) GetType() Type {
return Vend
}
type trnsData struct {
TrnsID string `iif:"TRNSID"`
TrnsType string `iif:"TRNSTYPE"`
Date string `iif:"DATE"`
Accnt string `iif:"ACCNT"`
Name string `iif:"NAME"`
Amount string `iif:"AMOUNT"`
Docnum string `iif:"DOCNUM"`
Memo string `iif:"MEMO"`
Clear string `iif:"CLEAR"`
ToPrint string `iif:"TOPRINT"`
}
func (t trnsData) GetType() Type {
return Trns
}
type splData struct {
SplID string `iif:"SPLID"`
TrnsType string `iif:"TRNSTYPE"`
Date string `iif:"DATE"`
Accnt string `iif:"ACCNT"`
Name string `iif:"NAME"`
Class string `iif:"CLASS"`
Amount string `iif:"AMOUNT"`
Docnum string `iif:"DOCNUM"`
Memo string `iif:"MEMP"`
Clear string `iif:"CLEAR"`
}
func (s splData) GetType() Type {
return Spl
}
func BenchmarkExport(b *testing.B) {
data := []DataLine{
accntData{
Name: "Accounts",
AccntType: "Payable",
Desc: "AP",
Accnum: "2000",
},
vendData{
Name: "Vendor",
Refnum: "1",
PrintAs: "",
Addr1: "Jon Vendor",
Addr2: "555",
Addr3: "Street St",
Addr4: `"Anywhere, AZ 85730"`,
Addr5: "USA",
Cont1: "Jon Vendor",
Phone1: "5555555555",
FirstName: "Jon",
LastName: "Vendor",
},
trnsData{
TrnsID: "",
TrnsType: "BILLPMT",
Date: "7/16/1998",
Accnt: "Checking",
Name: "Vendor",
Amount: "-35",
Docnum: "",
Memo: "Test Memo",
Clear: "N",
ToPrint: "Y",
},
}
for i := 0; i < b.N; i++ {
err := Export(data, "example")
if err != nil {
panic(err)
}
}
}