Skip to content

Commit 4d4820d

Browse files
ireestwpayne
authored andcommitted
Tests and test data
1 parent 2a1b49e commit 4d4820d

File tree

5 files changed

+69
-20
lines changed

5 files changed

+69
-20
lines changed

dbf.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,7 @@ func ReadDBF(r io.Reader, _ int64, options *ReadDBFOptions) (*DBF, error) {
171171
fieldData := recordData[offset : offset+fieldDescriptor.Length]
172172
offset += fieldDescriptor.Length
173173
field, err := fieldDescriptor.ParseRecord(fieldData, decoder)
174-
if options.SkipBrokenFields && err != nil {
175-
field = nil
176-
err = nil
177-
}
178-
if err != nil {
174+
if err != nil && !options.SkipBrokenFields {
179175
return nil, fmt.Errorf("field %s: %w", fieldDescriptor.Name, err)
180176
}
181177
record = append(record, field)

scanner.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -758,11 +758,7 @@ func (s *ScannerDBF) Scan() (DBFRecord, error) {
758758
fieldData := recordData[offset : offset+fieldDescriptor.Length]
759759
offset += fieldDescriptor.Length
760760
field, err := fieldDescriptor.ParseRecord(fieldData, s.decoder)
761-
if s.options.SkipBrokenFields {
762-
field = nil
763-
err = nil
764-
}
765-
if err != nil {
761+
if err != nil && !s.options.SkipBrokenFields {
766762
s.err = fmt.Errorf("field %s: %w", fieldDescriptor.Name, err)
767763
return nil, s.err
768764
}

scanner_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,12 +189,14 @@ func TestReadScannerFSAndZipFile(t *testing.T) {
189189
for _, tc := range []struct {
190190
filename string
191191
basename string
192+
options *ReadShapefileOptions
192193
expectedShapeType ShapeType
193194
expectedBounds *geom.Bounds
194195
expectedRecordsLen int
195196
expectedDBFRecord0Fields map[string]any
196197
expectedSHPRecord0 *SHPRecord
197198
expectedExport any
199+
expectedErr string
198200
}{
199201
{
200202
filename: "testdata/110m-admin-0-countries.zip",
@@ -242,6 +244,23 @@ func TestReadScannerFSAndZipFile(t *testing.T) {
242244
ShapeType: ShapeTypePolygon,
243245
},
244246
},
247+
{
248+
filename: "testdata/10m_populated_places_simple.zip",
249+
expectedRecordsLen: 177,
250+
expectedErr: `scanning DBF: field labelrank: "**": invalid numeric: strconv.ParseInt: parsing "**": invalid syntax`,
251+
},
252+
{
253+
filename: "testdata/10m_populated_places_simple.zip",
254+
options: &ReadShapefileOptions{
255+
DBF: &ReadDBFOptions{SkipBrokenFields: true},
256+
},
257+
expectedShapeType: ShapeTypePoint,
258+
expectedRecordsLen: 7342,
259+
expectedBounds: geom.NewBounds(geom.XY).Set(
260+
-179.5899789, -89.9999998,
261+
179.3833036, 82.4833232,
262+
),
263+
},
245264
{
246265
filename: "testdata/Luftfahrthindernisse.zip",
247266
basename: "Luftfahrthindernisse",
@@ -384,10 +403,14 @@ func TestReadScannerFSAndZipFile(t *testing.T) {
384403
}
385404

386405
t.Run("ReadZipFile", func(t *testing.T) {
387-
scanner, err := NewScannerFromZipFile(tc.filename, nil)
406+
scanner, err := NewScannerFromZipFile(tc.filename, tc.options)
388407
assert.NoError(t, err)
389408
assert.NotZero(t, scanner)
390409
shapefile, err := ReadScanner(scanner)
410+
if tc.expectedErr != "" {
411+
assert.Error(t, err)
412+
return
413+
}
391414
assert.NoError(t, err)
392415
assert.NotZero(t, scanner)
393416
testShapefile(t, shapefile)

shapefile_test.go

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,16 +386,50 @@ func TestReadFSAndZipFile(t *testing.T) {
386386
}
387387

388388
func TestShapefileRecords(t *testing.T) {
389-
s, err := ReadZipFile("testdata/110m-admin-0-countries.zip", nil)
390-
assert.NoError(t, err)
391-
fieldss := make([]map[string]any, 0, s.NumRecords())
392-
geoms := make([]geom.T, 0, s.NumRecords())
393-
for fields, g := range s.Records() {
394-
fieldss = append(fieldss, fields)
395-
geoms = append(geoms, g)
389+
for _, tc := range []struct {
390+
filename string
391+
expectedRecordsLen int
392+
expectedGeomsLen int
393+
expectedErr string
394+
ReadDBFOptions *ReadDBFOptions
395+
}{
396+
{
397+
filename: "testdata/110m-admin-0-countries.zip",
398+
expectedRecordsLen: 177,
399+
expectedGeomsLen: 177,
400+
},
401+
{
402+
filename: "testdata/10m_populated_places_simple.zip",
403+
expectedErr: "failed to parse DBF field",
404+
},
405+
{
406+
filename: "testdata/10m_populated_places_simple.zip",
407+
expectedRecordsLen: 7342,
408+
expectedGeomsLen: 7342,
409+
ReadDBFOptions: &ReadDBFOptions{
410+
SkipBrokenFields: true,
411+
},
412+
},
413+
} {
414+
t.Run(tc.filename, func(t *testing.T) {
415+
s, err := ReadZipFile(tc.filename, &ReadShapefileOptions{
416+
DBF: tc.ReadDBFOptions,
417+
})
418+
if tc.expectedErr != "" {
419+
assert.Error(t, err, tc.expectedErr)
420+
return
421+
}
422+
assert.NoError(t, err)
423+
fieldss := make([]map[string]any, 0, s.NumRecords())
424+
geoms := make([]geom.T, 0, s.NumRecords())
425+
for fields, g := range s.Records() {
426+
fieldss = append(fieldss, fields)
427+
geoms = append(geoms, g)
428+
}
429+
assert.Equal(t, tc.expectedRecordsLen, len(fieldss))
430+
assert.Equal(t, tc.expectedGeomsLen, len(geoms))
431+
})
396432
}
397-
assert.Equal(t, 177, len(fieldss))
398-
assert.Equal(t, 177, len(geoms))
399433
}
400434

401435
func addFuzzDataFromFS(f *testing.F, fsys fs.FS, root, ext string) error {
637 KB
Binary file not shown.

0 commit comments

Comments
 (0)