Skip to content

Commit

Permalink
added a new unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-nour committed Jun 29, 2024
1 parent 403139c commit f2d3653
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
workflow_dispatch:
push:
branches:
- master
- main
jobs:
build:
runs-on: ubuntu-latest
Expand Down
24 changes: 12 additions & 12 deletions geojson_shave/geojson_shave.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,30 @@ def get_parser():
nargs="+",
)

return parser
args = parser.parse_args()
return args


def _create_coordinates(coordinates, precision):
def create_coordinates(coordinates, precision):
"""Create truncuated coordinates."""
new_coordinates = []
for item in coordinates:
if isinstance(item, list):
new_coordinates.append(_create_coordinates(item, precision))
new_coordinates.append(create_coordinates(item, precision))
else:
item = round(item, precision)
new_coordinates.append(float(item))
return new_coordinates


def _process_geometry_collection(geometry_collection, precision):
def process_geometry_collection(geometry_collection, precision):
"""Parse and truncuate the coordinates of each geometry
object nested within a geometry collection."""
new_geometry_collection = {"type": "GeometryCollection"}
processed_geometry_objects = []
for geometry_object in geometry_collection["geometries"]:
object_type = geometry_object["type"]
new_coordinates = _create_coordinates(geometry_object["coordinates"], precision)
new_coordinates = create_coordinates(geometry_object["coordinates"], precision)
processed_geometry_objects.append(
{"type": object_type, "coordinates": new_coordinates}
)
Expand All @@ -119,7 +120,7 @@ def _process_geometry_collection(geometry_collection, precision):
return new_geometry_collection


def _process_features(geojson, precision, geometry_to_include, nullify_property):
def process_features(geojson, precision, geometry_to_include, nullify_property):
"""Process Feature objects, truncuating coordinates and/or replacing
the properties member with a blank value."""
# Create new GeoJSON object.
Expand Down Expand Up @@ -147,12 +148,12 @@ def _process_features(geojson, precision, geometry_to_include, nullify_property)
if (geo_type := feature["geometry"]["type"]) in geometry_to_include:
if geo_type == "GeometryCollection":
output_geojson["features"][index]["geometry"] = (
_process_geometry_collection(
process_geometry_collection(
feature["geometry"], precision
)
)
else:
new_coordinates = _create_coordinates(
new_coordinates = create_coordinates(
feature["geometry"]["coordinates"], precision
)
output_geojson["features"][index]["geometry"][
Expand All @@ -162,7 +163,7 @@ def _process_features(geojson, precision, geometry_to_include, nullify_property)

else: # Only one Feature.
if geojson["geometry"]["type"] in geometry_to_include:
new_coordinates = _create_coordinates(
new_coordinates = create_coordinates(
geojson["geometry"]["coordinates"], precision
)
output_geojson["geometry"] = {
Expand All @@ -182,8 +183,7 @@ def _process_features(geojson, precision, geometry_to_include, nullify_property)

def main():
"""Launch the command-line tool."""
parser = get_parser()
args = parser.parse_args()
args = get_parser()

if args.decimal_points < 0:
raise ValueError(
Expand All @@ -195,7 +195,7 @@ def main():
input_geojson = json.load(input_file)
except json.decoder.JSONDecodeError as e:
raise ValueError("Error: please provide a valid GeoJSON file.") from e
output_geojson = _process_features(
output_geojson = process_features(
input_geojson, args.decimal_points, args.geometry_object, args.properties
)

Expand Down
69 changes: 42 additions & 27 deletions tests/test_logic.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
"""Unit tests for geojson_shave.py"""

import unittest
from unittest import mock

from geojson_shave.geojson_shave import (
_create_coordinates,
_process_geometry_collection,
_process_features,
create_coordinates,
process_geometry_collection,
process_features,
GEOMETRY_OBJECTS,
main,
)


class TestMain(unittest.TestCase):
"""Tests for the main function."""

@mock.patch("geojson_shave.geojson_shave.get_parser")
def test_decimal_points_less_than_zero(self, cli):
"""Test that passing a negative number to the
decimal_points option raises a ValueError.
"""
cli.return_value = mock.Mock(decimal_points=-1)
with self.assertRaises(ValueError):
main()


class TestCreateCoordinates(unittest.TestCase):
"""Tests for the _create_coordinates function.
"""Tests for the create_coordinates function.
Note that both LineStrings' and MultiPoints' coordinates both consist
of an array of Points, hence only the LineString being tested.
Expand Down Expand Up @@ -92,7 +107,7 @@ def test_truncuating_point_coordinates(self):
precision = 3
geometry_coordinates = self.point["coordinates"]
self.assertEqual(
_create_coordinates(geometry_coordinates, precision), expected_return_value
create_coordinates(geometry_coordinates, precision), expected_return_value
)

def test_point_same_precision(self):
Expand All @@ -104,7 +119,7 @@ def test_point_same_precision(self):
precision = 6
geometry_coordinates = self.point["coordinates"]
self.assertEqual(
_create_coordinates(geometry_coordinates, precision), expected_return_value
create_coordinates(geometry_coordinates, precision), expected_return_value
)

def test_point_precision_larger_than_places(self):
Expand All @@ -116,7 +131,7 @@ def test_point_precision_larger_than_places(self):
precision = 50
geometry_coordinates = self.point["coordinates"]
self.assertEqual(
_create_coordinates(geometry_coordinates, precision), expected_return_value
create_coordinates(geometry_coordinates, precision), expected_return_value
)

# LineString.
Expand All @@ -130,7 +145,7 @@ def test_truncuating_linestring_coordinates(self):
precision = 3
geometry_coordinates = self.linestring["coordinates"]
self.assertEqual(
_create_coordinates(geometry_coordinates, precision), expected_return_value
create_coordinates(geometry_coordinates, precision), expected_return_value
)

def test_linestring_same_precision(self):
Expand All @@ -142,7 +157,7 @@ def test_linestring_same_precision(self):
precision = 6
geometry_coordinates = self.linestring["coordinates"]
self.assertEqual(
_create_coordinates(geometry_coordinates, precision), expected_return_value
create_coordinates(geometry_coordinates, precision), expected_return_value
)

def test_linestring_precision_larger_than_places(self):
Expand All @@ -154,7 +169,7 @@ def test_linestring_precision_larger_than_places(self):
precision = 50
geometry_coordinates = self.linestring["coordinates"]
self.assertEqual(
_create_coordinates(geometry_coordinates, precision), expected_return_value
create_coordinates(geometry_coordinates, precision), expected_return_value
)

# MultiLine strings.
Expand All @@ -167,7 +182,7 @@ def test_truncuating_multilinestring_coordinates(self):
precision = 3
geometry_coordinates = self.multilinestring["coordinates"]
self.assertEqual(
_create_coordinates(geometry_coordinates, precision), expected_return_value
create_coordinates(geometry_coordinates, precision), expected_return_value
)

def test_multilinestring_precision_larger_than_places(self):
Expand All @@ -179,7 +194,7 @@ def test_multilinestring_precision_larger_than_places(self):
precision = 50
geometry_coordinates = self.multilinestring["coordinates"]
self.assertEqual(
_create_coordinates(geometry_coordinates, precision), expected_return_value
create_coordinates(geometry_coordinates, precision), expected_return_value
)

def test_multilinestring_same_precision(self):
Expand All @@ -191,7 +206,7 @@ def test_multilinestring_same_precision(self):
precision = 6
geometry_coordinates = self.multipolygon["coordinates"]
self.assertEqual(
_create_coordinates(geometry_coordinates, precision), expected_return_value
create_coordinates(geometry_coordinates, precision), expected_return_value
)

# Polygon.
Expand All @@ -209,7 +224,7 @@ def test_truncuating_polygon_coordinates(self):
precision = 3
geometry_coordinates = self.polygon["coordinates"]
self.assertEqual(
_create_coordinates(geometry_coordinates, precision), expected_return_value
create_coordinates(geometry_coordinates, precision), expected_return_value
)

def test_polygon_precision_larger_than_places(self):
Expand All @@ -229,7 +244,7 @@ def test_polygon_precision_larger_than_places(self):
precision = 50
geometry_coordinates = self.polygon["coordinates"]
self.assertEqual(
_create_coordinates(geometry_coordinates, precision), expected_return_value
create_coordinates(geometry_coordinates, precision), expected_return_value
)

def test_polygon_same_precision(self):
Expand All @@ -249,7 +264,7 @@ def test_polygon_same_precision(self):
precision = 6
geometry_coordinates = self.polygon["coordinates"]
self.assertEqual(
_create_coordinates(geometry_coordinates, precision), expected_return_value
create_coordinates(geometry_coordinates, precision), expected_return_value
)

# MultiPolygon.
Expand Down Expand Up @@ -285,7 +300,7 @@ def test_truncuating_multipolygon_coordinates(self):
precision = 3
geometry_coordinates = self.multipolygon["coordinates"]
self.assertEqual(
_create_coordinates(geometry_coordinates, precision), expected_return_value
create_coordinates(geometry_coordinates, precision), expected_return_value
)

def test_multipolygon_precision_larger_than_places(self):
Expand All @@ -297,7 +312,7 @@ def test_multipolygon_precision_larger_than_places(self):
precision = 50
geometry_coordinates = self.multipolygon["coordinates"]
self.assertEqual(
_create_coordinates(geometry_coordinates, precision), expected_return_value
create_coordinates(geometry_coordinates, precision), expected_return_value
)

def test_multipolygon_same_precision(self):
Expand All @@ -309,12 +324,12 @@ def test_multipolygon_same_precision(self):
precision = 6
geometry_coordinates = self.multipolygon["coordinates"]
self.assertEqual(
_create_coordinates(geometry_coordinates, precision), expected_return_value
create_coordinates(geometry_coordinates, precision), expected_return_value
)


class TestProcessGeometryCollection(unittest.TestCase):
"""Tests for the _process_geometry_collection function."""
"""Tests for the process_geometry_collection function."""

def setUp(self):
self.geometry_collection = {
Expand Down Expand Up @@ -388,13 +403,13 @@ def test_truncuating_coordinates(self):
geo_collection = self.geometry_collection
precision = 3
self.assertEqual(
_process_geometry_collection(geo_collection, precision),
process_geometry_collection(geo_collection, precision),
expected_return_value,
)


class TestProcessFeatures(unittest.TestCase):
"""Tests for the _process_features function."""
"""Tests for the process_features function."""

def setUp(self):
self.feature_collection = {
Expand Down Expand Up @@ -464,7 +479,7 @@ def test_feature_collection_truncuation(self):
}

self.assertEqual(
_process_features(
process_features(
self.feature_collection, precision, geometry_to_include, False
),
expected_return_value,
Expand All @@ -483,15 +498,15 @@ def test_feature_truncuation(self):
}

self.assertEqual(
_process_features(self.feature, precision, geometry_to_include, False),
process_features(self.feature, precision, geometry_to_include, False),
expected_return_value,
)

def test_empty_gson_file(self):
"""Test that an exception is raised when an empty
GeoJSON file is passed."""
with self.assertRaises(ValueError):
_process_features(self.blank_feature_collection, 3, ["Point"], False)
process_features(self.blank_feature_collection, 3, ["Point"], False)

def test_properties_nullified(self):
"""Test that the properties key returns a null/empty dictionary."""
Expand Down Expand Up @@ -524,7 +539,7 @@ def test_properties_nullified(self):
}

self.assertEqual(
_process_features(
process_features(
self.feature_collection, precision, GEOMETRY_OBJECTS, True
),
expected_return_value,
Expand Down Expand Up @@ -562,7 +577,7 @@ def test_geometry_to_include_parameter(self):
}

self.assertEqual(
_process_features(
process_features(
self.feature_collection, precision, geometry_to_include, False
),
expected_return_value,
Expand Down

0 comments on commit f2d3653

Please sign in to comment.