
    S>ie*                         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
mZmZ  G d de      Z G d	 d
e      Z G d de      Z G d de      Zy)z,
Tests for WalletRepository implementation.
    )Decimal)TestCase)WalletRepository)WalletOperationError)TestUser
TestWalletTestWalletCustomFieldc                   (    e Zd ZdZd Zd Zd Zd Zy)#WalletRepositoryInitializationTestsz*Tests for WalletRepository initialization.c                     t        t        ddi      }| j                  |j                         t               | j                  |j	                         t               | j                  |j                         ddi       y)z)Test initialization with User model only.credit_balance   
user_modelpoint_typesN)r   r   assertEqualget_user_modelget_wallet_balance_modelget_point_typesselfrepos     K/home/cursorai/projects/django-wallet-utils/tests/test_wallet_repository.pytest_init_with_user_modelz=WalletRepositoryInitializationTests.test_init_with_user_model   sj    )1-

 	,,.9668(C--/2BA1FG    c                     t        t        t        ddi      }| j                  |j	                         t               | j                  |j                         t               y)z/Test initialization with separate wallet model.r   r   r   wallet_balance_modelr   N)r   r   r   r   r   r   r   s     r   $test_init_with_separate_wallet_modelzHWalletRepositoryInitializationTests.test_init_with_separate_wallet_model   sN    !+)1-
 	,,.9668*Er   c                 v    t        t        ddiddi      }|j                  d      }| j                  |d       y)z3Test initialization with custom field name mapping.reward_pointsr   points)r   r   point_type_field_mapN)r   r   get_point_type_fieldr   )r   r   
field_names      r   test_init_with_custom_field_mapzCWalletRepositoryInitializationTests.test_init_with_custom_field_map%   sA    (!,"18!<
 ..?
X.r   c                     t        t        t        ddid      }t        j                  j	                  dt        d            }|j                  dd      }| j                  |t        d             y)	z3Test initialization with custom user_id field name.r   r   owner_id)r   r   r   wallet_user_id_field{   100.00)r(   r   N)r   r   r	   objectscreater   get_user_balancer   )r   r   walletbalances       r   #test_init_with_custom_user_id_fieldzGWalletRepositoryInitializationTests.test_init_with_custom_user_id_field0   so    !6)1-!+	
 '..55"8, 6 

 ''-=>'("34r   N)__name__
__module____qualname____doc__r   r   r&   r1    r   r   r   r      s    4	H	F	/5r   r   c                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)&WalletRepositoryBalanceOperationsTestszTests for balance operations.c                     t         j                  j                  dd      | _        t	        t         ddi      | _        y)Set up test fixtures.testusertest@example.comusernameemailr   r   r   N)r   r,   r-   userr   r   r   s    r   setUpz,WalletRepositoryBalanceOperationsTests.setUpF   s7    $$++ZGY+Z	$)1-
	r   c                 B   | j                   j                  | j                  j                  dt	        d            }| j                  |t	        d             | j                  j                          | j                  | j                  j                  t	        d             y)zTest update_balance for adding.r   r+   user_id
point_typeamountNr   update_balancer@   idr   r   refresh_from_dbr   r   new_balances     r   test_update_balance_addz>WalletRepositoryBalanceOperationsTests.test_update_balance_addN   sw    ii..IILL'8$ / 
 	gh&78		!!#11783DEr   c                     | j                  t              5  | j                  j                  | j                  j
                  dt        d      d       ddd       y# 1 sw Y   yxY w)z.Test update_balance prevents negative balance.r   -100.00FrE   rF   rG   allow_negativeN)assertRaises
ValueErrorr   rI   r@   rJ   r   rA   s    r   (test_update_balance_negative_not_allowedzOWalletRepositoryBalanceOperationsTests.test_update_balance_negative_not_allowedZ   sL    z*II$$		+y)$	 %  +**s   =AA%c                 D   | j                   j                  | j                  j                  dt	        d      d      }| j                  |t	        d             | j                  j                          | j                  | j                  j                  t	        d             y)z,Test update_balance allows negative balance.r   rP   TrQ   NrH   rL   s     r   $test_update_balance_negative_allowedzKWalletRepositoryBalanceOperationsTests.test_update_balance_negative_allowedd   sz    ii..IILL'9%	 / 
 	gi&89		!!#11793EFr   c                    t        d      | j                  _        | j                  j                          | j                  j                  | j                  j                  dt        d            \  }}| j                  |       | j                  |t        d             | j                  j                          | j                  | j                  j                  t        d             y)z:Test atomic deduction succeeds when balance is sufficient.z1000.00r   500.00rD   N
r   r@   r   saver   deduct_balance_atomicrJ   
assertTruer   rK   r   successrM   s      r   "test_deduct_balance_atomic_successzIWalletRepositoryBalanceOperationsTests.test_deduct_balance_atomic_successq   s     $+9#5		 		#yy>>IILL'8$  ?  
 	 gh&78		!!#11783DEr   c                    t        d      | j                  _        | j                  j                          | j                  j                  | j                  j                  dt        d            \  }}| j                  |       | j                  |t        d             | j                  j                          | j                  | j                  j                  t        d             y)z9Test atomic deduction fails when balance is insufficient.r+   r   rY   rD   N)
r   r@   r   r[   r   r\   rJ   assertFalser   rK   )r   r_   current_balances      r   'test_deduct_balance_atomic_insufficientzNWalletRepositoryBalanceOperationsTests.test_deduct_balance_atomic_insufficient   s     $+8#4		 		#'99#B#BIILL'8$ $C $
  	!'(*;<		!!#11783DEr   c                 @   t        d      | j                  _        | j                  j                          | j                  j                  | j                  j                  dt        d            \  }}| j                  |       | j                  |t        d             y)z)Test atomic deduction with exact balance.rY   r   rD   z0.00N)	r   r@   r   r[   r   r\   rJ   r]   r   r^   s      r   (test_deduct_balance_atomic_exact_balancezOWalletRepositoryBalanceOperationsTests.test_deduct_balance_atomic_exact_balance   sy     $+8#4		 		#yy>>IILL'8$  ?  
 	 gfo6r   c                    t        d      | j                  _        | j                  j                          | j                  j                  | j                  j                  dt        d      d      \  }}| j                  |       | j                  |t        d             | j                  j                          | j                  | j                  j                  t        d             y)z/Test atomic deduction with allow_negative=True.r+   r   rY   TrQ   z-400.00NrZ   r^   s      r   )test_deduct_balance_atomic_allow_negativezPWalletRepositoryBalanceOperationsTests.test_deduct_balance_atomic_allow_negative   s     $+8#4		 		#yy>>IILL'8$	  ?  
 	 gi&89		!!#11793EFr   c                    t        d      | j                  _        | j                  j                          | j                  j                  | j                  j                  d      }| j                  |t        d             y)zTest get_user_balance.z123.45r   N)r   r@   r   r[   r   r.   rJ   r   r   r0   s     r   test_get_user_balancez<WalletRepositoryBalanceOperationsTests.test_get_user_balance   sU    #*8#4		 		)),,TYY\\;KL'("34r   N)r2   r3   r4   r5   rB   rN   rU   rW   r`   rd   rf   rh   rk   r6   r   r   r8   r8   C   s7    '

FGF"F"7G$5r   r8   c                   (    e Zd ZdZd Zd Zd Zd Zy)"WalletRepositorySeparateModelTestsz0Tests for repository with separate wallet model.c                    t         j                  j                  dd      | _        t        j                  j                  | j                  j
                  t        d            | _        t        t         t        ddi      | _	        y	)
r:   r;   r<   r=   rY   )rE   r   r   r   r   N)
r   r,   r-   r@   r   rJ   r   r/   r   r   rA   s    r   rB   z(WalletRepositorySeparateModelTests.setUp   sl    $$++ZGY+Z	 ((//IILL"8, 0 
 %!+)1-
	r   c                 B   | j                   j                  | j                  j                  dt	        d            }| j                  |t	        d             | j                  j                          | j                  | j                  j                  t	        d             y)z/Test update_balance with separate wallet model.r   r+   rD   z600.00N)	r   rI   r@   rJ   r   r   r/   rK   r   rL   s     r   "test_update_balance_separate_modelzEWalletRepositorySeparateModelTests.test_update_balance_separate_model   sw    ii..IILL'8$ / 
 	gh&78##%33WX5FGr   c                 j   | j                   j                  | j                  j                  dt	        d            \  }}| j                  |       | j                  |t	        d             | j                  j                          | j                  | j                  j                  t	        d             y)z1Test atomic deduction with separate wallet model.r   z200.00rD   z300.00N)
r   r\   r@   rJ   r   r]   r   r/   rK   r   r^   s      r   )test_deduct_balance_atomic_separate_modelzLWalletRepositorySeparateModelTests.test_deduct_balance_atomic_separate_model   s    #yy>>IILL'8$  ?  
 	 gh&78##%33WX5FGr   c                     | j                   j                  | j                  j                  d      }| j	                  |t        d             y)z1Test get_user_balance with separate wallet model.r   rY   N)r   r.   r@   rJ   r   r   rj   s     r   $test_get_user_balance_separate_modelzGWalletRepositorySeparateModelTests.test_get_user_balance_separate_model   s6    )),,TYY\\;KL'("34r   N)r2   r3   r4   r5   rB   rp   rr   rt   r6   r   r   rm   rm      s    :

HH5r   rm   c                   "    e Zd ZdZd Zd Zd Zy)&WalletRepositoryTransactionRecordTestsz&Tests for transaction record creation.c                 P   t         j                  j                  dd      | _        t	        t         ddi      | _        ddlm} dd	lm}  |d
dd| j                  j                  dt        d      t        d      dd|j                         j                         ddi      | _        y
)r:   r;   r<   r=   r   r   r   r   TransactionRecorddatetimeNcr+   i  zTest transactiontestdatarJ   wtypeiiduidtyperG   r0   
trans_typedescrcdate
extra_data)r   r,   r-   r@   r   r   wallet_utils.servicery   r{   rJ   r   now	isoformatrecord)r   ry   r{   s      r   rB   z,WalletRepositoryTransactionRecordTests.setUp   s    $$++ZGY+Z	$)1-
	 	;%'"		8$H%$,,.**,'
r   c                    | j                   j                  | j                        }| j                  |t               | j                  |d       ddlm} |j                  j                  |      }| j                  |j                  d       | j                  |j                  t        d             | j                  |j                  ddi       y)	z#Test creating a transaction record.r   )WalletTransaction)rJ   r   r+   r~   r   N)r   create_transaction_recordr   assertIsInstanceintassertGreaterwallet_utils.modelsr   r,   getr   r   rG   r   r   )r   transaction_idr   txns       r   test_create_transaction_recordzEWalletRepositoryTransactionRecordTests.test_create_transaction_record  s    <<T[[Inc2>1- 	:''++~+>$45WX%67&&)9:r   c                    ddl m } ddlm}  |ddd| j                  j                  dt        d      t        d      dd	|j                         i 
      }| j                  j                  |      }| j                  |t               y)z6Test creating transaction record with datetime object.r   rz   rx   Nr   r|   r}   z50.00Testr   )r{   r   ry   r@   rJ   r   r   r   r   r   r   )r   r{   ry   r   r   s        r   ,test_create_transaction_record_with_datetimezSWalletRepositoryTransactionRecordTests.test_create_transaction_record_with_datetime  sq    %:""		7#G$,,.
 <<VDnc2r   N)r2   r3   r4   r5   rB   r   r   r6   r   r   rv   rv      s    0
0;3r   rv   N)r5   decimalr   django.testr   wallet_utilsr   wallet_utils.exceptionsr   tests.modelsr   r   r	   r   r8   rm   rv   r6   r   r   <module>r      sS       ) 8 D D45( 45nw5X w5t,5 ,5^=3X =3r   