
    jj                     v   S r SSKrSSKrSSKrSSKJr  SSKrSSKrSSK	J
r  SSKJrJrJr  SSK	Jr  SSKJr  SSKJr  SSKrSSKJrJrJrJr   SS	KJr  \R>                  S
 5       r S\!\   4S jr"\"" 5       r#S\$4S jr%\R>                  " 5       S 5       r&\R>                  " 5       S 5       r'\R>                  " 5       S 5       r(S r)\R>                  S 5       r*\R>                  S 5       r+\R>                  S 5       r,\R>                  S 5       r-\R>                  S 5       r. " S S5      r/g! \ a    \r Nf = f)a  
Tests here include reading/writing to different types of spatial databases.
The spatial database tests may not work without additional system
configuration. postGIS tests require a test database to have been setup;
see geopandas.tests.util for more information.
    N)	find_spec)GeoDataFrame	read_fileread_postgis)
HAS_PYPROJ)	_get_conn)_write_postgis)create_postgiscreate_spatialitemockvalidate_boro_df)textc                     [        U 5      nU$ )N)r   )nybb_filenamedfs     ^/var/www/html/Sattelite-Image/venv/lib/python3.13/site-packages/geopandas/io/tests/test_sql.pydf_nybbr   &   s    	=	!BI    returnc                      / n [        S5      (       a  U R                  S5        [        S5      (       a  U R                  S5        U $ )zWork out which of psycopg2 and psycopg are available.
This prevents tests running if the relevant package isn't installed
(rather than being skipped, as skips are treated as failures during postgis CI)
psycopgpsycopg2)r   append)driverss    r   check_available_postgis_driversr   ,   s<    
 Gy!z"Nr   c                      S[         R                  R                  S5      [         R                  R                  S5      [         R                  R                  S5      [         R                  R                  S5      S.$ )zBGather postgres connection credentials from environment variables.test_geopandasPGUSER
PGPASSWORDPGHOSTPGPORT)dbnameuserpasswordhostport)osenvironget r   r   prepare_database_credentialsr+   <   sQ     #

x(JJNN<0

x(

x( r   c              #     #    [         R                  " U R                  5      n UR                  " S0 [	        5       D6n[        R                  " 5          [        R                  " SSS9  Wv   SSS5        WR                  5         g! UR
                   a    [         R                  " S5         Npf = f! , (       d  f       NK= f7f)zCreate a postgres connection using either psycopg2 or psycopg.

Use this as an indirect fixture, where the request parameter is POSTGIS_DRIVERS.'Cannot connect with postgresql databaseignorez-pandas only supports SQLAlchemy connectable.*messageNr*   )pytestimportorskipparamconnectr+   OperationalErrorskipwarningscatch_warningsfilterwarningsclose)requestr   cons      r   connection_postgisr=   G   s     
 !!'--0G?oo? < >? 
	 	 	"M	
 		 
#
 IIK ## ?=>?	"	"s?   !CB CB2-C&B/,C.B//C2
C <Cc              #   l  #    [         R                  " S5      nSSKJn  [	        5       n UR                  UR                  SU R                   3US   US   US   US   US	   S
95      nUR                  5         Wv   UR                  5         g! [         a    [         R                  " S5         N7f = f7f)z
Initiate a sqlalchemy connection engine using either psycopg2 or psycopg.

Use this as an indirect fixture, where the request parameter is POSTGIS_DRIVERS.

sqlalchemyr   )URLzpostgresql+r#   r"   r$   r%   r&   )
drivernameusernamedatabaser$   r%   r&   r-   N)r1   r2   sqlalchemy.engine.urlr@   r+   create_enginecreater3   r4   	Exceptionr6   dispose)r;   r?   r@   credentialsr<   s        r   engine_postgisrJ   Z   s      $$\2J).0K?&&JJ(8$V,$X.$Z0 ( (  	
 	 IKKM	  ?=>?s)   'B4AB 9B4 B1.B40B11B4c               #     #    [         R                  " S5      n  U R                  S5       nUR                  S5        UR	                  S5        UR                  S5        SSS5        Wv   UR                  5         g! , (       d  f       N#= f! [         a)    WR                  5         [         R                  " S5         NXf = f7f)a  
Return a memory-based SQLite3 connection with SpatiaLite enabled & initialized.

`The sqlite3 module must be built with loadable extension support
<https://docs.python.org/3/library/sqlite3.html#f1>`_ and
`SpatiaLite <https://www.gaia-gis.it/fossil/libspatialite/index>`_
must be available on the system as a SQLite module.
Packages available on Anaconda meet requirements.

Exceptions
----------
``AttributeError`` on missing support for loadable SQLite extensions
``sqlite3.OperationalError`` on missing SpatiaLite
sqlite3z:memory:Tmod_spatialitez SELECT InitSpatialMetaData(TRUE)Nz Cannot setup spatialite database)	r1   r2   r4   enable_load_extensionload_extensionexecuterG   r:   r6   )rL   r<   s     r   connection_spatialiterQ   x   s       !!),G8__Z(C%%d+/0KK:; ) IIIK )(  8		678sK   CB 4A<B 'C<
B
B 	C
B 0C =C?C  Cc                    [         R                  " S5      nUR                  U 5      R                  U5      (       a  UR	                  5       n[
        R                  " 5          [
        R                  " SSS9  UR                  U 5        S S S 5        UR                  R                  U5      nUb  UR                  U SS9  g g g ! , (       d  f       N?= f)Nr?   r.   z-Did not recognize type 'geometry' of column.*r/   T)
checkfirst)r1   r2   inspect	has_tableMetaDatar7   r8   r9   reflecttablesr)   drop)conn_or_enginetabler?   metadatas       r   drop_table_if_existsr]      s    $$\2J.)33E::&&($$&##"Q ^,	 '
 ##E*JJ~$J7  ;&&s   !'C
Cc            	          SSK Jn JnJn  [        R
                  " SU " SS/5      U" SS/SS//5      U" SS5      /0S	S
9nU$ )Nr   )
LineStringMultiLineStringPointgeometryr   r      re   )   rf   )   rg   re   	epsg:4326crs)shapely.geometryr_   r`   ra   	geopandasr   )r_   r`   ra   r   s       r   df_mixed_single_and_multirm      s^    CC			FF+,&&!1FF3C DEa	
 	
B Ir   c            
      r    SSK Jn   [        R                  " S U " SS/SS/SS/SS////5      /SS/S.SS9nU$ )Nr   )MultiPolygonre   )rb   idrh   ri   )rk   ro   rl   r   )ro   r   s     r   df_mixed_none_geometryrq      s`    -			 AAAA?@AB a&	
 	
B Ir   c                      SSK Jn JnJnJn  [
        R                  " SU " U" / SQ5      U" SS/5      U" SS5      /5      /0SS9nU$ )	Nr   )GeometryCollectionr_   ra   Polygonrb   )rc   rd   r   re   rc   rd   rh   ri   )rk   rs   r_   ra   rt   rl   r   )rs   r_   ra   rt   r   s        r   df_geom_collectionrv      s]    OO			" 89"FF#34a
	
 
B Ir   c                  N    SSK Jn   [        R                  " SU " S5      /0SS9nU$ )Nr   )
LinearRingrb   )rc   ru   rd   )re   r   rh   ri   )rk   rx   rl   r   )rx   r   s     r   df_linear_ringry      s/    +				j!ABCD+
B Ir   c            
      ~    SSK Jn JnJn  [        R
                  " SU " SS/5      U" / SQ5      U" SSS5      /0S	S
9nU$ )Nr   )r_   ra   rt   rb   r   r   r   re   re   re   )r{   r|   )r   re   re   re   rf   rh   ri   )rk   r_   ra   rt   rl   r   )r_   ra   rt   r   s       r   df_3D_geomsr}      sR    ;;			Iy129:aA	
 	
B Ir   c                   `   \ rS rSr\R
                  R                  S\SS9S 5       r\R
                  R                  S\SS9S 5       r	\R
                  R                  S\SS9S 5       r
\R
                  R                  S\SS9S	 5       r\R
                  R                  S\SS9S
 5       r\R
                  R                  S\SS9S 5       r\R
                  R                  S\SS9S 5       r\R
                  R                  S\SS9S 5       rS rS r\R
                  R                  S\SS9S 5       r\R
                  R                  S\SS9S 5       r\R
                  R                  S\SS9S 5       r\R
                  R                  S\SS9S 5       r\R
                  R                  S\SS9S 5       r\R
                  R                  S\SS9S 5       r\R
                  R                  S\SS9S 5       r\R
                  R                  S\SS9S 5       r\R
                  R                  S\SS9S 5       r\R
                  R                  S\SS9S 5       r\R
                  R                  S\SS9S 5       r\R
                  R                  S\SS9S 5       r\R
                  R                  S\SS9S 5       r\R
                  R                  S\SS9S 5       r\R
                  R                  S\SS9S 5       r \R
                  R                  S\SS9S 5       r!\R
                  R                  S\SS9S  5       r"\R
                  R                  S\SS9S! 5       r#\R
                  R                  S\SS9S" 5       r$\R
                  R                  S\SS9S# 5       r%\R
                  R                  S\SS9S$ 5       r&\R
                  R                  S\SS9\R
                  RO                  \(RR                  (       + S%S&9S' 5       5       r*\R
                  R                  S\SS9S( 5       r+\R
                  RY                  \-(       + S)S&9\.R^                  " S*5      \R
                  R                  S\SS9S+ 5       5       5       r0\.R^                  " S,5      \R
                  R                  S\SS9S- 5       5       r1\R
                  R                  S\SS9S. 5       r2\R
                  R                  S\SS9S/ 5       r3\R
                  R                  S\SS9S0 5       r4\R
                  R                  S\SS9S1 5       r5\R
                  R                  S\SS9S2 5       r6\R
                  R                  S\SS9S3 5       r7\R
                  R                  S\SS9S4 5       r8S5r9g6)7TestIO   rJ   T)indirectc                 @   [         R                  " S5      R                  nUn[        U5       n[	        XB5      (       d   e S S S 5        UR                  5        n[        U5       n[	        XB5      (       d   e S S S 5        S S S 5        [         R                  " [        5         [        [        5       5          S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       Nh= f! , (       d  f       Nq= f! , (       d  f       NJ= f! , (       d  f       g = f)Nzsqlalchemy.engine.base)	r1   r2   
Connectionget_conn
isinstancer4   raises
ValueErrorobject)selfrJ   r   engineoutputconns         r   test_get_connTestIO.test_get_conn   s    (()ABMM
ff1111 ^^$6!&5555    ]]:&&(# $ '&     $# '&sS   CC-&C:C-$D8C>:D
C
C*	&C--
C;>
D	D
Dr=   c                 l    Un[        X25        Sn[        XC5      n[        U5        UR                  b   eg )NSELECT * FROM nybb;)r
   r   r   rj   r   r=   r   r<   sqlr   s         r   test_read_postgis_default TestIO.test_read_postgis_default  s6     s$### vv~~r   c                 N    UnSn[        X2US9  Sn[        XSUS9n[        U5        g )Nthe_geomgeom_colr   r
   r   r   r   r=   r   r<   r   r   r   s          r   !test_read_postgis_custom_geom_col(TestIO.test_read_postgis_custom_geom_col  s/     sh7##X6r   c                 `    UnSnSn[        X2US9  SU SU S3n[        XcUS9n[        U5        g)z6Tests that a SELECT {geom} AS {some_other_geom} works.geomr   r   zGSELECT borocode, boroname, shape_leng, shape_area,
                    z as z FROM nybb;Nr   )r   r=   r   r<   	orig_geomout_geomr   r   s           r    test_read_postgis_select_geom_as'TestIO.test_read_postgis_select_geom_as  sJ     !	si8KtH:[<#X6r   c                     UnSnUR                  U5      n[        X5SS9  Sn[        Xc5      n[        U5        UR                  U:X  d   eg)z<Tests that an SRID can be read from a geodatabase (GH #451).z	epsg:4269  sridr   Nto_crsr
   r   r   rj   )r   r=   r   r<   rj   	df_reprojr   r   s           r   test_read_postgis_get_srid!TestIO.test_read_postgis_get_srid,  sM     !NN3'	sD1###vv}}r   c                     UnUR                   n[        X2SS9  Sn[        XSUS9n[        U5        UR                   U:X  d   eg)z?Tests that a user specified CRS overrides the geodatabase SRID.r   r   r   ri   N)rj   r
   r   r   )r   r=   r   r<   orig_crsr   r   s          r   test_read_postgis_override_srid&TestIO.test_read_postgis_override_srid:  sG     !;;s$/##1vv!!!r   c                 b    Un[        X25        Sn[        R                  " XC5      n[        USS9  g )Nr   Fcase_sensitiver
   r   from_postgisr   r   s         r   test_from_postgis_default TestIO.test_from_postgis_defaultG  s.     s$#&&s0E2r   c                 b    UnSn[        X2US9  Sn[        R                  " XSUS9n[        USS9  g )Nr   r   r   Fr   r   r   s          r   !test_from_postgis_custom_geom_col(TestIO.test_from_postgis_custom_geom_colQ  s7     sh7#&&s(CE2r   c                     UnUR                   R                  nSUR                  S'   [        X25        SU SU S3n[	        XSUS9n[        U5        g)z*Tests that geometry with NULL is accepted.N)r   rb   zDSELECT ogc_fid, borocode, boroname, shape_leng, shape_area, AsEWKB("") AS "" FROM nybbr   )rb   namelocr   r   r   r   rQ   r   r<   r   r   r   s          r   test_read_postgis_null_geom"TestIO.test_read_postgis_null_geom\  s`    ###((%)M"#'jz> 	 #X6r   c                     UnUR                   R                  n[        X25        SU SU S3n[        XSUS9n[	        U5        g)z/Tests that geometry read as binary is accepted.zISELECT ogc_fid, borocode, boroname, shape_leng, shape_area, ST_AsBinary("r   r   r   N)rb   r   r   r   r   r   s          r   test_read_postgis_binaryTestIO.test_read_postgis_binaryi  sS    ###((#'$:WXJkC 	 #X6r   c                     SnUn[        XB5        Sn[        R                  " [        XTUS95      n[	        U5        UR
                  b   eg)zTest chunksize argumentrf   r   	chunksizeN)r
   pdconcatr   r   rj   r   r=   r   r   r<   r   r   s          r   test_read_postgis_chunksize"TestIO.test_read_postgis_chunksizeu  sH     	 s$#YY|C	BC vv~~r   c                     UnSn[        X45        [        X#USS9  [        SU S35      n[        XSSS9n[	        U5        g)	@Tests that GeoDataFrame can be written to PostGIS with defaults.nybbfailr<   r   	if_existsSELECT * FROM ;rb   r   Nr]   write_postgisr   r   r   r   rJ   r   r   r[   r   r   s          r   test_write_postgis_default!TestIO.test_write_postgis_default  sM       	V+ 	gH^E7!,-#
;r   c                     UnSn[        X45        [        X#USS9  [        SU S35      n[        XSSS9n[	        U5        g)	z?Tests writing GeoDataFrame to PostGIS with uppercase tablename.
aTestTabler   r   SELECT * FROM "";rb   r   Nr   r   s          r   &test_write_postgis_uppercase_tablename-TestIO.test_write_postgis_uppercase_tablename  sM       	V+ 	gH_UG2./#
;r   c                     UR                  5        nSn[        X45        [        X#USS9  [        SU S35      n[	        XSSS9n[        U5        SSS5        g! , (       d  f       g= f)	r   nybb_conr   r   r   r   rb   r   N)beginr]   r   r   r   r   )r   rJ   r   r<   r[   r   r   s          r   (test_write_postgis_sqlalchemy_connection/TestIO.test_write_postgis_sqlalchemy_connection  sb     !!#sE !, '&Iwa01Cc<BR  $##s   =A
A%c                     UnSn[        X#USS9   [        X#USS9  g! [         a  nS[        U5      ;   a   SnAgUeSnAff = f)zK
Tests that uploading the same table raises error when: if_replace='fail'.
r   replacer   r   zalready existsN)r   r   str)r   rJ   r   r   r[   es         r   )test_write_postgis_fail_when_table_exists0TestIO.test_write_postgis_fail_when_table_exists  sQ    
   	gK	'EVL 	3q6)		s    
A==Ac                     UnSn[        X#USS9  [        X#USS9  [        SU S35      n[        XSSS9n[        U5        g)	zF
Tests that replacing a table is possible when: if_replace='replace'.
r   r   r   r   r   rb   r   N)r   r   r   r   r   s          r   ,test_write_postgis_replace_when_table_exists3TestIO.test_write_postgis_replace_when_table_exists  sO    
   	gKgK^E7!,-#
;r   c                     UnSnUR                   u  pV[        X#USS9  [        X#USS9  [        SU S35      n[        XsSS9nUR                   u  pXS	-  :X  d   S
US	-   SU	 345       eX:X  d   S
U SU
 345       eg)z\
Tests that appending to existing table produces correct results when:
if_replace='append'.
r   r   r   r   r   r   rb   r   rf   zThere should be z rows,found: z columns,found: N)shaper   r   r   )r   rJ   r   r   r[   	orig_rows	orig_colsr   r   new_rowsnew_colss              r   +test_write_postgis_append_when_table_exists2TestIO.test_write_postgis_append_when_table_exists  s      &}}	gKgJ^E7!,-#
;XX q=( 	
y1}o]8*E+
 	
( $ 	
yk)9(D'
 	
$r   c                    UnSnSUR                   R                  l        [        R                  " [
        SS9   [        X#USS9  SSS5        [        SR                  SUS	S
95      nUR                  5        nUR                  U5      R                  5       S   nSSS5        WS:X  d
   SU 35       eg! , (       d  f       Nt= f! , (       d  f       N0= f)zL
Tests that GeoDataFrame can be written to PostGIS without CRS information.
r   Nz%Could not parse CRS from the GeoDataFmatchr   r   6SELECT Find_SRID('{schema}', '{table}', '{geom_col}');publicrb   schemar[   r   r   zSRID should be 0, found )rb   arrayrj   r1   warnsUserWarningr   r   formatr4   rP   fetchone)r   rJ   r   r   r[   r   r   target_srids           r   test_write_postgis_without_crs%TestIO.test_write_postgis_without_crs  s    
   &*"\\+-TU'EYO V DKKuz L 

 ^^,,s+446q9K aI#;K=!II VU s   B37#C3
C
Cc                 0   UnSnUR                  S5      n[        XSUSS9  [        SR                  SUSS95      nUR	                  5        nUR                  U5      R                  5       S	   nS
S
S
5        WS:X  d
   SU 35       eg
! , (       d  f       N= f)zc
Tests that GeoDataFrame can be written to PostGIS with ESRI Authority
CRS information (GH #2414).
r   zESRI:102003r   r   r   r   rb   r   r   Nis zSRID should be 102003, found )r   r   r   r   r4   rP   r  )	r   rJ   r   r   r[   df_nybb_esrir   r   r  s	            r   &test_write_postgis_with_esri_authority-TestIO.test_write_postgis_with_esri_authority  s       ~~m4lUiPDKKuz L 

 ^^,,s+446q9K f$S(Ek]&SS$ s   #B
Bc                 |   UnSn[        X#USS9  [        SU S35      nUR                  5        nUR                  U5      R	                  5       S   nSSS5        [        SU S	35      n[        XSS
S9nWR                  5       S:X  d   eUR                  R                  5       S   S:X  d   eg! , (       d  f       Na= f)zC
Tests that writing a mix of different geometry types is possible.
geomtype_testsr   r   -SELECT DISTINCT(GeometryType(geometry)) FROM  ORDER BY 1;r   Nr   r   rb   r   GEOMETRYCOLLECTIONrs   )	r   r   r4   rP   r  r   upper	geom_typeunique)	r   rJ   rv   r   r[   r   r   r  r   s	            r   &test_write_postgis_geometry_collection-TestIO.test_write_postgis_geometry_collection#  s       (5IV B5'VW^^S)224Q7I ^E7!,-#
; $8888||""$Q'+???? s   #B--
B;c                 P   UnSn[        X#USS9  [        SU S35      nUR                  5        nUR                  U5      R	                  5       nSSS5        WS   S   R                  5       S:X  d   e[        R                  " US	   S   5      (       d   eg! , (       d  f       NN= f)
zA
Tests that writing a mix of None and MulitPolygons is possible.
r
  r   r   ,SELECT DISTINCT GeometryType(geometry) FROM r  Nr   MULTIPOLYGONre   )r   r   r4   rP   fetchallr  r   isna)r   rJ   rq   r   r[   r   r   ress           r   &test_write_postgis_none_geometry_types-TestIO.test_write_postgis_none_geometry_types:  s       "Ui	

 A%UV^^,,s#,,.C 1vay N222wws1vay!!!! s    B
B%c                 z   UnSn[        X#USS9  [        SU S35      nUR                  5        nUR                  U5      R	                  5       nSSS5        WS   S   R                  5       S:X  d   eUS	   S   R                  5       S
:X  d   eUS   S   R                  5       S:X  d   eg! , (       d  f       Nc= f)zE
Tests that writing a mix of single and MultiGeometries is possible.
r
  r   r   r  r  Nr   
LINESTRINGre   MULTILINESTRINGrf   POINT)r   r   r4   rP   r  r  )r   rJ   rm   r   r[   r   r   r  s           r   'test_write_postgis_mixed_geometry_types.TestIO.test_write_postgis_mixed_geometry_typesP  s       %	

 A%UV^^,,s#,,.C 1vay L0001vay $55551vay G+++	 s    B,,
B:c                    UnSn[        X#USS9  [        SU S35      nUR                  5        nUR                  U5      R	                  5       S   nSSS5        WR                  5       S:X  d   eg! , (       d  f       N%= f)	z"
Tests that writing a LinearRing.
r
  r   r   r  r  r   Nr  )r   r   r4   rP   r  r  )r   rJ   ry   r   r[   r   r   r  s           r   test_write_postgis_linear_ring%TestIO.test_write_postgis_linear_ringg  s}    
   nu	R B5'VW^^S)224Q7I   L000 s   #A11
A?c                 D   UnSn[        UUUSSS9  [        SU S35      nUR                  5        nUR                  U5      R	                  5       S   nSSS5        WS	:X  d   e[        S
U S35      nUR                  5        nUR                  U5      R                  5       nSSS5        WS   S   R                  5       S:X  d   eUS   S   R                  5       S:X  d   eUS   S   R                  5       S:X  d   eg! , (       d  f       N= f! , (       d  f       Nt= f)z#
Tests writing a LinearRing works.
r
  r   re   )r<   r   r   r   zSELECT COUNT(geometry) FROM r   r   Nrg   r  r  r  r  rf   r  )r   r   r4   rP   r  r  r  )	r   rJ   rm   r   r[   r   r   row_cntr  s	            r   test_write_postgis_in_chunks#TestIO.test_write_postgis_in_chunksy  s   
   %	
 1%:;^^ll3'00215G !|| A%UV^^,,s#,,.C 1vay L0001vay $55551vay G+++  s   #D  D 
D
Dc                 
   UnSnSn[        SU S35      nUR                  5        nUR                  U5        SSS5        [        X#USUS9  [        SU S	U S35      n[	        XcS
S9n[        U5        g! , (       d  f       NB= f)+
Tests writing data to alternative schema.
r   testCREATE SCHEMA IF NOT EXISTS r   Nr   r<   r   r   r   r   .rb   r   )r   r   rP   r   r   r   	r   rJ   r   r   r[   schema_to_user   r   r   s	            r   &test_write_postgis_to_different_schema-TestIO.test_write_postgis_to_different_schema  s    
  1-BC\\^tLL  	ey	
 ^M?!E7!<=#
; ^s   A44
Bc                    UnSnSn[        SU S35      nUR                  5        nUR                  U5        SSS5         [        X#USUS9  [        SU S	U S35      n[	        XcS
S9n[        U5        [        X#USUS9  [        SU S	U S35      n[	        XcS
S9n[        U5        g! , (       d  f       Nv= f! [         a     NQf = f)r)  r   r*  r+  r   Nr   r,  r   r-  rb   r   r   )r   r   rP   r   r   r   r   r.  s	            r   8test_write_postgis_to_different_schema_when_table_exists?TestIO.test_write_postgis_to_different_schema_when_table_exists  s      1-BC\\^tLL 	%6- awa@ACcJ?BR  	ey	
 ^M?!E7!<=#
;3 ^  		s   B(3B9 (
B69
CCc                     UnSn[        X#USS9  [        SU S35      n[        XSSS9n[        UR                  R
                  5      / SQ:X  d   eg	)
z5
Tests writing a geometries with 3 dimensions works.
r
  r   r   r   r   rb   r   )TTTN)r   r   r   listrb   has_z)r   rJ   r}   r   r[   r   r   s          r    test_write_postgis_3D_geometries'TestIO.test_write_postgis_3D_geometries  sY    
   kEYO ^E7!,-#
;BKK%%&*<<<<r   c                     UnSnUS   R                  5       n[        X#USS9  [        SU S35      n[        XcSS9nUS   R                  5       U:X  d   eg	)
zO
Tests that the row order in db table follows the order of the original frame.
row_order_testBoroCoder   r   r   r   rb   r   N)tolistr   r   r   )r   rJ   r   r   r[   correct_orderr   r   s           r   test_row_orderTestIO.test_row_order  si    
   
+224gK ^E7!,-#
;*~$$&-777r   c                     UnSn[        X45        [        X#USS9  [        SU S35      n[        XSSS9n[	        U5        g)	zP
Tests that insert works with if_exists='append' when table does not exist yet.
r   r   r   r   r   rb   r   Nr   r   s          r   test_append_before_table_exists&TestIO.test_append_before_table_exists  sK    
  V+gJ ^E7!,-#
;r   c                     UnSn[        X#USS9  UR                  SS9n[        R                  " [        SS9   [        XSUSS9  S	S	S	5        g	! , (       d  f       g	= f)
zC
Tests that the warning is raised if table CRS differs from frame.
r   r   r     )epsgzCRS of the target tabler   r   N)r   r   r1   r   r   r   rJ   r   r   r[   df_nybb2s         r   test_append_with_different_crs%TestIO.test_append_with_different_crs  sV    
  gK >>t>, ]]:-FG(UhO HGGs   A
Ac                 \    UnUR                  S SS9nSn[        X#USS9  Un[        XSUSS9  g )NT)allow_overrider   r   r   r   )set_crsr   rG  s         r   test_append_without_crsTestIO.test_append_without_crs  s?      //$t/<gK h(Kr   zEDuplicate columns are dropped in read_sql with pandas 2.0.0 and 2.0.1)reasonc                     UnSn[         R                  " [        5         [        X2SS9  SSS5        g! , (       d  f       g= f)zQ
Tests that a ValueError is raised if an SQL query returns two geometry columns.
z>select ST_MakePoint(0, 0) as geom, ST_MakePoint(0, 0) as geom;r   r   N)r1   r   r   r   )r   rJ   r   r   s       r   $test_duplicate_geometry_column_fails+TestIO.test_duplicate_geometry_column_fails(  s0      N]]:&v6 '&&s	   3
Ac                     UnUR                  SS9n[        X2SS9  Sn[        XC5      n[        U5        UR                  S:X  d   eg )N
esri:54052ri   $  r   r   
ESRI:54052r   r   s         r   test_read_non_epsg_crsTestIO.test_read_non_epsg_crs8  sJ     ..\.2s%0###vv%%%r   zpyproj not installedzshapely.get_sridc                 |   [         R                  " S5      nSUl        UnUR                  SS9n[	        XS5        Sn[         R
                  " UR                  R                  SS9   [         R                  " [        SS9   [        Xe5        S S S 5        S S S 5        g ! , (       d  f       N= f! , (       d  f       g = f)	Npyproji rh   ri   r   zcrs not foundr   zCould not find srid 99999)r1   r2   return_valuer   r
   r   
exceptionsCRSErrorr   r   r   )r   mock_get_sridr=   r   r[  r<   r   s          r   test_read_srid_not_in_table"TestIO.test_read_srid_not_in_tableC  s     $$X.%*" ..[.1s$#]]6,,55_Mk1LMS& N NMMM NMs$   %B-?BB-
B*	&B--
B;z(geopandas.io.sql._get_spatial_ref_sys_dfc                    [         R                  R                  Ul        UnUR	                  SS9n[        XCSS9  Sn[        R                  " [        SS9   [        XT5      nS S S 5        WR                  S:X  d   eg ! , (       d  f       N!= f)	Nrh   ri   rE  r   r   z1Could not find the spatial reference system tabler   z	EPSG:4326)r   errorsDatabaseErrorside_effectr   r
   r1   r   r   r   rj   )r   mock_get_spatial_ref_sys_dfr=   r   r<   r   r   s          r   -test_read_no_spatial_ref_sys_table_in_postgis4TestIO.test_read_no_spatial_ref_sys_table_in_postgisV  s}     35))2I2I#/ ..[.1s$/#\\R
 c'B

 vv$$$
 
s   A==
Bc                     SnUnUR                  SS9n[        XBSS9  Sn[        R                  " [	        XTUS95      n[        U5        UR                  S:X  d   eg	)
z)Test chunksize argument with non epsg crsrf   rU  ri   rV  r   r   r   rW  N)r   r
   r   r   r   r   rj   r   s          r    test_read_non_epsg_crs_chunksize'TestIO.test_read_non_epsg_crs_chunksizek  s\     	 ..\.2s%0#YY|C	BCvv%%%r   c                     UnSnUR                  5       nUR                  S5      n[        XSUSS9  [        XSUSS9  [        SU S35      n[	        XcSS9n[        U5      [        U5      S	-  :X  d   eg
)z
Tests that appending works correctly with custom geometry column names.
This ensures the SQL injection fix doesn't break normal functionality.
test_custom_geomcustom_geomr   r   r   r   r   r   rf   Ncopyrename_geometryr   r   r   lenr   rJ   r   r   r[   gdfr   r   s           r   7test_to_postgis_append_with_custom_geometry_column_name>TestIO.test_to_postgis_append_with_custom_geometry_column_namez  s      " lln!!-0 	cEYG 	cEXF _UG2./#>2w#c(Q,&&&r   c                 F   UnSnUR                  5       n[        XSUSS9  S[        R                  " 5       R                  SS  3nUR                  5        nUR                  [        SU S35      5        UR                  [        S	U S
35      5        SSS5        UR                  5       nSU S3n	UR                  U	5      n[        SU S35      n
[        SU S35      nUR                  5        nUR                  U
5      R                  5       S   nUR                  U5      R                  5       S   S:H  nSSS5        W(       d   S5       e [        XUSS9  UR                  5        nUR                  U
5      R                  5       S   nUR                  U5      R                  5       S   S:H  nSSS5        WW:X  d   S5       eW(       d   SU S35       eg! , (       d  f       GNN= f! , (       d  f       N= f! [        [        4 a     Nf = f! , (       d  f       Ng= f)aT  
Tests that SQL injection attempts via geometry column name are prevented.
This test verifies that malicious geometry column names are treated as
literal strings and not executed as SQL code.

This test is based on issue #3679 which reported SQL injection vulnerability
in the Find_SRID query when using to_postgis with if_exists='append'.
test_sql_injectionr   r   test_target_N   CREATE TABLE IF NOT EXISTS z (id INTEGER);INSERT INTO z (id) VALUES (1);zgeom'; DROP TABLE z; --SELECT COUNT(*) FROM r   r   re   6Target table should exist before SQL injection attemptr   9Test table should still exist after SQL injection attemptTarget table 'z(' should not be dropped by SQL injectionrp  r   uuiduuid4hexr   rP   r   rq  r4   r  r   rG   r   rJ   r   r   r[   
gdf_normaltest_target_tabler   gdf_maliciousmalicious_geom_name	sql_checksql_check_targetinitial_counttarget_existsfinal_counttarget_still_existss                   r   8test_to_postgis_sql_injection_prevention_geometry_column?TestIO.test_to_postgis_sql_injection_prevention_geometry_column  s&     $ \\^
j5IN
 +4::<+;+;CR+@*AB\\^tLL23D2E^TU LL->,??PQRS	    23D2ETJ%556IJ 0q9:	"78I7J! LM^^ LL3<<>qAM LL)9:CCEaHAMM  VVV}	-%8T ^^,,y1::<Q?K"&,,/?"@"I"I"KA"NRS"S  m+ 	
G	
+ # 	
.//WX	
"O ^  I& 		
 8   =G/AG+G< )AH
G(+
G9<HH
H c                 F   UnSnUR                  5       n[        XSUSS9  S[        R                  " 5       R                  SS  3nUR                  5        nUR                  [        SU S35      5        UR                  [        S	U S
35      5        SSS5        UR                  5       nSU S3n	UR                  U	5      n[        SU S35      n
[        SU S35      nUR                  5        nUR                  U
5      R                  5       S   nUR                  U5      R                  5       S   S:H  nSSS5        W(       d   S5       e [        XUSS9  UR                  5        nUR                  U
5      R                  5       S   nUR                  U5      R                  5       S   S:H  nSSS5        WW:X  d   S5       eW(       d   SU S35       eg! , (       d  f       GNN= f! , (       d  f       N= f! [        [        4 a     Nf = f! , (       d  f       Ng= f)zT
Tests another SQL injection pattern: UNION SELECT attack
via geometry column name.
test_sql_injection_unionr   r   ry  Nrz  r{  z (password TEXT);r|  z (password) VALUES ('secret');z"geom') UNION SELECT password FROM z WHERE '1'='1r}  r   r   re   r~  r   r  r  z)' should not be affected by SQL injectionr  r  s                   r   >test_to_postgis_sql_injection_prevention_geometry_column_unionETestIO.test_to_postgis_sql_injection_prevention_geometry_column_union  s2     * \\^
j5IN +4::<+;+;CR+@*AB\\^tLL23D2EEVWX LL|$5#66TUV	   01B0C=Q 	 &556IJ 0q9:	"78I7J! LM^^ LL3<<>qAM LL)9:CCEaHAMM  VVV}	-%8T ^^,,y1::<Q?K"&,,/?"@"I"I"KA"NRS"S  m+ 	
G	
+ # 	
./ 0( (	
"O ^$  I& 		
 r  c                 ,   UnSnUR                  5       n[        XSUSS9  UR                  5       nSnUR                  U5      n[        SU S35      nUR	                  5        n	U	R                  U5      R                  5       S   n
SSS5         [        XcUS	S9  UR	                  5        n	U	R                  U5      R                  5       S   nUW
:X  d   e SSS5        g! , (       d  f       N_= f! [        [        4 a     Nhf = f! , (       d  f       g= f)
zA
Tests SQL injection via comment syntax in geometry column name.
test_sql_injection_commentr   r   z/geom' -- DROP TABLE test_sql_injection_comment;r}  r   r   Nr   	rp  r   rq  r   r4   rP   r  r   rG   r   rJ   r   r   r[   r  r  r  r  r   r  r  s               r   @test_to_postgis_sql_injection_prevention_geometry_column_commentGTestIO.test_to_postgis_sql_injection_prevention_geometry_column_comment  s	     , \\^
j5IN  O%556IJ 0q9:	^^ LL3<<>qAM 	-%8T
 ^^,,y1::<Q?K-///   I& 		 *   "#CC/ )+D
C,/DD
Dc                 ,   UnSnUR                  5       n[        XSUSS9  UR                  5       nSnUR                  U5      n[        SU S35      nUR	                  5        n	U	R                  U5      R                  5       S   n
SSS5         [        XcUS	S9  UR	                  5        n	U	R                  U5      R                  5       S   nUW
:X  d   e SSS5        g! , (       d  f       N_= f! [        [        4 a     Nhf = f! , (       d  f       g= f)
z<
Tests SQL injection via semicolon in geometry column name.
test_sql_injection_semicolonr   r   z/geom; DELETE FROM test_sql_injection_semicolon;r}  r   r   Nr   r  r  s               r   Btest_to_postgis_sql_injection_prevention_geometry_column_semicolonITestIO.test_to_postgis_sql_injection_prevention_geometry_column_semicolon8  s	     . \\^
j5IN  O%556IJ 0q9:	^^ LL3<<>qAM 	-%8T
 ^^,,y1::<Q?K-///   I& 		 r  c                     UnSnUR                  5       nUR                  S5      n[        XSUSS9  [        XSUSS9  [        SU S35      n[	        XcSS9n[        U5      [        U5      S	-  :X  d   eg
)z
Tests that legitimate geometry column names with special characters
(but not SQL injection) still work correctly.
This ensures the fix doesn't break normal functionality with valid
but unusual column names.
test_special_charsgeom_with_underscores_123r   r   r   r   r   r   rf   Nro  rs  s           r   Atest_to_postgis_append_with_special_characters_in_geometry_columnHTestIO.test_to_postgis_append_with_special_characters_in_geometry_column[  s      $ lln!!"=> 	cEYG 	cEXF _UG2./#0KL2w#c(Q,&&&r   r*   N):__name__
__module____qualname____firstlineno__r1   markparametrizePOSTGIS_DRIVERSr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r"  r&  r0  r3  r8  r?  rB  rI  rN  xfailcompatPANDAS_GE_202rR  rX  skipifr   r   patchr`  rg  rj  ru  r  r  r  r  r  __static_attributes__r*   r   r   r   r      sj   [[-N O [[1?TR
 S
 [[1?TR S [[1?TR S [[1?TR S [[1?TR
" S
" [[1?TR3 S3 [[1?TR3 S3
 [[1?TR S [[-N O [[-N O [[-N! O! [[-N O& [[-N O" [[-N
 O
4 [[-NJ OJ, [[-NT OT, [[-N@ O@, [[-N" O"* [[-N, O,, [[-N1 O1" [[-N, O,: [[-N O* [[-N$ O$L [[-N= O= [[-N8 O8  [[-N O" [[-NP OP  [[-NL OL [[-N[[   V  	7	 O
	7 [[1?TR& S& [[J/EF	ZZ"#[[1?TR' S $ G'  
ZZ:;[[1?TR% S <%& [[1?TR& S& [[-N' O'2 [[-N@
 O@
D [[-N;
 O;
z [[-N 0 O 0D [[-N 0 O 0D [[-N' O'r   r   )0__doc__r'   r  r7   importlib.utilr   pandasr   rl   geopandas._compat_compatr  r   r   r   r   geopandas.io.sqlr   r   r	   r   r1   geopandas.tests.utilr
   r   r   r   r?   r   ImportErrorr   fixturer   r6  r   r  dictr+   r=   rJ   rQ   r]   rm   rq   rv   ry   r}   r   r*   r   r   <module>r     sv   
   $   " ; ; ( 2 <    

c 
 23d   $  :  :8        (     @' @'o   Ds   D- -D87D8