o
    3i                     @   sR   d dl Z d dlmZ d dlmZmZmZmZmZm	Z	m
Z
mZ G dd de jZdS )    N)patch)get_tagsnew_contextscopedtagidentify_contextset_context_sessionget_context_session_idget_context_distinct_idc                   @   s   e Zd Zdd Zdd Zdd Zeddd	 Zedd
d Zeddd Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )TestContextsc                 C   sj   t dd& tdd tdd t }|d dksJ |d dks#J W d    d S 1 s.w   Y  d S )NTfreshkey1value1key2   r   r   r   )selftags r   h/lsinfo/ai/hellotax_ai/llm_service/venv_embed/lib/python3.10/site-packages/posthog/test/test_contexts.pytest_tag_and_get_tags   s   

"z"TestContexts.test_tag_and_get_tagsc              	   C   s   t ddm tdd t dd$ t i ksJ tdd t d dks&J | dt  W d    n1 s7w   Y  t dd t ddiksKJ W d    n1 sUw   Y  | dt  t d dksjJ W d    d S 1 suw   Y  d S )NTr   outervalueinnerF)r   r   r   assertNotInr   r   r   r   test_new_context_isolation   s   

"z'TestContexts.test_new_context_isolationc              
   C   s   t dd] tdd t dd9 tdd t dd tdd t ddiks*J W d    n1 s4w   Y  t ddiksBJ W d    n1 sLw   Y  t ddiksZJ W d    d S 1 sew   Y  d S )	NTr   level1r   level2value2level3value3r   r   r   r   r   test_nested_contexts4   s   


"z!TestContexts.test_nested_contextszposthog.capture_exceptionc                 C   s>   t  dd }|dd}|dksJ |  t i ksJ d S )Nc                 S   s   t d|  t d| | | S )Nxyr   )r$   r%   r   r   r   successful_functionG   s   

zGTestContexts.test_scoped_decorator_success.<locals>.successful_function   r      )r   assert_not_calledr   )r   mock_capturer'   resultr   r   r   test_scoped_decorator_successE   s   

z*TestContexts.test_scoped_decorator_successc                    st   t d dd }||_t  fdd}| t  |  W d    n1 s'w   Y  |  t i ks8J d S )NzTest exceptionc                 [      t  }|ddksJ d S Nimportant_contextr   r   get	exceptionkwargscurrent_tagsr   r   r   check_context_on_capture\      zNTestContexts.test_scoped_decorator_exception.<locals>.check_context_on_capturec                      s   t dd  r/   r&   r   test_exceptionr   r   failing_functionc   s   
zFTestContexts.test_scoped_decorator_exception.<locals>.failing_function)
ValueErrorside_effectr   assertRaisesassert_called_once_withr   )r   r+   r7   r;   r   r9   r   test_scoped_decorator_exceptionX   s   
z,TestContexts.test_scoped_decorator_exceptionc              	   C   s   t d}dd }||_t 5 tdd zt  tdd |1 s#w   Y  W n	 t y2   Y nw t d dks<J W d    n1 sFw   Y  || d S )NzContext exceptionc                 [   r.   )Ninner_contextinner_valuer1   r3   r   r   r   r7   v   r8   zRTestContexts.test_new_context_exception_handling.<locals>.check_context_on_captureouter_contextouter_valuerA   rB   )RuntimeErrorr=   r   r   r   r?   )r   r+   r:   r7   r   r   r   #test_new_context_exception_handlingr   s    

z0TestContexts.test_new_context_exception_handlingc                 C   T   t dd t d u sJ td t dksJ W d    d S 1 s#w   Y  d S )NTr   user123)r   r
   r   r   r   r   r   test_identify_context   
   "z"TestContexts.test_identify_contextc                 C   rG   )NTr   
session456)r   r	   r   r   r   r   r   test_set_context_session   rJ   z%TestContexts.test_set_context_sessionc              	   C   s   t ddB td td t dd t d u sJ t d u s"J W d    n1 s,w   Y  t dks8J t dks?J W d    d S 1 sJw   Y  d S )NTr   rH   rK   r   r   r   r
   r	   r   r   r   r   &test_context_inheritance_fresh_context   s   "z3TestContexts.test_context_inheritance_fresh_contextc              	   C   s   t ddX td td t dd, t dksJ t dks"J td td t dks1J t dks8J W d    n1 sBw   Y  t dksNJ t dksUJ W d    d S 1 s`w   Y  d S )NTr   rH   rK   Fuser789
session999rM   r   r   r   r   *test_context_inheritance_non_fresh_context   s   "z7TestContexts.test_context_inheritance_non_fresh_contextc              	   C   s   t ddo tdd tdd t dd6 t d dksJ tdd td	d
 t }|d dks4J |d dks<J |d	 d
ksDJ W d    n1 sNw   Y  t }|d dks^J |d dksfJ d	|vslJ W d    d S 1 sww   Y  d S )NTr   
shared_keyparent_valueparent_onlyparentFchild_value
child_onlychildr   )r   r   parent_tagsr   r   r   9test_child_tags_override_parent_tags_in_non_fresh_context   s"   



"zFTestContexts.test_child_tags_override_parent_tags_in_non_fresh_contextc                 C   sP   t  dd }| \}}|dksJ |dksJ t d u sJ t d u s&J d S )Nc                   S   s   t d td t t fS )Nuser456
session789)r   r   r
   r	   r   r   r   r   function_with_context   s   zRTestContexts.test_scoped_decorator_with_context_ids.<locals>.function_with_contextr[   r\   )r   r
   r	   )r   r]   distinct_id
session_idr   r   r   &test_scoped_decorator_with_context_ids   s   

z3TestContexts.test_scoped_decorator_with_context_idsN)__name__
__module____qualname__r   r   r#   r   r-   r@   rF   rI   rL   rN   rQ   rZ   r`   r   r   r   r   r      s     	


		r   )unittestunittest.mockr   posthog.contextsr   r   r   r   r   r   r	   r
   TestCaser   r   r   r   r   <module>   s    (