Coverage for django_napse/utils/custom_test_case.py: 83%

29 statements  

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

1from django.test import TestCase 

2 

3from django_napse.core.models import EXCHANGE_ACCOUNT_DICT, NapseSpace 

4from django_napse.core.settings import napse_settings 

5 

6 

7class CustomTestCase(TestCase): 

8 exchange = None 

9 

10 def skip_condition(self): 

11 if self.__class__.__name__ == "ModelTestCase": 

12 return True, "because ModelTestCase (base class) tests shouldn't be run." 

13 if self.__class__.__name__ == "APITestCase": 

14 return True, "because ModelTestCase (base class) tests shouldn't be run." 

15 

16 if not self.__class__.__name__.endswith(f"{self.exchange}TestCase"): 

17 error_msg = f"CustomTestCase subclass {self.__class__.__name__} name must end with <Exchange>TestCase." 

18 error_msg += " Make sure you are writing tests as specified in the docs." 

19 if self.exchange is not None and self.exchange not in napse_settings.NAPSE_EXCHANGES_TO_TEST: 

20 return True, f"because {self.exchange} is not in {napse_settings.NAPSE_EXCHANGES_TO_TEST}" 

21 return False, "" 

22 

23 @property 

24 def exchange_account(self): 

25 try: 

26 return EXCHANGE_ACCOUNT_DICT[self.exchange].objects.get(name=f"{self.exchange} Test Account") 

27 except KeyError as e: 

28 error_msg = f"Exchange {self.exchange} isn't a valid choice." 

29 raise ValueError(error_msg) from e 

30 

31 @property 

32 def space(self): 

33 try: 

34 return NapseSpace.objects.get(name="Test Space", exchange_account=self.exchange_account, description="This is a test space") 

35 except NapseSpace.DoesNotExist: 

36 return NapseSpace.objects.create(name="Test Space", exchange_account=self.exchange_account, description="This is a test space")