From 3bbf84634817485ae931d48481b4c15545459cc4 Mon Sep 17 00:00:00 2001 From: Josef Vacha <69599105+JosefVacha@users.noreply.github.com> Date: Mon, 25 May 2026 11:44:06 +0200 Subject: [PATCH 1/2] FIX: Make test_features.FeaturesTest robust against Python version formatting differences - Replace string equality checks with JSON object comparison in test_protocol, test_feature_class, and test_geo_interface - This fixes Issue #175 (failing test on Python 3.9.6) by avoiding reliance on specific json.dumps() formatting - Tests now compare parsed JSON objects instead of raw strings Fixes #175. --- tests/test_features.py | 102 ++++++++++++++++++++++++----------------- 1 file changed, 60 insertions(+), 42 deletions(-) diff --git a/tests/test_features.py b/tests/test_features.py index 77477e3..f9d86f6 100644 --- a/tests/test_features.py +++ b/tests/test_features.py @@ -1,6 +1,6 @@ from io import StringIO +import json import unittest - import geojson @@ -16,22 +16,22 @@ def test_protocol(self): 'properties': {'title': 'Dict 1'}, } - json = geojson.dumps(f, sort_keys=True) - self.assertEqual(json, '{"geometry":' - ' {"coordinates": [53.0, -4.0],' - ' "type": "Point"},' - ' "id": "1",' - ' "properties": {"title": "Dict 1"},' - ' "type": "Feature"}') + json_str = geojson.dumps(f, sort_keys=True) + # Parse JSON to avoid formatting issues across Python versions + json_obj = json.loads(json_str) + + expected = { + "geometry": {"coordinates": [53.0, -4.0], "type": "Point"}, + "id": "1", + "properties": {"title": "Dict 1"}, + "type": "Feature" + } + self.assertEqual(json_obj, expected) - o = geojson.loads(json) - output = geojson.dumps(o, sort_keys=True) - self.assertEqual(output, '{"geometry":' - ' {"coordinates": [53.0, -4.0],' - ' "type": "Point"},' - ' "id": "1",' - ' "properties": {"title": "Dict 1"},' - ' "type": "Feature"}') + o = geojson.loads(json_str) + output_str = geojson.dumps(o, sort_keys=True) + output_obj = json.loads(output_str) + self.assertEqual(output_obj, expected) def test_unicode_properties(self): with open("tests/data.geojson") as file_: @@ -57,29 +57,34 @@ def test_feature_class(self): self.assertEqual(feature.properties['summary'], 'The first feature') self.assertEqual(feature.properties['link'], 'http://example.org/features/1') - self.assertEqual(geojson.dumps(feature.geometry, sort_keys=True), - '{"coordinates": [53.0, -4.0], "type": "Point"}') + + # Encoding - use JSON comparison to avoid formatting issues + geometry_obj = json.loads(geojson.dumps(feature.geometry, sort_keys=True)) + self.assertEqual(geometry_obj, {"coordinates": [53.0, -4.0], "type": "Point"}) - # Encoding - json = ('{"geometry": {"coordinates": [53.0, -4.0],' - ' "type": "Point"},' - ' "id": "1",' - ' "properties":' - ' {"link": "http://example.org/features/1",' - ' "summary": "The first feature",' - ' "title": "Feature 1"},' - ' "type": "Feature"}') - self.assertEqual(geojson.dumps(feature, sort_keys=True), json) + expected_feature = { + "geometry": {"coordinates": [53.0, -4.0], "type": "Point"}, + "id": "1", + "properties": { + "link": "http://example.org/features/1", + "summary": "The first feature", + "title": "Feature 1" + }, + "type": "Feature" + } + + feature_json_obj = json.loads(geojson.dumps(feature, sort_keys=True)) + self.assertEqual(feature_json_obj, expected_feature) # Decoding factory = geojson.examples.create_simple_web_feature - json = ('{"geometry": {"type": "Point",' + json_str = ('{"geometry": {"type": "Point",' ' "coordinates": [53.0, -4.0]},' ' "id": "1",' ' "properties": {"summary": "The first feature",' ' "link": "http://example.org/features/1",' ' "title": "Feature 1"}}') - feature = geojson.loads(json, object_hook=factory) + feature = geojson.loads(json_str, object_hook=factory) self.assertEqual(repr(type(feature)), "") self.assertEqual(feature.id, '1') @@ -87,8 +92,10 @@ def test_feature_class(self): self.assertEqual(feature.properties['summary'], 'The first feature') self.assertEqual(feature.properties['link'], 'http://example.org/features/1') - self.assertEqual(geojson.dumps(feature.geometry, sort_keys=True), - '{"coordinates": [53.0, -4.0], "type": "Point"}') + + # Compare geometry using JSON + geom_obj = json.loads(geojson.dumps(feature.geometry, sort_keys=True)) + self.assertEqual(geom_obj, {"coordinates": [53.0, -4.0], "type": "Point"}) def test_geo_interface(self): class Thingy: @@ -100,17 +107,28 @@ def __init__(self, id, title, x, y): @property def __geo_interface__(self): - return ({"id": self.id, + return {"id": self.id, "properties": {"title": self.title}, "geometry": {"type": "Point", - "coordinates": (self.x, self.y)}}) + "coordinates": (self.x, self.y)}} ob = Thingy('1', 'thingy one', -106.0, 40.0) - self.assertEqual(geojson.dumps(ob.__geo_interface__['geometry'], - sort_keys=True), - '{"coordinates": [-106.0, 40.0], "type": "Point"}') - self.assertEqual(geojson.dumps(ob, sort_keys=True), - ('{"geometry": {"coordinates": [-106.0, 40.0],' - ' "type": "Point"},' - ' "id": "1",' - ' "properties": {"title": "thingy one"}}')) + + # Use JSON comparison for geometry + geom_json = json.loads(geojson.dumps(ob.__geo_interface__['geometry'], + sort_keys=True)) + self.assertEqual(geom_json, {"coordinates": [-106.0, 40.0], "type": "Point"}) + + # Use JSON comparison for the whole object + expected_obj = { + "geometry": {"coordinates": [-106.0, 40.0], "type": "Point"}, + "id": "1", + "properties": {"title": "thingy one"}, + "type": "Feature" + } + ob_json = json.loads(geojson.dumps(ob, sort_keys=True)) + self.assertEqual(ob_json, expected_obj) + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file From 3f82a637d6c4153c5d81b8b3899ae8e0c785e95d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 25 May 2026 09:44:45 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_features.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/test_features.py b/tests/test_features.py index f9d86f6..9f2a71e 100644 --- a/tests/test_features.py +++ b/tests/test_features.py @@ -19,7 +19,7 @@ def test_protocol(self): json_str = geojson.dumps(f, sort_keys=True) # Parse JSON to avoid formatting issues across Python versions json_obj = json.loads(json_str) - + expected = { "geometry": {"coordinates": [53.0, -4.0], "type": "Point"}, "id": "1", @@ -57,7 +57,7 @@ def test_feature_class(self): self.assertEqual(feature.properties['summary'], 'The first feature') self.assertEqual(feature.properties['link'], 'http://example.org/features/1') - + # Encoding - use JSON comparison to avoid formatting issues geometry_obj = json.loads(geojson.dumps(feature.geometry, sort_keys=True)) self.assertEqual(geometry_obj, {"coordinates": [53.0, -4.0], "type": "Point"}) @@ -72,7 +72,7 @@ def test_feature_class(self): }, "type": "Feature" } - + feature_json_obj = json.loads(geojson.dumps(feature, sort_keys=True)) self.assertEqual(feature_json_obj, expected_feature) @@ -92,7 +92,7 @@ def test_feature_class(self): self.assertEqual(feature.properties['summary'], 'The first feature') self.assertEqual(feature.properties['link'], 'http://example.org/features/1') - + # Compare geometry using JSON geom_obj = json.loads(geojson.dumps(feature.geometry, sort_keys=True)) self.assertEqual(geom_obj, {"coordinates": [53.0, -4.0], "type": "Point"}) @@ -113,12 +113,12 @@ def __geo_interface__(self): "coordinates": (self.x, self.y)}} ob = Thingy('1', 'thingy one', -106.0, 40.0) - + # Use JSON comparison for geometry geom_json = json.loads(geojson.dumps(ob.__geo_interface__['geometry'], sort_keys=True)) self.assertEqual(geom_json, {"coordinates": [-106.0, 40.0], "type": "Point"}) - + # Use JSON comparison for the whole object expected_obj = { "geometry": {"coordinates": [-106.0, 40.0], "type": "Point"}, @@ -131,4 +131,4 @@ def __geo_interface__(self): if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()