1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
# -*- coding: utf-8 -*-
from typing import ( # NOQA
Optional,
Set,
)
from django.conf import settings
from django.test.utils import override_settings
from tests.management.commands.shell_plus_tests.test_utils import AutomaticShellPlusImportsTestCase
from tests.test_module_in_project_dir import FourthDerivedClass
from tests.testapp.derived_classes_for_testing import SecondDerivedClass
from tests.testapp.derived_classes_for_testing.test_module import (
ClassWhichShouldNotBeImported,
ThirdDerivedClass,
)
from tests.testapp.classes_to_include import (
BaseIncludedClass,
FirstDerivedClass,
IncludedMixin,
)
class ImportSubclassesTestCase(AutomaticShellPlusImportsTestCase):
def test_imports_no_subclasses(self):
self.assert_imports()
@override_settings(
SHELL_PLUS_SUBCLASSES_IMPORT=[],
)
def test_imports_empty_list(self):
self.assert_imports()
@override_settings(
SHELL_PLUS_SUBCLASSES_IMPORT=[BaseIncludedClass],
)
def test_imports_one_base_class(self):
self.assert_imports(
first={'FirstDerivedClass'},
second={'SecondDerivedClass'},
fourth={'FourthDerivedClass'}
)
@override_settings(
SHELL_PLUS_SUBCLASSES_IMPORT=['tests.testapp.classes_to_include.BaseIncludedClass'],
)
def test_imports_one_base_class_as_string(self):
self.assert_imports(
first={'FirstDerivedClass'},
second={'SecondDerivedClass'},
fourth={'FourthDerivedClass'}
)
@override_settings(
SHELL_PLUS_SUBCLASSES_IMPORT=[IncludedMixin],
)
def test_imports_one_base_mixin(self):
self.assert_imports(
first={'FirstDerivedClass'},
third={'ThirdDerivedClass'},
)
@override_settings(
SHELL_PLUS_SUBCLASSES_IMPORT=[BaseIncludedClass, IncludedMixin],
)
def test_imports_two_base_classes(self):
self.assert_imports(
first={'FirstDerivedClass'},
second={'SecondDerivedClass'},
third={'ThirdDerivedClass'},
fourth={'FourthDerivedClass'}
)
@override_settings(
SHELL_PLUS_SUBCLASSES_IMPORT=[BaseIncludedClass, IncludedMixin],
SHELL_PLUS_SUBCLASSES_IMPORT_MODULES_BLACKLIST=settings.SHELL_PLUS_SUBCLASSES_IMPORT_MODULES_BLACKLIST + [
'tests.testapp'
]
)
def test_imports_two_base_classes_exclude_testapp(self):
self.assert_imports(
fourth={'FourthDerivedClass'},
)
@override_settings(
SHELL_PLUS_SUBCLASSES_IMPORT=[BaseIncludedClass, IncludedMixin],
SHELL_PLUS_SUBCLASSES_IMPORT_MODULES_BLACKLIST=settings.SHELL_PLUS_SUBCLASSES_IMPORT_MODULES_BLACKLIST + [
'tests.testapp.derived_classes_for_testing'
]
)
def test_imports_two_base_classes_exclude_derived_class_for_testing(self):
self.assert_imports(
first={'FirstDerivedClass'},
fourth={'FourthDerivedClass'},
)
@override_settings(
SHELL_PLUS_SUBCLASSES_IMPORT=[BaseIncludedClass, IncludedMixin],
SHELL_PLUS_SUBCLASSES_IMPORT_MODULES_BLACKLIST=settings.SHELL_PLUS_SUBCLASSES_IMPORT_MODULES_BLACKLIST + [
'tests.testapp.derived_classes_for_testing.test_module'
]
)
def test_imports_two_base_classes_exclude_test_module(self):
self.assert_imports(
first={'FirstDerivedClass'},
second={'SecondDerivedClass'},
fourth={'FourthDerivedClass'},
)
@override_settings(
SHELL_PLUS_SUBCLASSES_IMPORT=[BaseIncludedClass, IncludedMixin],
SHELL_PLUS_SUBCLASSES_IMPORT_MODULES_BLACKLIST=settings.SHELL_PLUS_SUBCLASSES_IMPORT_MODULES_BLACKLIST + [
'tests.test_module_in_project_dir'
]
)
def test_imports_two_base_classes_exclude_classes_in_project_dir(self):
self.assert_imports(
first={'FirstDerivedClass'},
second={'SecondDerivedClass'},
third={'ThirdDerivedClass'},
)
@override_settings(
SHELL_PLUS_SUBCLASSES_IMPORT=[BaseIncludedClass, IncludedMixin],
SHELL_PLUS_SUBCLASSES_IMPORT_MODULES_BLACKLIST=settings.SHELL_PLUS_SUBCLASSES_IMPORT_MODULES_BLACKLIST + [
'tests.testapp.classes_to_include'
]
)
def test_imports_two_base_classes_exclude_classes_in_classes_to_include(self):
self.assert_imports(
second={'SecondDerivedClass'},
third={'ThirdDerivedClass'},
fourth={'FourthDerivedClass'},
)
def assert_imports(self, first=None, second=None, third=None, fourth=None):
"""
Auxiliary assertion which checks are classes imported under names.
:param first: set of expected names under which FirstDerivedClass should be available
:param second: set of expected names under which SecondDerivedClass should be available
:param third: set of expected names under which ThirdDerivedClass should be available
:param fourth: set of expected names under which FourthDerivedClass should be available
"""
# type: (Optional[Set[str]], Optional[Set[str]], Optional[Set[str]], Optional[Set[str]]) -> ()
self.run_shell_plus()
self.assert_imported_under_names(FirstDerivedClass, first or set())
self.assert_imported_under_names(SecondDerivedClass, second or set())
self.assert_imported_under_names(ThirdDerivedClass, third or set())
self.assert_imported_under_names(FourthDerivedClass, fourth or set())
self.assert_imported_under_names(ClassWhichShouldNotBeImported, set())
|