Repository: sgoodm/python-distance-rasters (language: python)

Issue:
Improve code coverage
Most of the remaining coverage involves testing various exceptions and checks that are not hit during typical use. These should generally be easy to create tests for by adapting existing tests or creating new simple ones.


Discussion / hints:
Only missing coverage is when vector data is passed as a geodataframe or a generic iterable (not covered by other cases)

Fix patch:
diff --git a/.gitignore b/.gitignore
index eea0dde..5a2a5fe 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,6 @@ dist/*
 .DS_Store
 MANIFEST
 *.egg-info
+htmlcov
+.coverage
+tests/testdata
\ No newline at end of file
diff --git a/src/distancerasters/utils.py b/src/distancerasters/utils.py
index e5d7cc9..2217edb 100644
--- a/src/distancerasters/utils.py
+++ b/src/distancerasters/utils.py
@@ -1,6 +1,4 @@
-import sys
 import os
-import errno
 import math
 from warnings import warn
 import fiona


Test patch:
diff --git a/.github/workflows/test-with-coverage.yml b/.github/workflows/test-with-coverage.yml
index dbd9bdb..e8651a7 100644
--- a/.github/workflows/test-with-coverage.yml
+++ b/.github/workflows/test-with-coverage.yml
@@ -21,7 +21,7 @@ jobs:
       run: |
         python -m pip install --upgrade pip
         pip install .
-        pip install pytest pytest-cov shapely build
+        pip install pytest pytest-cov shapely build geopandas
     - name: Test with pytest ✅
       run: |
         pytest --cov=distancerasters
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 227abc2..5720241 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -19,7 +19,7 @@ jobs:
       run: |
         python -m pip install --upgrade pip
         pip install .
-        pip install pytest pytest-cov shapely
+        pip install pytest pytest-cov shapely geopandas
     - name: Test with pytest ✅
       run: |
         pytest --cov=distancerasters
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 645af65..61c16b2 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -4,6 +4,7 @@
 import numpy as np
 from shapely.geometry import shape
 import rasterio
+import geopandas as gpd
 import pytest
 from distancerasters.utils import (
     get_affine_and_shape,
@@ -21,6 +22,15 @@ def example_shape():
     return shape({"type": "LineString", "coordinates": [(3, 1), (1, 3), (4, 4)]})
 
 
+@pytest.fixture
+def example_feature():
+    # geometry from (1/10 scale):
+    # https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry
+    return {"geometry": {"type": "LineString", "coordinates": [(3, 1), (1, 3), (4, 4)]}, "properties": {"id": 1}}
+
+
+
+
 @pytest.fixture
 def example_raster():
     # returns a raster that represents a rasterized example_shape
@@ -63,13 +73,17 @@ def test_bad_get_affine_and_shape():
 
 
 def test_rasterize(example_shape, example_raster, example_path):
-    
+
     # pass shapely shape geometry to rasterize
     output_raster = rasterize(
         example_shape, pixel_size=0.5, bounds=example_shape.bounds
     )[0]
     assert (output_raster == example_raster).all
 
+
+
+def test_rasterize_output(example_shape, example_raster, example_path):
+
     # same as above, with output path
     output_raster = rasterize(
         example_shape, pixel_size=0.5, bounds=example_shape.bounds, output=example_path
@@ -78,6 +92,30 @@ def test_rasterize(example_shape, example_raster, example_path):
     with rasterio.open(example_path) as src:
         assert (src.read() == example_raster).all
 
+
+def test_geodataframe_rasterize(example_shape, example_raster, example_path):
+
+    geodataframe = gpd.GeoDataFrame([{"id": 0, "geometry": example_shape}])
+
+    # pass geodataframe to rasterize
+    output_raster = rasterize(
+        geodataframe, pixel_size=0.5, bounds=geodataframe.total_bounds, attribute="id"
+    )[0]
+    assert (output_raster == example_raster).all
+
+
+def test_iterable_rasterize(example_feature, example_raster, example_path):
+
+    bounds = shape(example_feature["geometry"]).bounds
+    example_iterable = [example_feature]
+
+    # pass iterable of geometry to rasterize
+    output_raster = rasterize(
+        example_iterable, pixel_size=0.5, bounds=bounds, attribute="id"
+    )[0]
+    assert (output_raster == example_raster).all
+
+
 def test_bad_rasterize(example_shape, example_affine):
 
     # warns about ignoring pixel_size because valid affine and shape were passed