B
    .`>                 @   s  yd dl ZW n  ek
r,   ddlmZ Y nX d dlZd dlZd dlmZ d dlZd dl	Z	d dl
Z
d dlZd dlZd dlZd dlZyd dlZW n ek
r   Y nX ejrd dlmZ yd dlZdZW n ek
r   dZY nX dd	 Zd
d Zdd Zdd Zerd dlZdd Zdd Zdd Zdd Zda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&G d*d+ d+ej'Z(dS ),    N   )plugin_base)update_wrapper)SequenceTFc                s,   |  d  fdd}t| t  d S )NZ
sqlalchemyc                sp   | dd r.G fdddtj  |d< | dd r^G  fdddtj  |d< j| f| d S )Ncallbackc                   s   e Zd Zd fdd	ZdS )z=pytest_addoption.<locals>.make_option.<locals>.CallableActionNc                s    ||| d S )N )selfparser	namespacevaluesoption_string)	callback_r   U/home/dcms/DCMS/lib/python3.7/site-packages/sqlalchemy/testing/plugin/pytestplugin.py__call__,   s    zFpytest_addoption.<locals>.make_option.<locals>.CallableAction.__call__)N)__name__
__module____qualname__r   r   )r   r   r   CallableAction+   s   r   actionzeroarg_callbackc                   s.   e Zd Zd fdd	Zdfdd	Z  ZS )	z=pytest_addoption.<locals>.make_option.<locals>.CallableActionFNc          	      s"   t  | j||dd|||d d S )Nr   T)option_stringsdestnargsconstdefaultrequiredhelp)super__init__)r   r   r   r   r   r   )r   	__class__r   r   r   7   s    
zFpytest_addoption.<locals>.make_option.<locals>.CallableAction.__init__c                s    ||| d S )Nr   )r   r	   r
   r   r   )r   r   r   r   I   s    zFpytest_addoption.<locals>.make_option.<locals>.CallableAction.__call__)FFN)N)r   r   r   r   r   __classcell__r   )r   r   )r   r   r   6   s     )popargparseActionZ	addoption)namekw)group)r   r   r   r   make_option'   s    z%pytest_addoption.<locals>.make_option)Zgetgroupr   Zsetup_optionsZread_config)r	   r'   r   )r&   r   pytest_addoption$   s    
+
r(   c             C   s   t d t| dr2t| j t| jd  n&| jjrXt	j
| jjrXt	| jj t| j ttt| jdd tt d S )Nzsqlalchemy.testing.assertionsworkerinputfollower_identZ
cov_sourceF)pytestZregister_assert_rewritehasattrr   Z!restore_important_follower_configr)   Zconfigure_followeroptionZwrite_identsospathexistsremoveZ	pre_beginZset_coverage_flagboolgetattrZset_fixture_functionsPytestFixtureFunctions)configr   r   r   pytest_configureV   s    


r6   c             C   s   t   d S )N)r   Z
post_begin)sessionr   r   r   pytest_sessionstartk   s    r8   c             C   s   t   d S )N)r   Zfinal_process_cleanup)r7   r   r   r   pytest_sessionfinisho   s    r9   c             C   sH   t | j dt jdd  | jd< ddlm} || jd  d S )Nztest_%sr      r*   )	provision)	r   Z!memoize_important_follower_configr)   uuiduuid4hexsqlalchemy.testingr;   Zcreate_follower_db)noder;   r   r   r   pytest_configure_nodev   s    rA   c             C   s    ddl m} || jd  d S )Nr   )r;   r*   )r?   r;   Zdrop_follower_dbr)   )r@   errorr;   r   r   r   pytest_testnodedown   s    rC   c             C   s$  t dd }dd |D |d d < tdd |D }x|D ]}xt|j|jjD ]j}||jk	rT||j }tt	j
dt	j
}x@||j|jjd D ]&}	x |	 D ]}
||
j |
 qW qW qTW q<W g }x<|D ]4}|jj|kr|||jj |j  q|| qW t|d	d d
|d d < d S )Nc               S   s
   t tS )N)collectionsdefaultdictlistr   r   r   r   <lambda>       z/pytest_collection_modifyitems.<locals>.<lambda>c             S   s.   g | ]&}t |jtjr|jjjd s|qS )_)
isinstanceparentr+   Instancer$   
startswith).0itemr   r   r   
<listcomp>   s   z1pytest_collection_modifyitems.<locals>.<listcomp>c             s   s   | ]}|j V  qd S )N)rK   )rN   rO   r   r   r   	<genexpr>   s    z0pytest_collection_modifyitems.<locals>.<genexpr>from_parent)r$   rK   c             S   s   | j j j j| j j j| jfS )N)rK   r$   )rO   r   r   r   rG      s    
)key)rD   rE   setr   Zgenerate_sub_testsclsrK   moduler3   r+   Classr   Zcollectr$   appendextendsorted)r7   r5   itemsZrebuilt_itemsZtest_classesZ
test_classZsub_clsZper_cls_dictctorinsttnewitemsrO   r   r   r   pytest_collection_modifyitems   s.    



 
r`   c                sr   t |rBt||rBttjdtj fddt j|D S t 	|rjt
 tjrjt j|rjd S g S d S )NrR   c                s   g | ]}|j  d qS ))r$   rK   )r   )rN   Zparametrize_cls)	collectorr\   r   r   rP      s   z-pytest_pycollect_makeitem.<locals>.<listcomp>)inspectisclassr   Z
want_classr3   r+   rW   _parametrize_clsrV   
isfunctionrJ   rL   Zwant_methodrU   )ra   r$   objr   )ra   r\   r   pytest_pycollect_makeitem   s    
rg   c             C   s   d|j kr|gS |j}g }xtjdd |D  D ]}i }xZtdd |D |D ]B\}}|sbtdtd|}x t||jD ]\}	}
|
||	< q|W qNW d	dd	 |D }d
|j
|f }tt||f|}t| || || q0W |S )z6implement a class-based version of pytest parametrize._sa_parametrizec             S   s   g | ]\}}|qS r   r   )rN   argnameparamsr   r   r   rP      s    z$_parametrize_cls.<locals>.<listcomp>c             S   s   g | ]}|d  qS )r   r   )rN   Z	_sa_paramr   r   r   rP      s    z*need argnames for class-based combinationsz,\s*rI   c             s   s2   | ]*}|j d D ]}ttdd|V  qqdS )-z\W N)idsplitstrresub)rN   paramtokenr   r   r   rQ      s   z#_parametrize_cls.<locals>.<genexpr>z%s_%s)__dict__rh   	itertoolsproductzip	TypeErrorrp   rn   r   joinr   type__new__setattrrX   )rV   rU   rh   classesZfull_param_setZcls_variablesri   rr   Zargname_splitargvalZparametrized_namer$   Znewclsr   r   r   rd      s,    

rd   c                sR   t  tjsd S td krFt jj  jja fdd} jj| t  d S )Nc                  s   t  jj d ad S )N)class_teardownrK   _current_classr   )rO   r   r   finalize  s    z&pytest_runtest_setup.<locals>.finalize)rJ   r+   ZFunctionr   class_setuprK   Zaddfinalizer
test_setup)rO   r   r   )rO   r   pytest_runtest_setup   s    r   c             C   s   t |  d S )N)test_teardown)rO   r   r   r   pytest_runtest_teardown  s    r   c             C   s    t | | jjj| jj| j d S )N)r   Zbefore_testrK   rV   r   rU   r$   )rO   r   r   r   r   !  s    r   c             C   s   t |  d S )N)r   Z
after_test)rO   r   r   r   r   '  s    r   c             C   s   t | j d S )N)r   Zstart_test_classrU   )rO   r   r   r   r   +  s    r   c             C   s   t | j d S )N)r   Zstop_test_classrU   )rO   r   r   r   r   /  s    r   c             C   s$   t jjdkrt| S t| S d S )N   )sysversion_infomajorrb   getfullargspec
getargspec)fnr   r   r   r   3  s    
r   c                s8   ddl m ddlm dd  d	 fdd	}|S )
z:Port of langhelpers.decorator with pytest-specific tricks.r   )format_argspec_plus)inspect_getfullargspecc             S   s   t | | || S )N)exec)codeenvfn_namer   r   r   _exec_code_in_env@  s    
z/_pytest_fn_decorator.<locals>._exec_code_in_envr   c                s   | }|r|j | tdd| jd}||dd d| } || d| j}|szt| d| j|_| |_t|| S | j	|_	| j|_|S d S )	Ntargetr   )r   r   r$   F)ZgroupedzDdef %(name)s(%(args)s):
    return %(target)s(%(fn)s, %(apply_kw)s)
)r   r   Zim_func)
argsrY   dictr   updater3   __defaults____wrapped__r   r   )r   add_positional_parametersspecmetadatar   Z	decorated)r   r   r   r   r   r   decorateD  s     
z&_pytest_fn_decorator.<locals>.decorate)r   )Zsqlalchemy.util.langhelpersr   Zsqlalchemy.util.compatr   )r   r   r   )r   r   r   r   r   _pytest_fn_decorator:  s
    r   c               @   sL   e Zd Zdd Zdd eeeddZdd Z	d	d
 Z
dd Zdd ZdS )r4   c             O   s   t jj||S )N)r+   skip	Exception)r   r~   r%   r   r   r   skip_test_exceptionf  s    z*PytestFixtureFunctions.skip_test_exceptionc             C   s   | S )Nr   )rf   r   r   r   rG   j  rH   zPytestFixtureFunctions.<lambda>r   )irsnc          
      s  ddl m tjjdkr@t|dkrft|d drft|d }n&t|dkrft|d drft|d }|ddfd	d
}|dd}g }d|r:| j	 t
jddd t|D  } fddt|D }x|D ]^ttsf|\}}	||dd }
|	rd||
|	dfdd|D f qW nHxF|D ]>ttsVf|\}}	|	rld|||	df q@W g x>|D ]6\}
}	}r|
|	f7 }
tj|
d|i}| qW fdd}|S )a  Facade for pytest.mark.parametrize.

        Automatically derives argument names from the callable which in our
        case is always a method on a class with positional arguments.

        ids for parameter sets are derived using an optional template.

        r   )
exclusionsr   r   __next__nextargnamesNc                s@   g }g }x.| D ]&}t | jr*|| q|| qW ||fS )N)rJ   compoundrX   )r   resultZgathered_exclusionsa)r   r   r   _filter_exclusions  s    
z?PytestFixtureFunctions.combinations.<locals>._filter_exclusionsid_Fc             S   s   g | ]\}}|d kr|qS ))r   r   r   r   r   )rN   idxcharr   r   r   rP     s   z7PytestFixtureFunctions.combinations.<locals>.<listcomp>c                s*   g | ]"\}}| krt | | fqS r   )operator
itemgetter)rN   r   r   )_combination_id_fnsr   r   rP     s   Trk   c             3   s   | ]\}}|| V  qd S )Nr   )rN   getterZcomb_fn)r~   r   r   rQ     s    z6PytestFixtureFunctions.combinations.<locals>.<genexpr>rm   c                s   t | r:rtdd| jkr&g | _| j f | S  d krVt| jdd  }nt	d }r|dg7 }t
fdd}dd	 }|| d
d} tj|| S d S )Nz5exclusions not supported for class level combinationsrh   r   z, *_exclusionsc                s4   |d }|r"   j| }|| } | |dd |S )Nr   )r   add)r   r   r%   r   Zexlu)r   r   r   check_exclusions  s
    zOPytestFixtureFunctions.combinations.<locals>.decorate.<locals>.check_exclusionsc             S   s   | j d d S )Nr   )r   rX   )r   r   r   r   process_metadata  s    zOPytestFixtureFunctions.combinations.<locals>.decorate.<locals>.process_metadata)r   )r   )rb   rc   NotImplementedErrorrt   rh   rX   r   r   rp   rn   r   r+   markZparametrize)r   Z	_argnamesr   r   )r   r   has_exclusionspytest_paramsr   r   r     s&    



z5PytestFixtureFunctions.combinations.<locals>.decorate)r   )r?   r   r   r   r   lenr,   rF   r!   r   r   r   	enumeraterJ   tuplerX   ry   r+   rr   )r   Zarg_setsr%   r   r   Ztobuild_pytest_paramsZ_arg_getterfnsZ	fn_paramsZparam_exclusions
parametersrr   r   r   )r   r~   r   r   r   r   r   combinationsp  s^    	




&z#PytestFixtureFunctions.combinationsc             G   s    |d }t j|dd  d|iS )Nr   r   rm   )r+   rr   )r   r   identr   r   r   param_ident  s    z"PytestFixtureFunctions.param_identc             O   s   t j||S )N)r+   fixture)r   r~   r%   r   r   r   r     s    zPytestFixtureFunctions.fixturec             C   s   t jdS )NZPYTEST_CURRENT_TEST)r.   environget)r   r   r   r   get_current_test_name  s    z,PytestFixtureFunctions.get_current_test_nameN)r   r   r   r   reprro   r   
attrgetterr   r   r   r   r   r   r   r   r   r4   e  s    r4   ))Zsqla_plugin_baser   ImportErrorrl   r"   rD   	functoolsr   rb   ru   r   r.   rp   r   r+   typingTYPE_CHECKINGr   ZxdistZ	has_xdistr(   r6   r8   r9   r<   rA   rC   r`   rg   r   rd   r   r   r   r   r   r   r   r   ZFixtureFunctionsr4   r   r   r   r   <module>   sZ   
29#+