
    eiN                     X   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mZ ddlm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!y)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)get_collection_versionwait_for_version_increase)sleepbasic_http_clientreturnc                    t        j                  |       }|j                          |j                  dddi      }|j	                  dt
        dd      \  }}|J |d	u sJ t        ||j                        }|j                  t        d
d      D cg c]  }dj                  |       c}dgdz         |j                         dk(  sJ t        ||j                  |       t        d       |j                  d      j                  d      }|d   J |d   d
   d   dk(  sJ |j!                  |j                  d	      }|d	u sJ yc c}w )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paramsTr   ,  doc_{}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formatcountr   r   get_collectiongetdetach_function	r   client
collectionattached_fncreatedinitial_versioniresultsuccesss	            q/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/chromadb/test/distributed/test_task_api.py%test_count_function_attach_and_detachr@      s}   &&'89F
LLN 00!GH 1 J &55(/	 6 K """d??,VZ__EO NN).q#7AX__Q7"#c)   $$$fjooG	"I""#89==>OPF+***+q!-0C777 ((!% ) G d??- 8s   Ec                 B   t        j                  |       }|j                          |j                  d      }|j	                  dgdg       t        j                  t        d      5  |j                  t        j                  dd	d
       d
d
d
       y
# 1 sw Y   y
xY w)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   )r*   r+   r,   r-   r/   pytestraisesr   r.   r   _NONEXISTENT_TEST_ONLYr   r7   r8   s      r?   test_task_with_invalid_functionrM   K   s    &&'89F
LLN006M0NJNNw?*;N< 
{*>	? 
""441	 	# 	

 
 
s   ($BBc                    t        j                  |       }|j                          |j                  d      }|j	                  dgdg       |j                  t        ddd	      \  }}|d
u sJ |j                  dk(  sJ |j                  dk(  sJ |j                  d      }||k(  sJ |j                  |j                  d
       y)z^Test that attach_function and get_attached_function return function_name field instead of UUIDtest_function_namerC   rD   doc1r    
my_counterr   NrH   Trecord_counterr'   )r*   r+   r,   create_collectionr/   r.   r   function_namer   get_attached_functionr5   )r   r7   r8   r9   r:   retrieved_fns         r?   *test_attach_function_returns_function_namerW   ]   s    &&'89F
LLN))/C)DJNNw6(N3 &55(-	 6 K d??$$(8888|+++ 33LAL;&&& {//$O    c                 D   t        j                  |       }|j                          |j                  d      }|j	                  ddgddg       |j                  t        dd	d
      \  }}|J |du sJ |j                  d      }|j	                  ddgddg       |j                  t        ddd
      \  }}|J |du sJ |j                  |j                  k7  sJ |j                  |j                  d      du sJ |j                  |j                  d      du sJ y
)z0Test attaching functions on multiple collectionscollection_1rC   rD   id2rP   doc2r    task_1output_1NrH   Tcollection_2id3id4doc3doc4task_2output_2r'   
r*   r+   r,   rS   r/   r.   r   idr5   r   )r   r7   collection1attached_fn1created1collection2attached_fn2created2s           r?   "test_function_multiple_collectionsrn   z   sp   &&'89F
LLN ***?KOO662BOC(88($	 9 L( ###t ***?KOO662BOC(88($	 9 L( ###t ??loo--- 	##L$5$5PT#U		 	##L$5$5PT#U		rX   c                    t        j                  |       }|j                          |j                  d      }|j	                  g dg d       |j                  t        ddd	      \  }}|J |d
u sJ t        j                  t        d      5  |j                  t        ddd	       ddd       t        j                  t        d      5  |j                  t        ddd	       ddd       |j                  |j                  d
      d
u sJ |j                  t        ddd	      \  }}|J |d
u sJ |j                  |j                  k7  sJ |j                  |j                  d
      d
u sJ y# 1 sw Y   xY w# 1 sw Y   xY w)z>Test that only one attached function is allowed per collectionsingle_task_collectionrC   )rD   r[   r`   )rP   r\   rb   r    r]   r^   NrH   Tzmcollection already has an attached function: name=task_1, function=record_counter, output_collection=output_1rE   rd   re   output_differentr'   )r*   r+   r,   rS   r/   r.   r   rI   rJ   r   r   r5   r   rg   )r   r7   r8   ri   r:   rl   rm   s          r?   3test_functions_one_attached_function_per_collectionrr      s    &&'89F
LLN ))/G)HJNN,8PNQ '66($	 7 L' ###d?? 
}
 	
 	"",(	 	# 	
		
 
~
 	
 	""(0	 	# 	
		
 	""<#4#4t"T		
 (77($	 8 L( ###t??loo--- 	""<#4#4t"T		W	
 	
	
 	
s   E3E?3E<?Fc                 2   t        j                  |       }|j                          |j                  d      }|j	                  dgdg       t        j                  t        d      5  |j                  dt        d	d
di       ddd       y# 1 sw Y   yxY w)z?Test that attach_function with non-empty params raises an errortest_invalid_paramsrC   rD   r   r    z?params must be empty - no functions currently accept parametersrE   invalid_params_taskr   some_key
some_valuer   N
r*   r+   r,   rS   r/   rI   rJ   r   r.   r   rL   s      r?   (test_attach_function_with_invalid_paramsry      s    &&'89F
LLN))/D)EJNNw?*;N< 
O
 	
 	""&,1-	 	# 	
		
 	
 	
s   (BBc                 R   t        j                  |       }|j                          |j                  d      }|j	                  dgdg       |j                  d       t        j                  t        d      5  |j                  d	t        dd
       d
d
d
       y
# 1 sw Y   y
xY w)zJTest that attach_function fails when output collection name already existsinput_collectionrC   rD   r   r    existing_output_collectionz?Output collection \[existing_output_collection\] already existsrE   my_taskNr   rx   )r   r7   r{   s      r?   5test_attach_function_output_collection_already_existsr~     s     &&'89F
LLN //5G/HeW0AB ">? 
P
 	
 	((,:	 	) 	
		
 	
 	
s   :BB&c                    t        j                  |       }|j                          |j                  d      }|j	                  dgdg       |j                  t        ddd	      \  }}|j                  |j                  d
       t        j                  t        d      5  |j                  |j                  d
       ddd       y# 1 sw Y   yxY w)z<Test removing a task that doesn't exist raises NotFoundErrortest_collectionrC   rD   testr    test_functionr   NrH   Tr'   zdoes not existrE   )r*   r+   r,   rS   r/   r.   r   r5   r   rI   rJ   r	   )r   r7   r8   r9   _s        r?    test_function_remove_nonexistentr   "  s    &&'89F
LLN))/@)AJNNw6(N3//(-	 0 NK {//$O 
},<	= T"";#3#3d"ST T Ts   !CCc                    t        j                  |       }|j                          |j                  d      }|j	                  dgdg       |j                  dt        dd	      \  }}|j                  d      }t        j                  t        d
      5  |j                  dt        dd	      }ddd       y# 1 sw Y   yxY w)z<Test that attaching a function to an output collection failsr{   rC   rD   r   r    r   r   Nr   z.cannot attach function to an output collectionrE   test_function_2output_collection_2)r*   r+   r,   rS   r/   r.   r   r3   rI   rJ   r   )r   r7   r{   r   r   s        r?   &test_attach_to_output_collection_failsr   7  s    &&'89F
LLN //5G/HeW9++(-	 , DAq --3F-G	K
 
 --",3	 . 

 
 
s   B99Cc                    t        j                  |       }|j                          |j                  d      }|j	                  dgdg       |j                  dt        dd	      \  }}|J |d
u sJ |j                  d       t        j                  t              5  |j                  d       ddd       y# 1 sw Y   yxY w)zKTest that deleting an output collection also detaches the attached functionr{   rC   rD   r   r    my_functionr   Nr   T)r*   r+   r,   rS   r/   r.   r   delete_collectionrI   rJ   r	   rU   r   r7   r{   r9   r:   s        r?   /test_delete_output_collection_detaches_functionr   S  s    &&'89F
LLN //5G/HeW9+;;(-	 < K """d?? 01 
}	% >..}=> > >s   B88Cc                 @   t        j                  |       }|j                          |j                  d      }|j	                  dgdg       |j                  dt        dd	      \  }}|J |d
u sJ |j                  |j                  d       |j                  d       t        j                  t              5  |j                  d       ddd       t        j                  t              5  |j                  d       ddd       y# 1 sw Y   =xY w# 1 sw Y   yxY w)zOTest that deleting an output collection from a recently detached function worksr{   rC   rD   r   r    r   r   Nr   TFr'   )r*   r+   r,   rS   r/   r.   r   r5   r   r   rI   rJ   r	   rU   r3   r   s        r?   &test_delete_orphaned_output_collectionr   m  s   &&'89F
LLN //5G/HeW9+;;(-	 < K """d??$$[%5%5PU$V 01 
}	% >..}=> 
}	% 3123 3> >3 3s   :D-DDDc                    t        j                  |       }|j                          |j                  d      }|j	                  dt
        dd      \  }}|du sJ |J |j                  d      }t        j                  t        d	
      5  |j	                  dt
        dd      \  }}ddd       |j                  |j                  d      du sJ |j	                  dt
        dd      \  }}|J |du sJ y# 1 sw Y   QxY w)r   r   rC   r   r   Nr   Tmy_document2z8Output collection \[my_documents_counts\] already existsrE   r'   )r*   r+   r,   r-   r.   r   rI   rJ   r   r5   r   )r   r7   r8   r9   r:   rk   r   s          r?   #test_partial_attach_function_repairr     sS    &&'89F
LLN 00 1 J
 &55(/	 6 K d?? """11 2 K 
V
 
 %44 ,3	 5 
Q
 	"";#3#3d"S		
 '66(/	 7 K """d??1
 
s   C88Dc                    t        j                  |       }|j                          |j                  d      }|j	                  dgdg       |j                  dt        dd	      \  }}|J |d
u sJ |j                  d      }|J d|j                  j                         v sJ |j                  |j                  d
       y)z[Test that output collections are created with the source_attached_function_id in the schemar{   rC   rD   r   r    r   r   Nr   Tsource_attached_function_idr'   )r*   r+   r,   rS   r/   r.   r   r3   _modelpretty_schemar5   r   )r   r7   r{   r9   r:   r   s         r?   *test_output_collection_created_with_schemar     s    &&'89F
LLN //5G/HeW9+;;(-	 < K """d?? --3F-G(((
 ),=,D,D,R,R,TTTT $$[%5%5PT$UrX   c                 0   t        j                  |       }|j                          |j                  dddi      }|j	                  dt
        dd      \  }}|d	u sJ |J t        ||j                        }|j                  t        d
d      D cg c]  }dj                  |       c}dgdz         |j                         dk(  sJ t        ||j                  |       t        d       |j                  d      j                  d      }|d   J |d   d
   d   dk(  sJ |j!                  |j                  d	      }|d	u sJ |j	                  dt
        dd      \  }}|J |d	u sJ |j	                  dt
        dd      \  }}|du sJ |J yc c}w )r   r   r   r   r   r   r   Nr   Tr   r   r   r   r    r#   r$   r%   r&   r'   Fr)   r6   s	            r?   3test_count_function_attach_and_detach_attach_attachr     s    &&'89F
LLN 00!GH 1 J &55(/	 6 K d??""",VZ__EO NN).q#7AX__Q7"#c)   $$$fjooG	"I""#89==>OPF+***+q!-0C777 ((4 ) G
 d?? &55(/	 6 K """d?? &55(/	 6 K e"""S 8s   Fc                    t        j                  |       }|j                          |j                  d      }|j	                  dgdg       |j                  dt        dd	      \  }}|J |d
u sJ |j                  dt        dd	      \  }}|J |du sJ |j                  |j                  k(  sJ |j                  |j                  d
       y)zaTest that attach_function is idempotent - calling it twice with same params returns created=Falseidempotency_testrC   rD   r   r    r   r   Nr   TFr'   rf   )r   r7   r8   ri   rj   rl   rm   s          r?    test_attach_function_idempotencyr   +  s    &&'89F
LLN))/A)BJNNw?*;N< (77(-	 8 L( ###t (77(-	 8 L( ###u ??loo--- |004PrX   )"__doc__rI   chromadb.api.clientr   r*   chromadb.api.functionsr   r   r   chromadb.configr   chromadb.errorsr   r	   -chromadb.test.utils.wait_for_version_increaser
   r   timer   r@   rM   rW   rn   rr   ry   r~   r   r   r   r   r   r   r   r    rX   r?   <module>r      sk    7 
 # 6 0V 0 0f
v 
$ 
$P& PT P:,& ,T ,^EE	EP
 
4 
,

	
6T T4 T*
f 
 
8>v >RV >43f 3 3>66	6rV& VT V>E#E#	E#N Q  Q4  QrX   