Coverage for tests/django_tests/api/fleets/test_fleet_view.py: 42%

40 statements  

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

1from django_napse.api.fleets import FleetView 

2from django_napse.core.models import Bot, Controller, EmptyBotConfig, EmptyStrategy, Fleet, SinglePairArchitecture 

3from django_napse.utils.api_test_case import APITestCase, ViewTest 

4from django_napse.utils.dict_comparison import compare_responses 

5 

6""" 

7python tests/test_app/manage.py test tests.django_tests.api.fleets.test_fleet_view -v2 --keepdb --parallel 

8""" 

9 

10 

11class ListFleetViewTest(ViewTest): 

12 def __init__(self, *args, **kwargs): 

13 """Build a fleet.""" 

14 super().__init__(*args, **kwargs) 

15 # create simple fleet 

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

17 # config = EmptyBotConfig.objects.get(space=self.testcase_instance.space) 

18 print(f"config: {config}") 

19 controller = Controller.get( 

20 exchange_account=self.testcase_instance.exchange_account, 

21 base="BTC", 

22 quote="USDT", 

23 interval="1m", 

24 ) 

25 architecture = SinglePairArchitecture.objects.create(constants={"controller": controller}) 

26 strategy = EmptyStrategy.objects.create(config=config, architecture=architecture) 

27 bot = Bot.objects.create(name="Test Bot", strategy=strategy) 

28 self.fleet = Fleet.objects.create( 

29 name="Test Fleet", 

30 exchange_account=self.testcase_instance.exchange_account, 

31 clusters=[ 

32 { 

33 "template_bot": bot, 

34 "share": 0.7, 

35 "breakpoint": 1000, 

36 "autoscale": False, 

37 }, 

38 { 

39 "template_bot": bot, 

40 "share": 0.3, 

41 "breakpoint": 1000, 

42 "autoscale": True, 

43 }, 

44 ], 

45 ) 

46 

47 def check_response(self, response) -> bool: 

48 return compare_responses( 

49 response.data[0], 

50 { 

51 "uuid": str(), 

52 "name": str(), 

53 "value": float(), 

54 "bot_count": int(), 

55 }, 

56 ) 

57 

58 def run(self): 

59 print("list run") 

60 return [ 

61 { 

62 "name": "response", 

63 "setup": self.setup(), 

64 "status_code": 200, 

65 "checks": [self.check_response], 

66 }, 

67 ] 

68 

69 

70class RetrieveFleetViewTest(ViewTest): 

71 def __init__(self, *args, **kwargs): 

72 """Build a fleet.""" 

73 super().__init__(*args, **kwargs) 

74 # create simple fleet 

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

76 controller = Controller.get( 

77 exchange_account=self.testcase_instance.exchange_account, 

78 base="BTC", 

79 quote="USDT", 

80 interval="1m", 

81 ) 

82 architecture = SinglePairArchitecture.objects.create(constants={"controller": controller}) 

83 strategy = EmptyStrategy.objects.create(config=config, architecture=architecture) 

84 bot = Bot.objects.create(name="Test Bot", strategy=strategy) 

85 self.fleet = Fleet.objects.create( 

86 name="Test Fleet", 

87 exchange_account=self.testcase_instance.exchange_account, 

88 clusters=[ 

89 { 

90 "template_bot": bot, 

91 "share": 0.7, 

92 "breakpoint": 1000, 

93 "autoscale": False, 

94 }, 

95 { 

96 "template_bot": bot, 

97 "share": 0.3, 

98 "breakpoint": 1000, 

99 "autoscale": True, 

100 }, 

101 ], 

102 ) 

103 

104 def check_response(self, response) -> bool: 

105 return compare_responses( 

106 response.data, 

107 { 

108 "uuid": str(), 

109 "name": str(), 

110 "created_at": str(), 

111 "statistics": { 

112 "value": float(), 

113 "order_count_30": float(), 

114 "change_30": None, 

115 }, 

116 "wallet": {}, 

117 # "history": {}, 

118 "bots": [], 

119 }, 

120 authorize_abstract_comparison=True, 

121 ) 

122 

123 def run(self): 

124 print("retrieve run") 

125 return [ 

126 { 

127 "name": "response", 

128 "setup": self.setup(), 

129 "status_code": 200, 

130 "checks": [self.check_response], 

131 }, 

132 ] 

133 

134 

135class FleetViewTestCase: 

136 # def test_list(self): 

137 # self.run_tests("list") 

138 

139 # def test_retrieve(self): 

140 # self.run_tests("list") 

141 

142 def setup(self): 

143 list_test = ListFleetViewTest(self) 

144 # retrieve_test = RetrieveFleetViewTest(self) 

145 

146 return { 

147 "list": { 

148 "url_name": "fleet-list", 

149 "view": FleetView, 

150 "method": "GET", 

151 "tests": list_test.run(), 

152 }, 

153 # "retrieve": { 

154 # "url_name": "fleet-detail", 

155 # "view": FleetView, 

156 # "method": "GET", 

157 # "kwargs": {"pk": self.retrieve_test.fleet.uuid}, 

158 # "tests": retrieve_test.run(), 

159 # }, 

160 } 

161 

162 

163class FleetViewAPIBinanceTestCase(FleetViewTestCase, APITestCase): 

164 exchange = "BINANCE"