-
Notifications
You must be signed in to change notification settings - Fork 28
/
reader_test.go
604 lines (525 loc) · 14.3 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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
package dbf
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
var testDbf *DBF
var usingFile bool
// use testmain to run all the tests twice
// one time with a file opened from disk and one time with a stream
func TestMain(m *testing.M) {
fmt.Println("Running tests with file from disk...")
usingFile = true
testOpenFile()
result := m.Run()
testDbf.Close()
if result != 0 {
os.Exit(result)
}
fmt.Println("Running tests with byte stream...")
usingFile = false
testOpenStream()
result = m.Run()
os.Exit(result)
}
func testOpenFile() {
var err error
testDbf, err = OpenFile(filepath.Join("testdata", "TEST.DBF"), new(Win1250Decoder))
if err != nil {
log.Fatal(err)
}
}
func testOpenStream() {
dbfbytes, err := ioutil.ReadFile(filepath.Join("testdata", "TEST.DBF"))
if err != nil {
log.Fatal(err)
}
dbfreader := bytes.NewReader(dbfbytes)
fptbytes, err := ioutil.ReadFile(filepath.Join("testdata", "TEST.FPT"))
if err != nil {
log.Fatal(err)
}
fptreader := bytes.NewReader(fptbytes)
testDbf, err = OpenStream(dbfreader, fptreader, new(Win1250Decoder))
if err != nil {
log.Fatal(err)
}
}
// Quick check if the first field matches
func TestFieldHeader(t *testing.T) {
want := "{Name:[73 68 0 0 0 0 0 0 0 0 0] Type:73 Pos:1 Len:4 Decimals:0 Flags:0 Next:5 Step:1 Reserved:[0 0 0 0 0 0 0 78]}"
have := fmt.Sprintf("%+v", testDbf.fields[0])
if have != want {
t.Errorf("First field from header does not match signature: Want %s, have %s", want, have)
}
}
// Test if file stat size matches header file size, only run when using file mode
func TestStatAndFileSize(t *testing.T) {
if !usingFile {
t.Skip("Stat and FileSize not testing when using stream")
}
stat, err := testDbf.Stat()
if err != nil {
t.Fatal(err)
}
statSize := stat.Size()
hdrSize := testDbf.header.FileSize()
if statSize != hdrSize {
t.Errorf("Calculated header size: %d, stat size: %d", hdrSize, statSize)
}
stat, err = testDbf.StatFPT()
if err != nil {
t.Fatal(err)
}
fptbytes, err := ioutil.ReadFile(filepath.Join("testdata", "TEST.FPT"))
if err != nil {
log.Fatal(err)
}
if stat.Size() != int64(len(fptbytes)) {
t.Errorf("Real FPT size: %d, stat size: %d", len(fptbytes), stat.Size())
}
if testDbf.NumRecords() != uint32(4) {
t.Errorf("Want 4 records, have %d", testDbf.NumRecords())
}
if len(testDbf.Fields()) != 13 {
t.Errorf("Want 13 fields, have %d", len(testDbf.Fields()))
}
// Test modified date, because we use time.Local to represent the modified date it can change depending on the system we run
modified := testDbf.Header().Modified().UTC()
if modified.Format("2006-01") != "2015-09" || modified.Day() < 14 || modified.Day() > 16 {
t.Errorf("Want modified date between 2015-09-14 and 2015-09-16, have %s", modified.Format("2006-01-02"))
}
}
// Tests if field headers have been parsed, fails if there are no fields
func TestFieldNames(t *testing.T) {
fieldnames := testDbf.FieldNames()
want := 13
if len(fieldnames) != want {
t.Errorf("Expected %d fields, have %d", want, len(fieldnames))
}
// t.Log(fieldnames)
}
func TestNumFields(t *testing.T) {
header := testDbf.NumFields()
headerCalc := testDbf.Header().NumFields()
if header != headerCalc {
t.Errorf("NumFields not equal. DBF NumFields: %d, DBF Header NumField: %d", header, headerCalc)
}
}
func TestGoTo(t *testing.T) {
err := testDbf.GoTo(0)
if err != nil {
t.Error(err)
}
if !testDbf.BOF() {
t.Error("Expected to be at BOF")
}
err = testDbf.GoTo(1)
if err != nil {
t.Error(err)
}
if testDbf.EOF() {
t.Error("Did not expect to be at EOF")
}
err = testDbf.GoTo(4)
if err != nil {
if err != ErrEOF {
t.Error(err)
}
}
if !testDbf.EOF() {
t.Error("Expected to be at EOF")
}
}
func TestSkip(t *testing.T) {
testDbf.GoTo(0)
err := testDbf.Skip(1)
if err != nil {
t.Error(err)
}
if testDbf.EOF() {
t.Error("Did not expect to be at EOF")
}
err = testDbf.Skip(3)
if err != nil {
if err != ErrEOF {
t.Error(err)
}
}
if !testDbf.EOF() {
t.Error("Expected to be at EOF")
}
err = testDbf.Skip(-20)
if err != nil {
if err != ErrBOF {
t.Error(err)
}
}
if !testDbf.BOF() {
t.Error("Expected to be at BOF")
}
}
var wantValues = []struct {
pos int
name, strval, strtype string
}{
{0, "ID", "2", "int32"},
{10, "NUMBER", "1.2345678999e+08", "float64"},
{12, "BOOL", "true", "bool"},
{2, "DATUM", "2015-02-03 00:00:00 +0000 UTC", "time.Time"},
{7, "COMP_NAME", "TEST2", "string"},
}
func TestFieldPos(t *testing.T) {
for _, want := range wantValues {
pos := testDbf.FieldPos(want.name)
if pos != want.pos {
t.Errorf("Wanted fieldpos %d for field %s, have pos %d", want.pos, want.name, pos)
}
}
}
// Tests a complete record read, reads the second record which is also deleted,
// also tests getting field values from record object
func TestRecord(t *testing.T) {
err := testDbf.GoTo(1)
if err != nil {
t.Fatal(err)
}
// test if the record is deleted
deleted, err := testDbf.Deleted()
if err != nil {
t.Fatal(err)
}
if !deleted {
t.Fatal("Record should be deleted")
}
// read the same record using Record() and RecordAt()
recs := [2]*Record{}
recs[0], err = testDbf.Record()
if err != nil {
t.Fatal(err)
}
recs[1], err = testDbf.RecordAt(1)
if err != nil {
t.Fatal(err)
}
for irec, rec := range recs {
for _, want := range wantValues {
val, err := rec.Field(want.pos)
if err != nil {
t.Error(err)
}
strval := strings.TrimSpace(fmt.Sprintf("%v", val))
strtype := fmt.Sprintf("%T", val)
if want.strval != strval || want.strtype != strtype {
t.Errorf("Record %d: Wanted value %s with type %s, have value %s with type %s", irec, want.strval, want.strtype, strval, strtype)
}
}
}
}
// Test reading fields field by field
func TestField(t *testing.T) {
for _, want := range wantValues {
val, err := testDbf.Field(want.pos)
if err != nil {
t.Error(err)
}
strval := strings.TrimSpace(fmt.Sprintf("%v", val))
strtype := fmt.Sprintf("%T", val)
if want.strval != strval || want.strtype != strtype {
t.Errorf("Wanted value %s with type %s, have value %s with type %s", want.strval, want.strtype, strval, strtype)
}
}
}
func TestRecordToJson(t *testing.T) {
// below go 1.8 we want want1, for 1.8 and up we want want12
want1 := `{"BOOL":true,"COMP_NAME":"TEST2","COMP_OS":"Windows XP","DATUM":"2015-02-03T00:00:00Z","FLOAT":1.23456789e+08,"ID":2,"ID_NR":6425886,"MELDING":"Tësting wíth éncôdings!","NIVEAU":1,"NUMBER":1.2345678999e+08,"SOORT":12345678,"TIJD":"12:00","USERNR":-600}`
want12 := `{"BOOL":true,"COMP_NAME":"TEST2","COMP_OS":"Windows XP","DATUM":"2015-02-03T00:00:00Z","FLOAT":123456789,"ID":2,"ID_NR":6425886,"MELDING":"Tësting wíth éncôdings!","NIVEAU":1,"NUMBER":123456789.99,"SOORT":12345678,"TIJD":"12:00","USERNR":-600}`
want2 := `{"BOOL":true,"COMP_NAME":" ","COMP_OS":" ","DATUM":"0001-01-01T00:00:00Z","FLOAT":0,"ID":4,"ID_NR":0,"MELDING":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==","NIVEAU":0,"NUMBER":0,"SOORT":0,"TIJD":" ","USERNR":0}`
err := testDbf.GoTo(3)
if err != nil {
t.Error(err)
}
data, err := testDbf.RecordToJSON(1, true)
if err != nil {
t.Error(err)
}
if string(data) != want1 && string(data) != want12 {
t.Errorf("\nWanted json\n%s\nor%s\nhave json\n%s\n", want1, want2, string(data))
}
err = testDbf.GoTo(3)
if err != nil {
t.Error(err)
}
data, err = testDbf.RecordToJSON(0, false)
if err != nil {
t.Error(err)
}
if string(data) != want2 {
t.Errorf("\nWanted json\n%s\nhave json\n%s\n", want2, string(data))
}
}
// Close file handles
func TestClose(t *testing.T) {
err := testDbf.Close()
if err != nil {
t.Fatal(err)
}
}
// TestDbase30 runs some test against dbase_30.dbf which has more complex column types
func TestDbase30(t *testing.T) {
if !usingFile {
t.Skip("TestDbase30 is only tested from disk")
}
dbf, err := OpenFile(filepath.Join("testdata", "dbase_30.dbf"), new(Win1250Decoder))
if err != nil {
t.Fatal(err)
}
defer dbf.Close()
err = dbf.GoTo(13)
if err != nil {
t.Fatal(err)
}
rec, err := dbf.Record()
if err != nil {
t.Fatal(err)
}
fields := rec.FieldSlice()
if err != nil {
t.Fatal(err)
}
// test value and type of caption field
caption, ok := fields[5].(string)
if !ok {
t.Error("caption field should be of type string")
}
wc := "Joh L. McWilliams & son"
if strings.TrimSpace(caption) != wc {
t.Errorf("Want caption value %q, have %q", wc, strings.TrimSpace(caption))
}
// test value and type of classes field
classes, ok := fields[10].(string)
if !ok {
t.Error("classes field should be of type string")
}
wcl := "People\r\nMcWilliams"
if strings.TrimSpace(classes) != wcl {
t.Errorf("Want classes value %q, have %q", wc, strings.TrimSpace(classes))
}
// test value and type of catdate field
catdate, ok := fields[8].(time.Time)
if !ok {
t.Error("catdate field should be of type time.Time")
}
wcd := time.Date(2000, 6, 29, 0, 0, 0, 0, time.UTC)
if catdate.Equal(wcd) == false {
t.Errorf("Want catdate value %v, have %v", wcd, catdate)
}
// test value and type of flagdate field
flagdate, ok := fields[38].(time.Time)
if !ok {
t.Error("flagdate field should be of type time.Time")
}
wfd := time.Date(1982, 7, 5, 15, 34, 0, 0, time.UTC)
if flagdate.Equal(wfd) == false {
t.Errorf("Want flagdate value %v, have %v", wfd, flagdate)
}
}
// TestDbase31 runs some test against dbase_31.dbf which has more complex column types
func TestDbase31(t *testing.T) {
if !usingFile {
t.Skip("TestDbase31 is only tested from disk")
}
dbf, err := OpenFile(filepath.Join("testdata", "dbase_31.dbf"), new(Win1250Decoder))
if err != nil {
t.Fatal(err)
}
defer dbf.Close()
err = dbf.GoTo(28)
if err != nil {
t.Fatal(err)
}
// test value and type of PRODUCTNAM field
val, err := dbf.Field(dbf.FieldPos("PRODUCTNAM"))
if err != nil {
t.Fatal(err)
}
name, ok := val.(string)
if !ok {
t.Error("PRODUCTNAM field should be of type string")
}
wn := "Thüringer Rostbratwurst"
if strings.TrimSpace(name) != wn {
t.Errorf("Want PRODUCTNAM value %q, have %q", wn, strings.TrimSpace(name))
}
// test value and type of CATEGORYID field
val, err = dbf.Field(dbf.FieldPos("CATEGORYID"))
if err != nil {
t.Fatal(err)
}
cat, ok := val.(int32)
if !ok {
t.Error("CATEGORYID field should be of type int32")
}
wcat := int32(6)
if cat != wcat {
t.Errorf("Want CATEGORYID value %d, have %d", wcat, cat)
}
// test value and type of UNITPRICE field
val, err = dbf.Field(dbf.FieldPos("UNITPRICE"))
if err != nil {
t.Fatal(err)
}
price, ok := val.(float64)
if !ok {
t.Error("UNITPRICE field should be of type float64")
}
wprice := float64(123.79)
if price != wprice {
t.Errorf("Want UNITPRICE value %f, have %f", wprice, price)
}
// Test no FPT errors
_, err = dbf.StatFPT()
if err == nil {
t.Errorf("Want error %s, have no error", ErrNoFPTFile)
} else if err != ErrNoFPTFile {
t.Errorf("Want error %s, have error %s", ErrNoFPTFile, err)
}
}
// TestDkeza runs some test against dkeza.dbf, added in issue #2
func TestDkeza(t *testing.T) {
if !usingFile {
t.Skip("TestDkeza is only tested from disk")
}
dbf, err := OpenFile(filepath.Join("testdata", "dkeza.dbf"), new(UTF8Decoder))
if err != nil {
t.Fatal(err)
}
defer dbf.Close()
err = dbf.GoTo(0)
if err != nil {
t.Fatal(err)
}
// test value and type of NUMBER field
val, err := dbf.Field(dbf.FieldPos("NUMBER"))
if err != nil {
t.Fatal(err)
}
number, ok := val.(int64)
if !ok {
t.Error("NUMBER field should be of type int64")
}
wn := int64(8027846523)
if number != wn {
t.Errorf("Want NUMBER value %d, have %d", wn, number)
}
// test value and type of CURR field
val, err = dbf.Field(dbf.FieldPos("CURR"))
if err != nil {
t.Fatal(err)
}
curr, ok := val.(float64)
if !ok {
t.Error("CURR field should be of type float64")
}
wc := 1234567890.1234
if number != wn {
t.Errorf("Want CURR value %f, have %f", wc, curr)
}
// test value and type of DTIME field
val, err = dbf.Field(dbf.FieldPos("DTIME"))
if err != nil {
t.Fatal(err)
}
dtime, ok := val.(time.Time)
if !ok {
t.Error("DTIME field should be of type time.Time")
}
wdt := time.Date(2016, 11, 14, 9, 10, 44, 0, time.UTC)
if dtime.Equal(wdt) == false {
t.Errorf("Want DTIME value %s, have %s", wdt, dtime)
}
t.Logf("DTIME: %s", dtime)
}
func TestSetValidFileVersionFunc(t *testing.T) {
// open the file without overriding the validation function
_, err := OpenFile(filepath.Join("testdata", "dbase_03.dbf"), new(Win1250Decoder))
if err == nil || strings.HasPrefix(err.Error(), "untested") == false {
t.Fatal("expected to have an error when opening untested file dbase_03.dbf")
}
// override function
SetValidFileVersionFunc(func(version byte) error {
if version == 0x03 {
return nil
}
return errors.New("not 0x03")
})
defer SetValidFileVersionFunc(validFileVersion)
dbf, err := OpenFile(filepath.Join("testdata", "dbase_03.dbf"), new(Win1250Decoder))
if err != nil {
t.Fatalf("expected no error, have %s:", err)
}
defer dbf.Close()
}
func ExampleSetValidFileVersionFunc() {
// create function which checks that only file flag 0x03 is valid
validFileVersionFunc := func(version byte) error {
if version == 0x03 {
return nil
}
return errors.New("not 0x03")
}
// set the new function as verifier
SetValidFileVersionFunc(validFileVersionFunc)
// open DBF as usual
dbf, err := OpenFile("/var/test.dbf", new(Win1250Decoder))
if err != nil {
log.Fatal(err)
}
defer dbf.Close()
}
// Benchmark for reading all records sequentially
// Use a large DBF/FPT combo for more realistic results
func BenchmarkReadRecords(b *testing.B) {
for n := 0; n < b.N; n++ {
err := func() error {
dbf, err := OpenFile(filepath.Join("testdata", "dbase_30.dbf"), new(Win1250Decoder))
if err != nil {
return err
}
defer dbf.Close()
for i := uint32(0); i < dbf.NumRecords(); i++ {
_, err := dbf.Record()
if err != nil {
return err
}
dbf.Skip(1)
}
return nil
}()
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkRecordToJSONWithTrim(b *testing.B) {
dbf, err := OpenFile(filepath.Join("testdata", "dbase_30.dbf"), new(Win1250Decoder))
if err != nil {
b.Fatal(err)
}
defer dbf.Close()
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err = dbf.RecordToJSON(1, true)
if err != nil {
b.Fatal(err)
}
}
}