
    Bi/                        d 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mZ dd
lmZ ddlmZ ddlmZ  G d de
      Zy)z3Tests for the daily reward and commission pipeline.    )annotations)	timedelta)Decimal)call_command)TestCase)timezone)DistributeRewardsTask)
InvestmentReward)
Commission)Transaction)Userc                  D    e Zd ZddZd Zd Zd Zd Zd Zd Z	d Z
d	 Zy
)CalculateRewardsCommandTestsc                   t         j                  j                  ddd      | _        t         j                  j                  ddd| j                        | _        t         j                  j                  d	d
d| j                        | _        t         j                  j                  ddd| j
                        | _        t        j                  t        d            }t        j                         }t        j                  j                  | j                  t        d      |d   |d   |d   t        j                  |t        d      z
  |t        d      z         | _        y)z<Create a referral chain with one downline and three uplines.i  U1u1telegram_user_id
first_nameusernamei  U2u2r   r   r   referred_byi  U3u3i  Downlinedownline1000tierrateduration   days
   useramountr!   daily_reward_rateduration_daysstatus
start_dateend_dateN)r   objectscreate_userupline1upline2upline3r   r
   get_tier_infor   r   nowcreateSTATUS_PENDINGr   
investment)self	tier_infor6   s      ^/home/cursorai/projects/telegram-earn/backend/apps/investments/tests/test_calculate_rewards.pysetUpz"CalculateRewardsCommandTests.setUp   s4   ||//! 0 

 ||//!	 0 
 ||//!	 0 
 00!!	 1 
 ,,WV_=	lln$,,336?6"'/#J/,,YA..9"-- 4 	
    c                B   | j                   j                  t        d      z   j                         }t	        d|j                                t        |      j                  |       t        j                  j                  | j                   |      }| j                  |j                  t        d             t        j                  j                  |      j!                  d      }| j                  |j#                         d	       | j%                  |D cg c]  }|j&                   c}g d
       | j%                  |D cg c]  }|j                   c}t        d      t        d      t        d      g       | j(                  j+                          | j,                  j+                          | j.                  j+                          | j0                  j+                          | j                  | j(                  j2                  t        d             | j                  | j,                  j2                  t        d             | j                  | j.                  j2                  t        d             | j                  | j0                  j2                  t        d             t4        j                  j!                  d      }| j                  |j#                         d       | j                  |D cg c]  }|j6                   c}t4        j8                  t4        j:                  t4        j:                  t4        j:                  g       | j                   j+                          | j                  | j                   j<                  t>        j@                         yc c}w c c}w c c}w )z@Command credits reward to investor and commissions up the chain.   r%   calculate_rewardsdater9   reward_date20.00000000rewardlevel   )r@   r$   rJ   10.00000000
6.00000000
4.00000000
created_at   N)!r9   r.   r   rC   r   	isoformatr	   executer   r0   getassertEqualr*   r   r   filterorder_bycountassertListEqualrI   r   refresh_from_dbr2   r3   r4   credit_balancer   transaction_typeTYPE_REWARDTYPE_COMMISSIONr-   r
   STATUS_ACTIVE)r:   target_daterH   commissionsctransactionsts          r<   0test_creates_reward_commissions_and_transactionszMCalculateRewardsCommandTests.test_creates_reward_commissions_and_transactions;   s   11I14EEKKM({/D/D/FGk*22;?##tK#X(>? ((//v/>GGP**,a0)*kQWWk*	
 	*+{!QXX{+&%%	
 	%%'$$&$$&$$&55w}7MN44gl6KL44gl6KL44gm6LM #**33LA++-q1)56AQ6''++++++		
 	'')//1I1IJM + ,. 7s   NN,Nc                   | j                   j                  t        d      z   j                         }t	        d|j                                t        |      j                  |       t	        d|j                                t        |      j                  |       | j                  t        j                  j                  | j                   |      j                         d       | j                  t        j                  j                         d       | j                  t        j                  j                         d       | j                  j!                          | j"                  j!                          | j$                  j!                          | j&                  j!                          | j                  | j                  j(                  t+        d             | j                  | j"                  j(                  t+        d	             | j                  | j$                  j(                  t+        d
             | j                  | j&                  j(                  t+        d             y)zFRunning the command twice should not duplicate rewards or commissions.r@   r%   rA   rB   rD   rJ   rO   rF   rM   rL   rK   N)r9   r.   r   rC   r   rP   r	   rQ   rS   r   r0   rT   rV   r   r   r   rX   r2   r3   r4   rY   r   r:   r^   s     r<   #test_idempotent_for_existing_rewardz@CalculateRewardsCommandTests.test_idempotent_for_existing_rewardp   s   11I14EEKKM({/D/D/FGk*22;?({/D/D/FGk*22;?NN!!T__+!V\\^	
 	++113Q7,,224a8 	%%'$$&$$&$$&55w}7MN44gl6KL44gl6KL44gm6LMr>   c                   | j                   j                  j                         }t        d|j	                                | j                  t        j                  j                  | j                   |      j                                | j                  t        j                  j                         d       | j                  t        j                  j                         d       y)z=No reward/commission is created on the investment start date.rA   rB   rD   r   N)r9   r.   rC   r   rP   assertFalser   r0   rT   existsrS   r   rV   r   re   s     r<   "test_skips_before_first_reward_dayz?CalculateRewardsCommandTests.test_skips_before_first_reward_day   s    oo00557({/D/D/FGNN!!T__+!V]]_	
 	++113Q7,,224a8r>   c                   | j                   j                  j                         }t        d|j	                                t
        j                  j                  | j                   |      }| j                  |j                  t        d             | j                   j                          | j                  | j                   j                  t        j                         y)z<Status flips to completed when target_date reaches end_date.rA   rB   rD   rF   N)r9   r/   rC   r   rP   r   r0   rR   rS   r*   r   rX   r-   r
   STATUS_COMPLETED)r:   r^   rH   s      r<   +test_marks_investment_completed_on_end_datezHCalculateRewardsCommandTests.test_marks_investment_completed_on_end_date   s    oo..335({/D/D/FG##tK#X(>?'')//1L1LMr>   c                \   | j                   j                  t        d      z   j                         }t        j
                  | j                   _        | j                   j                  dg       t        d|j                                | j                  t        j                  j                  | j                   |      j                                | j                  t         j                  j#                         d       | j                  t$        j                  j#                         d       y	)
z?Completed investments are ignored even if the date is in range.r@   r%   r-   update_fieldsrA   rB   rD   r   N)r9   r.   r   rC   r
   rl   r-   saver   rP   rh   r   r0   rT   ri   rS   r   rV   r   re   s     r<   &test_skips_completed_investment_statuszCCalculateRewardsCommandTests.test_skips_completed_investment_status   s    11I14EEKKM!+!<!<H:6({/D/D/FGNN!!T__+!V]]_	
 	++113Q7,,224a8r>   c                   t        j                         t        d      z   j                         }t        j
                  | j                  _        | j                  j                  dg       t        j                  j                  ddd      }t	        j                  t        d	            }t        j                  j                  |t        d	      |d
   |d   |d   t        j                  t        j                         t        j                         t        d      z         }t!        d|j#                                t%        |      j'                  |       t(        j                  j+                  ||      }| j-                  |j.                  t        d             | j-                  t0        j                  j3                         d       t4        j                  j7                  |      }| j-                  |j3                         d       | j-                  |j9                         j:                  t4        j<                         |j?                          | j-                  |j@                  t        d             y)zERewards still post when the investor has no referrer; no commissions.   r%   r-   ro   i  Solosolor   100r!   r"   r#      r(   rA   rB   rD   z
1.00000000r   )r)   r@   N)!r   r6   r   rC   r
   rl   r9   r-   rq   r   r0   r1   r5   r   r7   r8   r   rP   r	   rQ   r   rR   rS   r*   r   rV   r   rT   firstrZ   r[   rX   rY   )r:   r^   	solo_userr;   solo_investmentrH   ra   s          r<   6test_creates_reward_without_commissions_when_no_uplinezSCalculateRewardsCommandTests.test_creates_reward_without_commissions_when_no_upline   s   ||~	q(99??A!+!<!<H:6LL,,!fv - 
	 ,,WU^<	$,,335>6"'/#J/,,||~\\^iR&88 4 	
 	({/D/D/FGk*22;?##K#X(=>++113Q7"**11y1A++-q1++->>@W@WX!!#117<3HIr>   c                   t        j                         t        d      z   j                         }t        j
                  | j                  _        | j                  j                  dg       t        j                  j                  ddd      }t        j                  j                  d	d
d|      }t	        j                  t        d            }t        j                  j                  |t        d      |d   |d   |d   t        j                  t        j                         t        j                         t        d      z         }t!        d|j#                                t%        |      j'                  |       t(        j                  j+                  ||      }| j-                  |j.                  t        d             t0        j                  j3                  |      }| j-                  |j5                         d       |j7                         }| j-                  |j8                  d       | j-                  |j.                  t        d             | j-                  t:        j                  j5                         d       |j=                          |j=                          | j-                  |j>                  t        d             | j-                  |j>                  t        d             y)zKOnly existing uplines receive commissions when the chain is shorter than 3.   r%   r-   ro   i  Toptopr   i  Downdownr   r    r!   r"   r#      r(   rA   rB   rD   rF   rG   r@   rK   r$   N) r   r6   r   rC   r
   rl   r9   r-   rq   r   r0   r1   r5   r   r7   r8   r   rP   r	   rQ   r   rR   rS   r*   r   rT   rV   ry   rI   r   rX   rY   )	r:   r^   upliner   r;   short_chain_investmentrH   r_   
commissions	            r<   :test_partial_referral_chain_only_credits_available_uplineszWCalculateRewardsCommandTests.test_partial_referral_chain_only_credits_available_uplines   sB   ||~	q(99??A!+!<!<H:6))!ee * 
 <<++!fvSY , 
 ,,WV_=	!+!3!3!:!:6?6"'/#J/,,||~\\^iR&88 "; 	"
 	({/D/D/FGk*22;?##/ES^#_(>? ((//v/>**,a0 &&(
))1-**GM,BC,,224a8   "..0FG00'-2HIr>   c                   t        j                         t        d      z   j                         }t        j
                  | j                  _        | j                  j                  dg       t        j                  j                  ddd      }t        j                  j                  |t        d	      d
t        d      dt        j                  t        j                         t        j                         t        d      z         }t        d|j!                                t#        |      j%                  |       t&        j                  j)                  ||      }| j+                  |j,                  t        d             t.        j                  j1                  |      j3                  d      }| j+                  |j5                         d       |j7                          | j+                  |j8                  t        d             y)z>Reward and commission amounts are rounded to 8 decimal places.	   r%   r-   ro   i  Precisepreciser   z1234.56789012r@   z1.25rx   (   r(   rA   rB   rD   z15.43209863rG   rI   r   N)r   r6   r   rC   r
   rl   r9   r-   rq   r   r0   r1   r7   r   r8   r   rP   r	   rQ   r   rR   rS   r*   r   rT   rU   rV   rX   rY   )r:   r^   precise_userprecise_investmentrH   r_   s         r<   3test_quantizes_reward_and_commissions_round_half_upzPCalculateRewardsCommandTests.test_quantizes_reward_and_commissions_round_half_up   s   ||~	q(99??A!+!<!<H:6||//!i) 0 
 (//66?+%fo,,||~\\^iR&88 7 	
 	({/D/D/FGk*22;?##/A{#[(>? ((//v/>GGP**,a0$$&44gm6LMr>   N)returnNone)__name__
__module____qualname__r=   rc   rf   rj   rm   rr   r|   r   r    r>   r<   r   r      s9    %
N3KjN4
9
N9J@%JNNr>   r   N)__doc__
__future__r   datetimer   decimalr   django.core.managementr   django.testr   django.utilsr   apps.investments.cron_tasksr	   apps.investments.modelsr
   r   apps.referrals.modelsr   apps.transactions.modelsr   apps.users.modelsr   r   r   r>   r<   <module>r      s9    9 "   /   ! = 6 , 0 "AN8 ANr>   