Coverage for tests/django_tests/db/transactions/test_transactions.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-03-12 13:49 +0000

1from django_napse.core.models import Credit, ExchangeAccount, NapseSpace, Transaction 

2from django_napse.utils.constants import TRANSACTION_TYPES 

3from django_napse.utils.errors import TransactionError 

4from django_napse.utils.model_test_case import ModelTestCase 

5 

6""" 

7python tests/test_app/manage.py test tests.django_tests.transactions.test_transactions -v2 --keepdb --parallel 

8""" 

9 

10 

11class TransactionTestCase: 

12 model = Transaction 

13 

14 def setUp(self): 

15 self.from_wallet = self.space.wallet 

16 space = NapseSpace.objects.create( 

17 name="Test Space 2", 

18 exchange_account=self.exchange_account, 

19 description="This is a test space", 

20 ) 

21 Credit.objects.create(wallet=self.from_wallet, amount=10, ticker="BTC") 

22 Credit.objects.create(wallet=space.wallet, amount=10, ticker="BTC") 

23 self.to_wallet = space.wallet 

24 

25 def simple_create(self): 

26 return Transaction.objects.create( 

27 from_wallet=self.from_wallet, 

28 to_wallet=self.to_wallet, 

29 amount=1, 

30 ticker="BTC", 

31 transaction_type=TRANSACTION_TYPES.TRANSFER, 

32 ) 

33 

34 def test_error_same_space(self): 

35 exchange_account2 = ExchangeAccount.objects.create( 

36 exchange=self.exchange_account.exchange, 

37 testing=True, 

38 name="random exchange account 2", 

39 description="random description", 

40 ) 

41 space2 = NapseSpace.objects.create( 

42 name="Test Space 2", 

43 exchange_account=exchange_account2, 

44 description="This is a test space", 

45 ) 

46 wallet = space2.wallet 

47 with self.assertRaises(TransactionError.DifferentAccountError): 

48 Transaction.objects.create( 

49 from_wallet=self.from_wallet, 

50 to_wallet=wallet, 

51 amount=1, 

52 ticker="BTC", 

53 transaction_type=TRANSACTION_TYPES.TRANSFER, 

54 ) 

55 

56 def test_amount_zero(self): 

57 transaction = Transaction.objects.create( 

58 from_wallet=self.from_wallet, 

59 to_wallet=self.to_wallet, 

60 amount=0, 

61 ticker="BTC", 

62 transaction_type=TRANSACTION_TYPES.TRANSFER, 

63 ) 

64 self.assertIsNone(transaction) 

65 

66 def test_error_transaction_type(self): 

67 with self.assertRaises(TransactionError.InvalidTransactionError): 

68 Transaction.objects.create( 

69 from_wallet=self.from_wallet, 

70 to_wallet=self.to_wallet, 

71 amount=1, 

72 ticker="BTC", 

73 transaction_type="random transaction type", 

74 ) 

75 

76 

77class TransactionBINANCETestCase(TransactionTestCase, ModelTestCase): 

78 exchange = "BINANCE"