From eba53ef675ab5b66b26e76b45b49261c5841164e Mon Sep 17 00:00:00 2001 From: Satyam Soni Date: Wed, 7 Feb 2024 17:25:55 +0530 Subject: [PATCH 1/4] Modified check_ctor_args to pass default SRS_ID value in case of null --- geoalchemy2/types/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/geoalchemy2/types/__init__.py b/geoalchemy2/types/__init__.py index 87d338db..cf278626 100644 --- a/geoalchemy2/types/__init__.py +++ b/geoalchemy2/types/__init__.py @@ -179,7 +179,8 @@ def process(bindvalue): @staticmethod def check_ctor_args(geometry_type, srid, dimension, use_typmod, nullable): try: - srid = int(srid) + # passing default SRID is it is NULL from DB + srid = int(srid or -1) except ValueError: raise ArgumentError("srid must be convertible to an integer") if geometry_type: From fdb8ed95a0a71cdac0e15edffcf701277a88533c Mon Sep 17 00:00:00 2001 From: satyam soni Date: Thu, 8 Feb 2024 09:02:44 +0530 Subject: [PATCH 2/4] Added Tests and refactored code for substuting Null values skipping 0 --- geoalchemy2/types/__init__.py | 4 ++-- tests/test_types.py | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/geoalchemy2/types/__init__.py b/geoalchemy2/types/__init__.py index cf278626..c9d9575d 100644 --- a/geoalchemy2/types/__init__.py +++ b/geoalchemy2/types/__init__.py @@ -180,8 +180,8 @@ def process(bindvalue): def check_ctor_args(geometry_type, srid, dimension, use_typmod, nullable): try: # passing default SRID is it is NULL from DB - srid = int(srid or -1) - except ValueError: + srid = int(srid if srid is not None else -1) + except (ValueError, TypeError): raise ArgumentError("srid must be convertible to an integer") if geometry_type: geometry_type = geometry_type.upper() diff --git a/tests/test_types.py b/tests/test_types.py index d97d9309..d34cf18a 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -44,6 +44,10 @@ def test_get_col_spec(self): g = Geometry(srid=900913) assert g.get_col_spec() == "geometry(GEOMETRY,900913)" + def test_get_col_spec_no_srid(self): + g = Geometry(srid=None) + assert g.get_col_spec() == "geometry(GEOMETRY,-1)" + def test_get_col_spec_no_typmod(self): g = Geometry(geometry_type=None) assert g.get_col_spec() == "geometry" From ebc4b3fbb0e0b4f4c20680ae86f650d7f1b8a523 Mon Sep 17 00:00:00 2001 From: satyam soni Date: Thu, 8 Feb 2024 09:17:20 +0530 Subject: [PATCH 3/4] Added test case for invalid string srid. --- tests/test_types.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_types.py b/tests/test_types.py index d34cf18a..0b9b4452 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -48,6 +48,12 @@ def test_get_col_spec_no_srid(self): g = Geometry(srid=None) assert g.get_col_spec() == "geometry(GEOMETRY,-1)" + def test_get_col_spec_invalid_srid(self): + with pytest.raises(ArgumentError) as e: + g = Geometry(srid="foo") + g.get_col_spec() + assert str(e.value) == "srid must be convertible to an integer" + def test_get_col_spec_no_typmod(self): g = Geometry(geometry_type=None) assert g.get_col_spec() == "geometry" From 78fa8212817e7c3d640c819ea661d446b5b3d43d Mon Sep 17 00:00:00 2001 From: satyam soni Date: Thu, 8 Feb 2024 20:35:01 +0530 Subject: [PATCH 4/4] Fixed typo --- geoalchemy2/types/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/geoalchemy2/types/__init__.py b/geoalchemy2/types/__init__.py index c9d9575d..3708b014 100644 --- a/geoalchemy2/types/__init__.py +++ b/geoalchemy2/types/__init__.py @@ -179,7 +179,7 @@ def process(bindvalue): @staticmethod def check_ctor_args(geometry_type, srid, dimension, use_typmod, nullable): try: - # passing default SRID is it is NULL from DB + # passing default SRID if it is NULL from DB srid = int(srid if srid is not None else -1) except (ValueError, TypeError): raise ArgumentError("srid must be convertible to an integer")