
    _jUO                     |   d Z ddlZddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZmZ ddlmZ ddlmZmZ dd	lmZ  e            gZd
e
ddfdZd
e
ddfdZd
e
ddfdZd
e
ddfdZd
e
ddfdZd
e
ddfdZd
e
ddfdZd
e
ddfdZd
e
ddfdZd
e
ddfdZd
e
ddfdZ d
e
ddfdZ!d
e
ddfdZ"d
e
ddfdZ#d
e
ddfdZ$dS )z
Integration test for Chroma's Task API

Tests the task creation, execution, and removal functionality
for automatically processing collections.
    N)Client)RECORD_COUNTER_FUNCTIONSTATISTICS_FUNCTIONFunction)System)ChromaErrorNotFoundError)skip_if_not_cluster)get_collection_versionwait_for_version_increase)sleepbasic_http_clientreturnc                    t          j        |           }|                                 |                    dddi          }|                    dt
          dd          \  }}|J |d	u sJ t          ||j                  }|                    d
 t          dd          D             dgdz             |
                                dk    sJ t          ||j        |           t          d           |                    d                              d          }|d         J |d         d         d         dk    sJ |                    |j        d	          }|d	u sJ dS )FTest creating and removing a function with the record_counter operatormy_documentdescription$Sample documents for task processingnamemetadatacount_my_docsmy_documents_countsNr   functionoutput_collectionparamsTc                 8    g | ]}d                      |          S zdoc_{}format.0is     d/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/chromadb/test/distributed/test_task_api.py
<listcomp>z9test_count_function_attach_and_detach.<locals>.<listcomp>5   $    777AX__Q777    r   ,  test documentids	documents<   function_output	metadatastotal_countdelete_output_collectionClientCreatorfrom_systemresetget_or_create_collectionattach_functionr   r   r   addrangecountr   r   get_collectiongetdetach_functionr   client
collectionattached_fncreatedinitial_versionresultsuccesss           r%   %test_count_function_attach_and_detachrH      s   &'899F
LLNNN 00!GH 1  J &55(/	 6  K """d????,VZ_EEO NN77q#777"#c)     $$$$fjoGGG	"III""#899==>OPPF+***+q!-0C7777 ((!% )  G d??????r(   c                 b   t          j        |           }|                                 |                    d          }|                    dgdg           t          j        t          d          5  |                    t          j
        dd	d
           d
d
d
           d
S # 1 swxY w Y   d
S )zBTest that creating a task with an invalid function raises an errortest_invalid_functionr   id1r*   r+   zfunction not foundmatchinvalid_taskr   Nr   r   r   r   )r5   r6   r7   r8   r:   pytestraisesr   r9   r   _NONEXISTENT_TEST_ONLYr   rA   rB   s      r%   test_task_with_invalid_functionrU   N   s    &'899F
LLNNN006M0NNJNNw?*;N<<< 
{*>	?	?	? 
 
""41	 	# 	
 	
 	

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   3$B$$B(+B(c                    t          j        |           }|                                 |                    d          }|                    dgdg           |                    t          ddd	          \  }}|d
u sJ |j        dk    sJ |j        dk    sJ |	                    d          }||k    sJ |
                    |j        d
           dS )z^Test that attach_function and get_attached_function return function_name field instead of UUIDtest_function_namerK   rL   doc1r+   
my_counterr   NrP   Trecord_counterr2   )r5   r6   r7   create_collectionr:   r9   r   function_namer   get_attached_functionr?   )r   rA   rB   rC   rD   retrieved_fns         r%   *test_attach_function_returns_function_namer_   `   s    &'899F
LLNNN))/C)DDJNNw6(N333 &55(-	 6  K d????$(88888|++++ 33LAAL;&&&& {/$OOOOOr(   c                 j   t          j        |           }|                                 |                    d          }|                    ddgddg           |                    t          dd	d
          \  }}|J |du sJ |                    d          }|                    ddgddg           |                    t          ddd
          \  }}|J |du sJ |j        |j        k    sJ |                    |j	        d          du sJ |                    |j	        d          du sJ d
S )z0Test attaching functions on multiple collectionscollection_1rK   rL   id2rX   doc2r+   task_1output_1NrP   Tcollection_2id3id4doc3doc4task_2output_2r2   
r5   r6   r7   r[   r:   r9   r   idr?   r   )r   rA   collection1attached_fn1created1collection2attached_fn2created2s           r%   "test_function_multiple_collectionsru   }   s   &'899F
LLNNN ***??KOO662BOCCC(88($	 9  L( ###t ***??KOO662BOCCC(88($	 9  L( ###t ?lo---- 	##L$5PT#UU	 	 	 	 	##L$5PT#UU	 	 	 	 	 	r(   c                 L   t          j        |           }|                                 |                    d          }|                    g dg d           |                    t          ddd	          \  }}|J |d
u sJ t          j        t          d          5  |                    t          ddd	           ddd           n# 1 swxY w Y   t          j        t          d          5  |                    t          ddd	           ddd           n# 1 swxY w Y   |                    |j        d
          d
u sJ |                    t          ddd	          \  }}|J |d
u sJ |j        |j        k    sJ |                    |j        d
          d
u sJ dS )z>Test that only one attached function is allowed per collectionsingle_task_collectionrK   )rL   rb   rg   )rX   rc   ri   r+   rd   re   NrP   Tzmcollection already has an attached function: name=task_1, function=record_counter, output_collection=output_1rM   rk   rl   output_differentr2   )r5   r6   r7   r[   r:   r9   r   rQ   rR   r   r   r?   r   rn   )r   rA   rB   rp   rD   rs   rt   s          r%   3test_functions_one_attached_function_per_collectionry      s    &'899F
LLNNN ))/G)HHJNN,,,8P8P8PNQQQ '66($	 7  L' ###d???? 
}
 
 
 	
 	
 	"",(	 	# 	
 	
 	
		
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 
~
 
 
 	
 	
 	""(0	 	# 	
 	
 	
		
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	""<#4t"TT	 	 	 	
 (77($	 8  L( ###t?lo---- 	""<#4t"TT	 	 	 	 	 	s$    CCC1DD #D c                 \   t          j        |           }|                                 |                    d          }|                    dgdg           t          j        t          d          5  |                    dt          d	d
di           ddd           dS # 1 swxY w Y   dS )z?Test that attach_function with non-empty params raises an errortest_invalid_paramsrK   rL   r*   r+   z?params must be empty - no functions currently accept parametersrM   invalid_params_taskr   some_key
some_valuer   N
r5   r6   r7   r[   r:   rQ   rR   r   r9   r   rT   s      r%   (test_attach_function_with_invalid_paramsr      s   &'899F
LLNNN))/D)EEJNNw?*;N<<< 
O
 
 
 	
 	
 	""&,1-	 	# 	
 	
 	
		
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
s   3!B!!B%(B%c                    t          j        |           }|                                 |                    d          }|                    dgdg           |                    d           t          j        t          d          5  |                    d	t          dd
           d
d
d
           d
S # 1 swxY w Y   d
S )zJTest that attach_function fails when output collection name already existsinput_collectionrK   rL   r*   r+   existing_output_collectionz?Output collection \[existing_output_collection\] already existsrM   my_taskNr   r   )r   rA   r   s      r%   5test_attach_function_output_collection_already_existsr   
  s"    &'899F
LLNNN //5G/HHeW0ABBB ">??? 
P
 
 
 	
 	
 	((,:	 	) 	
 	
 	
		
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
 	
s   	B55B9<B9c                    t          j        |           }|                                 |                    d          }|                    dgdg           |                    t          ddd	          \  }}|                    |j        d
           t          j
        t          d          5  |                    |j        d
           ddd           dS # 1 swxY w Y   dS )z<Test removing a task that doesn't exist raises NotFoundErrortest_collectionrK   rL   testr+   test_functionr   NrP   Tr2   zdoes not existrM   )r5   r6   r7   r[   r:   r9   r   r?   r   rQ   rR   r	   )r   rA   rB   rC   _s        r%    test_function_remove_nonexistentr   %  sE   &'899F
LLNNN))/@)AAJNNw6(N333//(-	 0  NK {/$OOO 
},<	=	=	= T T"";#3d"SSST T T T T T T T T T T T T T T T T Ts   0CC!Cc                    t          j        |           }|                                 |                    d          }|                    dgdg           |                    dt          dd	          \  }}|                    d          }t          j	        t          d
          5  |                    dt          dd	          }ddd           dS # 1 swxY w Y   dS )z<Test that attaching a function to an output collection failsr   rK   rL   r   r+   r   r   Nr   z.cannot attach function to an output collectionrM   test_function_2output_collection_2)r5   r6   r7   r[   r:   r9   r   r=   rQ   rR   r   )r   rA   r   r   r   s        r%   &test_attach_to_output_collection_failsr   :  sD   &'899F
LLNNN //5G/HHeW999++(-	 ,  DAq --3F-GG	K
 
 
 
 
 --",3	 . 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s   *CCCc                    t          j        |           }|                                 |                    d          }|                    dgdg           |                    dt          dd	          \  }}|J |d
u sJ |                    d           t          j	        t                    5  |                    d           ddd           dS # 1 swxY w Y   dS )zKTest that deleting an output collection also detaches the attached functionr   rK   rL   r   r+   my_functionr   Nr   T)r5   r6   r7   r[   r:   r9   r   delete_collectionrQ   rR   r	   r]   r   rA   r   rC   rD   s        r%   /test_delete_output_collection_detaches_functionr   V  s>   &'899F
LLNNN //5G/HHeW999+;;(-	 <  K """d???? 0111 
}	%	% > >..}===> > > > > > > > > > > > > > > > > >s   1CCCc                    t          j        |           }|                                 |                    d          }|                    dgdg           |                    dt          dd	          \  }}|J |d
u sJ |                    |j        d           |	                    d           t          j        t                    5  |                    d           ddd           n# 1 swxY w Y   t          j        t                    5  |                    d           ddd           dS # 1 swxY w Y   dS )zOTest that deleting an output collection from a recently detached function worksr   rK   rL   r   r+   r   r   Nr   TFr2   )r5   r6   r7   r[   r:   r9   r   r?   r   r   rQ   rR   r	   r]   r=   r   s        r%   &test_delete_orphaned_output_collectionr   p  s   &'899F
LLNNN //5G/HHeW999+;;(-	 <  K """d????$$[%5PU$VVV 0111 
}	%	% > >..}===> > > > > > > > > > > > > > > 
}	%	% 3 312223 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3s$   C//C36C3D66D:=D:c                 B   t          j        |           }|                                 |                    d          }|                    dt
          dd          \  }}|du sJ |J |                    d          }t          j        t          d	
          5  |                    dt
          dd          \  }}ddd           n# 1 swxY w Y   |	                    |j
        d          du sJ |                    dt
          dd          \  }}|J |du sJ dS )r   r   rK   r   r   Nr   Tmy_document2z8Output collection \[my_documents_counts\] already existsrM   r2   )r5   r6   r7   r8   r9   r   rQ   rR   r   r?   r   )r   rA   rB   rC   rD   rr   r   s          r%   #test_partial_attach_function_repairr     s    &'899F
LLNNN 00 1  J
 &55(/	 6  K d???? """11 2  K 
V
 
 
 
 
 %44 ,3	 5 
 
Q
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 	"";#3d"SS	 	 	 	
 '66(/	 7  K """d??????s   "C		CCc                    t          j        |           }|                                 |                    d          }|                    dgdg           |                    dt          dd	          \  }}|J |d
u sJ |                    d          }|J d|j        	                                v sJ |
                    |j        d
           dS )z[Test that output collections are created with the source_attached_function_id in the schemar   rK   rL   r   r+   r   r   Nr   Tsource_attached_function_idr2   )r5   r6   r7   r[   r:   r9   r   r=   _modelpretty_schemar?   r   )r   rA   r   rC   rD   r   s         r%   *test_output_collection_created_with_schemar     s   &'899F
LLNNN //5G/HHeW999+;;(-	 <  K """d???? --3F-GG(((
 ),=,D,R,R,T,TTTTT $$[%5PT$UUUUUr(   c                 t   t          j        |           }|                                 |                    dddi          }|                    dt
          dd          \  }}|d	u sJ |J t          ||j                  }|                    d
 t          dd          D             dgdz             |
                                dk    sJ t          ||j        |           t          d           |                    d                              d          }|d         J |d         d         d         dk    sJ |                    |j        d	          }|d	u sJ |                    dt
          dd          \  }}|J |d	u sJ |                    dt
          dd          \  }}|du sJ |J dS )r   r   r   r   r   r   r   Nr   Tc                 8    g | ]}d                      |          S r   r    r"   s     r%   r&   zGtest_count_function_attach_and_detach_attach_attach.<locals>.<listcomp>  r'   r(   r   r)   r*   r+   r.   r/   r0   r1   r2   Fr4   r@   s           r%   3test_count_function_attach_and_detach_attach_attachr     sC    &'899F
LLNNN 00!GH 1  J &55(/	 6  K d????""",VZ_EEO NN77q#777"#c)     $$$$fjoGGG	"III""#899==>OPPF+***+q!-0C7777 ((4 )  G
 d???? &55(/	 6  K """d???? &55(/	 6  K e"""""r(   c                    t          j        |           }|                                 |                    d          }|                    dgdg           |                    dt          dd	          \  }}|J |d
u sJ |                    dt          dd	          \  }}|J |du sJ |j        |j        k    sJ |                    |j	        d
           dS )zaTest that attach_function is idempotent - calling it twice with same params returns created=Falseidempotency_testrK   rL   r*   r+   r   r   Nr   TFr2   rm   )r   rA   rB   rp   rq   rs   rt   s          r%    test_attach_function_idempotencyr   0  s%   &'899F
LLNNN))/A)BBJNNw?*;N<<< (77(-	 8  L( ###t (77(-	 8  L( ###u ?lo---- |04PPPPPr(   )%__doc__rQ   chromadb.api.clientr   r5   chromadb.api.functionsr   r   r   chromadb.configr   chromadb.errorsr   r	   chromadb.test.conftestr
   -chromadb.test.utils.wait_for_version_increaser   r   timer   
pytestmarkrH   rU   r_   ru   ry   r   r   r   r   r   r   r   r   r   r    r(   r%   <module>r      s&     7 7 7 7 7 7         
 # " " " " " 6 6 6 6 6 6 6 6 6 6 6 6 6 6             !!##$
0V 0 0 0 0 0f
v 
$ 
 
 
 
$P& PT P P P P:,& ,T , , , ,^EE	E E E EP
 
4 
 
 
 
,

	
 
 
 
6T T4 T T T T*
f 
 
 
 
 
8>v >RV > > > >43f 3 3 3 3 3@66	6 6 6 6rV& VT V V V V>E#E#	E# E# E# E#P Q  Q4  Q  Q  Q  Q  Q  Qr(   