geoalchemy / geoalchemy2

Geospatial extension to SQLAlchemy
http://geoalchemy-2.readthedocs.org
MIT License
637 stars 112 forks source link

Type: Fix Modified check_ctor_args to pass default SRS_ID value in case of null #488

Closed satyamsoni2211 closed 9 months ago

satyamsoni2211 commented 9 months ago

Currently the column conversion from NullType to Geometry by SQLAlchemy is failing due to NULL SRS_ID from the DB. In such scenario, where a null value is received, function should be able to override it by default value of -1 rather than trying to convert NoneType to int. This fix takes into consideration failure and override NoneType by default value.

Below is the error that occurred due to missing SRS_ID.

  File "/Users/satyamsoni/Documents/alchemy/.venv/lib/python3.11/site-packages/geoalchemy2/types/__init__.py", line 127, in __init__
    geometry_type, srid = self.check_ctor_args(
                          ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/satyamsoni/Documents/alchemy/.venv/lib/python3.11/site-packages/geoalchemy2/types/__init__.py", line 182, in check_ctor_args
    srid = int(srid)
           ^^^^^^^^^
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'

Post fix this seems to be working fine.

image

Checklist

This pull request is:

satyamsoni2211 commented 9 months ago

@adrien-berchet test seems to pass now with this fix. I have added test_get_col_spec_no_srid test case for this scenario and test_get_col_spec_invalid_srid for invalid string srid.

 ~/Documents/alchemy ·················································································································································· alchemy Py │ at 09:15:53 ─╮
❯ pytest -v                                                                                                                                                                                      ─╯
======================================================================================= test session starts ========================================================================================
platform darwin -- Python 3.11.4, pytest-7.4.4, pluggy-1.4.0 -- /Users/satyamsoni/Documents/alchemy/.venv/bin/python
cachedir: .pytest_cache
rootdir: /Users/satyamsoni/Documents/alchemy
plugins: asyncio-0.23.4, custom-exit-code-0.3.0, cov-4.1.0, mock-3.12.0
asyncio: mode=Mode.STRICT
collected 38 items                                                                                                                                                                                 

tests/test_geometry.py::TestGeometry::test_get_col_spec PASSED                                                                                                                               [  2%]
tests/test_geometry.py::TestGeometry::test_get_col_spec_no_srid PASSED                                                                                                                       [  5%]
tests/test_geometry.py::TestGeometry::test_get_col_spec_invalid_srid PASSED                                                                                                                  [  7%]
tests/test_geometry.py::TestGeometry::test_get_col_spec_no_typmod PASSED                                                                                                                     [ 10%]
tests/test_geometry.py::TestGeometry::test_check_ctor_args_bad_srid PASSED                                                                                                                   [ 13%]
tests/test_geometry.py::TestGeometry::test_get_col_spec_geometryzm PASSED                                                                                                                    [ 15%]
tests/test_geometry.py::TestGeometry::test_get_col_spec_geometryz PASSED                                                                                                                     [ 18%]
tests/test_geometry.py::TestGeometry::test_get_col_spec_geometrym PASSED                                                                                                                     [ 21%]
tests/test_geometry.py::TestGeometry::test_check_ctor_args_srid_not_enforced PASSED                                                                                                          [ 23%]
tests/test_geometry.py::TestGeometry::test_check_ctor_args_use_typmod_nullable PASSED                                                                                                        [ 26%]
tests/test_geometry.py::TestGeometry::test_column_expression PASSED                                                                                                                          [ 28%]
tests/test_geometry.py::TestGeometry::test_select_bind_expression PASSED                                                                                                                     [ 31%]
tests/test_geometry.py::TestGeometry::test_insert_bind_expression PASSED                                                                                                                     [ 34%]
tests/test_geometry.py::TestGeometry::test_function_call PASSED                                                                                                                              [ 36%]
tests/test_geometry.py::TestGeometry::test_non_ST_function_call PASSED                                                                                                                       [ 39%]
tests/test_geometry.py::TestGeometry::test_subquery PASSED                                                                                                                                   [ 42%]
tests/test_geometry.py::TestGeography::test_get_col_spec PASSED                                                                                                                              [ 44%]
tests/test_geometry.py::TestGeography::test_get_col_spec_no_typmod PASSED                                                                                                                    [ 47%]
tests/test_geometry.py::TestGeography::test_column_expression PASSED                                                                                                                         [ 50%]
tests/test_geometry.py::TestGeography::test_select_bind_expression PASSED                                                                                                                    [ 52%]
tests/test_geometry.py::TestGeography::test_insert_bind_expression PASSED                                                                                                                    [ 55%]
tests/test_geometry.py::TestGeography::test_function_call PASSED                                                                                                                             [ 57%]
tests/test_geometry.py::TestGeography::test_non_ST_function_call PASSED                                                                                                                      [ 60%]
tests/test_geometry.py::TestGeography::test_subquery PASSED                                                                                                                                  [ 63%]
tests/test_geometry.py::TestPoint::test_get_col_spec PASSED                                                                                                                                  [ 65%]
tests/test_geometry.py::TestCurve::test_get_col_spec PASSED                                                                                                                                  [ 68%]
tests/test_geometry.py::TestLineString::test_get_col_spec PASSED                                                                                                                             [ 71%]
tests/test_geometry.py::TestPolygon::test_get_col_spec PASSED                                                                                                                                [ 73%]
tests/test_geometry.py::TestMultiPoint::test_get_col_spec PASSED                                                                                                                             [ 76%]
tests/test_geometry.py::TestMultiLineString::test_get_col_spec PASSED                                                                                                                        [ 78%]
tests/test_geometry.py::TestMultiPolygon::test_get_col_spec PASSED                                                                                                                           [ 81%]
tests/test_geometry.py::TestGeometryCollection::test_get_col_spec PASSED                                                                                                                     [ 84%]
tests/test_geometry.py::TestRaster::test_get_col_spec PASSED                                                                                                                                 [ 86%]
tests/test_geometry.py::TestRaster::test_column_expression PASSED                                                                                                                            [ 89%]
tests/test_geometry.py::TestRaster::test_insert_bind_expression PASSED                                                                                                                       [ 92%]
tests/test_geometry.py::TestRaster::test_function_call PASSED                                                                                                                                [ 94%]
tests/test_geometry.py::TestRaster::test_non_ST_function_call PASSED                                                                                                                         [ 97%]
tests/test_geometry.py::TestCompositeType::test_ST_Dump PASSED                                                                                                                               [100%]

======================================================================================== 38 passed in 0.11s ========================================================================================
adrien-berchet commented 9 months ago

Thanks @satyamsoni2211 ! Do you want a new release with this fix?

satyamsoni2211 commented 9 months ago

@adrien-berchet patch version bump would work 0.14.4 maybe. If you can release, I will incorporate in my project.

satyamsoni2211 commented 9 months ago

I would also like you to see if my library can be of use to you. Lazy Alchemy. This lazily reflects table from any schema and caches it.

adrien-berchet commented 9 months ago

The new release is up! https://pypi.org/project/GeoAlchemy2/0.14.4/

About your project, that's interesting indeed. But I currently don't work on any project where the number of schemas is an issue (I actually didn't work on any project with a DB for a while :sweat_smile: ). Did you propose to integrate this into SQLAchemy? So it would be available to all the users.