
    P1i<                     P   d Z 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 ddlZ e       Z G d	 d
e      Zedk(  rcddlZddlZej(                  j+                  dd        ej,                          ddlmZ ddlmZ  ee      Z e       Zej;                  dg      Zyy)ut  
Test BaZi → LiuYao temp user continuity scenario
This test verifies the exact scenario described by the user:
1. Public user creates BaZi record first - temp user created and has auth session
2. User proceeds to create LiuYao record - should reuse same temp user (not create new one)
3. User can see both BaZi and LiuYao records since they belong to the same temp user
    )TestCaseClient)get_user_model)reverse)Person)liuyao)SessionNc                   (    e Zd ZdZd Zd Zd Zd Zy) BaziLiuyaoTempUserContinuityTestu<   Test the exact BaZi → LiuYao temp user continuity scenarioc                 "    t               | _        y )N)r   client)selfs    `/home/cursorai/projects/iching/iching/tests/../../tests/test_bazi_liuyao_temp_user_continuity.pysetUpz&BaziLiuyaoTempUserContinuityTest.setUp   s    h    c           	         t        d       t        d       | j                  j                  t        d      t	        j
                  ddi      d      }| j                  |j                  d       |j	                         }|d	   }t        d
| d       | j                  j                  t        d      t	        j
                  d|d   i      d      }| j                  |j                  d       t        d|        ddddddd}| j                  j                  t        d      t	        j
                  |      d      }| j                  |j                  d       |j	                         }t        d|d    d|        t        j                  j                  |d         }| j                  |j                  j                  |       t        d|        t        d       t        j                  j                  d       j                         }	t        d!|	        t        d"       | j                  j                  t        d#            }
| j                  |
j                  d       t        d$       d%d&d'd(d'd(d'd(d)}| j                  j                  t        d*      t	        j
                  |      d      }| j                  |j                  d       |j	                         }|j                  d      xs$ |j                  d+      xs |j                  d,      }t        d-       t        d.       t        j                  j                  d       j                         }t        d/|        | j                  |	|d0       t        d1       t        d2       t         j                  j                  d%3      }|j                  }t        d4|        | j                  |j                  j                  |j"                  j                  d5       | j                  |j                  j                  |       | j                  |j"                  j                  |       t        d6|j                  j                          t        d7|j"                  j                          t        d8|        t        d9       | j                  j                  t        d            }| j                  |j                  d       |j	                         }| j                  j                  t        d*            }| j                  |j                  d       |j	                         }|d:   D cg c]  }|d   	 }}|d:   D cg c]  }|j                  d;       }}| j%                  |d   |d<       | j%                  d%|d=       t        d>|d           t        d?|        t        d@       t        dA       t        j                  j                  |      }| j'                  |j(                         t        j                  j                  |B      j                         }t         j                  j                  |C      j                         }| j                  |dDdE       | j                  |dDdF       t        dG| dH       t        dI| dJ       t        dI| dK       t        dL       t        dM       yNc c}w c c}w )Oa  
        Test the complete scenario:
        1. Create BaZi record (creates temp user 247 with session)
        2. Create LiuYao record (should reuse temp user 247, not create 248)
        3. Verify both records belong to same user
        4. Verify user can access both records
        u:   🎯 Testing BaZi → LiuYao temp user continuity scenariou<   
📊 Step 1: Creating BaZi record (should create temp user)api:temp_user_createnamez	BaZi Userapplication/jsoncontent_type   user_idu      ✓ Temp user z created for BaZi flowapi:jwt_to_sessionaccess_tokenaccess   u)      ✓ Session auth established for user zTest PersonMz
1990-05-1510:30:00r   zTest BaZi recordr   gender
birth_date
birth_time	twin_typenotesapi:bazi-listu      ✓ BaZi record idz created for user r'   u,      ✓ Verified BaZi record belongs to user u3   
📈 Step 2: Checking state before LiuYao creationTis_temporary_useru"      📊 Temp users before LiuYao: uF   
🧿 Step 3: Creating LiuYao record (should reuse existing temp user)z
main:indexu      ✓ Session still activezTest question2024-01-15T14:30:00Z10questionqdatey1y2y3y4y5y6api:liuyao-listpkuuidu)      ✓ LiuYao record created successfullyu,   
🔍 Step 4: Verifying temp user continuityu!      📊 Temp users after LiuYao: z/LiuYao creation should NOT create new temp useru8      ✅ No new temp user created - continuity maintained!u(   
👤 Step 5: Verifying record ownershipr/   u$      ✓ Found LiuYao record with ID: z7BaZi and LiuYao records should belong to same temp useru#      ✓ BaZi record belongs to user u%      ✓ LiuYao record belongs to user u-      ✅ Both records belong to same temp user u*   
📋 Step 6: Verifying data accessibilityresultsr/   z!User should see their BaZi recordz#User should see their LiuYao recordu$      ✓ User can access BaZi record: u&      ✓ User can access LiuYao record: u1      ✅ All data accessible to the same temp user!u    
🎉 Step 7: Final verification
created_byuser   zShould have 1 BaZi recordzShould have 1 LiuYao recordu      📊 Temp user z has:z	       - z BaZi record(s)z LiuYao record(s)u(      ✅ Perfect data continuity achieved!u;   
🎯 ✅ BaZi → LiuYao temp user continuity test PASSED!N)printr   postr   jsondumpsassertEqualstatus_coder   objectsgetr=   r'   Userfiltercountr   r?   assertIn
assertTruer*   )r   bazi_responsebazi_temp_user_databazi_user_idsession_responsebazi_record_databazi_create_responsebazi_recordbazi_persontemp_users_beforeresponseliuyao_dataliuyao_responseliuyao_recordliuyao_record_idtemp_users_after
liuyao_objbazi_list_response	bazi_listliuyao_list_responseliuyao_listrecordbazi_idsliuyao_questions	temp_useruser_bazi_countuser_liuyao_counts                              r   2test_bazi_then_liuyao_same_temp_user_full_scenariozSBaziLiuyaoTempUserContinuityTest.test_bazi_then_liuyao_same_temp_user_full_scenario   su    	JK 	MN ((*+JJ,-+ ) 

 	22C8+002*95!,/EFG  ;;++()JJ(;H(EFG+ , 

 	)55s;9,HI "&$'
  ${{//O$JJ'(+  0  

 	-993?*//1#K$5#66HWX nn((K,=(>//22LA<\NKL 	DE LL//$/GMMO23D2EFG 	WX ;;??7<#89--s3+, (+S3c
 ++**%&JJ{#+ + 

 	44c:',,. ),,T2jm6G6G6MjQ^QbQbciQj9; 	=><<...FLLN12B1CDE 	*,<I	KHI 	9: ^^'''A
%==45E4FGH 	//22JOO4F4FQ	S//22LA++\:3K4J4J4M4M3NOP5joo6H6H5IJK=l^LM 	;< "[[__W_-EF+77=&++-	  ${{w7H/IJ-993?*//1 09/CD/CVF4L/CDALYAWXAWvFJJz2AWXk$'3VWo'79^_4[5F4GHI67G6HIJAB 	12LL$$$5		334 !..//9/EKKM"NN11y1AGGI!-HI*A/LM"<.67	/*/:;	+,,=>?89LM9 EXs   [)0[.c                    t        d       | j                  j                  t        d      t	        j
                  ddi      d      }|j	                         }|d   }| j                  j                  t        d      t	        j
                  d	|d
   i      d       t        d|        g }t        d      D ]  }d|dz    dd|z    dddd|dz    d}| j                  j                  t        d      t	        j
                  |      d      }| j                  |j                  d       |j                  |j	                                 t        dt        |       d       g }t        d      D ]  }d|dz    dd|z    dt        |dz        t        |dz   dz        t        |dz        t        |dz   dz        t        |dz        t        |dz   dz        d}	| j                  j                  t        d       t	        j
                  |	      d      }
| j                  |
j                  d       |j                  |
j	                                 t        dt        |       d!       t        j                  j                  |"      }t        j                  j!                  |#      j#                         }t$        j                  j!                  |$      j#                         }| j                  |dd%       | j                  |dd&       t        j                  j!                  d'(      j#                         }| j                  |dd)       t        d*|        t        d+       y,)-zUTest creating multiple BaZi records then LiuYao - all should belong to same temp useru:   
🔄 Testing multiple BaZi → LiuYao with same temp userr   r   zMulti Record Userr   r   r   r   r   r         ✓ Created temp user    zPerson r@   r   i  z-05-15r   r   zTest BaZi record r    r&   r   u      ✓ Created z BaZi records   z	Question z2024-01-   z
T14:30:00Zr.   r7   z LiuYao recordsr(   r<   r>   zShould have 3 BaZi recordszShould have 2 LiuYao recordsTr)   Should only have 1 temp useru,      ✅ All records belong to same temp user u/      ✅ Multiple records continuity test PASSED!N)rA   r   rB   r   rC   rD   rangerE   rF   appendlenstrrI   rG   rH   r   rJ   rK   r   )r   rW   temp_user_datatemp_user_idbazi_recordsi	bazi_datarN   liuyao_recordsrX   rY   re   
bazi_countliuyao_counttotal_temp_userss                  r   (test_multiple_bazi_then_liuyao_same_userzIBaziLiuyaoTempUserContinuityTest.test_multiple_bazi_then_liuyao_same_user   s@    	KL ;;##*+JJ 345+ $ 

 "%i0 	()JJx(@AB+ 	 	
 	),89 qA!!A#!%
&1(,QqSE2I !KK,,(

9%/ - M
 ]66< 2 2 45! $ 	L 12-@A qA'!u-#BF8:6!a%jQqSAIc!a%j1Q3!)nCAJc1Q3!)n	K #kk..)*

;'/ / O
 _88#>!!/"6"6"89   	N 34ODE LL$$$5	^^**i*@FFH
~~,,),<BBDQ(DEq*HI  <<...FLLN)1.LM<\NKL?@r   c           	         t        d       | j                  j                  t        d      t	        j
                  ddi      d      }|j	                         }|d   }| j                  j                  t        d      t	        j
                  d	|d
   i      d       t        d| d       ddddddddd}| j                  j                  t        d      t	        j
                  |      d      }| j                  |j                  d       |j	                         }t        d|j                  dd              ddddddd}| j                  j                  t        d      t	        j
                  |      d      }| j                  |j                  d       |j	                         }	t        d|	d            t        j                  j                  |!      }
t        j                  j                  |	d    !      }t        j                  j                  d"      }| j                  |j                  j                  |       | j                  |j                  j                  |       t        j                  j!                  d#$      j#                         }| j                  |d%d&       t        d'|        t        d(       y))*zJTest reverse scenario: LiuYao first, then BaZi - should use same temp useru2   
🔄 Testing LiuYao → BaZi temp user continuityr   r   zLiuYao First Userr   r   r   r   r   r   rj   z via LiuYao flowzFirst questionr+   r,   r-   r.   r7   r   u      ✓ Created LiuYao record r/   UnknownzBaZi After LiuYaoFz
1995-08-20z14:15:00r   zBaZi after LiuYao testr    r&   u      ✓ Created BaZi record r'   r(   r:   Tr)   r@   rn   u(      ✅ Both records belong to temp user u.      ✅ LiuYao → BaZi continuity test PASSED!N)rA   r   rB   r   rC   rD   rE   rF   rH   rI   rG   r   r   r=   r'   r?   rJ   rK   )r   rW   rs   rt   rX   rY   rZ   rw   rN   rT   re   rU   r]   r{   s                 r   %test_liuyao_first_then_bazi_same_userzFBaziLiuyaoTempUserContinuityTest.test_liuyao_first_then_bazi_same_user  s    	CD ;;##*+JJ 345+ $ 

 "%i0 	()JJx(@AB+ 	 	
 	),7GHI )+S3c
 ++**%&JJ{#+ + 

 	44c:',,.-m.?.?
I.V-WXY (&$-
	 ((O$JJy!+ ) 

 	22C8#((*+K,=+>?@ LL$$$5	nn((K,=(>^^''1A'B
//22LA++\:  <<...FLLN)1.LM8GH>?r   N)__name__
__module____qualname____doc__r   rh   r|   r    r   r   r   r      s!    FfNPOAbG@r   r   __main__DJANGO_SETTINGS_MODULEziching.settings)
get_runner)settingsz+tests.test_bazi_liuyao_temp_user_continuity)r   django.testr   r   django.contrib.authr   django.urlsr   bazi.modelsr   liuyao.modelsr   django.contrib.sessions.modelsr	   rC   rI   r   r   djangoosenviron
setdefaultsetupdjango.test.utilsr   django.confr   
TestRunnertest_runner	run_testsfailuresr   r   r   <module>r      s    ) .     2 F@x F@P
 zJJ24EFFLLN,$H%J,K$$&S%TUH r   