
    P1iJ                        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 d dlmZmZ d d	lZd d	lZd d
lmZ d dlmZ  G d de      Z G d de      Z G d de      Zy	)    )annotations)TestCase)get_user_model)reverse)timezone)call_command)CommandError)patch	MagicMockNPerson)GroupRelationc                  :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
PersonRelationsThreadingTestsz>Test the new threading approach for background task execution.c           	        t               }|j                  j                  ddd      | _        | j                  j                  | j                         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   dater   ownerM     Tnamegender
birth_date
created_byr   p1  r!   r"   r#   r$   p2  )r   objectscreate_useruserclientforce_logindatetimer   bazi.modelsr   creater   calculate_bazisaveselfUserr   r   r%   r(   s         K/home/cursorai/projects/iching/api/tests/test_person_relations_threading.pysetUpz#PersonRelationsThreadingTests.setUp   s9   LL,,! - 
	
 			* 	"&^^**D!Q'yy + 

 	

!!#

 ^^""D!Q'yy	 # 
 	
	^^""D!Q'yy	 # 
 	
	    c                f   t        d      }| j                  j                  |      }| j                  |j                  d       | j                  |j                         j                  d      d       | j                  j                          | j                  | j                  j                  d       y)z@Test that the API spawns a background thread for task execution.api:bazi-person-relations   status
processingN	r   r-   getassertEqualstatus_codejsonr,   refresh_from_dbgroup_relations_stater5   urlresps      r7   !test_api_spawns_background_threadz?PersonRelationsThreadingTests.test_api_spawns_background_thread>       12 {{s# 	))3/2LA 			!!#88,Gr9   c                f   t        d      }| j                  j                  |      }| j                  |j                  d       | j                  |j                         j                  d      d       | j                  j                          | j                  | j                  j                  d       y)zITest that the background thread properly executes the management command.r;   r<   r=   r>   Nr?   rF   s      r7   2test_background_thread_executes_management_commandzPPersonRelationsThreadingTests.test_background_thread_executes_management_commandM   rJ   r9   c                f   t        d      }| j                  j                  |      }| j                  |j                  d       | j                  |j                         j                  d      d       | j                  j                          | j                  | j                  j                  d       y)zHTest that user state transitions work properly in the background thread.r;   r<   r=   r>   Nr?   rF   s      r7   %test_user_state_transitions_in_threadzCPersonRelationsThreadingTests.test_user_state_transitions_in_thread_   rJ   r9   c                f   t        d      }| j                  j                  |      }| j                  |j                  d       | j                  |j                         j                  d      d       | j                  j                          | j                  | j                  j                  d       y)z=Test that user state transitions to error on command failure.r;   r<   r=   r>   Nr?   rF   s      r7   $test_user_state_transitions_on_errorzBPersonRelationsThreadingTests.test_user_state_transitions_on_errorq   rJ   r9   c                `   t        d      }t        j                         }| j                  j                  |      }t        j                         }| j	                  ||z
  d       | j                  |j                  d       | j                  |j                         j                  d      d       y)z@Test that the threading approach doesn't block the API response.r;   g?r<   r=   r>   N)r   timer-   r@   
assertLessrA   rB   rC   )r5   rG   
start_timerH   end_times        r7   *test_threading_approach_does_not_block_apizHPersonRelationsThreadingTests.test_threading_approach_does_not_block_api   s    12 YY[
{{s#99; 	:-s3 	))3/2LAr9   c                f   t        d      }| j                  j                  |      }| j                  |j                  d       | j                  |j                         j                  d      d       | j                  j                          | j                  | j                  j                  d       y)z;Test that the dummy process object maintains compatibility.r;   r<   r=   r>   Nr?   rF   s      r7   'test_dummy_process_object_compatibilityzEPersonRelationsThreadingTests.test_dummy_process_object_compatibility   rJ   r9   N)__name__
__module____qualname____doc__r8   rI   rL   rN   rP   rV   rX    r9   r7   r   r      s/    H(THH$H$H$B Hr9   r   c                  @    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)ManagementCommandTimeoutTestsz8Test the new timeout handling in the management 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 r   r   r*   r+   r,   r/   r   r0   r   r1   r   r2   r3   r4   s         r7   r8   z#ManagementCommandTimeoutTests.setUp   s!   LL,,! - 
	 	"&^^**D!Q'yy + 

 	

!!#

 ^^""D!Q'yy	 # 
 	
	^^""D!Q'yy	 # 
 	
	r9   c                *   	 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)z:Test that main thread execution uses signal-based timeout.recalc_bazi_relationsr,   	completed signal only works in main threadN)	r   r,   idrD   rA   rE   	ExceptionassertNotInstr)r5   es     r7   $test_main_thread_uses_signal_timeoutzBManagementCommandTimeoutTests.test_main_thread_uses_signal_timeout   so    	I0tyy||DII%%'TYY<<kJ 	I?QHH	Is   A!A$ $	B-BBc                    ddl } fd} |j                  |      }|j                          |j                  d        j	                  |j                                 |       }|dk7  r j                  d|       y j                  j                           j                   j                  j                  d	d
g       y)zHTest that the management command can handle background thread execution.r   Nc                     	 t        dj                  j                         y# t        $ r} t	        |       cY d } ~ S d } ~ ww xY w)Nrc   rd   successr   r,   rg   rh   rj   )rk   r5   s    r7   run_command_in_threadzeManagementCommandTimeoutTests.test_background_thread_timeout_mechanism.<locals>.run_command_in_thread   s7    4499<<H  1vs   !% 	A
>AAtarget   timeoutro   rf   re   idle)	threadingThreadstartjoinassertFalseis_aliveri   r,   rD   assertInrE   )r5   rx   rq   threadresults   `    r7   (test_background_thread_timeout_mechanismzFManagementCommandTimeoutTests.test_background_thread_timeout_mechanism   s     		 "!!)>?B 	*+ '(Y?H II%%' MM$))99K;PQr9   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   rx   current_threadmain_thread
assertTruer~   rj   _handle_single_user__code__	co_constsr|   )r5   r   cmdis_mains       r7    test_timeout_detection_mechanismz>ManagementCommandTimeoutTests.test_timeout_detection_mechanism   sp    J i **,	0E0E0GGOOG$MM(C(?(?(H(H(R(R$ST W%r9   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.r>   rE   group_relations_started_atupdate_fieldsrc   rd   re   N)
r,   rE   dj_tznowr   r3   r   rg   rD   rA   )r5   s    r7   "test_signal_timeout_in_main_threadz@ManagementCommandTimeoutTests.test_signal_timeout_in_main_thread  s|     +7		'/4yy{		,		&=?[%\] 	,499<<@ 			!!#88+Fr9   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.r>   rE   r   r   r=   Nc                     	 t        dj                  j                         dd<   y # t        $ r} dt	        |        d<   Y d } ~ y d } ~ ww xY w)Nrc   rd   ro   r=   error: rp   rk   r   r5   s    r7   run_commandzZManagementCommandTimeoutTests.test_event_timeout_in_background_thread.<locals>.run_command/  sJ    64499<<H#,x  6%,SVH#5x 6s   &* 	AA		Arr   rt   ru   ro   re   rf   )r,   rE   r   r   r   r3   rx   ry   rz   r{   r|   r}   rD   rA   ri   )r5   r   r   r   s   `  @r7   'test_event_timeout_in_background_threadzEManagementCommandTimeoutTests.test_event_timeout_in_background_thread#  s    
 +7		'/4yy{		,		&=?[%\] D!	6 !!5B 	*+ (y(II%%'TYY<<kJ ?AQRr9   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   rc   rd   re   rf   Nr=   c                     	 t        dj                  j                  d       dd<   y # t        $ r} dt	        |        d<   Y d } ~ y d } ~ ww xY wNrc   T)r,   forcero   r=   r   rp   r   s    r7   r   zYManagementCommandTimeoutTests.test_command_handles_both_thread_types.<locals>.run_commandV  sL    64499<<tT#,x  6%,SVH#5x 6   '+ 	AA

Arw   rE   r   rr   rt   ru   ro   )r   r   rx   r   r   r   r,   rg   rD   rA   rE   rh   ri   rj   r3   ry   rz   r{   r|   r}   )r5   r   rk   r   r   r   s   `    @r7   &test_command_handles_both_thread_typeszDManagementCommandTimeoutTests.test_command_handles_both_thread_typesE  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   rc   rd   re   rf   Nr=   c                     	 t        dj                  j                  d       dd<   y # t        $ r} dt	        |        d<   Y d } ~ y d } ~ ww xY wr   rp   r   s    r7   r   zSManagementCommandTimeoutTests.test_timeout_mechanism_selection.<locals>.run_command  sM    : !8tyy||SWX'0F8$  :)0Q'9F8$:r   rw   rE   r   rr   rt   ru   ro   )r   r   rx   r   r   r   r   r,   rg   rD   rA   rE   rh   ri   rj   r|   r3   ry   rz   r{   r}   )r5   r   r   r   rk   r   r   r   s   `      @r7    test_timeout_mechanism_selectionz>ManagementCommandTimeoutTests.test_timeout_mechanism_selectionp  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)rY   rZ   r[   r\   r8   rl   r   r   r   r   r   r   r]   r9   r7   r_   r_      s7    B'RIR@&&G" SD)SV1Wr9   r_   c                  (    e Zd ZdZd Zd Zd Zd Zy)SignalHandlerTestsz6Test the signal handler for spawning background tasks.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    ra   )r5   r6   r   r   s       r7   r8   zSignalHandlerTests.setUp  s    LL,,! - 
	 	"&^^**D!Q'yy + 

 	

!!#

r9   c           	     @   ddl m} ddlm} |j                  j                  dd |ddd      | j                        }|j                          |j                          | j                  |j                         | j                  |j                  | j                         y	)
zBTest that creating a new person triggers background task spawning.r   r   r   r%   r   r&   r   r'   N)r0   r   r/   r   r*   r1   r,   r2   r3   assertIsNotNonerg   rA   r$   r5   r   r   r%   s       r7   -test_person_creation_triggers_background_taskz@SignalHandlerTests.test_person_creation_triggers_background_task  s    &! ^^""D!Q'yy	 # 
 	
	 	RUU#		2r9   c           	        ddl m} ddlm} |j                  j                  dd |ddd      | j                        }|j                          |j                           |d	dd      |_	        |j                          |j                          |j                          | j                  |j                   |d	dd             y
)zBTest that updating a person's day pillar triggers background task.r   r   r   r%   r   r&   r   r'   r)   N)r0   r   r/   r   r*   r1   r,   r2   r3   r#   rD   rA   r   s       r7   +test_person_update_triggers_background_taskz>SignalHandlerTests.test_person_update_triggers_background_task  s    &! ^^""D!Q'yy	 # 
 	
	 T1a(

	 	T1a(89r9   c           	     d   ddl m} ddlm} |j                  j                  dd |ddd      | j                        }|j                          |j                          |j                          | j                  |j                  j                  |j                  	      j                                y
)z5Test that deleting a person triggers background task.r   r   r   r%   r   r&   r   r'   )rg   N)r0   r   r/   r   r*   r1   r,   r2   r3   deleter|   filterrg   existsr   s       r7   -test_person_deletion_triggers_background_taskz@SignalHandlerTests.test_person_deletion_triggers_background_task  s    &! ^^""D!Q'yy	 # 
 	
	 			 	.."%%.8??ABr9   N)rY   rZ   r[   r\   r8   r   r   r   r]   r9   r7   r   r     s    @,3(:2Cr9   r   )
__future__r   django.testr   django.contrib.authr   django.urlsr   django.utilsr   r   django.core.managementr   django.core.management.baser	   unittest.mockr
   r   rx   rR   r0   r   bazi.models_groupr   r   r_   r   r]   r9   r7   <module>r      s[    "   .  * / 4 *    +OHH OHj{WH {W|ZC ZCr9   