
    jKjE1                         d dl Z d dlZd dlmZmZ 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ej"                        Zy)    N)	MagicMockpatch)Client)get_tagsnew_contextscopedtagidentify_contextset_context_sessionget_context_session_idget_context_distinct_idc                       e Zd Zd Zd Zd Z ed      d        Z ed      d        Z ed      d        Z	 ed      d        Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zy)TestContextsc                     t        d      5  t        dd       t        dd       t               }|d   dk(  sJ |d   dk(  sJ 	 d d d        y # 1 sw Y   y xY w)NTfreshkey1value1key2   r   r	   r   )selftagss     k/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/posthog/test/test_contexts.pytest_tag_and_get_tagsz"TestContexts.test_tag_and_get_tags   sT    t$!N:D<8+++<1$$$ %$$s   7AAc                    t        d      5  t        dd       t        d      5  t               i k(  sJ t        dd       t               d   dk(  sJ | j                  dt                      d d d        t        d      5  t               ddik(  sJ 	 d d d        | j                  dt                      t               d   dk(  sJ 	 d d d        y # 1 sw Y   fxY w# 1 sw Y   KxY w# 1 sw Y   y xY w)NTr   outervalueinnerF)r   r	   r   assertNotInr   s    r   test_new_context_isolationz'TestContexts.test_new_context_isolation   s    t$!4(zR''' GW%z'*g555   (*5 ) 5)zgw%7777 *
 Whj1 :g&'111/ %$ )( *) %$s;   C+AC.C+C4C+C	C+C(	$C++C4c                 ~   t        d      5  t        dd       t        d      5  t        dd       t        d      5  t        dd       t               ddik(  sJ 	 d d d        t               ddik(  sJ 	 d d d        t               ddik(  sJ 	 d d d        y # 1 sw Y   >xY w# 1 sw Y   0xY w# 1 sw Y   y xY w)	NTr   level1r   level2value2level3value3r   r!   s    r   test_nested_contextsz!TestContexts.test_nested_contexts7   s    t$(#4(Hh' t,(+#:(H)==== -
  zh%9999 ) :(H!5555 %$ -, )( %$s9   B3B'BB'8B3B$ B''B0	,B33B<zposthog.capture_exceptionc                     t               d        } |dd      }|dk(  sJ |j                          t               i k(  sJ y )Nc                 <    t        d|        t        d|       | |z   S )Nxyr	   )r,   r-   s     r   successful_functionzGTestContexts.test_scoped_decorator_success.<locals>.successful_functionJ   s    QKQKq5L       r      )r   assert_not_calledr   )r   mock_capturer/   results       r   test_scoped_decorator_successz*TestContexts.test_scoped_decorator_successH   sR    		 
	
 %Q* {{ 	&&( zRr0   c                    d }||_         dD ]  \  }}| j                  |      5  t        d| d      |rt        d      fd       fd	}nt        d      fd
       }| j	                  t              5   |        d d d        |j                         t               i k(  sJ |j                          d d d         y # 1 sw Y   DxY w# 1 sw Y   xY w)Nc                 D    t               }|j                  d      dk(  sJ y Nimportant_contextr   r   get	exceptionkwargscurrent_tagss      r   check_context_on_capturezNTestContexts.test_scoped_decorator_exception.<locals>.check_context_on_capture]   s#    #:L##$78GCCCr0   ))syncF)asyncTnamezTest z
 exceptionTcapture_exceptionsc                  (   K   t        dd        wr9   r.   test_exceptions   r   failing_functionzFTestContexts.test_scoped_decorator_exception.<locals>.failing_functionj   s     /9,,s   c                  8    t        j                                 S Nasynciorun)rK   s   r   rP   z9TestContexts.test_scoped_decorator_exception.<locals>.runo   s    &{{+;+=>>r0   c                       t        dd        r9   r.   rI   s   r   rK   zFTestContexts.test_scoped_decorator_exception.<locals>.failing_functiont   s    /9,,r0   )side_effectsubTest
ValueErrorr   assertRaisesassert_called_once_withr   
reset_mock)r   r4   rA   rE   is_asyncrP   rK   rJ   s         @@r   test_scoped_decorator_exceptionz,TestContexts.test_scoped_decorator_exception[   s    	D
 $< @ND(4(!+eD6,D!Et4- 5-?
 t4- 5- +C &&z2E 3 44^D  zR''''')A )( A0 32/ )(s$   AC>C	8C	CCC	c                 >   t        d      }d }||_        t               5  t        dd       	 t        d      5  t        dd       |# 1 sw Y   nxY wn# t         $ r Y nw xY wt	               d   dk(  sJ 	 d d d        n# 1 sw Y   nxY w|j                  |       y )	NContext exceptionc                 D    t               }|j                  d      dk(  sJ y )Ninner_contextinner_valuer;   r=   s      r   rA   zRTestContexts.test_new_context_exception_handling.<locals>.check_context_on_capture   s"    #:L##O4EEEr0   outer_contextouter_valueTrF   r]   r^   )RuntimeErrorrR   r   r	   r   rV   )r   r4   rJ   rA   s       r   #test_new_context_exception_handlingz0TestContexts.test_new_context_exception_handling   s    %&9:	F
 $<  ]/ D97(( :99    :o.-??? ]] 	,,^<s?   BAA		A	AB	A"B!A""BB
c           	      z   g d}|D ]  \  }}}| j                  |||      5  t        j                  }t        j                  }d t        _        |t        _        t	        d      }	 | j                  t              5  i }	|||	d<   t        j                  di |	5  | y # 1 sw Y   nxY w	 d d d        n# 1 sw Y   nxY w|t        _        |t        _        n# |t        _        |t        _        w xY w|r|j                  |       n|j                          |j                          d d d        )# 1 sw Y   3xY w)N))FNF)FTT)TFF)global_settingrG   expect_capturedr[   rG    )
rS   posthogdefault_clientenable_exception_autocapturera   rU   r   rV   r3   rW   )
r   r4   casesrd   explicit_argre   original_default_client%original_enable_exception_autocapturerJ   r?   s
             r   >test_new_context_respects_global_exception_autocapture_settingzKTestContexts.test_new_context_respects_global_exception_autocapture_setting   s2   
 >C9NL/-#/ /  
 +2*@*@'88 6 *.&7E4!-.A!B**<8!#'3;GF#78$00:6:"00+ >C( ;:: 988 .EG*= 8 .EG*= 8 # 88H 224'')?  sN   AD0%C:B4B	B'#B4+	C4B=9C D0C//7D00D:	c                 n   t        dddd      }t               |_        t        d      }	 | j	                  t              5  |j                         5  |# 1 sw Y   nxY w	 d d d        n# 1 sw Y   nxY w|j                          n# |j                          w xY w|j                  j                          y )Nphc_testTF)	sync_modedisabledri   r[   )r   r   capture_exceptionra   rU   r   shutdownr3   )r   clientrJ   s      r   Itest_new_context_defaults_to_custom_client_exception_autocapture_disabledzVTestContexts.test_new_context_defaults_to_custom_client_exception_autocapture_disabled   s    ).	
 $-; %&9:	""<0'')(( *)) 100 OOFOO  224s5   B  A*AA	A*!	B *A3/B Bc                     t        d      5  t               J t        d       t               dk(  sJ 	 d d d        y # 1 sw Y   y xY w)NTr   user123)r   r   r
   r!   s    r   test_identify_contextz"TestContexts.test_identify_context   s@    t$*,444 Y'*,	999 %$$	   '>Ac                     t        d      5  t               J t        d       t               dk(  sJ 	 d d d        y # 1 sw Y   y xY w)NTr   
session456)r   r   r   r!   s    r   test_set_context_sessionz%TestContexts.test_set_context_session   s@    t$)+333  -)+|;;; %$$rz   c                 $   t        d      5  t        d       t        d       t        d      5  t               J t	               J 	 d d d        t               dk(  sJ t	               dk(  sJ 	 d d d        y # 1 sw Y   1xY w# 1 sw Y   y xY w)NTr   rx   r|   r   r
   r   r   r   r!   s    r   &test_context_inheritance_fresh_contextz3TestContexts.test_context_inheritance_fresh_context   s    t$Y'-4(.0888-/777 ) +,	999)+|;;; %$ )(	 %$s"   #BA:
&B:B	?BBc                    t        d      5  t        d       t        d       t        d      5  t               dk(  sJ t	               dk(  sJ t        d       t        d       t               dk(  sJ t	               dk(  sJ 	 d d d        t               dk(  sJ t	               dk(  sJ 	 d d d        y # 1 sw Y   1xY w# 1 sw Y   y xY w)NTr   rx   r|   Fuser789
session999r   r!   s    r   *test_context_inheritance_non_fresh_contextz7TestContexts.test_context_inheritance_non_fresh_context  s    t$Y'-5).0I===-/<??? !+#L1.0I===-/<??? * +,	999)+|;;;# %$ *)	 %$s#   #C AB4&C 4B=	9C  C	c                    t        d      5  t        dd       t        dd       t        d      5  t               d   dk(  sJ t        dd       t        d	d
       t               }|d   dk(  sJ |d   dk(  sJ |d	   d
k(  sJ 	 d d d        t               }|d   dk(  sJ |d   dk(  sJ d	|vsJ 	 d d d        y # 1 sw Y   7xY w# 1 sw Y   y xY w)NTr   
shared_keyparent_valueparent_onlyparentFchild_value
child_onlychildr   )r   r   parent_tagss      r   9test_child_tags_override_parent_tags_in_non_fresh_contextzFTestContexts.test_child_tags_override_parent_tags_in_non_fresh_context  s    t$n-x(5)z-0H<<< L-0L'*zL)]:::M*h666L)W444 *  #*K|,>>>}-999{222/ %$ *)	 %$s#   %CAB<,C<C	CCc                 $   t               d        }t               d        }d|d fd|d fg}|D ]S  \  }}}| j                  |      5   ||      \  }}|dk(  sJ |d	k(  sJ t               J t               J 	 d d d        U y # 1 sw Y   `xY w)
Nc                  V    t        d       t        d       t               t               fS Nuser456
session789r
   r   r   r   rf   r0   r   sync_function_with_contextzWTestContexts.test_scoped_decorator_with_context_ids.<locals>.sync_function_with_context1  s$    Y'-*,.D.FFFr0   c                  ^   K   t        d       t        d       t               t               fS wr   r   rf   r0   r   async_function_with_contextzXTestContexts.test_scoped_decorator_with_context_ids.<locals>.async_function_with_context7  s(     Y'-*,.D.FFFs   +-rB   c                      |        S rM   rf   funcs    r   <lambda>zETestContexts.test_scoped_decorator_with_context_ids.<locals>.<lambda>>  s    dfr0   rC   c                 6    t        j                   |              S rM   rN   r   s    r   r   zETestContexts.test_scoped_decorator_with_context_ids.<locals>.<lambda>?  s    DF@Sr0   rD   r   r   )r   rS   r   r   )	r   r   r   rj   rE   r   rP   distinct_id
session_ids	            r   &test_scoped_decorator_with_context_idsz3TestContexts.test_scoped_decorator_with_context_ids0  s    		G 
	G
 
	G 
	G /1DE13ST

  %OD$4(*-d)'Z"i///!\111 /0888-/777 )(  %((s   2BB	c                 $   t        j                         t        j                         t        j                         t               fd       t               fd       fd}t        j                   |             ddgk(  sJ y )Nc                     K   t        d       j                          j                          d {    t               } j                          | S 7  w)Nuser_1)r
   setwaitr   )r   first_checkedfirst_readysecond_readys    r   firstzTTestContexts.test_scoped_decorator_async_concurrent_context_isolation.<locals>.firstQ  sJ     X&OO##%%%13K &s   /AA!Ac                     K   j                          d {    t        d       j                           j                          d {    t               S 7 A7 w)Nuser_2)r   r
   r   r   )r   r   r   s   r   secondzUTestContexts.test_scoped_decorator_async_concurrent_context_isolation.<locals>.secondZ  sS     ""$$$X&$$&&&*,,	 % 's!   AA2A
AAAc                     K   t        j                  t        j                                         d       d {   S 7 w)Nr1   )timeout)rO   wait_forgather)r   r   s   r   rP   zRTestContexts.test_scoped_decorator_async_concurrent_context_isolation.<locals>.runb  s0      ))'..&(*KUVWWWWs   9AAAr   r   )rO   Eventr   rP   )r   rP   r   r   r   r   r   s     @@@@@r   8test_scoped_decorator_async_concurrent_context_isolationzETestContexts.test_scoped_decorator_async_concurrent_context_isolationL  st    mmo}}		 
	 
	- 
	-	X {{35!h%9999r0   N)__name__
__module____qualname__r   r"   r)   r   r6   rY   rb   rn   rv   ry   r}   r   r   r   r   r   rf   r0   r   r   r      s    %246" &'  ( $ &')* ()*V &'= (=6 &')* ()*V5&:<<<(3488:r0   r   )rO   unittestunittest.mockr   r   rg   posthog.clientr   posthog.contextsr   r   r   r	   r
   r   r   r   TestCaser   rf   r0   r   <module>r      s7      *  !	 	 	R:8$$ R:r0   