Skip to content

Commit ca190d7

Browse files
committed
Fixed general tests for Python 3.2.
1 parent 8055a55 commit ca190d7

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

tests/test_general.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,20 @@
1616
class GeneralTests(TestCase):
1717

1818
@classmethod
19-
def _find_subclasses_for_package_py32(cls, base_class, package):
19+
def _find_localflavor_subclasses_py32(cls, base_class):
2020
classes = []
2121
for attr in dir(localflavor):
2222
if attr.startswith('_'):
2323
continue
24-
sub_module = importlib.import_module(package.__name__ + '.' + attr)
25-
sub_module_fields = cls._find_subclasses_for_package(base_class, sub_module)
26-
if len(sub_module_fields) > 0:
27-
classes.extend(sub_module_fields)
28-
24+
sub_module = importlib.import_module(localflavor.__name__ + '.' + attr)
25+
if hasattr(sub_module, '__path__'):
26+
sub_module_fields = cls._find_subclasses_for_package(base_class, sub_module)
27+
if len(sub_module_fields) > 0:
28+
classes.extend(sub_module_fields)
2929
return classes
3030

3131
@classmethod
3232
def _find_subclasses_for_package(cls, base_class, package):
33-
# Finding the localflavor model classes directly with walk_packages doesn't work with Python 3.2. The workaround
34-
# is to find the classes in all of the submodules.
35-
if sys.version_info[:2] == (3, 2):
36-
return cls._find_subclasses_for_package_py32(base_class, package)
37-
3833
classes = []
3934
for importer, modname, ispkg in pkgutil.walk_packages(path=package.__path__, prefix=package.__name__ + '.',
4035
onerror=lambda x: None):
@@ -58,7 +53,12 @@ def test_model_field_deconstruct_methods_with_default_options(self):
5853
# This test can only check the choices and max_length options. Specific tests are required for model fields
5954
# with options that users can set. See to the IBAN tests for an example.
6055

61-
model_classes = self._find_subclasses_for_package(models.Field, localflavor)
56+
# Finding the localflavor model classes directly with walk_packages doesn't work with Python 3.2. The workaround
57+
# is to find the classes in all of the submodules.
58+
if sys.version_info[:2] == (3, 2):
59+
model_classes = self._find_localflavor_subclasses_py32(models.Field)
60+
else:
61+
model_classes = self._find_subclasses_for_package(models.Field, localflavor)
6262
self.assertTrue(len(model_classes) > 0, 'No localflavor models.Field classes were found.')
6363

6464
for cls in model_classes:
@@ -81,7 +81,12 @@ def test_model_field_deconstruct_methods_with_default_options(self):
8181
self.assertEqual(getattr(test_instance, attr), getattr(new_instance, attr))
8282

8383
def test_forms_char_field_empty_value_allows_none(self):
84-
form_classes = self._find_subclasses_for_package(forms.CharField, localflavor)
84+
# Finding the localflavor model classes directly with walk_packages doesn't work with Python 3.2. The workaround
85+
# is to find the classes in all of the submodules.
86+
if sys.version_info[:2] == (3, 2):
87+
form_classes = self._find_localflavor_subclasses_py32(forms.CharField)
88+
else:
89+
form_classes = self._find_subclasses_for_package(forms.CharField, localflavor)
8590
self.assertTrue(len(form_classes) > 0, 'No localflavor forms.CharField classes were found.')
8691

8792
for cls in form_classes:

0 commit comments

Comments
 (0)