Coverage for tests/django_tests/db/wallets/test_wallet.py: 100%

112 statements  

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

1from django.db.utils import IntegrityError 

2from django.test import TestCase 

3 

4from django_napse.core.models import Currency, SpaceWallet, Wallet 

5from django_napse.utils.errors import WalletError 

6from django_napse.utils.model_test_case import ModelTestCase 

7 

8""" 

9python tests/test_app/manage.py test tests.django_tests.wallets.test_wallet -v2 --keepdb --parallel 

10""" 

11 

12 

13class WalletTestCase(TestCase): 

14 def test_cant_create_wallet(self): 

15 with self.assertRaises(WalletError.CreateError): 

16 Wallet.objects.create(title="Test Wallet", owner=None) 

17 

18 

19class BaseWalletTestCase: 

20 def simple_create(self): 

21 return self.owner.wallet 

22 

23 def test_error_duplicate_wallet(self): 

24 with self.assertRaises(IntegrityError): 

25 self.model.objects.create(title="Test Wallet", owner=self.owner) 

26 

27 def test_property_testing(self): 

28 wallet = self.simple_create() 

29 self.assertTrue(wallet.testing) 

30 

31 # Spend 

32 def test_spend_security(self): 

33 wallet = self.simple_create() 

34 with self.assertRaises(WalletError.SpendError): 

35 wallet.spend(ticker="BTC", amount=0.5) 

36 

37 def test_force_spend(self): 

38 wallet = self.simple_create() 

39 Currency.objects.create(wallet=wallet, ticker="BTC", amount=1, mbp=20000) 

40 wallet.spend(ticker="BTC", amount=0.5, force=True) 

41 

42 def test_insufficient_funds(self): 

43 wallet = self.simple_create() 

44 Currency.objects.create(wallet=wallet, ticker="BTC", amount=1, mbp=20000) 

45 with self.assertRaises(WalletError.SpendError): 

46 wallet.spend(ticker="BTC", amount=2, force=True) 

47 

48 def test_non_existing_funds(self): 

49 wallet = self.simple_create() 

50 Currency.objects.create(wallet=wallet, ticker="BTC", amount=1, mbp=20000) 

51 with self.assertRaises(WalletError.SpendError): 

52 wallet.spend(ticker="ETH", amount=2, force=True) 

53 

54 def test_spend_negative(self): 

55 wallet = self.simple_create() 

56 with self.assertRaises(ValueError): 

57 wallet.spend(ticker="BTC", amount=-2, force=True) 

58 

59 def test_error_spend_locked(self): 

60 wallet = self.simple_create() 

61 wallet.locked = True 

62 wallet.save() 

63 with self.assertRaises(TimeoutError): 

64 wallet.spend(ticker="BTC", amount=2, force=True) 

65 

66 # Top Up 

67 def test_topup_security(self): 

68 wallet = self.simple_create() 

69 Currency.objects.create(wallet=wallet, ticker="BTC", amount=1, mbp=20000) 

70 with self.assertRaises(WalletError.TopUpError): 

71 wallet.top_up(ticker="BTC", amount=0.5) 

72 

73 def test_force_topup(self): 

74 wallet = self.simple_create() 

75 Currency.objects.create(wallet=wallet, ticker="BTC", amount=1, mbp=20000) 

76 wallet.top_up(ticker="BTC", amount=0.5, mbp=1, force=True) 

77 

78 def test_error_topup_locked(self): 

79 wallet = self.simple_create() 

80 wallet.locked = True 

81 wallet.save() 

82 with self.assertRaises(TimeoutError): 

83 wallet.top_up(ticker="BTC", amount=2, mbp=1, force=True) 

84 

85 def test_topup_negative(self): 

86 wallet = self.simple_create() 

87 with self.assertRaises(ValueError): 

88 wallet.top_up(ticker="BTC", amount=-2, mbp=1, force=True) 

89 

90 # Other 

91 def test_exchange_account(self): 

92 wallet = self.simple_create() 

93 self.assertEqual(wallet.exchange_account, self.owner.exchange_account) 

94 

95 def test_has_funds(self): 

96 wallet = self.simple_create() 

97 self.assertFalse(wallet.has_funds(ticker="BTC", amount=1)) 

98 Currency.objects.create(wallet=wallet, ticker="BTC", amount=1, mbp=20000) 

99 self.assertTrue(wallet.has_funds(ticker="BTC", amount=1)) 

100 

101 def test_get_amout(self): 

102 wallet = self.simple_create() 

103 self.assertEqual(wallet.get_amount(ticker="BTC"), 0) 

104 Currency.objects.create(wallet=wallet, ticker="BTC", amount=1, mbp=20000) 

105 self.assertEqual(wallet.get_amount(ticker="BTC"), 1) 

106 

107 def test_value_mbp(self): 

108 wallet = self.simple_create() 

109 self.assertEqual(wallet.value_mbp(), 0) 

110 Currency.objects.create(wallet=wallet, ticker="BTC", amount=1, mbp=20000) 

111 Currency.objects.create(wallet=wallet, ticker="ETH", amount=0, mbp=1000) 

112 self.assertEqual(wallet.value_mbp(), 20000) 

113 

114 def test_to_dict(self): 

115 wallet = self.simple_create() 

116 Currency.objects.create(wallet=wallet, ticker="BTC", amount=1, mbp=20000) 

117 Currency.objects.create(wallet=wallet, ticker="USDT", amount=1, mbp=1) 

118 self.assertEqual( 

119 wallet.to_dict(), 

120 { 

121 "title": "Wallet for space Test Space", 

122 "testing": True, 

123 "locked": False, 

124 "created_at": wallet.created_at, 

125 "currencies": {"BTC": {"amount": 1.0, "mbp": 20000.0}, "USDT": {"amount": 1.0, "mbp": 1.0}}, 

126 }, 

127 ) 

128 

129 

130class SpaceWalletTestCase(BaseWalletTestCase): 

131 model = SpaceWallet 

132 

133 def setUp(self): 

134 super().setUp() 

135 self.owner = self.space 

136 

137 def test_with_real_exchange(self): 

138 self.space.wallet.top_up(ticker="BTC", amount=0.5, force=True) 

139 

140 def test_space(self): 

141 wallet = self.simple_create() 

142 self.assertEqual(wallet.space, self.owner) 

143 

144 def test_value_market_BTC(self): # noqa: N802 

145 Currency.objects.create(wallet=self.space.wallet, ticker="BTC", amount=1, mbp=20000) 

146 self.assertTrue(self.space.wallet.value_market() > 0) 

147 

148 def test_value_market_USDT(self): # noqa: N802 

149 Currency.objects.create(wallet=self.space.wallet, ticker="USDT", amount=1, mbp=1) 

150 self.assertEqual(self.space.wallet.value_market(), 1) 

151 

152 def test_value_zero(self): 

153 Currency.objects.create(wallet=self.space.wallet, ticker="BTC", amount=0, mbp=1) 

154 Currency.objects.create(wallet=self.space.wallet, ticker="USDT", amount=0, mbp=1) 

155 self.assertEqual(self.space.wallet.value_market(), 0) 

156 

157 

158class SpaceWalletBINANCETestCase(SpaceWalletTestCase, ModelTestCase): 

159 exchange = "BINANCE" 

160 

161 

162# class OrderWalletTestCase(BaseWalletTestCase): 

163# model = OrderWallet 

164 

165# def setUp(self) -> None: 

166# super().setUp() 

167# config = EmptyBotConfig.objects.create(space=self.space, settings={"empty": True}) 

168# fleet = Fleet.objects.create(name="test_fleet", configs=[config], exchange_account=self.exchange_account) 

169# bot = fleet.bots.first() 

170# self.owner = Order.objects.create(bot=bot, buy_amount=100, sell_amount=100, price=1) 

171# def test_space(self): 

172# wallet = self.simple_create() 

173# self.assertEqual(wallet.space, self.owner.space) 

174 

175 

176# class OrderWalletBINANCETestCase(OrderWalletTestCase, ModelTestCase): 

177# exchange = "BINANCE" 

178 

179 

180# class ConnectionWalletTestCase(BaseWalletTestCase, TestCase): 

181# wallet_class = ConnectionWallet 

182 

183# def setUp(self) -> None: 

184# self.owner = Conne.objects.create(name="Test Space", testing=True) 

185# self.owner = Conne.objects.create(name="Test Space", testing=True) 

186# self.owner = Conne.objects.create(name="Test Space", testing=True)