
    tni/                         d Z ddlZddlmZmZ ddlZddlZddlmZ ddl	m
Z
mZmZ  G d dej                  Z G d d	ej                  Z G d
 dej                  ZdS )z5
Tests for runpod | endpoint | modules | endpoint.py
    N)Mockpatch)runner)EndpointJobRunPodClientc                   "   e Zd ZdZd Zd Zd Zd Z ej	        e
j        d          d             Z ej	        e
j        d          d	             Z ej	        e
j        d
          d             Z ej	        e
j        d          d             ZdS )TestRunPodClientzTests for RunPodClientc                     |                      t                    5  dt          _        t	                       ddd           dS # 1 swxY w Y   dS )z4
        Tests RunPodClient with no api_key
        N)assertRaisesRuntimeErrorrunpodapi_keyr   selfs    \/var/www/html/gpu-tools/venv/lib/python3.11/site-packages/tests/test_endpoint/test_runner.pytest_no_api_keyz TestRunPodClient.test_no_api_key   s     |,, 	 	!FNNNN	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   AA
Ac                     d}t          |          }|                     |j        d         d|            |                     |j        |           dS )z%Test RunPodClient with custom API keyCUSTOM_API_KEYr   AuthorizationBearer N)r   assertEqualheadersr   r   
custom_keyclients      r   test_client_with_custom_api_keyz0TestRunPodClient.test_client_with_custom_api_key   sf    %
j111 	N?+"j""	
 	
 	
 	44444    c                     dt           _        t                      }|                     |j        d         d           |                     |j        d           dS )z.Test RunPodClient falls back to global API keyGLOBAL_API_KEYr   zBearer GLOBAL_API_KEYNr   r   r   r   r   )r   r   s     r   test_client_fallback_to_globalz/TestRunPodClient.test_client_fallback_to_global&   sX    )N?+#	
 	
 	
 	)9:::::r   c                     dt           _        d}t          |          }|                     |j        d         d|            |                     |j        |           dS )z$Test custom API key overrides globalr!   r   r   r   r   Nr"   r   s      r   #test_client_custom_overrides_globalz4TestRunPodClient.test_client_custom_overrides_global1   sk    )%
j111N?+"j""	
 	
 	
 	44444r   postc                    t                      }d|_        ||_        |                     t                    5  dt
          _        t                      }|                    ddi i           ddd           dS # 1 swxY w Y   dS )z>
        Tests RunPodClient.post with 401 status code
          MOCK_API_KEYENDPOINT_ID/runinputN)	r   status_codereturn_valuer   r   r   r   r   r&   )r   	mock_postmock_responser   s       r   test_post_with_401z#TestRunPodClient.test_post_with_401>   s    
 $'!!.	|,, 	: 	:+FN!^^FKK)GR=999	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:s   3A77A;>A;requestc                     t                      }d|_        ddi|j        _        ||_        dt          _        t                      }|                    ddi i          }|                     |ddi           dS )z)
        Tests RunPodClient.post
           id123r)   r*   r+   N)	r   r,   jsonr-   r   r   r   r&   r   )r   r.   r/   r   responses        r   	test_postzTestRunPodClient.test_postL   st    
 $'!+/-'!.	';;07B-@@D%=11111r   getc                    t                      }d|_        ||_        |                     t                    5  dt
          _        t                      }|                    d           ddd           dS # 1 swxY w Y   dS )z=
        Tests RunPodClient.get with 401 status code
        r(   r)   ENDPOINT_ID/status/123N)	r   r,   r-   r   r   r   r   r   r9   )r   mock_getr/   r   s       r   test_get_with_401z"TestRunPodClient.test_get_with_401\   s    
 $'! -|,, 	1 	1+FN!^^FJJ/000	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1s   0A44A8;A8c                     t                      }d|_        ddi|j        _        ||_        dt          _        t                      }|                    d          }|                     |ddi           dS )z(
        Tests RunPodClient.get
        r3   status	COMPLETEDr)   r;   N)	r   r,   r6   r-   r   r   r   r9   r   )r   r<   r/   r   r7   s        r   test_getzTestRunPodClient.test_getj   sp    
 $'!+3[*A' -'::677Hk#:;;;;;r   N)__name__
__module____qualname____doc__r   r   r#   r%   r   objectrequestsSessionr0   r8   r=   rA    r   r   r
   r
      s
           
5 
5 
5	; 	; 	;
5 
5 
5 U\("F++: : ,+: U\("I..2 2 /.2 U\("E**1 1 +*1 U\("I..< < /.< < <r   r
   c                      e Zd ZdZdZdZddiZddiZd Zd	 Z	 e
d
          d             Z e
d
          d             Z e
d
          d             Z e
d
          d             Zd Z e
j        ej        d          d             Z e
j        ej        j        j        d          d             Z e
j        ej        j        j        d          d             Z e
j        ej        j        j        d          d             ZdS )TestEndpointzTests for EndpointENDPOINT_IDr)   YOUR_MODEL_INPUT_JSONYOUR_MODEL_INPUT_VALUEresultYOUR_MODEL_OUTPUT_VALUEc                 Z    | j         t          _        t          | j                  | _        dS )zCommon setup for the tests.N)r)   r   r   r   rL   endpointr   s    r   setUpzTestEndpoint.setUp   s"    * !122r   c                 v    d}t          | j        |          }|                     |j        j        |           dS )z)Test Endpoint with instance-level API keyINSTANCE_API_KEYr   N)r   rL   r   	rp_clientr   )r   r   rR   s      r   #test_endpoint_with_instance_api_keyz0TestEndpoint.test_endpoint_with_instance_api_key   s@    '
D,jAAA 	+3Z@@@@@r   z,runpod.endpoint.runner.RunPodClient._requestc                    ddd|_         | j                            | j                  }|                    d| j         ddddiid	           |                     |t                     |                     |j	        d           |                     |
                                d           |                    d
| j         dd	           dS )zATest the run method of Endpoint with a successful job initiation.r5   IN_PROGRESSr4   r?   POSTz/runr+   rM   rN   
   GETz/status/123timeoutN)r-   rR   runMODEL_INPUTassert_called_once_withrL   assertIsInstancer   r   job_idr?   assert_called_withr   mock_client_requestrun_requests      r   test_endpoint_runzTestEndpoint.test_endpoint_run   s     38=+Q+Q(m''(899 	33%%%.0HIJ		
 	
 	
 	k3///+U333++--}===..d&333R 	/ 	
 	
 	
 	
 	
r   c                     dd| j         d|_        | j                            | j                  }|                     || j                    |                    d| j         ddddiid	           d
S )zFTest the run_sync method of Endpoint with a successful job initiation.r5   r@   r4   r?   outputr[   z/runsyncr+   rM   rN   iQ N)MODEL_OUTPUTr-   rR   run_syncra   r   rb   rL   rf   s      r   test_endpoint_run_syncz#TestEndpoint.test_endpoint_run_sync   s     !',
 ,
( m,,T-=>> 	d&788833))).0HIJ		
 	
 	
 	
 	
r   c                 x    | j                                          |                    d| j         dd           dS )"Test the health method of Endpointr]   z/health   r^   N)rR   healthrb   rL   r   rg   s     r   test_endpoint_healthz!TestEndpoint.test_endpoint_health   sR     	33d&/// 	4 	
 	
 	
 	
 	
r   c                 x    | j                                          |                    d| j         ddd           dS )rq   r[   z/purge-queueNrr   )rR   purge_queuerb   rL   rt   s     r   test_endpoint_purge_queuez&TestEndpoint.test_endpoint_purge_queue   sO     	!!###33t'555tQ	
 	
 	
 	
 	
r   c                     |                      t                    5  dt          _        | j                            | j                   ddd           dS # 1 swxY w Y   dS )z4
        Tests Endpoint.run without api_key
        N)r   r   r   r   rR   r`   ra   r   s    r   test_missing_api_keyz!TestEndpoint.test_missing_api_key   s     |,, 	0 	0!FNMd.///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s   ,AAAr&   c                     t                      }d|_        ||_        t          j        d          }ddi}|                     t                    5  |                    |           ddd           dS # 1 swxY w Y   dS )z9
        Tests Endpoint.run with 401 status code
        r(   rL   rM   rN   N)r   r,   r-   r   r   r   r   r`   )r   r.   r/   rR   request_datas        r   test_run_with_401zTestEndpoint.test_run_with_401   s    
 $'!!.	?=11/1IJ|,, 	' 	'LL&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	's   A22A69A6_requestc                    ddd|_         dt          _        t          j        d          }ddi}|                    |          }|                     |j        d           |                     |                                d           dS )	z$
        Tests Endpoint.run
        r5   rY   rZ   r)   rL   rM   rN   N)r-   r   r   r   r`   r   rd   r?   r   rg   rR   r|   rh   s        r   test_runzTestEndpoint.test_run   s    
 38=+Q+Q('?=11/1IJll<00+U333++--}=====r   c                     ddddid|_         dt          _        t          j        d          }dd	i}|                    |          }|                     |ddi           d
S )z)
        Tests Endpoint.run_sync
        r5   r@   rO   rP   rk   r)   rL   rM   rN   N)r-   r   r   r   rn   r   r   s        r   test_run_synczTestEndpoint.test_run_sync   s{     !!:;,
 ,
( (?=11/1IJ''55x1J&KLLLLLr   c                     ddd|_         dt          _        t          j        d          }ddi}|                     t
                    5  |                    |d	           d
d
d
           d
S # 1 swxY w Y   d
S )z6
        Tests Endpoint.run_sync with timeout
        r5   rY   rZ   r)   rL   rM   rN      r^   N)r-   r   r   r   r   TimeoutErrorrn   )r   rg   rR   r|   s       r   test_run_sync_with_timeoutz'TestEndpoint.test_run_sync_with_timeout
  s    
 38=+Q+Q('?=11/1IJ|,, 	7 	7lA666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7s   	A..A25A2N)rB   rC   rD   rE   rL   r)   ra   rm   rS   rW   r   ri   ro   ru   rx   rz   rF   rG   rH   r}   r   rR   r   r   r   r   r   rI   r   r   rK   rK   {   s       K!L*,DEK78L3 3 3
A A A U9::
 
 ;:
, U9::
 
 ;:
( U9::
 
 ;:
 U9::
 
 ;:
0 0 0 U\("F++' ' ,+' U\&/(5zBB> > CB> U\&/(5zBBM M CBM$ U\&/(5zBB7 7 CB7 7 7r   rK   c                   2   e Zd ZdZddiZ ed          d             Z ed          d             Z ed          d             Z ed          d             Z	 ed          d	             Z
 ed          d
             Z ed          d             ZdS )TestJobzTests for JobrO   rP   z#runpod.endpoint.runner.RunPodClientc                     ddi|j         _        t          j        dd|          }|                                }|                     |d           dS )"
        Tests Job.status
        r?   r@   endpoint_idrd   N)r9   r-   r   r   r?   r   )r   mock_clientjobr?   s       r   test_statuszTestJob.test_status  sO    
 )1+'>$j+>>-----r   c                     ddd|j         _        t          j        dd|          }|                                }|                     |d           dS )z"
        Tests Job.output
        r@   
Job outputr?   rl   r   rd   N)r9   r-   r   r   rl   r   r   r   r   rl   s       r   test_outputzTestJob.test_output)  sR    
 3>'V'V$j+>>.....r   c                     ddidddg|j         _        t          j        dd|          }|                    d	          }|                     |d           d
S )z-
        Tests Job.output with sleep
        r?   rY   r@   r   r   r   rd   r\   r^   N)r9   side_effectr   r   rl   r   r   s       r   test_output_with_sleepzTestJob.test_output_with_sleep4  sg     }%"l;;'
#
 j+>>B''.....r   c                     ddi|j         _        t          j        dd|          }|                     t
                    5  |                    d           ddd           dS # 1 swxY w Y   dS )z/
        Tests Job.output with timeout
        r?   rY   r   rd   r   r^   N)r9   r-   r   r   r   r   rl   r   r   r   s      r   test_output_timeoutzTestJob.test_output_timeoutC  s    
 )1-'@$j+>>|,, 	" 	"JJqJ!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s   A##A'*A'c                     t          j        dd|          }|                                 |j                            ddd           dS )z?Test the cancel method of Job with a successful job initiation.r   rd   zendpoint_id/cancel/job_idNrr   )datar_   )r   r   cancelr&   re   r   s      r   test_cancelzTestJob.test_cancelN  sU     j+>>

++'dA 	, 	
 	
 	
 	
 	
r   c                 F   ddiddig|j         _        t          j        dd|          }|                     |                                d           |                     |                                d           |                     |                                d           dS )r   r?   rY   r@   r   rd   N)r9   r   r   r   r   r?   r   s      r   test_job_statuszTestJob.test_job_statusY  s     }%{#'
#
 j+>>}555{333{33333r   c                     dddiddigddg dg|j         _        t          j        dd|          }t	          |                                          }|                     |ddg           d	S )
z"
        Tests Job.stream
        rY   rl   zJob output 1zJob output 2)r?   streamr@   r   rd   N)r9   r   r   r   listr   r   r   s       r   test_job_streamzTestJob.test_job_streamh  s     ($n5.7QR  #b11'
# j+>>cjjll##..!ABBBBBr   N)rB   rC   rD   rE   rm   r   r   r   r   r   r   r   r   rI   r   r   r   r     s9       78L
U011. . 21. U011/ / 21/ U011/ / 21/ U011" " 21" U011
 
 21
 U0114 4 214 U011C C 21C C Cr   r   )rE   unittestunittest.mockr   r   rG   r   runpod.endpointr   runpod.endpoint.runnerr   r   r   TestCaser
   rK   r   rI   r   r   <module>r      s+     % % % % % % % %   " " " " " " > > > > > > > > > >i< i< i< i< i<x( i< i< i<X[7 [7 [7 [7 [78$ [7 [7 [7|^C ^C ^C ^C ^Ch ^C ^C ^C ^C ^Cr   