
    %xEi0                         d 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 ddlmZmZ ddlmZ d	 Z G d
 de      Zy)z&Tests for ROI tracking in investments.    )Decimal)date	timedelta)TestCase)timezone)User)
InvestmentReward)DistributeRewardsTaskc                  >    t        t        j                               S )z<Helper to create task instance with required execution_date.)execution_date)r   r   today     Y/home/cursorai/projects/telegram-earn/backend/apps/investments/tests/test_roi_tracking.pyget_taskr      s     

==r   c                   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)ROITrackingTestCasez2Test ROI accumulation and percentage calculations.c                     t         j                  j                  dd      | _        t	        d      | j                  _        | j                  j                          y)zSet up test data.i z	Test User)telegram_user_id
first_namez10000.00000000N)r   objectscreate_useruserr   credit_balancesave)selfs    r   setUpzROITrackingTestCase.setUp   sE    LL,,#" - 
	 $++;#<		 		r   c                    t         j                  j                  | j                  t	        d      dt	        d      dt         j
                  t        j                         t        j                         t        d      z         }| j                  |j                  t	        d             | j                  |j                  t	        d             y	)
z:Test newly created investment starts with zero ROI totals.1000.00000000   1.00x   daysr   amounttierdaily_reward_rateduration_daysstatus
start_dateend_date
0.00000000z0.0000N)r	   r   creater   r   STATUS_PENDINGr   nowr   assertEqualroi_total_amountroi_total_percent)r   
investments     r    test_new_investment_has_zero_roiz4ROITrackingTestCase.test_new_investment_has_zero_roi   s    ''..?+%fo,,||~\\^iS&99 / 	

 	44gl6KL55wx7HIr   c                    t         j                  j                  | j                  t	        d      dt	        d      dt         j
                  t        j                         t        d      z
  t        j                         t        d      z         }t	        d	      }t        j                  j                  ||t        j                         t        d      z
  
      }t               }|j                  |j                  t        j                                |j                          | j!                  |j"                  |       | j!                  |j$                  t	        d             y)zBTest ROI totals update correctly after single reward distribution.r    r!   r"   r#      r$   v   r&   10.00000000r5   r'   reward_date1.0000N)r	   r   r/   r   r   STATUS_ACTIVEr   r1   r   r
   r   r   r   _distribute_rewardsr<   refresh_from_dbr2   r3   r4   r   r5   reward_amountrewardtasks        r   #test_roi_accumulation_single_rewardz7ROITrackingTestCase.test_roi_accumulation_single_reward.   s
   ''..?+%fo++||~	q(99\\^iS&99 / 	

  .&&! 

ya'88 ' 
 z  !3!3X\\^D 	""$44mD55wx7HIr   c                    t         j                  j                  | j                  t	        d      dt	        d      dt         j
                  t        j                         t        d      z
  t        j                         t        d      z         }t	        d	      }t        d
      D ]  }t	        d      }t        j                  j                  ||t        j                         t        d
|z
        z
        }t               }|j                  |j                  t        j                                ||z  } |j!                          | j#                  |j$                  |       | j#                  |j&                  t	        d             y)z=Test ROI totals accumulate correctly across multiple rewards.r    r!   r"   r#      r$   s   r&   r.      r:   r;   z3.0000N)r	   r   r/   r   r   r>   r   r1   r   ranger
   r   r   r   r?   r<   r@   r2   r3   r4   )r   r5   total_expectedirB   rC   rD   s          r   &test_roi_accumulation_multiple_rewardsz:ROITrackingTestCase.test_roi_accumulation_multiple_rewardsM   s0   ''..?+%fo++||~	q(99\\^iS&99 / 	

 !.qA#M2M^^**%$ JJL9!A#+>> + F :D$$V%7%7Hm+N  	""$44nE55wx7HIr   c                    t         j                  j                  | j                  t	        d      dt	        d      dt         j
                  t        j                         t        d      z
  t        j                         t        d      z         }t	        d	      }t        j                  j                  ||t        j                         t        d      z
  
      }t               }|j                  |j                  t        j                                |j                          | j!                  t#        |j$                        dd       y)z0Test ROI percentage maintains correct precision.z333.33333333r!   z1.50r#   r8   r$   r9   r&   z
4.99999999r;   g      ?placesNr	   r   r/   r   r   r>   r   r1   r   r
   r   r   r   r?   r<   r@   assertAlmostEqualfloatr4   rA   s        r   )test_roi_percentage_calculation_precisionz=ROITrackingTestCase.test_roi_percentage_calculation_precisionn       ''..>*%fo++||~	q(99\\^iS&99 / 	

  -&&! 

ya'88 ' 
 z  !3!3X\\^D""$ 	*../ 	 	
r   c                    t         j                  j                  | j                  t	        d      dt	        d      dt         j
                  t        j                         t        d      z
  t        j                         t        d      z         }t	        d      }t        j                  j                  ||t        j                         t        d      z
  	      }t               }|j                  |j                  t        j                                |j                          | j!                  |j"                         y
)zLTest ROI percentage calculation handles edge case of zero investment amount.z
0.00000001r!   r"   r#   r8   r$   r9   r&   r;   N)r	   r   r/   r   r   r>   r   r1   r   r
   r   r   r   r?   r<   r@   assertIsNotNoner4   rA   s        r   !test_roi_zero_division_protectionz5ROITrackingTestCase.test_roi_zero_division_protection   s      ''..<(%fo++||~	q(99\\^iS&99 / 	

  -&&! 

ya'88 ' 
 z  !3!3X\\^D""$ 	Z99:r   c                    t         j                  j                  | j                  t	        d      dt	        d      dt         j
                  t        j                         t        d      z
  t        j                         t        d      z         }t	        d	      }t        j                  j                  ||t        j                         t        d
      z
        }t               }|j                  |j                  t        j                                |j                          | j!                  |j"                  |       | j%                  t'        |j(                        dd       y)z5Test ROI tracking with very large investment amounts.z999999.99999999   z3.50   r8   r$      r&   z34999.99999999r!   r;   g      @rO   N)r	   r   r/   r   r   r>   r   r1   r   r
   r   r   r   r?   r<   r@   r2   r3   rR   rS   r4   rA   s        r   test_roi_large_amountsz*ROITrackingTestCase.test_roi_large_amounts   s   ''..,-%fo++||~	q(99\\^iS&99 / 	

   01&&! 

ya'88 ' 
 z  !3!3X\\^D""$44mD*../ 	 	
r   c                    t         j                  j                  | j                  t	        d      dt	        d      dt         j
                  t        j                         t        j                         t        d      z         }| j                  |j                  t	        d             t         j                  |_        |j                          t        j                  j                  |t	        d      t        j                          	      }t#               }|j%                  |j&                  t        j                                |j)                          | j                  |j                  t	        d             t         j*                  |_        |j                          |j)                          | j                  |j                  t	        d             | j                  |j,                  t	        d
             y)z6Test ROI tracking from pending -> active -> completed.r    r!   r"   r#   r$   r&   r.   r:   r;   r=   N)r	   r   r/   r   r   r0   r   r1   r   r2   r3   r>   r+   r   r
   r   r   r   r?   r<   r@   STATUS_COMPLETEDr4   )r   r5   rC   rD   s       r   -test_roi_tracking_across_investment_lifecyclezAROITrackingTestCase.test_roi_tracking_across_investment_lifecycle   sj   ''..?+%fo,,||~\\^iS&99 / 	

 	44gl6KL '44
&&!=)

 ' 
 z  !3!3X\\^D""$44gm6LM '77
""$44gm6LM55wx7HIr   c                    t         j                  j                  | j                  t	        d      dt	        d      dt         j
                  t        j                         t        d      z
  t        j                         t        d      z         }t	        d	      }t        j                  j                  ||t        j                         t        d      z
  
      }t               }|j                  |j                  t        j                                |j                          |j                   }|j                  |j                  t        j                                |j                          | j#                  |j                   |       y)z=Test distributing the same reward twice doesn't double-count.r    r!   r"   r#   r8   r$   r9   r&   r:   r;   N)r	   r   r/   r   r   r>   r   r1   r   r
   r   r   r   r?   r<   r@   r3   r2   )r   r5   rB   rC   rD   first_totals         r   test_roi_idempotencyz(ROITrackingTestCase.test_roi_idempotency   s$   ''..?+%fo++||~	q(99\\^iS&99 / 	

  .&&! 

ya'88 ' 
 z  !3!3X\\^D""$ 11 	  !3!3X\\^D""$ 	44kBr   c                    t         j                  j                  | j                  t	        d      dt	        d      dt         j
                  t        j                         t        d      z
  t        j                         t        d      z         }t	        d      }t        j                  j                  ||t        j                         t        d	      z
  
      }t               }|j                  |j                  t        j                                |j                          | j!                  t#        |j$                        dd       y)z8Test ROI percentage calculation with fractional results.z777.77777777r8   z1.23   r$      r&   z
9.56666666r!   r;   gGz?rO   NrQ   rA   s        r   test_roi_fractional_percentagesz3ROITrackingTestCase.test_roi_fractional_percentages  rU   r   c                 x   ddl m} ddlm} t        j
                  j                  | j                  t        d      dt        d      dt        j                  t        j                         t        j                         t        d      z   t        d	      t        d
      
      } ||      }|j                  }| j                  d|       | j                  d|       | j                  t        t!        |d               t        d	             | j                  t        t!        |d               t        d
             y)z2Test ROI fields are included in API serialization.r   )	APIClient)InvestmentSerializerr    r!   r"   r#   r$   z50.00000000z5.0000)
r   r'   r(   r)   r*   r+   r,   r-   r3   r4   r3   r4   N)rest_framework.testri   apps.investments.serializersrj   r	   r   r/   r   r   r>   r   r1   r   dataassertInr2   str)r   ri   rj   r5   
serializerrm   s         r   test_roi_serializer_outputz.ROITrackingTestCase.test_roi_serializer_output9  s    1E''..?+%fo++||~\\^iS&99$]3%h/ / 

 **5
($/)40T*<%=!>?AWXT*=%>!?@'(BSTr   N)__name__
__module____qualname____doc__r   r6   rE   rM   rT   rX   r]   r`   rc   rg   rq   r   r   r   r   r      sH    <J J>JB
B;:
B&JP!CF
BUr   r   N)ru   decimalr   datetimer   r   django.testr   django.utilsr   apps.users.modelsr   apps.investments.modelsr	   r
   apps.investments.cron_tasksr   r   r   r   r   r   <module>r}      s2    ,  $   ! " 6 =>
U( Ur   