B
    18™\¯  ã               @   sx   d dl Z d dlZd dlZdd„ ZG dd„ dejƒZG dd„ dejƒZG dd	„ d	ejƒZd
d„ Z	dd„ Z
edkrte
ƒ  dS )é    Nc               C   s   dS )NZsqlite_testdb© r   r   r   ú]/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/sqlite3/test/transactions.pyÚget_db_path   s    r   c               @   sˆ   e Z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
e ejdk d¡dd„ ƒZe ejdk d¡dd„ ƒZdd„ ZdS )ÚTransactionTestsc             C   sf   yt  tƒ ¡ W n tk
r$   Y nX tjtƒ dd| _| j ¡ | _tjtƒ dd| _	| j	 ¡ | _
d S )Ngš™™™™™¹?)Útimeout)ÚosÚremover   ÚOSErrorÚsqliteÚconnectÚcon1ÚcursorÚcur1Úcon2Úcur2)Úselfr   r   r   ÚsetUp   s    zTransactionTests.setUpc             C   sR   | j  ¡  | j ¡  | j ¡  | j ¡  yt tƒ ¡ W n tk
rL   Y nX d S )N)	r   Úcloser   r   r   r   Úunlinkr   r	   )r   r   r   r   ÚtearDown+   s    



zTransactionTests.tearDownc             C   sN   | j  d¡ | j  d¡ | j  d¡ | j d¡ | j ¡ }|  t|ƒd¡ d S )Nzcreate table test(i)zinsert into test(i) values (5)zcreate table test2(j)zselect i from testr   )r   Úexecuter   ÚfetchallÚassertEqualÚlen)r   Úresr   r   r   ÚCheckDMLDoesNotAutoCommitBefore7   s    
z0TransactionTests.CheckDMLDoesNotAutoCommitBeforec             C   sB   | j  d¡ | j  d¡ | j d¡ | j ¡ }|  t|ƒd¡ d S )Nzcreate table test(i)zinsert into test(i) values (5)zselect i from testr   )r   r   r   r   r   r   )r   r   r   r   r   ÚCheckInsertStartsTransaction?   s
    
z-TransactionTests.CheckInsertStartsTransactionc             C   sX   | j  d¡ | j  d¡ | j ¡  | j  d¡ | j d¡ | j ¡ d }|  |d¡ d S )Nzcreate table test(i)zinsert into test(i) values (5)zupdate test set i=6zselect i from testr   é   )r   r   r   Úcommitr   Úfetchoner   )r   r   r   r   r   ÚCheckUpdateStartsTransactionF   s    
z-TransactionTests.CheckUpdateStartsTransactionc             C   sX   | j  d¡ | j  d¡ | j ¡  | j  d¡ | j d¡ | j ¡ }|  t|ƒd¡ d S )Nzcreate table test(i)zinsert into test(i) values (5)zdelete from testzselect i from testé   )r   r   r   r   r   r   r   r   )r   r   r   r   r   ÚCheckDeleteStartsTransactionO   s    

z-TransactionTests.CheckDeleteStartsTransactionc             C   sl   | j  d¡ | j  d¡ | j ¡  | j  d¡ | j d¡ | j ¡ }|  t|ƒd¡ |  |d d d¡ d S )Nzcreate table test(i)zinsert into test(i) values (5)zreplace into test(i) values (6)zselect i from testr!   r   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   ÚCheckReplaceStartsTransactionX   s    

z.TransactionTests.CheckReplaceStartsTransactionc             C   s¤   | j  d¡ | j  d¡ d | j_|  | jjd ¡ | j d¡ | j ¡ }|  t|ƒd¡ d| j_|  | jjd¡ | j  d¡ | j d¡ | j ¡ }|  t|ƒd¡ d S )Nzcreate table test(i)zinsert into test(i) values (5)zselect i from testr!   ZDEFERRED)r   r   r   Úisolation_levelr   r   r   r   )r   r   r   r   r   ÚCheckToggleAutoCommitb   s    

z&TransactionTests.CheckToggleAutoCommit)é   é   r'   z.test hangs on sqlite versions older than 3.2.2c          	   C   s@   | j  d¡ | j  d¡ |  tj¡ | j d¡ W d Q R X d S )Nzcreate table test(i)zinsert into test(i) values (5))r   r   ÚassertRaisesr
   ÚOperationalErrorr   )r   r   r   r   ÚCheckRaiseTimeoutr   s    z"TransactionTests.CheckRaiseTimeoutc          	   C   sJ   | j  d¡ | j  d¡ |  tj¡ | j d¡ W dQ R X | j ¡  dS )z
        This tests the improved concurrency with pysqlite 2.3.4. You needed
        to roll back con2 before you could commit con1.
        zcreate table test(i)zinsert into test(i) values (5)N)r   r   r(   r
   r)   r   r   r   )r   r   r   r   ÚCheckLockingz   s
    zTransactionTests.CheckLockingc          	   C   s\   t  d¡}| ¡ }| d¡ | d¡ | d¡ | ¡  |  t j¡ | ¡  W dQ R X dS )z…
        Checks if cursors on the connection are set into a "reset" state
        when a rollback is done on the connection.
        z:memory:zcreate table test(x)zinsert into test(x) values (5)z&select 1 union select 2 union select 3N)r
   r   r   r   Úrollbackr(   ÚInterfaceErrorr   )r   ÚconÚcurr   r   r   ÚCheckRollbackCursorConsistencyˆ   s    



z/TransactionTests.CheckRollbackCursorConsistencyN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r    r"   r#   r%   ÚunittestÚskipIfr
   Úsqlite_version_infor*   r+   r0   r   r   r   r   r      s   		
r   c               @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚSpecialCommandTestsc             C   s   t  d¡| _| j ¡ | _d S )Nz:memory:)r
   r   r.   r   r/   )r   r   r   r   r   ˜   s    zSpecialCommandTests.setUpc             C   s(   | j  d¡ | j  d¡ | j  d¡ d S )Nzcreate table test(i)zinsert into test(i) values (5)zdrop table test)r/   r   )r   r   r   r   ÚCheckDropTableœ   s    z"SpecialCommandTests.CheckDropTablec             C   s(   | j  d¡ | j  d¡ | j  d¡ d S )Nzcreate table test(i)zinsert into test(i) values (5)zpragma count_changes=1)r/   r   )r   r   r   r   ÚCheckPragma¡   s    zSpecialCommandTests.CheckPragmac             C   s   | j  ¡  | j ¡  d S )N)r/   r   r.   )r   r   r   r   r   ¦   s    
zSpecialCommandTests.tearDownN)r1   r2   r3   r   r8   r9   r   r   r   r   r   r7   —   s   r7   c               @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚTransactionalDDLc             C   s   t  d¡| _d S )Nz:memory:)r
   r   r.   )r   r   r   r   r   «   s    zTransactionalDDL.setUpc             C   s6   | j  d¡ | j  ¡  | j  d¡ ¡ }|  |g ¡ d S )Nzcreate table test(i)zselect * from test)r.   r   r,   r   r   )r   Úresultr   r   r   Ú#CheckDdlDoesNotAutostartTransaction®   s    
z4TransactionalDDL.CheckDdlDoesNotAutostartTransactionc          	   C   sJ   | j  d¡ | j  d¡ | j  ¡  |  tj¡ | j  d¡ W d Q R X d S )Nzbegin immediatezcreate table test(i)zselect * from test)r.   r   r,   r(   r
   r)   )r   r   r   r   ÚCheckImmediateTransactionalDDL¶   s
    
z/TransactionalDDL.CheckImmediateTransactionalDDLc          	   C   sJ   | j  d¡ | j  d¡ | j  ¡  |  tj¡ | j  d¡ W d Q R X d S )NÚbeginzcreate table test(i)zselect * from test)r.   r   r,   r(   r
   r)   )r   r   r   r   ÚCheckTransactionalDDL¿   s
    
z&TransactionalDDL.CheckTransactionalDDLc             C   s   | j  ¡  d S )N)r.   r   )r   r   r   r   r   È   s    zTransactionalDDL.tearDownN)r1   r2   r3   r   r<   r=   r?   r   r   r   r   r   r:   ª   s
   		r:   c              C   s4   t  td¡} t  td¡}t  td¡}t  | ||f¡S )NÚCheck)r4   Ú	makeSuiter   r7   r:   Ú	TestSuite)Zdefault_suiteZspecial_command_suiteZ	ddl_suiter   r   r   ÚsuiteË   s    rC   c              C   s   t  ¡ } |  tƒ ¡ d S )N)r4   ÚTextTestRunnerÚrunrC   )Úrunnerr   r   r   ÚtestÑ   s    rG   Ú__main__)r   r4   Úsqlite3r
   r   ÚTestCaser   r7   r:   rC   rG   r1   r   r   r   r   Ú<module>   s   y!