Coverage for tests/django_tests/db/fleets/test_fleet.py: 100%
53 statements
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-12 13:49 +0000
« prev ^ index » next coverage.py v7.4.3, created at 2024-03-12 13:49 +0000
1from django_napse.core.models import Bot, Controller, Credit, EmptyBotConfig, EmptyStrategy, Fleet, NapseSpace, SinglePairArchitecture
2from django_napse.utils.model_test_case import ModelTestCase
4"""
5python tests/test_app/manage.py test tests.django_tests.db.fleets.test_fleet -v2 --keepdb --parallel
6"""
9class FleetTestCase:
10 model = Fleet
12 def simple_create(self):
13 config = EmptyBotConfig.objects.create(space=self.space, settings={"empty": True})
14 controller = Controller.get(
15 exchange_account=self.exchange_account,
16 base="BTC",
17 quote="USDT",
18 interval="1m",
19 )
20 architecture = SinglePairArchitecture.objects.create(constants={"controller": controller})
21 strategy = EmptyStrategy.objects.create(config=config, architecture=architecture)
22 bot = Bot.objects.create(name="Test Bot", strategy=strategy)
23 return self.model.objects.create(
24 name="Test Fleet",
25 exchange_account=self.exchange_account,
26 clusters=[
27 {
28 "template_bot": bot,
29 "share": 0.7,
30 "breakpoint": 1000,
31 "autoscale": False,
32 },
33 {
34 "template_bot": bot,
35 "share": 0.3,
36 "breakpoint": 1000,
37 "autoscale": True,
38 },
39 ],
40 )
42 def test_invest(self):
43 Credit.objects.create(wallet=self.space.wallet, amount=1000, ticker="USDT")
44 fleet = self.simple_create()
45 connections = fleet.invest(self.space, 1000, "USDT")
46 self.assertEqual(self.space.wallet.get_amount("USDT"), 0)
47 self.assertEqual(connections[0].wallet.get_amount("USDT"), 700)
48 self.assertEqual(connections[1].wallet.get_amount("USDT"), 300)
50 def test_invest_twice(self):
51 Credit.objects.create(wallet=self.space.wallet, amount=2000, ticker="USDT")
52 fleet = self.simple_create()
53 fleet.invest(self.space, 1000, "USDT")
54 connections = fleet.invest(self.space, 1000, "USDT")
55 self.assertEqual(self.space.wallet.get_amount("USDT"), 0)
56 self.assertEqual(connections[0].wallet.get_amount("USDT"), 1400)
57 self.assertEqual(connections[1].wallet.get_amount("USDT"), 600)
59 def _two_spaces_on_same_fleet(self):
60 fleet = self.simple_create()
62 space = NapseSpace.objects.create(name="Random Space", exchange_account=self.exchange_account, description="This is a test space")
63 Credit.objects.create(wallet=space.wallet, amount=2000, ticker="USDT")
64 fleet.invest(space, 1000, "USDT")
66 Credit.objects.create(wallet=self.space.wallet, amount=2000, ticker="USDT")
67 fleet.invest(self.space, 1000, "USDT")
69 return fleet
71 def test_value(self):
72 Credit.objects.create(wallet=self.space.wallet, amount=1000, ticker="USDT")
73 fleet = self.simple_create()
74 fleet.invest(self.space, 1000, "USDT")
75 self.assertEqual(fleet.value, 1000)
77 def test_value_with_two_spaces(self):
78 fleet = self._two_spaces_on_same_fleet()
79 self.assertEqual(fleet.value, 2000)
81 def test_space_frame_value(self):
82 Credit.objects.create(wallet=self.space.wallet, amount=1000, ticker="USDT")
83 fleet = self.simple_create()
84 fleet.invest(self.space, 1000, "USDT")
85 self.assertEqual(fleet.space_frame_value(self.space), 1000)
87 def test_space_frame_value_with_two_spaces(self):
88 fleet = self._two_spaces_on_same_fleet()
89 self.assertEqual(fleet.space_frame_value(self.space), 1000)
92class FleetBINANCETestCase(FleetTestCase, ModelTestCase):
93 exchange = "BINANCE"