o
    NK&h?C                     @   s   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
mZ d dlmZ d dlZdd	lmZ G d
d de
ZdS )    N)Path)OperationalError)TemporaryDirectoryThread)IsolatedAsyncioTestCaseSkipTest)patch   setup_loggerc                   @   s   e Zd Ze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 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/d0 Zd1d2 Zd3d4 Zd5S )6	SmokeTestc                 C   s
   t   d S Nr   )cls r   H/var/www/html/venv/lib/python3.10/site-packages/aiosqlite/tests/smoke.py
setUpClass   s   
zSmokeTest.setUpClassc                 C   s*   t  }| |j t|j d | _d S )Nztest.db)r   
addCleanupcleanupr   nameresolvedb)selftdr   r   r   setUp   s   zSmokeTest.setUpc              	      s   t | jI d H }| |t j |d4 I d H }| I d H }| |dg W d   I d H  n1 I d H s9w   Y  | I d H  d S Nselect 1, 2)r
      )		aiosqliteconnectr   assertIsInstance
ConnectionexecutefetchallassertEqualcloser   r   cursorrowsr   r   r   test_connection_await   s   (zSmokeTest.test_connection_awaitc              
      s   t | j4 I d H @}| |t j |d4 I d H }| I d H }| |dg W d   I d H  n1 I d H s;w   Y  W d   I d H  d S 1 I d H sQw   Y  d S r   )r   r   r   r    r!   r"   r#   r$   r&   r   r   r   test_connection_context%   s   *.z!SmokeTest.test_connection_contextc              
      s4  | j   G  fddd}t    | f}t|d 4 I d H 1}|dI d H  |dI d H  | I d H  |dI d H }| I d H }W d   I d H  n1 I d H s\w   Y  |D ]4}t|4 I d H }|dI d H }| 	| I d H | W d   I d H  n1 I d H sw   Y  qcd S )Nc                       s   e Zd Z fddZdS )z1SmokeTest.test_connection_locations.<locals>.Fakec                    s    S r   r   r   TEST_DBr   r   __str__1      z9SmokeTest.test_connection_locations.<locals>.Fake.__str__N)__name__
__module____qualname__r.   r   r,   r   r   Fake0   s    r3   r   z'create table foo (i integer, k integer)z$insert into foo (i, k) values (1, 5)select * from foo)
r   as_posixr   encoder   r   r"   commitr#   r$   )r   r3   locsr   r'   r(   locr   r,   r   test_connection_locations-   s$   
((z#SmokeTest.test_connection_locationsc              	      s   t j4 I d H }|dI d H  W d   I d H  n1 I d H s$w   Y  fdd tj fddtdD  I d H  t j4 I d H }|dI d H }| I d H }W d   I d H  n1 I d H sjw   Y  t|dkswJ d S )NzHcreate table multiple_connections (i integer primary key asc, k integer)c              	      sj   t  j4 I d H }|d| gI d H  | I d H  W d   I d H  d S 1 I d H s.w   Y  d S )Nz/insert into multiple_connections (k) values (?))r   r   r   r"   r7   )ir   r+   r   r   do_one_connJ   s
   .z8SmokeTest.test_multiple_connections.<locals>.do_one_connc                    s   g | ]} |qS r   r   .0r;   )r<   r   r   
<listcomp>O   s    z7SmokeTest.test_multiple_connections.<locals>.<listcomp>
   z"select * from multiple_connections)	r   r   r   r"   asynciogatherranger#   lenr&   r   )r<   r   r   test_multiple_connectionsC   s   ("(z#SmokeTest.test_multiple_connectionsc              	      s   t | j4 I d H +  dI d H  tj fddtdD  I d H    I d H  W d   I d H  n1 I d H s<w   Y  t | j4 I d H   dI d H }| I d H }W d   I d H  n1 I d H skw   Y  t	|dksxJ d S )NzDcreate table multiple_queries (i integer primary key asc, k integer)c                    s   g | ]	}  d |gqS )z+insert into multiple_queries (k) values (?))r"   r=   r   r   r   r?   _   s    z3SmokeTest.test_multiple_queries.<locals>.<listcomp>r@   zselect * from multiple_queries)
r   r   r   r"   rA   rB   rC   r7   r#   rD   )r   r'   r(   r   rF   r   test_multiple_queriesW   s"   


((zSmokeTest.test_multiple_queriesc              	      s  t | j4 I d H 1}| I d H }|dI d H  |ddd tdD I d H  | I d H  W d   I d H  n1 I d H sBw   Y  t | j4 I d H $}|dI d H }g }|2 z3 d H W }|| q^6 W d   I d H  n1 I d H s{w   Y  t	|dksJ d S )NzCcreate table iterable_cursor (i integer primary key asc, k integer)z*insert into iterable_cursor (k) values (?)c                 S      g | ]}|gqS r   r   r=   r   r   r   r?   t       z2SmokeTest.test_iterable_cursor.<locals>.<listcomp>r@   zselect * from iterable_cursor
r   r   r   r'   r"   executemanyrC   r7   appendrD   r   r   r'   r(   rowr   r   r   test_iterable_cursorm   s&   

(
(zSmokeTest.test_iterable_cursorc              	      s   i fddt d4 I d H A  dI d H   ddI d H    I d H   fddtd	D }|D ]}|  q:|D ]}|  qCW d   I d H  n1 I d H sZw   Y  td	 	 D ]
}t|d
 qkd S )Nc                    sb    fdd} |  t }|| }|  || < W d    d S 1 s*w   Y  d S )Nc               	      sf     d4 I d H } |  I d H }t|d |W  d   I d H  S 1 I d H s,w   Y  d S )Nr4   r   )r"   r#   r$   rD   )r'   r(   )connr   r   r   query   s   0z>SmokeTest.test_multi_loop_usage.<locals>.runner.<locals>.query)subTestrA   new_event_looprun_until_completer%   )krP   rQ   loopr(   )resultsr   )rP   r   runner   s   
"z/SmokeTest.test_multi_loop_usage.<locals>.runner:memory:z'create table foo (id int, name varchar)z%insert into foo values (?, ?), (?, ?))r
   Sallyr   Janetc                    s   g | ]
}t | fd qS ))targetargsr   )r>   rU   )r   rX   r   r   r?      s    z3SmokeTest.test_multi_loop_usage.<locals>.<listcomp>   r   )
r   r   r"   r7   rC   startjoinr$   rD   values)r   threadsthreadr(   r   )r   rW   rX   r   r   test_multi_loop_usage   s(   


(zSmokeTest.test_multi_loop_usagec              
      sp  t | j4 I d H H}| 4 I d H *}|dI d H  |ddd tdD I d H  | I d H  W d   I d H  n1 I d H sDw   Y  W d   I d H  n1 I d H sYw   Y  t | j4 I d H ;}|d4 I d H }g }|2 z3 d H W }|| qw6 W d   I d H  n1 I d H sw   Y  W d   I d H  n1 I d H sw   Y  t	|dksJ d S )NzBcreate table context_cursor (i integer primary key asc, k integer)z)insert into context_cursor (k) values (?)c                 S   rH   r   r   r=   r   r   r   r?      rI   z1SmokeTest.test_context_cursor.<locals>.<listcomp>r@   zselect * from context_cursorrJ   rM   r   r   r   test_context_cursor   s,   

*(*(zSmokeTest.test_context_cursorc              	      s   t | j4 I d H A}| I d H }|dI d H }| ||d |dddgI d H }| || |dI d H }| || W d   I d H  d S 1 I d H sRw   Y  d S )Nz;create table test_cursor_return_self (i integer, k integer)zcursor execute returns itselfz1insert into test_cursor_return_self values (?, ?))r
   r
   )r   r   zinsert into test_cursor_return_self values (3, 3);insert into test_cursor_return_self values (4, 4);insert into test_cursor_return_self values (5, 5);)r   r   r   r'   r"   r$   rK   executescript)r   r   r'   resultr   r   r   test_cursor_return_self   s    


.z!SmokeTest.test_cursor_return_selfc              
      sL  t | j4 I d H }| |jd | 4 I d H 4}| |j |dI d H  |dI d H  | 	|j |
 I d H  | |j W d   I d H  n1 I d H sVw   Y  | |jd | |j | |jt | 4 I d H ?}|dI d H  | I d H }| |t | |d | t |d }W d    n1 sw   Y  W d   I d H  n1 I d H sw   Y  | 4 I d H M}t j|_| |jt j |dI d H  | I d H }| |t j | |d d | |d d	 | |d d | |d
 d	 W d   I d H  n1 I d H sw   Y  t j|_t|_| |jt j | |jt | 4 I d H A}|dI d H  | I d H }| |t j | |d d | |d d | |d d | |d
 d W d   I d H  n1 I d H sw   Y  W d   I d H  d S 1 I d H sw   Y  d S )Nr   zKcreate table test_properties (i integer primary key asc, k integer, d text)z3insert into test_properties (k, d) values (1, 'hi')r
   zselect * from test_properties)r
   r
   hirU   r   ri   ds   hi)r   r   r   r$   total_changesr'   assertFalsein_transactionr"   
assertTruer7   assertIsNonerow_factorytext_factorystrfetchoner    tupleassertRaises	TypeErrorRowbytes)r   r   r'   rN   _r   r   r   test_connection_properties   sh   

(
(*,0z$SmokeTest.test_connection_propertiesc              	      s   t | j4 I d H "}|dI d H  |dI d H  | I d H  W d   I d H  n1 I d H s3w   Y  t | j4 I d H #}|dI d H }| I d H }| |g d W d   I d H  d S 1 I d H skw   Y  d S )NzBcreate table test_fetch_all (i integer primary key asc, k integer)z<insert into test_fetch_all (k) values (10), (24), (16), (32)z)select k from test_fetch_all where k < 30))r@   )   )   )r   r   r   r"   r7   r#   r$   r&   r   r   r   test_fetch_all  s   

(	.zSmokeTest.test_fetch_allc                    s   t | j4 I dH S}z|dI dH  |dI dH  W n) ty7 } zd|jvs-J W Y d}~n d}~w tyG } ztd|d}~ww W d  I dH  dS W d  I dH  dS 1 I dH sdw   Y  dS )z@Assert that after enabling extension loading, they can be loadedNTtestznot authorizedzKpython was not compiled with sqlite3 extension support, so we can't test it)	r   r   r   enable_load_extensionload_extensionr   r]   AttributeErrorr   )r   r   er   r   r   test_enable_load_extension  s(   .z$SmokeTest.test_enable_load_extensionc              	      s   t | j4 I dH 4}|dd dI dH  | t |dI dH  W d   n1 s/w   Y  W d  I dH  dS 1 I dH sEw   Y  dS )ze
        Assert that after setting a progress handler returning 1, DB operations are aborted
        Nc                   S      dS )Nr
   r   r   r   r   r   <lambda>$  s    z5SmokeTest.test_set_progress_handler.<locals>.<lambda>r
   zIcreate table test_progress_handler (i integer primary key asc, k integer))r   r   r   set_progress_handlerru   r   r"   r   r   r   r   r   test_set_progress_handler  s   .z#SmokeTest.test_set_progress_handlerc              
      s8  dd }dd }t | j4 I dH |}|dd|I dH  |dd	|I dH  |d
4 I dH }| I dH }| |d d W d  I dH  n1 I dH sQw   Y  |d4 I dH }| I dH }| |d d W d  I dH  n1 I dH sw   Y  W d  I dH  dS 1 I dH sw   Y  dS )z<Assert that after creating a custom function, it can be usedc                   S   r   )Nno argr   r   r   r   r   no_arg-  r/   z.SmokeTest.test_create_function.<locals>.no_argc                 S      | d S Nr   r   numr   r   r   one_arg0     z/SmokeTest.test_create_function.<locals>.one_argNr   r   r   r
   zSELECT no_arg();r   zSELECT one_arg(10);   )r   r   r   create_functionr"   rs   r$   )r   r   r   r   resrN   r   r   r   test_create_function*  s   (*.zSmokeTest.test_create_functionc              	      s   dd }t | j4 I dH (}|jdd|ddI dH  |dI dH  |d	I dH  W d  I dH  dS 1 I dH s=w   Y  dS )
zAssert that after creating a deterministic custom function, it can be used.

        https://sqlite.org/deterministic.html
        c                 S   r   r   r   r   r   r   r   r   E  r   z=SmokeTest.test_create_function_deterministic.<locals>.one_argNr   r
   T)deterministicz"create table foo (id int, bar int)z#create index t on foo(one_arg(bar)))r   r   r   r   r"   )r   r   r   r   r   r   "test_create_function_deterministic?  s   .z,SmokeTest.test_create_function_deterministicc              	      s   g  dt f fdd}t| j4 I d H "}||I d H  |dI d H  | d  W d   I d H  d S 1 I d H s>w   Y  d S )N	statementc                    s     |  d S r   )rL   )r   
statementsr   r   callbackR  s   z3SmokeTest.test_set_trace_callback.<locals>.callbackz	select 10)rr   r   r   r   set_trace_callbackr"   assertIn)r   r   r   r   r   r   test_set_trace_callbackO  s   .z!SmokeTest.test_set_trace_callbackc              
      s   t d}| td, t|4 I d H }| | W d   I d H  n1 I d H s+w   Y  W d    n1 s:w   Y  | td t|I d H  W d    d S 1 sYw   Y  d S )Nz!/something/that/shouldnt/exist.dbzunable to open database)r   assertRaisesRegexr   r   r   ro   )r   bad_dbr   r   r   r   test_connect_error[  s   ("zSmokeTest.test_connect_errorc                    s   dd }t dd d}tjtd|d; | tjd$ |4 I d H  W d   I d H  n1 I d H s5w   Y  W d    n1 sDw   Y  W d    n1 sSw   Y  |jrc|	  t
d	d S )
Nc                  _   s
   t d)NI changed my mind)rA   CancelledError)ry   __r   r   r   _raise_cancelled_errorf  s   
zESmokeTest.test_connect_base_exception.<locals>._raise_cancelled_errorc                   S   s
   t dS )NrY   )sqlite3r   r   r   r   r   r   i  s   
 z7SmokeTest.test_connect_base_exception.<locals>.<lambda>@   r   )side_effectr   z!connection thread was not stopped)r   r!   r	   objectr   r   rA   r   _running_stop_runningAssertionError)r   r   
connectionr   r   r   test_connect_base_exceptiond  s$   ( z%SmokeTest.test_connect_base_exceptionc              	      s   t d4 I d H 3}|dI d H  |dddgI d H  dd | 2 I d H }| |g d W d   I d H  d S 1 I d H sCw   Y  d S )	NrY   ,create table foo (i integer, k charvar(250))insert into foo values (?, ?)r
   hellor   worldc                    s   g | z3 d H W }|q6 S r   r   )r>   liner   r   r   r?   |  s    z+SmokeTest.test_iterdump.<locals>.<listcomp>)zBEGIN TRANSACTION;z-CREATE TABLE foo (i integer, k charvar(250));z$INSERT INTO "foo" VALUES(1,'hello');z$INSERT INTO "foo" VALUES(2,'world');zCOMMIT;)r   r   r"   rK   iterdumpr$   )r   r   linesr   r   r   test_iterdumpu  s   
.zSmokeTest.test_iterdumpc                    s   t | jI d H }|dI d H }| I d H  | td | I d H  W d    n1 s1w   Y  | td | I d H  W d    d S 1 sOw   Y  d S )Nr   zConnection closed)r   r   r   r"   r%   r   
ValueErrorr#   )r   r   r'   r   r   r    test_cursor_on_closed_connection  s   "z*SmokeTest.test_cursor_on_closed_connectionc              	      s   t | jI d H }|dI d H }g }tdD ]}|dkr(|t|  |t|	  q|D ]}z|I d H  W q5 t
jyH   Y q5w d S )Nr   d   2   )r   r   r   r"   rC   rL   rA   ensure_futurer%   r#   r   ProgrammingError)r   r   r'   tasksr;   taskr   r   r   %test_cursor_on_closed_connection_loop  s   z/SmokeTest.test_cursor_on_closed_connection_loopc                    s4   t | jI d H }| I d H  | I d H  d S r   )r   r   r   r%   r   r   r   r   test_close_twice  s   zSmokeTest.test_close_twicec                    sl  dd }t d4 I d H }t d4 I d H {}|dI d H  |dddgI d H  | I d H  | td |d	I d H  W d    n1 sLw   Y  |j||d
I d H  |d	4 I d H }| I d H }| 	|ddg W d   I d H  n1 I d H sw   Y  W d   I d H  n1 I d H sw   Y  W d   I d H  d S 1 I d H sw   Y  d S )Nc                 S   s   t | || d S r   )print)abcr   r   r   progress  s   z1SmokeTest.test_backup_aiosqlite.<locals>.progressrY   r   r   r   r   no such table: foor4   )r   )
r   r   r"   rK   r7   r   r   backupr#   r$   )r   r   db1db2r'   r(   r   r   r   test_backup_aiosqlite  s,   


*XzSmokeTest.test_backup_aiosqlitec              
      s  t d4 I d H o}tdV}|dI d H  |dddgI d H  | I d H  | td |d W d    n1 sAw   Y  ||I d H  |d}|	 }| 
|ddg W d    n1 siw   Y  W d   I d H  d S 1 I d H sw   Y  d S )NrY   r   r   r   r   r   r4   )r   r   r   r"   rK   r7   r   r   r   r#   r$   )r   r   r   r'   r(   r   r   r   test_backup_sqlite  s"   

.zSmokeTest.test_backup_sqliteN)r0   r1   r2   classmethodr   r   r)   r*   r:   rE   rG   rO   rd   re   rh   rz   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s8    

!7	
r   )rA   r   pathlibr   r   tempfiler   	threadingr   unittestr   r   unittest.mockr	   r   helpersr   r   r   r   r   r   <module>   s   