U
    x^                     @   s   d dl mZ d dlZd dlZddlmZ ddlmZ ddlmZ ddlmZ dd	lm	Z	 dd
lm
Z
 ddlmZ G dd dejZG dd deZdS )    )dequeN   )config)fixtures)	profiling   )create_engine)MetaData)util)Sessionc                   @   s:   e Zd Zejdd Zdd Zdd Zdd Zd	d
 Z	dS )ReplayFixtureTestc                 o   s
   d V  d S N )selfargkwr   r   I/tmp/pip-install-dq5v43_d/SQLAlchemy/sqlalchemy/testing/replay_fixture.py
_dummy_ctx   s    zReplayFixtureTest._dummy_ctxc                    s   t  tjjj  fdd}ttjj|dd}t|| _|| _	t
|| _|   z| j| jd W 5 |   |  X fdd}ttjj|dd}t|| _|| _	t
|| _|   z| jtjd W 5 | j  |  X d S )Nc                      s      S r   )recorderr   creatordbapi_sessionr   r   r      s    z3ReplayFixtureTest.test_invocation.<locals>.recorderF)r   Zuse_native_hstore)ctxc                      s      S r   )playerr   )r   r   r   r   )   s    z1ReplayFixtureTest.test_invocation.<locals>.player)ReplayableSessionr   dbpoolZ_creatorr   urlr	   metadataenginer   sessionsetup_engineteardown_engineZdispose
_run_stepsr   closer   Zcount_functions)r   r   r   r   r   r   r   test_invocation   s<    
  


  


z!ReplayFixtureTest.test_invocationc                 C   s   d S r   r   r   r   r   r   r!   ;   s    zReplayFixtureTest.setup_enginec                 C   s   d S r   r   r&   r   r   r   r"   >   s    z!ReplayFixtureTest.teardown_enginec                 C   s
   t  d S r   )NotImplementedError)r   r   r   r   r   r#   A   s    zReplayFixtureTest._run_stepsN)
__name__
__module____qualname__
contextlibcontextmanagerr   r%   r!   r"   r#   r   r   r   r   r      s   
(r   c                   @   s   e Zd ZdZe Ze ZejrFe	dd e
eD dd dD Zn:e	dd e
eD dd e D dd d	D Zd
d Zdd Zdd ZG dd deZG dd deZdS )r   a  A simple record/playback tool.

    This is *not* a mock testing class.  It only records a session for later
    playback and makes no assertions on call consistency whatsoever.  It's
    unlikely to be suitable for anything other than DB-API recording.

    c                 C   s    g | ]}| d stt|qS _
startswithgetattrtypes.0tr   r   r   
<listcomp>S   s     
 zReplayableSession.<listcomp>c                 C   s   g | ]}t t|qS r   r1   r2   r3   r   r   r   r6   U   s   )FunctionTypeBuiltinFunctionType
MethodTypeBuiltinMethodType
LambdaTypeZUnboundMethodTypec                 C   s    g | ]}| d stt|qS r-   r/   r3   r   r   r   r6   d   s   
c                 C   s"   g | ]}t |tst|n|qS r   )
isinstancetyper3   r   r   r   r6   k   s   c                 C   s   g | ]}t t|qS r   r7   r3   r   r   r   r6   q   s   )r8   r9   r:   r;   r<   c                 C   s   t  | _d S r   )r   bufferr&   r   r   r   __init__~   s    zReplayableSession.__init__c                 C   s   |  | j|S r   )Recorderr?   )r   baser   r   r   r      s    zReplayableSession.recorderc                 C   s   |  | jS r   )Playerr?   r&   r   r   r   r      s    zReplayableSession.playerc                   @   s0   e Zd Zdd Zdd Zedd Zdd Zd	S )
zReplayableSession.Recorderc                 C   s   || _ || _d S r   )_buffer_subject)r   r?   subjectr   r   r   r@      s    z#ReplayableSession.Recorder.__init__c                    sZ    fdddD \}}|||}t |tjkrH|tj t  ||S || |S d S )Nc                    s   g | ]}t  |qS r   object__getattribute__r4   xr&   r   r   r6      s   z7ReplayableSession.Recorder.__call__.<locals>.<listcomp>rE   rD   )r>   r   NativesappendCallable)r   argsr   rF   r?   resultr   r&   r   __call__   s    



z#ReplayableSession.Recorder.__call__c                 C   s   | j S r   )rE   r&   r   r   r   _sqla_unwrap   s    z'ReplayableSession.Recorder._sqla_unwrapc                    s   zt  |W S  tk
r"   Y nX  fdddD \}}zt|||}W n" tk
rp   |tj  Y n8X t|tjkr|tj t ||S || |S d S )Nc                    s   g | ]}t  |qS r   rG   rJ   r&   r   r   r6      s   z?ReplayableSession.Recorder.__getattribute__.<locals>.<listcomp>rL   )	rH   rI   AttributeErrorr>   rN   r   NoAttributerM   rO   )r   keyrF   r?   rQ   r   r&   r   rI      s"    


z+ReplayableSession.Recorder.__getattribute__Nr(   r)   r*   r@   rR   propertyrS   rI   r   r   r   r   rA      s
   
rA   c                   @   s0   e Zd Zdd Zdd Zedd Zdd Zd	S )
zReplayableSession.Playerc                 C   s
   || _ d S r   )rD   )r   r?   r   r   r   r@      s    z!ReplayableSession.Player.__init__c                 O   s*   t | d}| }|tjkr"| S |S d S NrD   )rH   rI   popleftr   rO   )r   rP   r   r?   rQ   r   r   r   rR      s
    
z!ReplayableSession.Player.__call__c                 C   s   d S r   r   r&   r   r   r   rS      s    z%ReplayableSession.Player._sqla_unwrapc                 C   sb   zt | |W S  tk
r"   Y nX t | d}| }|tjkrF| S |tjkrZt|n|S d S rY   )rH   rI   rT   rZ   r   rO   rU   )r   rV   r?   rQ   r   r   r   rI      s    


z)ReplayableSession.Player.__getattribute__NrW   r   r   r   r   rC      s
   
rC   N)r(   r)   r*   __doc__rH   rO   rU   r
   Zpy2ksetdirr2   
differencerM   union__builtins__valuesr@   r   r   rA   rC   r   r   r   r   r   E   s<   .r   )collectionsr   r+   r2    r   r   r   r   r	   r
   Zormr   ZTestBaser   rH   r   r   r   r   r   <module>   s   7