Coverage for tests/django_tests/db/test_validation.py: 62%

34 statements  

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

1from contextlib import suppress 

2 

3from django.test import TestCase 

4 

5from django_napse.core.settings import napse_settings 

6from django_napse.utils.model_test_case import ModelTestCase 

7 

8""" 

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

10""" 

11 

12 

13class ValidateModelTestCaseSubclassesTestCase(TestCase): 

14 def test_naming_convention(self): 

15 for subclass in ModelTestCase.__subclasses__(): 

16 if not subclass.__name__.endswith("TestCase"): 

17 error_msg = f"ModelTestCase subclass {subclass.__name__} name must end with TestCase." 

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

19 raise ValueError(error_msg) 

20 

21 def test_all_exchanges_are_tested(self): 

22 all_tests = {} 

23 for subclass in ModelTestCase.__subclasses__(): 

24 with suppress(AttributeError): 

25 if subclass.skip_exchange_validation: 

26 continue 

27 if subclass.exchange is None: 

28 error_msg = f"ModelTestCase subclass {subclass.__name__} must have an exchange attribute." 

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

30 raise ValueError(error_msg) 

31 

32 if subclass.__name__.endswith(f"{subclass.exchange}TestCase"): 

33 subclass_name = subclass.__name__.replace(f"{subclass.exchange}TestCase", "") 

34 all_tests[subclass_name] = [*all_tests.get(subclass_name, []), subclass.exchange] 

35 

36 else: 

37 error_msg = f"ModelTestCase subclass {subclass.__name__} must end with <Exchange>TestCase." 

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

39 raise ValueError(error_msg) 

40 

41 for subclass_name, exchanges in all_tests.items(): 

42 if exchanges != napse_settings.NAPSE_EXCHANGES_TO_TEST: 

43 print(exchanges, napse_settings.NAPSE_EXCHANGES_TO_TEST) 

44 diff = [exchange for exchange in napse_settings.NAPSE_EXCHANGES_TO_TEST if exchange not in exchanges] 

45 error_msg = f"ModelTestCase subclass {subclass_name} is missing tests for exchanges {diff}" 

46 raise ValueError(error_msg)