88import json
99import os .path
1010from pathlib import Path
11+ import shutil
1112
1213# third party imports
1314import pytest
@@ -2164,4 +2165,50 @@ def test_encode_dbf_field_values(value,encoded_len,codec,errors):
21642165 with context :
21652166 r = shapefile .DbfReader (stream , encoding = codec , encodingErrors = errors , strict = False )
21662167 assert r .record (0 )[0 ] == value
2167- r .close ()
2168+ r .close ()
2169+
2170+ @pytest .fixture
2171+ def tmp_latin1_shapefile_shp (tmp_path ):
2172+ name = "latin1"
2173+ test_shapefile_dir = tmp_path / name
2174+ test_shapefile_dir .mkdir ()
2175+ for file in shapefiles_dir .glob ("latin1.*" ):
2176+ shutil .copy (file , test_shapefile_dir )
2177+ test_shapefile = test_shapefile_dir / f"{ name } .shp"
2178+ return test_shapefile
2179+
2180+ ENCODINGS_AND_CONTEXTS = [
2181+ ("latin1" , contextlib .nullcontext ()),
2182+ ("utf8" , pytest .raises (shapefile .dbfFileException )),
2183+ ]
2184+ @pytest .mark .parametrize ("encoding, context" , ENCODINGS_AND_CONTEXTS )
2185+ def test_read_latin1_shapefile (encoding , context , tmp_latin1_shapefile_shp ):
2186+ """ Extend the smoke test in README.md doctests """
2187+
2188+ assert tmp_latin1_shapefile_shp .is_file ()
2189+
2190+ r = shapefile .Reader (tmp_latin1_shapefile_shp , encoding = encoding )
2191+ with context :
2192+ rec = r .record (0 )
2193+ r .close ()
2194+ if encoding == "latin1" :
2195+ assert rec == [2 , u'Ñandú' ]
2196+
2197+ @pytest .mark .xfail (reason = "Support for reading encodings from .cpg files not implemented yet" )
2198+ @pytest .mark .parametrize ("encoding, context" , ENCODINGS_AND_CONTEXTS [:1 ])
2199+ def test_read_latin1_shapefile_cpg_file (encoding , context , tmp_latin1_shapefile_shp ):
2200+ """ Extend the smoke test in README.md doctests """
2201+
2202+ assert tmp_latin1_shapefile_shp .is_file ()
2203+
2204+ cpg_file = tmp_latin1_shapefile_shp .with_suffix (".cpg" )
2205+ cpg_file .write_text (encoding .upper ().replace ("_" ,"-" ))
2206+
2207+ r = shapefile .Reader (tmp_latin1_shapefile_shp )
2208+ with context :
2209+ rec = r .record (0 )
2210+ r .close ()
2211+ if encoding == "latin1" :
2212+ assert rec == [2 , u'Ñandú' ]
2213+
2214+
0 commit comments