Coverage for tests/django_tests/db/accounts/test_exchange.py: 95%

55 statements  

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

1import json 

2from typing import TYPE_CHECKING 

3from unittest import skipIf 

4 

5from django_napse.core.models import BinanceAccount, Exchange, ExchangeAccount 

6from django_napse.core.settings import napse_settings 

7from django_napse.utils.errors import ExchangeAccountError 

8from django_napse.utils.model_test_case import ModelTestCase 

9 

10if TYPE_CHECKING: 

11 from pathlib import Path 

12 

13""" 

14python tests/test_app/manage.py test tests.django_tests.db.accounts.test_exchange -v2 --keepdb --parallel 

15""" 

16 

17 

18class ExchangeTestCase(ModelTestCase): 

19 model = Exchange 

20 skip_exchange_validation = True 

21 

22 def simple_create(self): 

23 return self.model.objects.create( 

24 name="random exchange", 

25 description="random description", 

26 ) 

27 

28 def test_default_exchanges(self): 

29 base_configs = napse_settings.NAPSE_EXCHANGE_CONFIGS 

30 all_exchanges = Exchange.objects.all() 

31 

32 self.assertEqual([exchange.name for exchange in all_exchanges], list(base_configs.keys())) 

33 

34 def test_default_exchange_accounts(self): 

35 path: Path = napse_settings.NAPSE_SECRETS_FILE_PATH 

36 json_file = path.open("r") 

37 secrets = json.load(json_file) 

38 json_file.close() 

39 for exchange_id, exchange_secrets in secrets["Exchange Accounts"].items(): 

40 exchange_name = exchange_secrets.pop("exchange") 

41 exchange = Exchange.objects.get(name=exchange_name) 

42 exchange_account = ExchangeAccount.objects.get(exchange=exchange, name=exchange_id) 

43 

44 self.assertEqual(exchange_account.name, exchange_id) 

45 self.assertEqual(exchange_account.exchange.name, exchange_name) 

46 

47 exchange_account_real_model = exchange_account.find() 

48 

49 for key, value in exchange_secrets.items(): 

50 self.assertEqual(getattr(exchange_account_real_model, key), value) 

51 

52 

53class BaseExchangeAccountTestCase(ModelTestCase): 

54 model = ExchangeAccount 

55 skip_exchange_validation = True 

56 

57 def simple_create(self): 

58 exchange = Exchange.objects.create( 

59 name="random exchange", 

60 description="random description", 

61 ) 

62 return self.model.objects.create( 

63 exchange=exchange, 

64 testing=True, 

65 name="random exchange account 1", 

66 description="random description", 

67 ) 

68 

69 

70class ExchangeUtilsTestCase: 

71 @skipIf(napse_settings.NAPSE_IS_IN_PIPELINE, "No creadentials in pipelines") 

72 def test_ping(self): 

73 for exchange_account in self.model.objects.all(): 

74 exchange_account.ping() 

75 

76 def test_enough_accounts(self): 

77 self.assertTrue(self.model.objects.count() >= 1) 

78 

79 def test_error_in_ping(self): 

80 with self.assertRaises(ExchangeAccountError.APIPermissionError): 

81 self.simple_create().ping() 

82 

83 

84class AccountUtilsBINANCETestCase(ExchangeUtilsTestCase, ModelTestCase): 

85 model = BinanceAccount 

86 exchange = "BINANCE" 

87 

88 def simple_create(self): 

89 exchange = Exchange.objects.create( 

90 name="random exchange", 

91 description="random description", 

92 ) 

93 return self.model.objects.create( 

94 exchange=exchange, 

95 testing=True, 

96 name="random exchange account 1", 

97 description="random description", 

98 private_key="random private key", 

99 public_key="random public key", 

100 )