
    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)    )TestCase)reverse)status)	APIClient)User)Person)liuyao)create_temporary_userc                   .    e Zd ZdZd Zd Zd Zd Zd Zy)SingleRecordCreationTestCasez6Test that single API calls create only one record eachc                     t               | _        dddddd| _        ddd	d	d	d
d	dd| _        dddddd	d	d	d
d	dd| _        y)zSet up test dataAPI Test PersonMz
1990-01-01z12:00:00zAPI test notes)namegender
birth_date
birth_timenotesz2024-01-01T12:00:00ZTest question for API00001)qdatequestiony1y2y3y4y5y6i     z12:00z Test question for calculator API)yearmonthdaytimer   r   r   r   r   r   r    N)r   
api_clientbazi_api_dataliuyao_dataliuyao_calc_data)selfs    [/home/cursorai/projects/iching/iching/tests/../../tests/test_temp_user_duplicate_records.pysetUpz"SingleRecordCreationTestCase.setUp   sw    #+ &&$%
 ,/	
 :!
    c                    t         j                  j                         }t        j                  j                         }t	               }| j
                  j                  |       | j
                  j                  t        d      | j                  d      }| j                  |j                  t        j                         |j                         }| j                  d|       | j                  d|       t         j                  j                         }| j                  ||dz   d       t        j                  j                         }| j                  ||dz   d	       t        j                  j!                  |
      j#                         }| j%                  |       | j                  |j&                  d       | j%                  |j(                         | j+                  |j,                         y)zJTest that one BaZi API call creates exactly one User and one Person recorduserapi:bazi-listjsonformatmessageidr!   Should create exactly one userz'Should create exactly one Person record
created_byr   N)r   objectscountr   r
   r&   force_authenticatepostr   r'   assertEqualstatus_coder   HTTP_201_CREATEDr2   assertInfilterfirstassertIsNotNoner   r   
assertTruecreated_by_temp_user)	r*   initial_user_countinitial_person_count	temp_userresponseresponse_datafinal_user_countfinal_person_countperson_records	            r+   /test_single_bazi_api_call_creates_single_recordzLSingleRecordCreationTestCase.test_single_bazi_api_call_creates_single_record6   s    "\\//1%~~335 *+	 	**	*: ??''O$ ( 
 	--v/F/FG i/dM*  <<--/)+=+ACcd $^^113+-AA-EGpq ---CIIK]+++->?]556::;r-   c                 H   t         j                  j                         }t        j                  j                         }t	               }| j
                  j                  |       | j
                  j                  d| j                  d      }| j                  |j                  t        j                         |j                         }t         j                  j                         }| j                  ||dz   d       t        j                  j                         }| j                  ||dz   d       t        j                  j                  |      j                         }| j!                  |       | j                  |j"                  d       | j%                  |j&                         y	)
zLTest that one LiuYao API call creates exactly one User and one LiuYao recordr/   /api/liuyao/liuyao/r2   r3   r!   r7   z'Should create exactly one LiuYao recordr   N)r   r:   r;   r	   r
   r&   r<   r=   r(   r>   r?   r   r@   r2   rB   rC   rD   r   rE   rF   )	r*   rG   initial_liuyao_countrI   rJ   rK   rL   final_liuyao_countliuyao_records	            r+   1test_single_liuyao_api_call_creates_single_recordzNSingleRecordCreationTestCase.test_single_liuyao_api_call_creates_single_record^   sU    "\\//1%~~335 *+	 	**	*: ??''! ( 
 	--v/F/FG   <<--/)+=+ACcd $^^113+-AA-EGpq --9-=CCE]+//1HI::;r-   c                    t         j                  j                         }t        j                  j                         }t        j                  j                         }t               }| j                  j                  |       | j                  j                  t        d      | j                  d      }| j                  |j                  t        j                         | j                  j                  d| j                  d      }| j                  |j                  t        j                         t         j                  j                         }| j                  ||dz   d       t        j                  j                         }t        j                  j                         }	| j                  ||dz          | j                  |	|dz          | j                  t        j                  j!                  |      j                         d       | j                  t        j                  j!                  |      j                         d       y	)
zETest that multiple API calls from same session use the same temp userr/   r1   r2   r3   rQ   r!   zShould reuse the same temp userr8   N)r   r:   r;   r   r	   r
   r&   r<   r=   r   r'   r>   r?   r   r@   r(   rB   )
r*   rG   rH   rR   rI   bazi_responseliuyao_responserL   rM   rS   s
             r+   (test_multiple_api_calls_single_temp_userzESingleRecordCreationTestCase.test_multiple_api_calls_single_temp_user   s    "\\//1%~~335%~~335 *+	 	**	*: ,,O$ - 

 	22F4K4KL //..! / 

 	44f6M6MN  <<--/)+=+ACde $^^113#^^113+-AA-EF+-AA-EF 	..).DJJLaP..I.>DDFJr-   c                    t         j                  j                         }t        j                  j                         }| j                  j                  d| j                  d      }| j                  |j                  t        j                         |j                         }| j                  d|       | j                  d|       | j                  d|       | j                  d|       | j                  d|       | j                  d	|       t         j                  j                         }t        j                  j                         }| j                  ||d
       | j                  ||d       y)zZTest that unauthenticated LiuYao Calculator API call only performs calculation (no saving)z/api/liuyao/calc/r2   r3   guar   yaorI   r6   r9   zShould not create any usersz$Should not create any LiuYao recordsN)r   r:   r;   r	   r&   r=   r)   r>   r?   r   HTTP_200_OKr2   rA   assertNotIn)r*   rG   rR   rJ   rK   rL   rS   s          r+   7test_single_liuyao_calculator_api_call_calculation_onlyzTSingleRecordCreationTestCase.test_single_liuyao_calculator_api_call_calculation_only   s-    "\\//1%~~335 ??''!! ( 
 	--v/A/AB  	e]+j-0e]+ 	m4}-}5  <<--/#^^113)+=?\]+-ACijr-   N)	__name__
__module____qualname____doc__r,   rO   rU   rY   r_    r-   r+   r   r      s$    @&
P&<P#<J)KVkr-   r   N)django.testr   django.urlsr   rest_frameworkr   rest_framework.testr   main.user.userr   bazi.modelsr   liuyao.modelsr	   accounts.utilsr
   r   rd   r-   r+   <module>rm      s-       ! )     0Bk8 Bkr-   