o
    gJT                     @  sv  U d dl mZ d dlZd dlmZ d dlZd dlad dlZd dlm	Z	 d dl
Z
d dlZd dlmZ d dlmZ 	 dZtdZdadadadadadadadZdadadadadae Z e Z!da"d	e#d
< dd Z$dd Z%dd Z&dd Z'dd Z(dd Z)dd Z*dd Z+dd Z,dd Z-dd  Z.d!d" Z/d#d$ Z0d%d& Z1d'd( Z2g Z3g Z4d)d* Z5d+d, Z6e5d-d. Z7e5d/d0 Z8e6d1d2 Z9e6d3d4 Z:e5d5d6 Z;e6d7d8 Z<e6d9d: Z=d;d< Z>e6d=d> Z?e6d?d@ Z@e6dAdB ZAdCdD ZBdEdF ZCdGdH ZDdIdJ ZEdKdL ZFdMdN ZGdOdP ZHdQdR ZIdSdT ZJdUdV ZKdWdX ZLdYdZ ZMded[d\ZNd]d^ ZOd_d` ZPG dadb dbejQZRdaSdcdd ZTdS )f    )annotationsN)	Namespace)Path)Any)asyncioFzsqlalchemy.testing.plugin_baser   optionsc                 C  s  | ddt tdd | ddt tdd | ddt d	d
d | ddtdd | ddt ddd | ddt ddd | ddddd | dddd | ddtddd | d dtd!d"d | d#dtd$d%d | d&dtd'd(d | d)t d*d+d,d- | d.t d/d0d1 | d2dd3d4d | d5t d6d7d1 | d8dt td9d | d:dtt d;d< | d=dtt d>d< | d?dd@dAdBdC | dDddEdAdFdC | dGt dHdId1 | dJt dg dKdLdM | dNt dOdP | dQddRd d S )SNz
--log-infocallbackz,turn on info logging for <LOG> (multiple OK))actiontyper   helpz--log-debugz-turn on debug logging for <LOG> (multiple OK)z--dbappenddbzBUse prefab database uri. Multiple OK, first one is run by default.)r	   r
   destr   z--dbszList available prefab dbs)r	   zeroarg_callbackr   z--dburidburiz8Database uri.  Multiple OK, first one is run by default.z
--dbdriverdbdriverz|Additional database drivers to include in tests.  These are linked to the existing database URLs by the provisioning system.z--dropfirst
store_true	dropfirstz,Drop all tables in the target database first)r	   r   r   z--disable-asyncioz8disable test / fixtures / provisoning running in asyncio)r	   r   z--backend-onlybackendz}Run only tests marked with __backend__ or __sparse_backend__; this is now equivalent to the pytest -m backend mark expressionz
--nomemorymemory_intensivezpDon't run memory profiling tests; this is now equivalent to the pytest -m 'not memory_intensive' mark expressionz--notimingintensivetiming_intensivezpDon't run timing intensive tests; this is now equivalent to the pytest -m 'not timing_intensive' mark expressionz--nomypymypyz_Don't run mypy typing tests; this is now equivalent to the pytest -m 'not mypy' mark expressionz--profile-sort
cumulativeprofilesortz*Type of sort for profiling standard output)r
   defaultr   r   z--profile-dumpprofiledumpz2Filename where a single profile run will be dumped)r
   r   r   z--low-connectionslow_connectionsz>Use a low number of distinct connections - i.e. for Oracle TNSz--write-identswrite_identszCwrite out generated follower idents to <file>, when -n<num> is usedz--requirementsz3requirements class for testing, overrides setup.cfgz--include-tagzAInclude tests with tag <tag>; legacy, use pytest -m 'tag' instead)r	   r   r
   r   z--exclude-tagzEExclude tests with tag <tag>; legacy, use pytest -m 'not tag' insteadz--write-profileswrite_profilesFz$Write/update failing profiling data.)r	   r   r   r   z--force-write-profilesforce_write_profilesz,Unconditionally write/update profiling data.z--dump-pyannotatedump_pyannotatez/Run pyannotate and dump json info to given filez--mypy-extra-test-pathmypy_extra_test_pathszlAdditional test directories to add to the mypy tests. This is used only when running mypy tests. Multiple OK)r
   r	   r   r   r   z--postgresql-templatedbz^name of template database to use for PostgreSQL CREATE DATABASE (defaults to current database))r
   r   z--oracledb-thick-modez:enables the 'thick mode' when testing with oracle+oracledb)str_log	_list_dbs_set_tag_include_set_tag_exclude_requirements_opt_include_tag_exclude_tag)make_option r+   _/var/www/html/api-tag/env/lib/python3.10/site-packages/sqlalchemy/testing/plugin/plugin_base.pysetup_options;   s@  		

r-   c                 C  s   ddl m} | |_dS )zConfigure required state for a follower.

    This invokes in the parent process and typically includes
    database creation.

    r   	provisionN)sqlalchemy.testingr/   FOLLOWER_IDENT)follower_identr/   r+   r+   r,   configure_follower   s   
r3   c                 C     dS )zStore important configuration we will need to send to a follower.

    This invokes in the parent process after normal config is set up.

    Hook is currently not used.

    Nr+   dict_r+   r+   r,   !memoize_important_follower_config       r7   c                 C  r4   )zRestore important configuration needed by a follower.

    This invokes in the follower process.

    Hook is currently not used.

    Nr+   r5   r+   r+   r,   !restore_important_follower_config  r8   r9   c                 C  s*   t  att| d t| d g d S )Nz	setup.cfgztest.cfg)configparserConfigParserfile_configreadr"   )	root_pathr+   r+   r,   read_config  s   r?   c                 C  s   | a tD ]}|t t qdS )z7things to set up early, before coverage might be setup.N)r   pre_configurer<   )optfnr+   r+   r,   	pre_begin  s   rC   c                 C  s
   | t _d S N)r   has_coverage)valuer+   r+   r,   set_coverage_flag   s   
rG   c                  C  sp   t D ]} | tt qddlma ddlmamama ddlm	a	m
a
ma ddlmama ddlma t
  dS )z9things to set up later, once we know coverage is running.r   testing)fixturesengines
exclusions)
assertionswarnings	profiling)configr/   )utilN)post_configurer   r<   
sqlalchemyrI   r0   rJ   rK   rL   rM   rN   rO   rP   r/   rQ   setup_filtersrB   r+   r+   r,   
post_begin$  s   rV   c                 C  sX   t s
dd l a t   | drt |t j d S | dr*t |t j d S d S )Nr   z-infoz-debug)loggingbasicConfigendswith	getLoggersetLevelINFODEBUGopt_strrF   parserr+   r+   r,   r#   6  s   

r#   c                  G  sV   t d u r
tt  td tt dD ]}td|t d|f  qt	d d S )Nz0Available --db options (use --dburi to override)r   z%20s	%sr   )
r<   r?   r   cwdprintsortedr   getsysexit)argsmacror+   r+   r,   r$   C  s   r$   c                 C  s   t | d S rD   )_setup_requirementsr^   r+   r+   r,   r'   N  s   r'   c                       fdd}|S )Nc                      t |  | d S rD   )r(   r^   tagr+   r,   _do_include_tagS     z)_set_tag_include.<locals>._do_include_tagr+   )rm   rn   r+   rl   r,   r%   R     r%   c                   rj   )Nc                   rk   rD   )r)   r^   rl   r+   r,   _do_exclude_tagZ  ro   z)_set_tag_exclude.<locals>._do_exclude_tagr+   )rm   rq   r+   rl   r,   r&   Y  rp   r&   c                 C     t |dd d S N-_)exclude_tagsaddreplacer^   r+   r+   r,   r)   `     r)   c                 C  rr   rs   )include_tagsrw   rx   r^   r+   r+   r,   r(   d  ry   r(   c                 C     t |  | S rD   )r@   r   rU   r+   r+   r,   prel     
r|   c                 C  r{   rD   )rR   r   rU   r+   r+   r,   postq  r}   r~   c                 C     | a d S rD   r   rA   r<   r+   r+   r,   _setup_optionsv  s   r   c                 C  s,   ddl m} |ddd |ddd d S )Nr   )registryzsqlite.pysqlite_numericz#sqlalchemy.dialects.sqlite.pysqlite_SQLiteDialect_pysqlite_numericzsqlite.pysqlite_dollar_SQLiteDialect_pysqlite_dollar)sqlalchemy.dialectsr   register)rA   r<   r   r+   r+   r,    _register_sqlite_numeric_dialect|  s   r   c              
   C  sV   t jdddkr)ddlm} z|dd W d S  ty( } ztd|d }~ww d S )	NREQUIRE_SQLALCHEMY_CEXT01r   )has_compiled_extT)raise_zEREQUIRE_SQLALCHEMY_CEXT is set but can't import the cython extensions)osenvironrd   sqlalchemy.utilr   ImportErrorAssertionError)rA   r<   r   errr+   r+   r,   __ensure_cext  s   r   c                 C  s   ddl m} t |_d S Nr   rP   )r0   rP   _fixture_fn_class_fixture_functions)r   r<   rP   r+   r+   r,   _init_symbols  s   r   c                 C  s   | j rdt_d S d S NF)disable_asyncior   ENABLE_ASYNCIOr   r+   r+   r,   _set_disable_asyncio  s   
r   c              	     sz  ddl m} ddlm} ddlm} ddlm  | jr!t| j}ng }| j	p'g }| j
rP| j
D ]!}td|D ]}||dvrEtd| ||d| q6q.|s[||dd	 d |_| jr|jr fd
d|D D ](}	t| jd}
|
|j d|	jdd d W d    n1 sw   Y  qmt|||}|D ]}	td|	 ||	| ||j}|js||| qd S )Nr   rH   r   r.   )urlz[,\s]+r   z:Unknown URI specifier '%s'.  Specify --dbs for known uris.r   c                   s   g | ]}  |qS r+   )make_url).0db_urlsa_urlr+   r,   
<listcomp>  s    z_engine_uri.<locals>.<listcomp>a F)hide_password
zAdding database URL: %s)rS   rI   r0   rP   r/   sqlalchemy.enginer   r   listr   r   resplitr   RuntimeErrorr   rd   _currentr   r1   openwriterender_as_stringgenerate_db_urlsloginfosetup_configset_as_current)r   r<   rI   rP   r/   db_urlsextra_driversdb_tokenr   r   file_expanded_urlscfgr+   r   r,   _engine_uri  sT   

	

r   c                 C  s   | dd}t| d S )Nsqla_testingrequirement_cls)rd   ri   )r   r<   r   r+   r+   r,   _requirements  s   r   c                 C  sr   ddl m} ddlm} | d\}}t|}|ddd  D ]}t||}q t||}|  |_|_t	|_	d S )Nr   r   rH   :.   )
r0   rP   rS   rI   r   
__import__getattrrequirementsrequiresbootstrapped_as_sqlalchemy)argumentrP   rI   modnameclsnamemod	componentreq_clsr+   r+   r,   ri     s   

ri   c                 C  sD   ddl m} | jrddl m} |j D ]}|||j qd S d S )Nr   r   r.   )r0   rP   r   r/   Configall_configsdrop_all_schema_objectsr   )r   r<   rP   r/   r   r+   r+   r,   _prep_testing_database  s   r   c                 C  s   ddl m} t|_||_d S r   )r0   rP   r   r<   )rA   r<   rP   r+   r+   r,   _post_setup_options  s   
r   c                 C  s.   ddl m} |j|dd| j| jd|_d S )Nr   )rO   r   profile_file)sortdump)r0   rO   ProfileStatsFilerd   r   r   _profile_stats)r   r<   rO   r+   r+   r,   _setup_profiling  s   
r   c                 C  s"   t |tjsdS | drdS dS )NFru   T)
issubclassrJ   TestBase
startswith)nameclsr+   r+   r,   
want_class  
   
r   c                 C  s"   |j dsdS |jd u rdS dS )Ntest_FT)__name__r   
__module__)r   rB   r+   r+   r,   want_method  r   r   c           	      c  s    d|v s	d|v rDd|v }t | |dD ].}| j}tdd|j}tdd|}d| j|f }t|| f||d	}t||| |V  qd S | V  d S )
Nr   sparse_backend)sparsez
[_\[\]\.]+ru   z_+$ z%s_%s)_sa_orig_cls_name__only_on_config__)_possible_configs_for_clsr   r   subr   r
   setattr)	r   modulemarkersr   r   	orig_name
alpha_namer   subclsr+   r+   r,   generate_sub_tests!  s"   
r   c                 C  s   t |  t|  d S rD   )	_do_skips_setup_enginer   r+   r+   r,   !start_test_class_outside_fixtures9  s   r   c                 C  s   t |  tj  d S rD   )rJ   stop_test_class_inside_fixturesrK   testing_reaperr   r+   r+   r,   stop_test_class>  s   
r   c                 C  sJ   t j  tttj|  ztjst	  W t
  d S W t
  d S t
  w rD   )rK   r    stop_test_class_outside_fixturesr/   rP   r   r   r   rM   global_cleanup_assertions_restore_enginer   r+   r+   r,   r   G  s   


r   c                   C  s   t jrt jt d S d S rD   )rP   r   resetrI   r+   r+   r+   r,   r   Q  s   r   c                   C  s   t j  t  t  d S rD   )rK   r   final_cleanuprM   r   r   r+   r+   r+   r,   final_process_cleanupV  s   

r   c                 C  s@   t | dd rt| j}d|d< tj|d}tj|t d S d S )N__engine_options__classscoper   )	r   dictr   rK   testing_enginerP   r   push_enginerI   )r   optsengr+   r+   r,   r   \  s   
r   c                 C  s*   t |d|j}d|||f }t| d S )Nr   z%s.%s.%s)r   r   rO   _start_current_test)testtest_module_name
test_class	test_namer   id_r+   r+   r,   before_testd  s   r  c                 C  s   t   tj  d S rD   )rJ   
after_testrK   r   r	  r+   r+   r,   r  o  s   r  c                 C  s   t j|  d S rD   )rK   r   after_test_outside_fixturesr  r+   r+   r,   after_test_fixturest  ro   r  c                 C  s  t tj }| jr tj| j }t|D ]}||r|| qt	| dd r?tjt
| j }t|D ]}||s>|| q3t	| dd rL|| jg t| dr}tj}t|D ]$}| jD ]}t	||}||}	|	r{|| |d ury||	  nq]qXt| drt  }
tj}t|D ]}| jD ]}t	||}||s|
| qq||
r||
 |ri }tt|dd dD ]}|jj}||vr|||< q| S |S )N__only_on__r   __requires____prefer_requires__c                 S  s   | j j| j j| j jjfS rD   )r   r   driverdialectserver_version_info)r   r+   r+   r,   <lambda>  s   z+_possible_configs_for_cls.<locals>.<lambda>)key)setrP   r   r   __unsupported_on__rL   db_specr   remover   rQ   to_listr  intersection_updater   hasattrr   r  matching_config_reasonsextendr  enabled_for_configrw   
differencedifference_updatereversedrc   r   r   values)r   reasonsr   r   spec
config_objr   requirementcheckskip_reasonsnon_preferredper_dialectr   r   r+   r+   r,   r   x  sl   















r   c                 C  s   g }t | |}t| ddr#t| dD ]}| r"td| j|jf  q|sCd| j| jddd tj D d|f }t| n)t	| drlt
 }tjt| j }|D ]}||sa|| qV||rl|| tj|vrzt| |  d S d S )	N__skip_if__Fz'%s' skipped by %sz1'%s.%s' unsupported on any DB implementation %s%sz, c              	   s  s>    | ]}d |j jddd t|j D |j jf V  qdS )z'%s(%s)+%s'r   c                 s  s    | ]}t |V  qd S rD   )r"   )r   digr+   r+   r,   	<genexpr>  s
    
z&_do_skips.<locals>.<genexpr>.<genexpr>N)r   r   joinrL   _server_versionr  )r   r+  r+   r+   r,   r3    s    



z_do_skips.<locals>.<genexpr>__prefer_backends__)r   r   rP   	skip_testr   r   r4  r   r   r!  r  rL   r  rQ   r  r6  rw   r%  r&  r   _setup_configpop)r   r)  r   cmsgr/  r*  r+  r+   r+   r,   r     s>   







r   c                 C  s   t j| t d S rD   )rP   r   pushrI   )r+  ctxr+   r+   r,   r8    s   r8  c                   @  sj   e Zd Zejdd Zejdd Zejdd Zejdd Zd	d
 Z	ejdddZ
ejdd ZdS )FixtureFunctionsc                 O     t  rD   NotImplementedErrorselfargkwr+   r+   r,   skip_test_exception     z$FixtureFunctions.skip_test_exceptionc                 O  r?  rD   r@  rC  rg   rE  r+   r+   r,   combinations  rG  zFixtureFunctions.combinationsc                 O  r?  rD   r@  rH  r+   r+   r,   param_ident  rG  zFixtureFunctions.param_identc                 O  r?  rD   r@  rB  r+   r+   r,   fixture  rG  zFixtureFunctions.fixturec                 C  r?  rD   r@  rC  r+   r+   r,   get_current_test_name  s   z&FixtureFunctions.get_current_test_namereturnr   c                 C  r?  rD   r@  rL  r+   r+   r,   mark_base_test_class  rG  z%FixtureFunctions.mark_base_test_classc                 C  r?  rD   r@  rL  r+   r+   r,   add_to_marker  rG  zFixtureFunctions.add_to_markerN)rN  r   )r   r   __qualname__abcabstractmethodrF  rI  rJ  rK  rM  rO  abstractpropertyrP  r+   r+   r+   r,   r>    s    



r>  c                 C  r   rD   )r   )fixture_fn_classr+   r+   r,   set_fixture_functions	  s   rV  r   )U
__future__r   rR  argparser   r:   rW   r   pathlibr   r   re   typingr   r0   r   r   rZ   r   rJ   rK   rL   rN   rO   r/   rM   r   rP   rI   rQ   r<   r  rz   rv   r   __annotations__r-   r3   r7   r9   r?   rC   rG   rV   r#   r$   r'   r%   r&   r)   r(   r@   rR   r|   r~   r   r   r   r   r   r   r   ri   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r   r   r8  ABCr>  r   rV  r+   r+   r+   r,   <module>   s   	
 6







1





			

B+