
    P1i|6                        d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlZd dlZd dlZ G d	 d
e      Z G d de      Zy)    )annotations)TestCase)get_user_model)call_command)CommandError)timezone)patch	MagicMockNc                  @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)RecalcBaziRelationsTimeoutTestszCTest the new timeout handling in the recalc_bazi_relations command.c           	        t               }|j                  j                  ddd      | _        ddlm} ddlm} |j                  j                  dd	 |d
dd      | j                  d      | _	        | j                  j                          | j                  j                          |j                  j                  dd	 |ddd      | j                        }|j                          |j                          |j                  j                  dd	 |ddd      | j                        }|j                          |j                          y )N13900000000xu@example.comphonepasswordemailr   datePersonownerM     Tnamegender
birth_date
created_byr   p1i  )r   r   r    r!   p2i  r   objectscreate_useruserdatetimer   bazi.modelsr   creater   calculate_bazisave)selfUserr   r   r"   r#   s         O/home/cursorai/projects/iching/bazi/tests/test_recalc_bazi_relations_timeout.pysetUpz%RecalcBaziRelationsTimeoutTests.setUp   s!   LL,,! - 
	 	"&^^**D!Q'yy + 

 	

!!#

 ^^""D!Q'yy	 # 
 	
	^^""D!Q'yy	 # 
 	
	    c                   | j                  t        j                         t        j                         u        	 t	        d| j
                  j                         | j
                  j                          | j                  | j
                  j                  d       y# t        $ r%}| j                  dt        |             Y d}~yd}~ww xY w)z4Test that signal-based timeout works in main thread.recalc_bazi_relationsr'   	completed signal only works in main threadN)
assertTrue	threadingcurrent_threadmain_threadr   r'   idrefresh_from_dbassertEqualgroup_relations_state	ExceptionassertNotInstr)r-   es     r/   %test_main_thread_signal_timeout_workszERecalcBaziRelationsTimeoutTests.test_main_thread_signal_timeout_works>   s     		002i6K6K6MMN	I0tyy||DII%%'TYY<<kJ 	I?QHH	Is   A!B 	C	$CC	c                    ddd fd}t        j                  |      }|j                          |j                  d        j	                  |j                                d   r j                  dd          y j                  j                           j                   j                  j                  d	       y)
z:Test that event-based timeout works in background threads.N)statuserrorc                     	 t        dj                  j                         dd<   y # t        $ r} t	        |       d<   Y d } ~ y d } ~ ww xY w)Nr3   r4   successrE   rF   r   r'   r;   r?   rA   rB   resultr-   s    r/   run_commandz_RecalcBaziRelationsTimeoutTests.test_background_thread_event_timeout_works.<locals>.run_commandR   sB    )4499<<H#,x  )"%a&w)s   &* 	AAAtarget   timeoutrF   r6   r5   )r8   ThreadstartjoinassertFalseis_aliver@   r'   r<   r=   r>   r-   rL   threadrK   s   `  @r/   *test_background_thread_event_timeout_workszJRecalcBaziRelationsTimeoutTests.test_background_thread_event_timeout_worksM   s     !40	) !!5B 	*+ '??Q II%%'TYY<<kJr1   c                *   ddl m}  |       }t        j                         t        j                         u }|rK| j                  |       | j                  dt        |j                  j                  j                               y| j                  |       y)z6Test that the command properly detects thread context.r   CommandsignalN).bazi.management.commands.recalc_bazi_relationsr\   r8   r9   r:   r7   assertInrA   _handle_single_user__code__	co_constsrU   )r-   r\   cmdis_mains       r/    test_timeout_detection_mechanismz@RecalcBaziRelationsTimeoutTests.test_timeout_detection_mechanismj   sp    J i **,	0E0E0GGOOG$MM(C(?(?(H(H(R(R$ST W%r1   c                j   d| j                   _        t        j                         | j                   _        | j                   j                  ddg       t        d| j                   j                         | j                   j                          | j                  | j                   j                  d       y)	z7Test that signal timeout actually works in main thread.
processingr>   group_relations_started_atupdate_fieldsr3   r4   r5   N)
r'   r>   dj_tznowrh   r,   r   r;   r<   r=   )r-   s    r/   "test_signal_timeout_in_main_threadzBRecalcBaziRelationsTimeoutTests.test_signal_timeout_in_main_thread}   s|     +7		'/4yy{		,		&=?[%\] 	,499<<@ 			!!#88+Fr1   c                *    d j                   _        t        j                          j                   _         j                   j                  ddg       ddi fd}t        j                  |      }|j                          |j                  d	
        j                  |j                                d   dk(  rA j                   j                           j                   j                   j                  d       y j                  dd          y)z3Test that event timeout works in background thread.rg   r>   rh   ri   rE   Nc                     	 t        dj                  j                         dd<   y # t        $ r} dt	        |        d<   Y d } ~ y d } ~ ww xY w)Nr3   r4   rH   rE   error: rI   rJ   s    r/   rL   z\RecalcBaziRelationsTimeoutTests.test_event_timeout_in_background_thread.<locals>.run_command   sJ    64499<<H#,x  6%,SVH#5x 6s   &* 	AA		ArM   rO   rP   rH   r5   r6   )r'   r>   rk   rl   rh   r,   r8   rR   rS   rT   rU   rV   r<   r=   r@   rW   s   `  @r/   'test_event_timeout_in_background_threadzGRecalcBaziRelationsTimeoutTests.test_event_timeout_in_background_thread   s    
 +7		'/4yy{		,		&=?[%\] D!	6 !!5B 	*+ (y(II%%'TYY<<kJ ?AQRr1   c                f    ddl m} t        j                         t        j                         u rb	 t        d j                  j                          j                  j                           j                   j                  j                  d       ddi fd	}d
 j                  _
         j                  j                  dg       t        j                  |      }|j!                          |j#                  d        j%                  |j'                                d   dk(  rA j                  j                           j                   j                  j                  d       y j                  dd          y# t        $ r&} j                  dt        |             Y d}~d}~ww xY w)zBTest that the command can handle both main and background threads.r   r[   r3   r4   r5   r6   NrE   c                     	 t        dj                  j                  d       dd<   y # t        $ r} dt	        |        d<   Y d } ~ y d } ~ ww xY wNr3   Tr'   forcerH   rE   rp   rI   rJ   s    r/   rL   z[RecalcBaziRelationsTimeoutTests.test_command_handles_both_thread_types.<locals>.run_command   L    64499<<tT#,x  6%,SVH#5x 6   '+ 	AA

Aidler>   ri   rM   rO   rP   rH   )r^   r\   r8   r9   r:   r   r'   r;   r<   r=   r>   r?   r@   rA   r,   rR   rS   rT   rU   rV   )r-   r\   rB   rL   rX   rK   s   `    @r/   &test_command_handles_both_thread_typeszFRecalcBaziRelationsTimeoutTests.test_command_handles_both_thread_types   sU   J ##%)>)>)@@M4499<<H		))+  !@!@+N
 D!	6 +1		'		&=%>?!!5B 	*+ (y(II%%'TYY<<kJ ?AQR=  M  !CSVLLMs   A!F 	F0
F++F0c                    ddl m}  |       }t        j                         t        j                         u }|rt j                  |       	 t        d j                  j                          j                  j                           j                   j                  j                  d       y j                  |       ddi fd	}d
 j                  _         j                  j!                  dg       t        j"                  |      }|j%                          |j'                  d        j                  |j)                                d   dk(  rA j                  j                           j                   j                  j                  d       y j                  dd          y# t        $ r%} j                  dt        |             Y d}~yd}~ww xY w)zLTest that the correct timeout mechanism is selected based on thread context.r   r[   r3   r4   r5   r6   NrE   c                     	 t        dj                  j                  d       dd<   y # t        $ r} dt	        |        d<   Y d } ~ y d } ~ ww xY wrt   rI   rJ   s    r/   rL   zURecalcBaziRelationsTimeoutTests.test_timeout_mechanism_selection.<locals>.run_command   sM    : !8tyy||SWX'0F8$  :)0Q'9F8$:rx   ry   r>   ri   rM   rO   rP   rH   )r^   r\   r8   r9   r:   r7   r   r'   r;   r<   r=   r>   r?   r@   rA   rU   r,   rR   rS   rT   rV   )r-   r\   rc   rd   rB   rL   rX   rK   s   `      @r/    test_timeout_mechanism_selectionz@RecalcBaziRelationsTimeoutTests.test_timeout_mechanism_selection   sy   J i **,	0E0E0GGOOG$M4499<<H		))+  !@!@+N
 W%%F: /5DII+IINN*A)BNC%%[9FLLNKKK# V__./h9,		))+  !@!@+N  !CVHEUV=  M  !CSVLLMs   A!F- -	G6GGN)__name__
__module____qualname____doc__r0   rC   rY   re   rm   rq   rz   r}    r1   r/   r   r      s6    M'RIK:&&G" SD)SV1Wr1   r   c                  "    e Zd ZdZd Zd Zd Zy)#RecalcBaziRelationsIntegrationTestszNIntegration tests for the recalc_bazi_relations command with timeout handling.c           	     L   t               }|j                  j                  ddd      | _        ddlm} ddlm} |j                  j                  dd	 |d
dd      | j                  d      | _	        | j                  j                          | j                  j                          y )Nr   r   r   r   r   r   r   r   r   r   r   Tr   r$   )r-   r.   r   r   s       r/   r0   z)RecalcBaziRelationsIntegrationTests.setUp  s    LL,,! - 
	 	"&^^**D!Q'yy + 

 	

!!#

r1   c                
    	 t        d j                  j                          j                  j                           j	                   j                  j
                  d       ddi fd}d j                  _         j                  j                  d	g
       t        j                  |      }|j                          |j                  d        j                  |j                                d   dk(  rA j                  j                           j	                   j                  j
                  d       y j                  dd          y# t        $ r&} j                  dt        |             Y d}~d}~ww xY w)zMIntegration test that the command works in both main and background contexts.r3   r4   r5   r6   NrE   c                     	 t        dj                  j                  d       dd<   y # t        $ r} dt	        |        d<   Y d } ~ y d } ~ ww xY wrt   rI   rJ   s    r/   rL   z\RecalcBaziRelationsIntegrationTests.test_command_works_in_both_contexts.<locals>.run_command5  rw   rx   ry   r>   ri   rM   rO   rP   rH   r   r'   r;   r<   r=   r>   r?   r@   rA   r,   r8   rR   rS   rT   rU   rV   r-   rB   rL   rX   rK   s   `   @r/   #test_command_works_in_both_contextszGRecalcBaziRelationsIntegrationTests.test_command_works_in_both_contexts(  s2   	I0tyy||DII%%'TYY<<kJ
 D!	6 +1		'		&=%>?!!5B 	*+(y(II%%'TYY<<kJ?AQR9  	I?QHH	Is   A!E 	FE==Fc                    	 t        d j                  j                  d        j                  j                           j	                   j                  j
                  d       ddi fd}d	 j                  _         j                  j                  d
g       t        j                  |      }|j                          |j                  d        j                  |j                                d   dk(  rA j                  j                           j	                   j                  j
                  d       y j                  dd          y# t        $ r&} j                  dt        |             Y d}~d}~ww xY w)z9Test that the --force flag works in both thread contexts.r3   Tru   r5   r6   NrE   c                     	 t        dj                  j                  d       dd<   y # t        $ r} dt	        |        d<   Y d } ~ y d } ~ ww xY wrt   rI   rJ   s    r/   rL   z_RecalcBaziRelationsIntegrationTests.test_force_flag_works_in_both_contexts.<locals>.run_commandZ  rw   rx   ry   r>   ri   rM   rO   rP   rH   r   r   s   `   @r/   &test_force_flag_works_in_both_contextszJRecalcBaziRelationsIntegrationTests.test_force_flag_works_in_both_contextsM  s4   	I0tyy||4PII%%'TYY<<kJ
 D!	6 +1		'		&=%>?!!5B 	*+(y(II%%'TYY<<kJ?AQR9  	I?QHH	Is   A"E 	FE>>FN)r~   r   r   r   r0   r   r   r   r1   r/   r   r     s    X,#SJ#Sr1   r   )
__future__r   django.testr   django.contrib.authr   django.core.managementr   django.core.management.baser   django.utilsr   rk   unittest.mockr	   r
   r8   timer]   r   r   r   r1   r/   <module>r      sD    "   . / 4 * *   zWh zWzaS( aSr1   