
    P1i?                         d dl mZ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mZ d dlmZ d d	lmZmZmZmZ d d
lZ e       Z G d de      Zy
)    )TestCaseClient)reverse)get_user_model)Person)UserProfile)patch	MagicMock)status)timezone)datetimedatetime	timedeltaNc                   ,   e Zd ZdZd Zd Zd Z ed      d        Z ed      d        Z	d Z
 ed      d	        Z ed      d
        Z ed      d        Z ed      d        Z ed      d        Zd Zd Zd Zd Z ed      d        Zd Zd Zy)NumberAnalysisTestsz,Test suite for the number_analysis endpoint.c                 z   t         j                  j                  ddd      | _        t         j                  j                  ddd      | _        t         j                  j                  dddd	
      | _        d| j                  j                  _        | j                  j                  j                          d	| j                  j                  _        | j                  j                  j                          t        j                  j                  ddt        ddd      t        dd      | j                  dt        ddd      t        ddd            | _        t        j                  j                  ddt        ddd      t        dd      | j                  d      | _        t        j                  j                  ddt        ddd      t        dd      | j
                  d      | _        t        j                  j                  d dt        d!d"d"      t        d#d      d$d      | _        t#               | _        d%d&d'd(d)d'd*d+d,d-d.d/| _        | j&                  | j                  _        d0| j                  _        t-        j.                         | j                  _        | j                  j                          d1| _        y$)2zSet up test data.
1234567890zregular@example.comtestpassword123)phoneemailpassword
2345678901zregen@example.com
3456789012zstaff@example.comT)r   r   r   is_staffFzRegular User PersonMi        r   i  i  )namegender
birth_date
birth_time
created_by	twin_type
father_dob
mother_dobzRegen User PersonFi        )r   r    r!   r"   r#   r$   zStaff User PersonNi        zPublic Personi        Nu   个人数字意义u1   您的个人数字是4，代表成功和稳定。)titlecontentu   性格优势uO   作为数字4的人，您具有出色的组织能力和严谨的逻辑思维。)personal_number_meaningcharacter_strengthstest_provider
test_modelzTest promptzTest thinking process)number_analysisprovidermodelpromptthink	completedz/api/number/{}/analysis/)Userobjectscreate_userregular_user
regen_user
staff_userprofilecan_regenerate_aisaver   creater   r   person_regularperson_regenperson_staffperson_publicr   clientmock_analysis_datanumber_ai_analysisnumber_analysis_statusr   nownumber_analysis_timestampurl_template)selfs    D/home/cursorai/projects/iching/api/tests/test_number_analysis_api.pysetUpzNumberAnalysisTests.setUp   s    !LL44'& 5 
 ,,22%& 3 
 ,,22%&	 3 
 7<!!3!!&&(481$$& %nn33&D!Q'B{((D!Q'D!Q' 4 	
 #NN11$D!Q'B{ 2 
 #NN11$D!Q'B{ 2 
 $^^22 D!Q'B{ 3 
 h 2R,
 ,p(	  (!#,#
$ 261H1H.5@28@5  " 7    c                    | j                   j                  | j                  j                        }| j                  j                  dd       | j                  j                  |      }| j                  |j                  t        j                         | j                  |j                  d   d       | j                  |j                  d   | j                         y)z/Test GET request to retrieve existing analysis.r   r   r   r   r   r:   analysisN)rO   formatrE   idrI   logingetassertEqualstatus_coder   HTTP_200_OKdatarJ   rP   urlresponses      rQ   test_get_existing_analysisz.NumberAnalysisTests.test_get_existing_analysisu   s    &&t':':'='=>7HI;;??3'--v/A/ABx0+>z2D4K4KLrS   c                 d   | j                   j                  | j                  j                        }| j                  j                  dd       | j                  j                  |      }| j                  |j                  t        j                         | j                  |j                  d   d       y)z-Test GET request for person without analysis.r   r   rU   r   	not_foundN)rO   rW   rF   rX   rI   rY   rZ   r[   r\   r   r]   r^   r_   s      rQ   test_get_nonexistent_analysisz1NumberAnalysisTests.test_get_nonexistent_analysis   s    &&t'8'8';';<7HI;;??3'--v/A/ABx0+>rS   api.views.analyze_numberc                    | j                   j                  | j                  j                        }| j                  j                  dd       | j                  |_        | j                  j                  |      }| j                  |j                  t        j                         | j                  |j                  d   d       | j                  |j                  d   | j                         |j                          y)z-Test POST request to generate a new analysis.r   r   rU   r   r:   rV   N)rO   rW   rF   rX   rI   rY   rJ   return_valuepostr[   r\   r   r]   r^   assert_called_oncerP   mock_analyzer`   ra   s       rQ   test_post_generate_analysisz/NumberAnalysisTests.test_post_generate_analysis   s     &&t'8'8';';<7HI %)$;$;!;;##C(--v/A/ABx0+>z2D4K4KL'')rS   c                 $   | j                   j                  | j                  j                        }| j                  j                  dd       | j                  |_        | j                  j                  |dddd      }| j                  |j                  t        j                         |j                          |j                  \  }}| j                  |d	   | j                         | j                  |d
   d       | j                  |d   d       y)z5Test POST request with provider and model parameters.r   r   rU   custom_providercustom_model)r6   r7   application/jsonr^   content_typer   r   r(   N)rO   rW   rF   rX   rI   rY   rJ   rh   ri   r[   r\   r   r]   rj   	call_args)rP   rl   r`   ra   argskwargss         rQ   $test_post_with_provider_model_paramsz8NumberAnalysisTests.test_post_with_provider_model_params   s     &&t'8'8';';<7HI %)$;$;! ;;##/.I+ $ 
 	--v/A/AB'')#--fa$"3"34a.1a"34rS   c                 <   | j                   j                  d      }| j                  j                  dd       | j                  j	                  |      }| j                  |j                  t        j                         | j                  |j                  d   d       y)z'Test request for a non-existent person.i'  r   r   rU   errorzPerson not foundN)
rO   rW   rI   rY   rZ   r[   r\   r   HTTP_404_NOT_FOUNDr^   r_   s      rQ   test_nonexistent_personz+NumberAnalysisTests.test_nonexistent_person   sx    &&t,7HI;;??3'--v/H/HIw/1CDrS   c                    | j                   j                  | j                  j                        }| j                  j                  dd       t        d      |_        | j                  j                  |      }| j                  |j                  t        j                         | j                  d|j                         | j                  d|j                         y)z-Test handling of analysis generation failure.r   r   rU   zAnalysis generation failedry   detailN)rO   rW   rF   rX   rI   rY   	Exceptionside_effectri   r[   r\   r   HTTP_500_INTERNAL_SERVER_ERRORassertInr^   rk   s       rQ   test_analysis_failurez)NumberAnalysisTests.test_analysis_failure   s     &&t'8'8';';<7HI $--I#J ;;##C(--v/T/TUgx}}-h.rS   c                 p   | j                   j                  | j                  j                        }| j                  |_        | j                  j                  |ddid      }| j                  |j                  t        j                         | j                  d|j                         |j                          y)z1Test regeneration attempt without authentication.
regenerateTrq   rr   ry   N)rO   rW   rE   rX   rJ   rh   rI   ri   r[   r\   r   HTTP_403_FORBIDDENr   r^   assert_not_calledrk   s       rQ   &test_regenerate_without_authenticationz:NumberAnalysisTests.test_regenerate_without_authentication   s     &&t':':'='=> %)$;$;!;;##%+ $ 
 	--v/H/HIgx}}-&&(rS   c                    | j                   j                  | j                  j                        }| j                  j                  dd       | j                  |_        | j                  j                  |ddid      }| j                  |j                  t        j                         | j                  d|j                         |j                          y	)
zBTest regeneration attempt by user without regeneration permission.r   r   rU   r   Trq   rr   ry   N)rO   rW   rE   rX   rI   rY   rJ   rh   ri   r[   r\   r   r   r   r^   r   rk   s       rQ   "test_regenerate_without_permissionz6NumberAnalysisTests.test_regenerate_without_permission   s     &&t':':'='=>7HI %)$;$;!;;##%+ $ 
 	--v/H/HIgx}}-&&(rS   c                 r   | j                   j                  | j                  j                        }| j                  j                  dd       | j                  |_        | j                  j                  |ddid      }| j                  |j                  t        j                         |j                          y)	z*Test regeneration by user with permission.r   r   rU   r   Trq   rr   N)rO   rW   rF   rX   rI   rY   rJ   rh   ri   r[   r\   r   r]   rj   rk   s       rQ   test_regenerate_with_permissionz3NumberAnalysisTests.test_regenerate_with_permission        &&t'8'8';';<7HI %)$;$;!;;##%+ $ 
 	--v/A/AB'')rS   c                 r   | j                   j                  | j                  j                        }| j                  j                  dd       | j                  |_        | j                  j                  |ddid      }| j                  |j                  t        j                         |j                          y)	zCTest regeneration by staff user (should override permission check).r   r   rU   r   Trq   rr   N)rO   rW   rG   rX   rI   rY   rJ   rh   ri   r[   r\   r   r]   rj   rk   s       rQ   test_regenerate_by_staffz,NumberAnalysisTests.test_regenerate_by_staff  r   rS   c                 ^   | j                   j                  | j                  j                        }| j                  j                  dd       | j                  j                  |      }| j                  |j                  t        j                         | j                  d|j                         y)z5Test user attempting to access another user's person.r   r   rU   ry   N)rO   rW   rG   rX   rI   rY   rZ   r[   r\   r   r   r   r^   r_   s      rQ   &test_user_accessing_other_users_personz:NumberAnalysisTests.test_user_accessing_other_users_person  s|    &&t'8'8';';<7HI;;??3'--v/H/HIgx}}-rS   c                     | j                   j                  | j                  j                        }| j                  j                  |      }| j                  |j                  t        j                         y)z)Test anonymous access to a public person.N)
rO   rW   rH   rX   rI   rZ   r[   r\   r   r]   r_   s      rQ   &test_anonymous_access_to_public_personz:NumberAnalysisTests.test_anonymous_access_to_public_person"  sR    &&t'9'9'<'<=;;??3'--v/A/ABrS   c                    | j                   j                  | j                  j                        }| j                  j                  dd       t        d      5 }| j                  |_        | j                  j                  |ddid      }| j                  |j                  t        j                         |j                          d	d	d	       y	# 1 sw Y   y	xY w)
z.Test handling of boolean regenerate parameter.r   r   rU   rf   r   Trq   rr   N)rO   rW   rF   rX   rI   rY   r	   rJ   rh   ri   r[   r\   r   r]   rj   rP   r`   rl   ra   s       rQ   test_boolean_regenerate_paramz1NumberAnalysisTests.test_boolean_regenerate_param,  s    &&t'8'8';';<7HI-.,(,(?(?L% {{''"D)/ ( H X1163E3EF++- /..s   A,CCc                 .   | j                   j                  | j                  j                        }| j                  j                  dd       t        d      5 }| j                  |_        | j                  j                  |ddid      }| j                  |j                  t        j                         |j                          |j                          | j                  j                  |dd	id      }| j                  |j                  t        j                         |j                          |j                          | j                  j                  |dd
id      }| j                  |j                  t        j                         |j                          |j                          | j                  j                  |ddid      }| j                  |j                  t        j                         |j                          ddd       y# 1 sw Y   yxY w)zBTest handling of different string values for regenerate parameter.r   r   rU   rf   r   truerq   rr   yes1tN)rO   rW   rF   rX   rI   rY   r	   rJ   rh   ri   r[   r\   r   r]   rj   
reset_mockr   s       rQ   %test_various_regenerate_string_valuesz9NumberAnalysisTests.test_various_regenerate_string_values>  s   &&t'8'8';';<7HI-.,(,(?(?L% {{''"F+/ ( H X1163E3EF++-##% {{''"E*/ ( H X1163E3EF++-##% {{''"C(/ ( H X1163E3EF++-##% {{''"C(/ ( H X1163E3EF++-Y /..s   F*HHc                 r   | j                   j                  | j                  j                        }| j                  j                  dd       i |_        | j                  j                  |      }| j                  |j                  t        j                         | j                  |j                  d   i        y)z(Test handling of empty analysis results.r   r   rU   rV   N)rO   rW   rF   rX   rI   rY   rh   ri   r[   r\   r   r]   r^   rk   s       rQ   test_empty_results_handlingz/NumberAnalysisTests.test_empty_results_handlingq  s     &&t'8'8';';<7HI %'!;;##C(--v/A/ABz2B7rS   c                 l   | j                   j                  | j                  j                        }| j                  j                  dd       | j                  j                  |      }| j                  |j                  t        j                         | j                  j                  dd       | j                  j                  |      }| j                  |j                  t        j                         | j                  j                  dd       | j                  j                  |      }| j                  |j                  t        j                         | j                  j                          | j                  j                  |      }| j                  |j                  t        j                         y)z3Test access to a person without a created_by field.r   r   rU   r   r   N)rO   rW   rH   rX   rI   rY   rZ   r[   r\   r   r]   logoutr_   s      rQ   test_person_without_created_byz2NumberAnalysisTests.test_person_without_created_by  s8   &&t'9'9'<'<= 	7HI;;??3'--v/A/AB7HI;;??3'--v/A/AB7HI;;??3'--v/A/AB 	;;??3'--v/A/ABrS   c                    | j                   j                  | j                  j                        }| j                  j                  dd       | j                  j                  |      }| j                  |j                  t        j                         | j                   j                  | j                  j                        }| j                  j                  |      }| j                  |j                  t        j                         y)z&Test that staff can access any person.r   r   rU   N)rO   rW   rE   rX   rI   rY   rZ   r[   r\   r   r]   rF   r_   s      rQ   test_staff_access_to_any_personz3NumberAnalysisTests.test_staff_access_to_any_person  s     &&t':':'='=>7HI;;??3'--v/A/AB &&t'8'8';';<;;??3'--v/A/ABrS   N)__name__
__module____qualname____doc__rR   rb   re   r	   rm   rw   r{   r   r   r   r   r   r   r   r   r   r   r   r    rS   rQ   r   r      s   6`7H	M? %&* '* %&5 '52E %&/ '/  %&) ')" %&) ')$ %&* '*" %&* '*".C.$1.f %&8 '8C,CrS   r   )django.testr   r   django.urlsr   django.contrib.authr   bazi.modelsr   accounts.modelsr   unittest.mockr	   r
   rest_frameworkr   django.utilsr   r   r   r   r   jsonr;   r   r   rS   rQ   <module>r      s>    (  .  ' * ! ! 4 4 SC( SCrS   