Skip to content

Commit 37e1bfc

Browse files
authored
Create fixture for test files of other file types (#74)
1 parent 4f81551 commit 37e1bfc

File tree

2 files changed

+87
-48
lines changed

2 files changed

+87
-48
lines changed

pyogrio/tests/conftest.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55

66
from pyogrio import __gdal_version_string__, __version__, list_drivers
7-
7+
import pyogrio
88

99
_data_dir = Path(__file__).parent.resolve() / "fixtures"
1010

@@ -18,14 +18,34 @@ def pytest_report_header(config):
1818
)
1919

2020

21+
def prepare_testfile(testfile_path, dst_dir, ext):
22+
if ext == testfile_path.suffix:
23+
return testfile_path
24+
25+
dst_path = dst_dir / f"{testfile_path.stem}{ext}"
26+
if dst_path.exists():
27+
return dst_path
28+
gdf = pyogrio.read_dataframe(testfile_path)
29+
pyogrio.write_dataframe(gdf, dst_path)
30+
return dst_path
31+
32+
2133
@pytest.fixture(scope="session")
2234
def data_dir():
2335
return _data_dir
2436

2537

26-
@pytest.fixture(scope="session")
27-
def naturalearth_lowres():
28-
return _data_dir / Path("naturalearth_lowres/naturalearth_lowres.shp")
38+
@pytest.fixture(scope="function")
39+
def naturalearth_lowres(tmp_path, request):
40+
ext = getattr(request, "param", ".shp")
41+
testfile_path = _data_dir / Path("naturalearth_lowres/naturalearth_lowres.shp")
42+
43+
return prepare_testfile(testfile_path, tmp_path, ext)
44+
45+
46+
@pytest.fixture(scope="function", params=[".shp", ".gpkg", ".json"])
47+
def naturalearth_lowres_all_ext(tmp_path, naturalearth_lowres, request):
48+
return prepare_testfile(naturalearth_lowres, tmp_path, request.param)
2949

3050

3151
@pytest.fixture(scope="function")
@@ -44,4 +64,3 @@ def naturalearth_lowres_vsi(tmp_path, naturalearth_lowres):
4464
@pytest.fixture(scope="session")
4565
def test_fgdb_vsi():
4666
return f"/vsizip/{_data_dir}/test_fgdb.gdb.zip"
47-

pyogrio/tests/test_geopandas_io.py

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
pytestmark = pytest.mark.skipif(not has_geopandas, reason="GeoPandas not available")
2222

2323

24-
def test_read_dataframe(naturalearth_lowres):
25-
df = read_dataframe(naturalearth_lowres)
24+
def test_read_dataframe(naturalearth_lowres_all_ext):
25+
df = read_dataframe(naturalearth_lowres_all_ext)
2626

2727
assert isinstance(df, gp.GeoDataFrame)
2828

@@ -45,8 +45,19 @@ def test_read_dataframe_vsi(naturalearth_lowres_vsi):
4545
assert len(df) == 177
4646

4747

48-
def test_read_no_geometry(naturalearth_lowres):
49-
df = read_dataframe(naturalearth_lowres, read_geometry=False)
48+
@pytest.mark.parametrize(
49+
"naturalearth_lowres, expected_ext",
50+
[(".gpkg", ".gpkg"), (".shp", ".shp")],
51+
indirect=["naturalearth_lowres"])
52+
def test_fixture_naturalearth_lowres(naturalearth_lowres, expected_ext):
53+
# Test the fixture with "indirect" parameter
54+
assert naturalearth_lowres.suffix == expected_ext
55+
df = read_dataframe(naturalearth_lowres)
56+
assert len(df) == 177
57+
58+
59+
def test_read_no_geometry(naturalearth_lowres_all_ext):
60+
df = read_dataframe(naturalearth_lowres_all_ext, read_geometry=False)
5061
assert isinstance(df, pd.DataFrame)
5162
assert not isinstance(df, gp.GeoDataFrame)
5263

@@ -82,9 +93,9 @@ def test_read_layer(test_fgdb_vsi):
8293
assert "RIVER_MILE" in df.columns
8394

8495

85-
def test_read_layer_invalid(naturalearth_lowres):
96+
def test_read_layer_invalid(naturalearth_lowres_all_ext):
8697
with pytest.raises(DataLayerError, match="Layer 'wrong' could not be opened"):
87-
read_dataframe(naturalearth_lowres, layer="wrong")
98+
read_dataframe(naturalearth_lowres_all_ext, layer="wrong")
8899

89100

90101
@pytest.mark.filterwarnings("ignore: Measured")
@@ -98,77 +109,87 @@ def test_read_null_values(test_fgdb_vsi):
98109

99110
# make sure that Null values are preserved
100111
assert df.SEGMENT_NAME.isnull().max() == True
101-
assert df.loc[df.SEGMENT_NAME.isnull()].SEGMENT_NAME.iloc[0] == None
112+
assert df.loc[df.SEGMENT_NAME.isnull()].SEGMENT_NAME.iloc[0] is None
102113

103114

104-
def test_read_fid_as_index(naturalearth_lowres):
115+
def test_read_fid_as_index(naturalearth_lowres_all_ext):
105116
kwargs = {"skip_features": 2, "max_features": 2}
106117

107118
# default is to not set FIDs as index
108-
df = read_dataframe(naturalearth_lowres, **kwargs)
119+
df = read_dataframe(naturalearth_lowres_all_ext, **kwargs)
109120
assert_index_equal(df.index, pd.RangeIndex(0, 2))
110121

111-
df = read_dataframe(naturalearth_lowres, fid_as_index=False, **kwargs)
122+
df = read_dataframe(naturalearth_lowres_all_ext, fid_as_index=False, **kwargs)
112123
assert_index_equal(df.index, pd.RangeIndex(0, 2))
113124

114-
df = read_dataframe(naturalearth_lowres, fid_as_index=True, **kwargs)
115-
assert_index_equal(df.index, pd.Index([2, 3], name="fid"))
125+
df = read_dataframe(naturalearth_lowres_all_ext, fid_as_index=True, **kwargs)
126+
if naturalearth_lowres_all_ext.suffix in ['.gpkg']:
127+
# File format where fid starts at 1
128+
assert_index_equal(df.index, pd.Index([3, 4], name="fid"))
129+
else:
130+
# File format where fid starts at 0
131+
assert_index_equal(df.index, pd.Index([2, 3], name="fid"))
116132

117133

118134
@pytest.mark.filterwarnings("ignore: Layer")
119-
def test_read_where(naturalearth_lowres):
135+
def test_read_where(naturalearth_lowres_all_ext):
120136
# empty filter should return full set of records
121-
df = read_dataframe(naturalearth_lowres, where="")
137+
df = read_dataframe(naturalearth_lowres_all_ext, where="")
122138
assert len(df) == 177
123139

124140
# should return singular item
125-
df = read_dataframe(naturalearth_lowres, where="iso_a3 = 'CAN'")
141+
df = read_dataframe(naturalearth_lowres_all_ext, where="iso_a3 = 'CAN'")
126142
assert len(df) == 1
127143
assert df.iloc[0].iso_a3 == "CAN"
128144

129-
df = read_dataframe(naturalearth_lowres, where="iso_a3 IN ('CAN', 'USA', 'MEX')")
145+
df = read_dataframe(naturalearth_lowres_all_ext, where="iso_a3 IN ('CAN', 'USA', 'MEX')")
130146
assert len(df) == 3
131147
assert len(set(df.iso_a3.unique()).difference(["CAN", "USA", "MEX"])) == 0
132148

133149
# should return items within range
134150
df = read_dataframe(
135-
naturalearth_lowres, where="POP_EST >= 10000000 AND POP_EST < 100000000"
151+
naturalearth_lowres_all_ext, where="POP_EST >= 10000000 AND POP_EST < 100000000"
136152
)
137153
assert len(df) == 75
138154
assert df.pop_est.min() >= 10000000
139155
assert df.pop_est.max() < 100000000
140156

141157
# should match no items
142-
df = read_dataframe(naturalearth_lowres, where="ISO_A3 = 'INVALID'")
158+
df = read_dataframe(naturalearth_lowres_all_ext, where="ISO_A3 = 'INVALID'")
143159
assert len(df) == 0
144160

145161

146-
def test_read_where_invalid(naturalearth_lowres):
147-
with pytest.raises(ValueError, match="Invalid SQL"):
148-
read_dataframe(naturalearth_lowres, where="invalid")
162+
def test_read_where_invalid(naturalearth_lowres_all_ext):
163+
if naturalearth_lowres_all_ext.suffix in [".gpkg"]:
164+
# Geopackage doesn't raise, but returns empty df?
165+
gdf = read_dataframe(naturalearth_lowres_all_ext, where="invalid")
166+
assert len(gdf) == 0
167+
else:
168+
with pytest.raises(ValueError, match="Invalid SQL"):
169+
read_dataframe(naturalearth_lowres_all_ext, where="invalid")
149170

150171

151172
@pytest.mark.parametrize("bbox", [(1,), (1, 2), (1, 2, 3)])
152-
def test_read_bbox_invalid(naturalearth_lowres, bbox):
173+
def test_read_bbox_invalid(naturalearth_lowres_all_ext, bbox):
153174
with pytest.raises(ValueError, match="Invalid bbox"):
154-
read_dataframe(naturalearth_lowres, bbox=bbox)
175+
read_dataframe(naturalearth_lowres_all_ext, bbox=bbox)
155176

156177

157-
def test_read_bbox(naturalearth_lowres):
178+
def test_read_bbox(naturalearth_lowres_all_ext):
158179
# should return no features
159180
with pytest.warns(UserWarning, match="does not have any features to read"):
160-
df = read_dataframe(naturalearth_lowres, bbox=(0, 0, 0.00001, 0.00001))
181+
df = read_dataframe(naturalearth_lowres_all_ext, bbox=(0, 0, 0.00001, 0.00001))
161182
assert len(df) == 0
162183

163-
df = read_dataframe(naturalearth_lowres, bbox=(-140, 20, -100, 40))
184+
df = read_dataframe(naturalearth_lowres_all_ext, bbox=(-140, 20, -100, 40))
164185
assert len(df) == 2
165186
assert np.array_equal(df.iso_a3, ["USA", "MEX"])
166187

167188

168-
def test_read_fids(naturalearth_lowres):
189+
def test_read_fids(naturalearth_lowres_all_ext):
169190
# ensure keyword is properly passed through
170-
fids = np.array([0, 10, 5], dtype=np.int64)
171-
df = read_dataframe(naturalearth_lowres, fids=fids, fid_as_index=True)
191+
fids = np.array([1, 10, 5], dtype=np.int64)
192+
df = read_dataframe(naturalearth_lowres_all_ext, fids=fids, fid_as_index=True)
172193
assert len(df) == 3
173194
assert np.array_equal(fids, df.index.values)
174195

@@ -238,27 +259,27 @@ def test_write_empty_dataframe(tmpdir, driver, ext):
238259
assert_geodataframe_equal(df, expected)
239260

240261

241-
def test_write_dataframe_gdalparams(tmpdir, naturalearth_lowres):
262+
def test_write_dataframe_gdalparams(tmp_path, naturalearth_lowres):
242263
original_df = read_dataframe(naturalearth_lowres)
243-
244-
test_noindex_filename = os.path.join(str(tmpdir), f"test_gdalparams_noindex.shp")
264+
265+
test_noindex_filename = tmp_path / "test_gdalparams_noindex.shp"
245266
write_dataframe(original_df, test_noindex_filename, SPATIAL_INDEX="NO")
246-
assert os.path.exists(test_noindex_filename) is True
247-
test_noindex_index_filename = os.path.join(str(tmpdir), f"test_gdalparams_noindex.qix")
248-
assert os.path.exists(test_noindex_index_filename) is False
249-
250-
test_withindex_filename = os.path.join(str(tmpdir), f"test_gdalparams_withindex.shp")
267+
assert test_noindex_filename.exists() is True
268+
test_noindex_index_filename = tmp_path / "test_gdalparams_noindex.qix"
269+
assert test_noindex_index_filename.exists() is False
270+
271+
test_withindex_filename = tmp_path / "test_gdalparams_withindex.shp"
251272
write_dataframe(original_df, test_withindex_filename, SPATIAL_INDEX="YES")
252-
assert os.path.exists(test_withindex_filename) is True
253-
test_withindex_index_filename = os.path.join(str(tmpdir), f"test_gdalparams_withindex.qix")
254-
assert os.path.exists(test_withindex_index_filename) is True
273+
assert test_withindex_filename.exists() is True
274+
test_withindex_index_filename = tmp_path / "test_gdalparams_withindex.qix"
275+
assert test_withindex_index_filename.exists() is True
255276

256277

257278
@pytest.mark.filterwarnings(
258279
"ignore: You will likely lose important projection information"
259280
)
260-
def test_custom_crs_io(tmpdir, naturalearth_lowres):
261-
df = read_dataframe(naturalearth_lowres)
281+
def test_custom_crs_io(tmpdir, naturalearth_lowres_all_ext):
282+
df = read_dataframe(naturalearth_lowres_all_ext)
262283
# project Belgium to a custom Albers Equal Area projection
263284
expected = df.loc[df.name == "Belgium"].to_crs(
264285
"+proj=aea +lat_1=49.5 +lat_2=51.5 +lon_0=4.3"
@@ -275,4 +296,3 @@ def test_custom_crs_io(tmpdir, naturalearth_lowres):
275296
assert crs["lat_2"] == 51.5
276297
assert crs["lon_0"] == 4.3
277298
assert df.crs.equals(expected.crs)
278-

0 commit comments

Comments
 (0)