
    P1i=                    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 d dlmZ  G d	 d
e      Zy)    )annotations)TestCase)get_user_model)reverse)timezone)Person)GroupRelation)patchc                  Z    e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zy)PersonRelationsAPITestsc           	        t               }|j                  j                  ddd      | _        | j                  j                  | j                         ddlm}m} t        j                  j                  dd |d	d
d       |dd      | j                  d      | _        | j                  j                          | j                  j                          | j                  j                  j                  di       }t!        d|j                  d       d|j                  d              |j                  d      r|j                  d      s.t!        d       t!        d| j                  j                          y y )N13900000000xzu@example.com)phonepasswordemailr   datetimeownerMi           T)namegender
birth_date
birth_time
created_byr   dayzSetup - Owner day pillar: God=god_idx, Earth=	earth_idxz4Warning: Owner BaZi not calculated properly in setUpzOwner bazi_result: )r   objectscreate_useruserclientforce_logindatetimer   r   r   creater   calculate_bazisavebazi_resultgetprint)selfUserr   r   	owner_days        E/home/cursorai/projects/iching/api/tests/test_person_relations_api.pysetUpzPersonRelationsAPITests.setUp   s,   LL,,=3Ve,f			*'^^**D!R(B{yy	 + 

 	

!!#

 JJ**..ub9	.y}}Y/G.HQZQ^Q^_jQkPlmn}}Y'y}}[/IHJ'

(>(>'?@A 0J    c                   t        d      }| j                  j                  |      }| j                  |j                  d       | j                  |j                         j                  d      d       ddlm}  |d| j                  j                         | j                  j                  |      }| j                  |j                  d       | j                  |j                         j                  d      d	       y )
Napi:bazi-person-relations   status
processingr   call_commandrecalc_bazi_relations)r&   ready)
r   r'   r.   assertEqualstatus_codejsondjango.core.managementr<   r&   id)r0   urlrespr<   resp2s        r3   "test_idle_to_processing_then_readyz:PersonRelationsAPITests.test_idle_to_processing_then_ready%   s    12{{s#))3/2LA7,499<<@$**C0))(3W=r5   c                   t               }|j                  j                  | j                  j                        }d|_        t        j                         t        j                  d      z
  |_	        |j                  ddg       t        d      5 }t        d	d
      5  t        d      }| j                  j                  |      }| j                  |j                  d       | j                  |j!                         j                  d      d       |j#                          |j$                  }| j                  |j&                  d   d       | j)                  d
|j&                  d          | j)                  d| j                  j                   |j&                  d          | j)                  d|j&                  d          d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)NrC   r:   X  secondsgroup_relations_stategroup_relations_started_atupdate_fields$api.views_person_relations.send_mail!django.conf.settings.TECH_CONTACTadmin@example.comr7   r8   r9   errorsubjectzBaZi group relations timeoutrecipient_listzUser messagezexceeded timeout)r   r$   r.   r&   rC   rM   dj_tznow	timedeltarN   r,   r
   r   r'   r?   r@   rA   assert_called_once	call_argskwargsassertInr0   r1   umock_send_mailrD   rE   r\   s          r3   !test_timeout_sets_error_and_emailz9PersonRelationsAPITests.test_timeout_sets_error_and_email2   s}   LL		-".',yy{U__S5Q'Q$	57STU 9:n:<OP9:{{s+  !1!137  !:GD 113*44	  !1!1)!<>\]193C3CDT3UVdiill^4i6F6Fy6QR0)2B2B92MN Q ;:PP ;:s%   GD!G?GG	GG%c                   t               }|j                  j                  | j                  j                        }d|_        t        j                         t        j                  d      z
  |_	        |j                  ddg       t        d      5 }t        d	d
      5  t        dd
      5  t        d      }| j                  j                  |      }| j                  |j                  d       | j                  |j!                         j                  d      d       |j#                          d
d
d
       d
d
d
       d
d
d
       y
# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y
xY w)z?Test that no email is sent when TECH_CONTACT is not configured.rI   r:   rJ   rK   rM   rN   rO   rQ   rR   N"django.conf.settings.CONTACT_EMAILr7   r8   r9   rT   )r   r$   r.   r&   rC   rM   rX   rY   rZ   rN   r,   r
   r   r'   r?   r@   rA   assert_not_calledr0   r1   r`   ra   rD   rE   s         r3   0test_timeout_email_not_sent_when_no_tech_contactzHPersonRelationsAPITests.test_timeout_email_not_sent_when_no_tech_contactJ   s   LL		-".',yy{U__S5Q'Q$	57STU 9:n:DA?F!"=>C;;??3/D$$T%5%5s;$$TYY[__X%>H #446 G B ;:FF BA ;:s=   EE+BE-E5EEEE	EE'c                *   t               }|j                  j                  | j                  j                        }d|_        t        j                         t        j                  d      z
  |_	        |j                  ddg       t        d      5 }t        d	d
      5  t        dd      5  t        d      }| j                  j                  |      }| j                  |j                  d       | j                  |j!                         j                  d      d       |j#                          |j$                  }| j'                  d|j(                  d          d
d
d
       d
d
d
       d
d
d
       y
# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y
xY w)zITest that email falls back to CONTACT_EMAIL when TECH_CONTACT is not set.rI   r:   rJ   rK   rM   rN   rO   rQ   rR   Nrd   zcontact@example.comr7   r8   r9   rT   rV   )r   r$   r.   r&   rC   rM   rX   rY   rZ   rN   r,   r
   r   r'   r?   r@   rA   r[   r\   r^   r]   r_   s          r3   ,test_timeout_email_fallback_to_contact_emailzDPersonRelationsAPITests.test_timeout_email_fallback_to_contact_email^   s:   LL		-".',yy{U__S5Q'Q$	57STU 9:n:DA?AVW!"=>C;;??3/D$$T%5%5s;$$TYY[__X%>H #557 . 8 8IMM"79I9IJZ9[\ X B ;:WW BA ;:s=   F	E=+B-E1E= F	1E:6E==F	F		Fc                   t               }|j                  j                  | j                  j                        }d|_        t        j                         t        j                  d      z
  |_	        |j                  ddg       t        d      5 }t        d	      |_        t        d
d      5  t        d      }| j                  j                  |      }| j!                  |j"                  d       | j!                  |j%                         j                  d      d       |j'                          ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)z?Test that email sending failure doesn't break the API response.rI   r:   rJ   rK   rM   rN   rO   rQ   zEmail service downrR   rS   r7   r8   r9   rT   N)r   r$   r.   r&   rC   rM   rX   rY   rZ   rN   r,   r
   	Exceptionside_effectr   r'   r?   r@   rA   r[   rf   s         r3   #test_timeout_email_graceful_failurez;PersonRelationsAPITests.test_timeout_email_graceful_failuret   s	   LL		-".',yy{U__S5Q'Q$	57STU 9:n)23G)HN&:<OP9:{{s+   !1!137  !:GD 113 Q ;:PP ;:s%   E.BE0EE
	EEc           	        ddl m} t        j                  j	                  dd |ddd      | j
                        }|j                          |j                          t        j                  j	                  dd |d	dd      | j
                        }|j                          |j                          t        j                  j	                  | j
                  ||d
       t        j                  j                  | j
                        j                          | j                  t        j                  j                  | j
                        j                         d       y )Nr   r   p1r     r   r   r   r   r   p2  sanhe
owner_userperson1person2relation_type)r   )rw   )r)   r   r   r$   r*   r&   r+   r,   r	   filterdeleter?   count)r0   r   rp   rs   s       r3   &test_delete_all_persons_removes_groupsz>PersonRelationsAPITests.test_delete_all_persons_removes_groups   s   !^^""ST$PQSTEUbfbkbk"l
RWWY^^""ST$PQSTEUbfbkbk"l
RWWY$$		2rah$i3::<..555KQQSUVWr5   c                   t               }|j                  j                  | j                  j                        }d|_        t        j                         |_        |j                  ddg       t        d      }| j                  j                  |      }| j                  j                  |      }| j                  |j                         j                  d      d       | j                  |j                         j                  d      d       y )NrI   r:   rM   rN   rO   r7   r9   )r   r$   r.   r&   rC   rM   rX   rY   rN   r,   r   r'   r?   rA   )r0   r1   r`   rD   r1r2s         r3   test_concurrency_guardz.PersonRelationsAPITests.test_concurrency_guard   s    LL		-".',yy{$	57STU12[[__S![[__S!x0,?x0,?r5   c           	     j   ddl m}m} | j                  j                  j                  di       }t        d|j                  d       d|j                  d              t        j                  j                  dd	 |d
dd       |dd      | j                        }|j                          |j                          |j                  j                  di       }t        d|j                  d       d|j                  d              |j                  d      }|j                  d      }t        d| d| d       t        d       | j                  |dd       | j                  |dd       t        dd|j                  i      }| j                   j                  |      }	| j                  |	j"                  d       |	j%                         }
| j'                  d|
       | j'                  d|
       t        d|
d           t        d|
d           t)        d |
d   D              }|szt+        |
d         D ]i  \  }}t        d | d!|        |j                  d"      d#k(  s,t        d$|j                  d%              d&|v rt        d'|d&           _t        d(       k | j-                  |d)       y )*Nr   r   r    zOwner day pillar: God=godr"   earthpr   i  r      r   r   r   r   r   r   zPerson day pillar: God=zOwner god: u    (甲), Person god: u    (己)u=   Expected: Owner god=0 (甲), Person god=5 (己) for 甲己合u   Owner should have 甲 (god=0)   u   Person should have 己 (god=5)zapi:bazi-detailpk)r]   r8   relation_goodrelation_badzRelation good: zRelation bad: c              3     K   | ]=  }|j                  d       dk(  xr# |j                  d      dk(  xr d|v xr |d   dv  ? yw)trc   5e)r   r   r         N)r.   ).0r   s     r3   	<genexpr>zrPersonRelationsAPITests.test_pairwise_detail_includes_relation_fields_and_5e_only_for_tianganhe.<locals>.<genexpr>   sP      
* EE#J#Z!%%*/ZdaiZAdG{DZZ*s   AAz	Relation z: r   r   z  - Type 'r' with code r   r   z  - Has 5e field: z  - Missing 5e fieldu,   Should find 天干合 relation with 5e field)r)   r   r   r   r-   r.   r/   r   r$   r*   r&   r+   r,   r?   r   rC   r'   r@   rA   r^   any	enumerate
assertTrue)r0   r   r   r2   r   
person_day	owner_god
person_godrD   rE   datahas_tianganheirels                 r3   Gtest_pairwise_detail_includes_relation_fields_and_5e_only_for_tianganhez_PersonRelationsAPITests.test_pairwise_detail_includes_relation_fields_and_5e_only_for_tianganhe   s   
 	( JJ**..ub9	&y}}U';&<HY]]SZE[D\]^ NN!!SD!R(B{yy	 " 
 	
	 ]]&&ub1
'
u(='>hz~~V]G^F_`a MM%(	^^E*
I;&::,fMNMO 	A'FGQ(HI 'qtt={{s#))3/yy{ot,nd+ 	_ 5678tN3456  
/*
 

 #D$9:3	!Bse,-773<3&3CGGCL>BCs{ 23t9+>? 46 ; 	'UVr5   c           	     n   ddl m} t        j                  j	                  dd |ddd      | j
                        }|j                          |j                          t        j                  j	                  dd |d	dd      | j
                        }|j                          |j                          t        j                  j	                  | j
                  ||d
      }|j                          | j                  t        j                  j                  |j                        j                         d       y )Nr   ro   p1xr   rq   r   rr   p2xrt   ru   rv   rI   )r)   r   r   r$   r*   r&   r+   r,   r	   r|   r?   r{   rC   r}   )r0   r   rp   rs   grs        r3   1test_delete_one_person_removes_specific_group_rowzIPersonRelationsAPITests.test_delete_one_person_removes_specific_group_row   s    !^^""cd4QRTUFVcgclcl"m
RWWY^^""cd4QRTUFVcgclcl"m
RWWY""))TYYTVfm)n
		..555?EEGKr5   c                   t               }|j                  j                  | j                  j                        }d|_        t        j                         t        j                  d      z
  |_	        |j                  ddg       t        d      5 }d	d
lm}  |d       |j                          | j                  |j
                  d       d d d        y # 1 sw Y   y xY w)NrI   r:   rJ   rK   rM   rN   rO   rQ   r   r;   scan_group_relations_timeoutsrT   )r   r$   r.   r&   rC   rM   rX   rY   rZ   rN   r,   r
   rB   r<   refresh_from_dbr?   )r0   r1   r`   smr<   s        r3   )test_scan_command_marks_timeout_and_errorzAPersonRelationsAPITests.test_scan_command_marks_timeout_and_error   s    LL		-".',yy{U__S5Q'Q$	57STU9:b;89Q44g>	 ;::s   ;CCc           	        ddl m}m} t        j                  j                  dd |ddd       |dd      | j                        }|j                          |j                          d	| j                  _	        | j                  j                  d
g       | j                  j                  d   j                         } |dd      | j                  _        | j                  j                          | j                  j                          | j                  j                          | j                  j                  d   }|j                  d      |j                  d      k(  re|j                  d      |j                  d      k(  rA| j                  j                          | j!                  | j                  j                  d	       yyy)zLTest that owner DOB change without day pillar change doesn't trigger recalc.r   r   test_personr     r   
   r   	completedrM   rO   r       r   r   N)r)   r   r   r   r$   r*   r&   r+   r,   rM   r   r-   copyr   r   r.   r?   )r0   r   r   r   original_daynew_days         r3   4test_owner_dob_change_no_day_pillar_change_no_recalczLPersonRelationsAPITests.test_owner_dob_change_no_day_pillar_change_no_recalc  s]   'NN!!stD!Q7GB{tyy " 
 	
	 +6		'		&=%>? zz--e499; !%R



 	

""$

!!#**((/ U#w{{5'99W%W)==II%%'TYY<<kJ > :r5   c           	        ddl m}m} t        j                  j                  dd |ddd       |dd      | j                        }|j                          |j                          d	| j                  _	        | j                  j                  d
g       | j                  j                  d   j                         } |ddd      | j                  _        | j                  j                          | j                  j                          | j                  j                          | j                  j                  d   }|j                  d      |j                  d      k7  xs# |j                  d      |j                  d      k7  }|rC| j                  j                          | j!                  | j                  j                  dd	g       yy)zFTest that owner DOB change with day pillar change triggers processing.r   r   r   r   r   r   r   r   r   rM   rO   r    i        r   r   r:   N)r)   r   r   r   r$   r*   r&   r+   r,   rM   r   r-   r   r   r   r.   r^   )r0   r   r   r   r   r   day_changeds          r3   @test_owner_dob_change_with_day_pillar_change_triggers_processingzXPersonRelationsAPITests.test_owner_dob_change_with_day_pillar_change_triggers_processing$  sn   'NN!!stD!Q7GB{tyy " 
 	
	 +6		'		&=%>? zz--e499; !%T1b 1



 	

""$

!!#**((/ $''.'++e2DD H"&&w/7;;w3GG 	II%%' MM$))99L+;VW	 r5   N)__name__
__module____qualname__r4   rG   rb   rg   ri   rm   r~   r   r   r   r   r   r    r5   r3   r   r      sO    B.>O07(],4,
X@DWL	L? KD"Xr5   r   N)
__future__r   django.testr   django.contrib.authr   django.urlsr   django.utilsr   rX   bazi.modelsr   bazi.models_groupr	   unittest.mockr
   r   r   r5   r3   <module>r      s-    "   .  *  + yXh yXr5   