
    P1i:                        d Z ddlZddlZddlmZ ddlmZ ddlmZm	Z	 ddl
Z
ej                  j                  dd       ej                  j                  dd	       dd
lmZ ddlmZ ddlmZmZ ddlmZ  e       Z G d de      Z G d de      Z G d de      Z G d de      Zedk(  r' ej8                          ddlZ ej<                          yy)z|
Unit tests for TemporaryUserRegistrationForm.
Tests the specific bug fix for profile field saving, especially twin_type=0.
    N)TestCase)get_user_model)datetimeDJANGO_SETTINGS_MODULEziching.settings
DJANGO_ENVdevelopment)UserProfile)TemporaryUserRegistrationForm)get_first_bazi_recordextract_user_data_from_bazi)Personc                       e Zd ZdZd Zd Zy)%TemporaryUserRegistrationFormTestCasez,Test cases for TemporaryUserRegistrationFormc                 6   t        t        j                               dd | _        t        j
                  j                  d| j                   d| j                   dd      | _        t        j
                  j                  | j                        \  | _
        }y)	zSet up test dataN   
temp_test_temp_z	@test.comT)phoneemailis_temporary_user)user)struuiduuid4	unique_idUserobjectscreate_user	temp_userr
   get_or_createprofile)selfcreateds     [/home/cursorai/projects/iching/iching/tests/../../tests/test_temp_user_registration_form.pysetUpz+TemporaryUserRegistrationFormTestCase.setUp   s     TZZ\*2A. 11t~~./$..)3" 2 
 !, 3 3 A At~~ A Vg    c                 t    t         j                  j                  | j                        j	                          y)zClean up after each test)phone__containsN)r   r   filterr   delete)r#   s    r%   tearDownz.TemporaryUserRegistrationFormTestCase.tearDown,   s&     	DNN;BBDr'   N)__name__
__module____qualname____doc__r&   r,    r'   r%   r   r      s    6WEr'   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)!TempUserRegistrationFormSaveTestsz5Test the form's save method with focus on the bug fixc           
         d| j                    d| j                    ddddddd	d
d	}t        | j                  |      }| j                  |j	                         d|j
                          |j                  | j                        }|j                          | j                  |j                  j                  d       | j                  |j                  j                  t        ddd             | j                  |j                  j                  t        dd             | j                  |j                  j                  t        ddd             | j                  |j                  j                   t        ddd             y)uI   Test that twin_type=0 (正常) is saved correctly - this was the main bug1234567890_test_@example.comTestr   z
1990-01-01z12:000
1960-01-01
1965-01-01)	r   r   
first_name	last_name
birth_date
birth_time	twin_type
father_dob
mother_dobr    dataForm should be valid. Errors: r               N)r   r   r    
assertTrueis_validerrorssaverefresh_from_dbassertEqualr"   r@   r>   r   r?   r   rA   rB   r#   	test_dataformconverted_users       r%   #test_twin_type_zero_saved_correctlyzETempUserRegistrationFormSaveTests.test_twin_type_zero_saved_correctly5   sA    #4>>"23T^^,L9 &!&&

	 -t~~IV+I$++)WX 4>>2&&( 	//991=//::Dq!<LM//::DQKH//::Dq!<LM//::Dq!<LMr'   c                    d| j                    d| j                    dddddd}t        | j                  |	      }| j                  |j	                         d
|j
                          |j                  | j                        }|j                          | j                  |j                  j                  d       | j                  |j                  j                  t        ddd             y)u7   Test that twin_type=1 (双胞胎大) is saved correctly1234567891_test1_r7   r8   r   1r:   )r   r   r<   r=   r@   rA   rC   rE   rG   rI   N)r   r   r    rK   rL   rM   rN   rO   rP   r"   r@   rA   r   rQ   s       r%   "test_twin_type_one_saved_correctlyzDTempUserRegistrationFormSaveTests.test_twin_type_one_saved_correctlyQ        #4>>"23dnn-\: &
	 -t~~IV+I$++)WX 4>>2&&( 	//991=//::Dq!<LMr'   c                    d| j                    d| j                    dddddd}t        | j                  |	      }| j                  |j	                         d
|j
                          |j                  | j                        }|j                          | j                  |j                  j                  d       | j                  |j                  j                  t        ddd             y)u7   Test that twin_type=2 (双胞胎小) is saved correctly1234567892_test2_r7   r8   r   2r;   )r   r   r<   r=   r@   rB   rC   rE      rJ   rG   N)r   r   r    rK   rL   rM   rN   rO   rP   r"   r@   rB   r   rQ   s       r%   "test_twin_type_two_saved_correctlyzDTempUserRegistrationFormSaveTests.test_twin_type_two_saved_correctlyg   r[   r'   c                    d| j                    d| j                    ddddddd	d
dd
}t        | j                  |      }| j                  |j	                         d|j
                          |j                  | j                        }|j                          | j                  |j                  d       | j                  |j                  d       | j                  |j                  d       | j                  |j                         |j                  }| j                  |j                  t!        ddd             | j                  |j"                  t%        dd             | j                  |j&                  d       | j                  |j(                  t!        ddd             | j                  |j*                  t!        ddd             y)z0Test that all profile fields are saved correctly1234567893_test3_r7   JohnDoeMz
1985-12-25z15:30r9   z
1955-06-15z
1958-09-22
r   r   r<   r=   genderr>   r?   r@   rA   rB   rC   rE   i  rH            r   i     i  	      N)r   r   r    rK   rL   rM   rN   rO   rP   r<   r=   ri   assertFalser   r"   r>   r   r?   r   r@   rA   rB   )r#   rR   rS   rT   r"   s        r%   'test_all_profile_fields_saved_correctlyzITempUserRegistrationFormSaveTests.test_all_profile_fields_saved_correctly}   s    #4>>"23dnn-\: &!&&
	 -t~~IV+I$++)WX 4>>2&&( 	22F;1159..499: !((++T$B-?@++T"b\:**A.++T$2->?++T$2->?r'   c                    t        ddd      | j                  _        d| j                  _        t        ddd      | j                  _        | j                  j                          d| j                   d| j                   dddd	d
dd}t        | j                  |      }| j                  |j                         d|j                          |j                  | j                        }|j                          | j                  |j                  j                  t        ddd             | j                  |j                  j                  d       | j                  |j                  j                  t        ddd             y)z8Test that partial updates preserve existing profile datarF   rG   rI   1234567894_test4_r7   r8   r   z
1995-05-05rY   r:   )r   r   r<   r=   r>   r@   rA   rC   rE   i     N)r   r"   r>   r@   rA   rN   r   r   r    rK   rL   rM   rO   rP   rQ   s       r%   +test_partial_update_preserves_existing_datazMTempUserRegistrationFormSaveTests.test_partial_update_preserves_existing_data   s<    #'tQ"2!""&tQ"2 #4>>"23dnn-\: &&
	 -t~~IV+I$++)WX 4>>2&&( 	//::Dq!<LM//991=//::Dq!<LMr'   N)	r-   r.   r/   r0   rU   rZ   ra   rq   rv   r1   r'   r%   r3   r3   2   s$    ?N8N,N,"@HNr'   r3   c                       e Zd ZdZd Zd Zy)'TempUserRegistrationFormPrefillingTestsz'Test form pre-filling from BaZi recordsc                    t         j                  j                  ddt        ddd      t	        dd      dt        ddd      t        ddd      | j
                  d	d	

      }t        | j
                        }| j                  |j                  d   j                  d       | j                  |j                  d   j                  d       | j                  |j                  d   j                  d       | j                  |j                  d   j                  t        ddd             | j                  |j                  d   j                  t	        dd             | j                  |j                  d   j                  d       | j                  |j                  d   j                  t        ddd             | j                  |j                  d   j                  t        ddd             y)z7Test that form is pre-filled with data from BaZi recordu   张三rg   rF   rG   rH   r   rI   rJ   T
nameri   r>   r?   r@   rA   rB   
created_bycreated_by_temp_userownerr    r<   u   三r=   u   张ri   r>   r?   r@   rA   rB   N)
r   r   creater   r   r    r   rP   fieldsinitial)r#   bazi_recordrS   s      r%   $test_form_prefilled_from_bazi_recordzLTempUserRegistrationFormPrefillingTests.test_form_prefilled_from_bazi_record   s    nn++D!Q'B{D!Q'D!Q'~~!% , 
 -t~~F 	\2::EB[1995AX.66<\2::Dq!<LM\2::DQKH[1991=\2::Dq!<LM\2::Dq!<LMr'   c                 z   t         j                  j                  ddt        ddd      t	        dd      dt        d	d
d      t        ddd      | j
                  dd
      }t        | j
                        }| j                  |j                  d   j                  d       | j                  |j                  d   j                  d       | j                  |j                  d   j                  d       | j                  |j                  d   j                  t        ddd             | j                  |j                  d   j                  t	        dd             | j                  |j                  d   j                  d       | j                  |j                  d   j                  t        d	d
d             | j                  |j                  d   j                  t        ddd             d| j                   d| j                   ddddddd d!d"d#
}t        | j
                  |$      }| j                  |j                         d%|j                          |j                  | j
                        }|j                          | j                  |j                   d       | j                  |j"                  d       | j                  |j$                  d       | j                  |j&                  j(                  t        ddd             | j                  |j&                  j*                  t	        dd             | j                  |j&                  j,                  d       | j                  |j&                  j.                  t        d	d
d             | j                  |j&                  j0                  t        ddd             y&)'zITest the user's reported scenario: LiuYao first, then BaZi, then registeru   李四Fi  ru   rk   r   rl   r   rJ      
   i     Trz   r   r<   u   四r=   u   李ri   r>   r?   r@   rA   rB   1234567895_test5_r7   z
1992-05-15z08:30r9   z
1965-03-10z
1970-08-20rh   rC   rE   N)r   r   r   r   r   r    r   rP   r   r   r   rK   rL   rM   rN   rO   r<   r=   ri   r"   r>   r?   r@   rA   rB   )r#   r   rS   rR   form_with_datarT   s         r%   1test_user_scenario_liuyao_then_bazi_then_registerzYTempUserRegistrationFormPrefillingTests.test_user_scenario_liuyao_then_bazi_then_register   s    nn++D!R(Ar{D!R(D!R(~~!% , 
 -t~~F 	\2::EB[1995AX.66<\2::Dq"<MN\2::DBKH[1991=\2::Dq"<MN\2::Dq"<MN #4>>"23dnn-\:&!&&
	 7V_`//15STbTiTiSj3kl (,,T^^<&&( 	22E:1159..4//::Dq"<MN//::DBKH//991=//::Dq"<MN//::Dq"<MNr'   N)r-   r.   r/   r0   r   r   r1   r'   r%   rx   rx      s    1N::Or'   rx   c                   "    e Zd ZdZd Zd Zd Zy)'TempUserRegistrationFormValidationTestszTest form validationc                    d| j                    d| j                    ddd}t        | j                  |      }| j                  |j	                                | j                  d|j                         d|d	<   t        | j                  |      }| j                  |j	                                | j                  d
|j                         y)z7Test validation for twin_type requiring specific fields1234567896_test6_r7   rY   )r   r   r@   rC   rA   r_   r@   rB   Nr   r   r    rp   rL   assertInrM   r#   rR   rS   s      r%   .test_twin_type_validation_with_required_fieldszVTempUserRegistrationFormValidationTests.test_twin_type_validation_with_required_fields!  s     #4>>"23dnn-\:
	 -t~~IV)lDKK0 "%	+ -t~~IV)lDKK0r'   c                     d| j                    d| j                    dddd}t        | j                  |      }| j                  |j	                                | j                  d|j                         y	)
zTest password validation1234567897_test7_r7   test123different123)r   r   password	password2rC   r   Nr   r   s      r%   test_password_validationz@TempUserRegistrationFormValidationTests.test_password_validation7  si     #4>>"23dnn-\:!'	
	 -t~~IV)k4;;/r'   c                 N   t         j                  j                  d| j                   d| j                   d      }d| j                   d| j                   dd}t	        | j
                  |      }| j                  |j                                | j                  d|j                         d| j                   |d<   d| j                   d|d<   t	        | j
                  |      }| j                  |j                                | j                  d|j                         y	)
z-Test validation for duplicate phone and email	existing_r7   )r   r   test8_rC   r   unique_r   N)
r   r   r   r   r   r    rp   rL   r   rM   )r#   
other_userrR   rS   s       r%   %test_duplicate_phone_email_validationzMTempUserRegistrationFormValidationTests.test_duplicate_phone_email_validationD  s    \\--dnn-.dnn-\: . 

 ! 01dnn-\:
	
 -t~~IV)gt{{+  't~~&67	'((8E	',t~~IV)gt{{+r'   N)r-   r.   r/   r0   r   r   r   r1   r'   r%   r   r     s    1,0,r'   r   __main__)r0   osdjangodjango.testr   django.contrib.authr   datetimer   r   r   environ
setdefaultaccounts.modelsr
   accounts.formsr   accounts.utilsr   r   bazi.modelsr   r   r   r3   rx   r   r-   setupunittestmainr1   r'   r%   <module>r      s    
    .   

  .0A B 

  lM 2 ( 8 M EH E0LN(M LN^ZO.S ZOz=,.S =,@ zFLLNHMMO r'   