
    P1iAB                    n    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mZ  G d d	e      Zy)
    )annotations)TestCase)get_user_model)reverse)datetimeN)Personc                  R    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y)BaziUpdateAPITestszETest PATCH and PUT operations for bazi records with owner=True/False.c           	        t               }|j                  j                  ddd      | _        | j                  j                  | j                         t        j                  j                  ddt        ddd	      t        d
d      | j                  dd      | _
        | j                  j                          | j                  j                          t        j                  j                  ddt        ddd      t        d	d      | j                  dd      | _        | j                  j                          | j                  j                          y )N13900000001testpass123ztest@example.comphonepasswordemailzOwner PersonMi        	      TzOriginal owner note)namegender
birth_date
birth_time
created_byownernoteszNon-Owner PersonFi        -   FzOriginal non-owner note)r   objectscreate_useruserclientforce_loginr	   creater   r   r   calculate_bazisave	non_owner)selfUsers     @/home/cursorai/projects/iching/api/tests/test_bazi_update_api.pysetUpzBaziUpdateAPITests.setUp   s   LL,,-?Q - 
	 			* ^^**D!R(Ar{yy' + 

 	

!!#

  ..#D!R(B|yy+ / 
 	%%'    c                   t        dd| j                  j                  i      }| j                  j                  }| j                  j                  }d}d|i}| j
                  j                  |t        j                  |      d      }| j                  |j                  d       |j                         }| j                  |d   |       | j                  |d	   | j                  j                         | j                  |d
   d       | j                  j                          | j                  t        | j                  j                        |       | j                  || j                  j                         | j                  || j                  j                         | j                  |d          | j                  d|d          | j                  d|d          | j                  d|d          | j                  d|d          y)z7Test PATCH request updates owner=True record correctly.api:bazi-detailpkkwargsz
1987-06-10r   application/jsondatacontent_type   idr   TresultyearmonthdayhourN)r   r   r;   r   
updated_atr&   patchjsondumpsassertEqualstatus_coderefresh_from_dbstrassertNotEqualassertIsNotNoneassertInr,   urloriginal_birth_dateoriginal_updated_atnew_birth_date
patch_dataresponseresponse_datas           r.   test_patch_owner_true_recordz/BaziUpdateAPITests.test_patch_owner_true_record0   s   'tzz}}0EF #jj33"jj33 &.

 ;;$$J'+ % 
 	--s3  	|4nEt,djjmm<w/6 	

""$TZZ223^D/1F1FG/1F1FG 	]845fmH56g}X67e]845fmH56r0   c                   t        dd| j                  j                  i      }| j                  j                  }| j                  j                  }d}d|i}| j
                  j                  |t        j                  |      d      }| j                  |j                  d       |j                         }| j                  |d   |       | j                  |d	   | j                  j                         | j                  |d
   d       | j                  j                          | j                  t        | j                  j                        |       | j                  || j                  j                         | j                  || j                  j                         | j                  |d          | j                  d|d          | j                  d|d          | j                  d|d          | j                  d|d          y)z8Test PATCH request updates owner=False record correctly.r2   r3   r4   z
1992-12-05r   r6   r7   r:   r;   r   Fr<   r=   r>   r?   r@   N)r   r+   r;   r   rA   r&   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   s           r.   test_patch_owner_false_recordz0BaziUpdateAPITests.test_patch_owner_false_recordZ   s   't~~7H7H0IJ #nn77"nn77 &.

 ;;$$J'+ % 
 	--s3  	|4nEt,dnn.?.?@w/7 	&&(T^^667H/1J1JK/1J1JK 	]845fmH56g}X67e]845fmH56r0   c           	        t        dd| j                  j                  i      }| j                  j                  }| j                  j                  }d}d}|d|ddd	d	d
d}| j
                  j                  |t        j                  |      d      }| j                  |j                  d       |j                         }| j                  |d   |       | j                  |d   |       | j                  |d   | j                  j                         | j                  |d   d       | j                  j                          | j                  t        | j                  j                        |       | j                  | j                  j                  |       | j                  || j                  j                         | j                  || j                  j                         | j                  |d          | j!                  d|d          | j!                  d|d          | j!                  d|d          | j!                  d|d          y	)z5Test PUT request updates owner=True record correctly.r2   r3   r4   z
1986-11-28zUpdated Owner Namer   z14:20:00r   NzUpdated owner noter   r   r   r   	twin_type
father_dob
mother_dobr   r6   r7   r:   r   r   r;   r   Tr<   r=   r>   r?   r@   )r   r   r;   r   rA   r&   putrC   rD   rE   rF   rG   rH   r   rI   rJ   rK   	r,   rM   rN   rO   rP   new_nameput_datarR   rS   s	            r.   test_put_owner_true_recordz-BaziUpdateAPITests.test_put_owner_true_record   s   'tzz}}0EF #jj33"jj33 &'($)	
 ;;??H%+ # 
 	--s3  	|4nEv.9t,djjmm<w/6 	

""$TZZ223^D(3/1F1FG/1F1FG 	]845fmH56g}X67e]845fmH56r0   c           	        t        dd| j                  j                  i      }| j                  j                  }| j                  j                  }d}d}|d|ddd	d	d
d}| j
                  j                  |t        j                  |      d      }| j                  |j                  d       |j                         }| j                  |d   |       | j                  |d   |       | j                  |d   | j                  j                         | j                  |d   d       | j                  j                          | j                  t        | j                  j                        |       | j                  | j                  j                  |       | j                  || j                  j                         | j                  || j                  j                         | j                  |d          | j!                  d|d          | j!                  d|d          | j!                  d|d          | j!                  d|d          y	)z6Test PUT request updates owner=False record correctly.r2   r3   r4   z
1991-04-18zUpdated Non-Owner Namer   z08:15:00r   NzUpdated non-owner noterX   r6   r7   r:   r   r   r;   r   Fr<   r=   r>   r?   r@   )r   r+   r;   r   rA   r&   r\   rC   rD   rE   rF   rG   rH   r   rI   rJ   rK   r]   s	            r.   test_put_owner_false_recordz.BaziUpdateAPITests.test_put_owner_false_record   s   't~~7H7H0IJ #nn77"nn77 &+($-	
 ;;??H%+ # 
 	--s3  	|4nEv.9t,dnn.?.?@w/7 	&&(T^^667H,,h7/1J1JK/1J1JK 	]845fmH56g}X67e]845fmH56r0   c                   t        dd| j                  j                  i      }ddddd}| j                  j	                  |t        j                  |      d	
      }| j                  |j                  d       |j                         }| j                  |d   |d          | j                  |d   |d          | j                  |d   |d          | j                  |d   |d          | j                  |d   d       | j                  j                          | j                  t        | j                  j                        |d          | j                  t        | j                  j                        |d          | j                  | j                  j                  |d          | j                  | j                  j                  |d          y)z=Test PATCH request with multiple fields on owner=True record.r2   r3   r4   z
1988-09-14z16:30:00zMultiple fields updatedr   )r   r   r   r   r6   r7   r:   r   r   r   r   r   TN)r   r   r;   r&   rB   rC   rD   rE   rF   rG   rH   r   r   r   r   r,   rM   rQ   rR   rS   s        r.   %test_patch_multiple_fields_owner_truez8BaziUpdateAPITests.test_patch_multiple_fields_owner_true   s   'tzz}}0EF '$.	

 ;;$$J'+ % 
 	--s3  	|4j6NO|4j6NOw/G1DEx0*X2FGw/6 	

""$TZZ223Z5MNTZZ223Z5MN)):g+>?**Jx,@Ar0   c                   t        dd| j                  j                  i      }ddddd}| j                  j	                  |t        j                  |      d	
      }| j                  |j                  d       |j                         }| j                  |d   |d          | j                  |d   |d          | j                  |d   |d          | j                  |d   |d          | j                  |d   d       | j                  j                          | j                  t        | j                  j                        |d          | j                  t        | j                  j                        |d          | j                  | j                  j                  |d          | j                  | j                  j                  |d          y)z>Test PATCH request with multiple fields on owner=False record.r2   r3   r4   z
1993-02-28z11:45:00z!Non-owner multiple fields updatedzPatched Non-Owner)r   r   r   r   r6   r7   r:   r   r   r   r   r   FN)r   r+   r;   r&   rB   rC   rD   rE   rF   rG   rH   r   r   r   r   rd   s        r.   &test_patch_multiple_fields_owner_falsez9BaziUpdateAPITests.test_patch_multiple_fields_owner_false  s   't~~7H7H0IJ '$8'	

 ;;$$J'+ % 
 	--s3  	|4j6NO|4j6NOw/G1DEv.
60BCw/7 	&&(T^^667L9QRT^^667L9QR--z'/BC,,j.@Ar0   c                   t        dd| j                  j                  i      }| j                  j                  d   d   }d}d|i}| j                  j                  |t        j                  |      d	      }| j                  |j                  d
       |j                         }|d   d   d   }| j                  |       | j                  ||       dD ]  }|d   |   }	| j                  |	       | j                  d|	       | j                  d|	       | j                  d|	       | j                  d|	       | j                  |	d          | j                  |	d          | j                  |	d          | j                  |	d           y)zCTest that bazi calculations are properly updated after API changes.r2   r3   r4   r=   god
2000-01-01r   r6   r7   r:   r<   god_idx)r=   r>   r?   r@   earth	earth_idxN)r   r   r;   bazi_resultr&   rB   rC   rD   rE   rF   rJ   rI   rK   )
r,   rM   original_year_godrP   rQ   rR   rS   new_year_godpillarpillar_datas
             r.   "test_bazi_calculation_after_updatez5BaziUpdateAPITests.test_bazi_calculation_after_update4  s   'tzz}}0EF !JJ226:5A &.

 ;;$$J'+ % 
 	--s3  %X.v6yA\* 	-|< 7F'1&9K  -MM%-MM';/MM)[1MM+{3  U!34  W!56  Y!78  [!9: 7r0   c                   t        dd| j                  j                  i      }ddi}| j                  j	                  |t        j                  |      d      }| j                  |j                  d       |j                         }| j                  |d   |d          | j                  j                          | j                  t        | j                  j                        |d          y	)
zUTest that relation calculation is skipped during API updates to prevent interference.r2   r3   r4   r   z
1989-05-25r6   r7   r:   N)r   r   r;   r&   rB   rC   rD   rE   rF   rG   rH   r   rd   s        r.   3test_relation_calculation_skipped_during_api_updatezFBaziUpdateAPITests.test_relation_calculation_skipped_during_api_update_  s    'tzz}}0EF ,

 ;;$$J'+ % 
 	--s3  	|4j6NO 	

""$TZZ223Z5MNr0   c           	        t               }|j                  j                  ddd      }t        j                  j	                  ddt        ddd      |d	
      }t        dd|j                  i      }ddi}| j                  j                  |t        j                  |      d      }| j                  |j                  d       y)z8Test that unauthorized users cannot update bazi records.13900000002r   zother@example.comr   zOther Personr   i     T)r   r   r   r   r   r2   r3   r4   r   rj   r6   r7   i  N)r   r#   r$   r	   r(   r   r   r;   r&   rB   rC   rD   rE   rF   )r,   r-   
other_userother_personrM   rQ   rR   s          r.   test_unauthorized_update_failsz1BaziUpdateAPITests.test_unauthorized_update_fails|  s     \\---?R . 


 ~~,,D!Q'! - 
 '|0GH,

 ;;$$J'+ % 
 	--s3r0   c                   t        dd| j                  j                  i      }ddi}| j                  j	                  |t        j                  |      d      }| j                  |j                  d       d	d
i}| j                  j	                  |t        j                  |      d      }| j                  j                          | j                  | j                  j                  d
       y)z:Test that invalid data is properly validated and rejected.r2   r3   r4   r   zinvalid-dater6   r7   i  r   XN)r   r   r;   r&   rB   rC   rD   rE   rF   rG   rI   r   )r,   rM   rQ   rR   s       r.   test_invalid_data_validationz/BaziUpdateAPITests.test_invalid_data_validation  s    'tzz}}0EF .

 ;;$$J'+ % 
 	--s3 c

 ;;$$J'+ % 
 	

""$DJJ--s3r0   N)__name__
__module____qualname____doc__r/   rT   rV   r`   rb   re   rg   rs   ru   r{   r~    r0   r.   r   r      sH    OB(7T(7T27h27h"BH"BH);VO:4@4r0   r   )
__future__r   django.testr   django.contrib.authr   django.urlsr   datetimer   r   rC   bazi.modelsr	   r   r   r0   r.   <module>r      s(    "   .    n4 n4r0   