diff --git a/opentech/apply/categories/blocks.py b/opentech/apply/categories/blocks.py index ecd61dc8c8151f7f7332db1bff15624c6fe2604e..b51e9e2651a1bd080a1dc29e63f96fe5a64bbccc 100644 --- a/opentech/apply/categories/blocks.py +++ b/opentech/apply/categories/blocks.py @@ -42,11 +42,11 @@ class CategoryQuestionBlock(FormFieldBlock): return forms.ChoiceField def use_defaults_from_category(self, kwargs, category): - map = {'field_label': 'label', 'help_text': 'help_text'} + category_fields = {'label': 'name', 'help_text': 'help_text'} - for field in ['field_label', 'help_text']: - if field not in kwargs: - kwargs[map[field]] = getattr(category, field) + for field in category_fields.keys(): + if not kwargs.get(field): + kwargs[field] = getattr(category, category_fields[field]) return kwargs diff --git a/opentech/apply/categories/models.py b/opentech/apply/categories/models.py index 525cb37f7313e92478acc32fcb8164f84f6ad3c6..8b223aa5cf2a474be9064e1110dc53977ceb245d 100644 --- a/opentech/apply/categories/models.py +++ b/opentech/apply/categories/models.py @@ -22,10 +22,6 @@ class Category(ClusterableModel): name = models.CharField(max_length=255) help_text = models.CharField(max_length=255, blank=True) - @property - def field_label(self): - return self.name - panels = [ FieldPanel('name'), InlinePanel('options', label='Options'), diff --git a/opentech/apply/categories/tests.py b/opentech/apply/categories/tests.py deleted file mode 100644 index a79ca8be565f44aacce95bad20c1ee34d175ed20..0000000000000000000000000000000000000000 --- a/opentech/apply/categories/tests.py +++ /dev/null @@ -1,3 +0,0 @@ -# from django.test import TestCase - -# Create your tests here. diff --git a/opentech/apply/categories/tests/__init__.py b/opentech/apply/categories/tests/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/opentech/apply/categories/tests/factories.py b/opentech/apply/categories/tests/factories.py new file mode 100644 index 0000000000000000000000000000000000000000..b311c2c6daef14a82a394a77088e75982d092004 --- /dev/null +++ b/opentech/apply/categories/tests/factories.py @@ -0,0 +1,19 @@ +import factory + +from ..models import Category, Option + + +class CategoryFactory(factory.DjangoModelFactory): + class Meta: + model = Category + + name = factory.Faker('word') + help_text = factory.Faker('sentence') + + +class OptionFactory(factory.DjangoModelFactory): + class Meta: + model = Option + + value = factory.Faker('word') + category = factory.SubFactory(CategoryFactory) diff --git a/opentech/apply/categories/tests/test_blocks.py b/opentech/apply/categories/tests/test_blocks.py new file mode 100644 index 0000000000000000000000000000000000000000..fa8bee4ea1240337661588cc3ccbc5215c840076 --- /dev/null +++ b/opentech/apply/categories/tests/test_blocks.py @@ -0,0 +1,54 @@ +from django import forms +from django.test import TestCase + +from opentech.apply.categories.blocks import CategoryQuestionBlock + +from .factories import CategoryFactory, OptionFactory + + +class TestCategoryQuestionBlock(TestCase): + @classmethod + def setUp(self): + self.category = CategoryFactory() + self.block = CategoryQuestionBlock() + + def get_field(self, **kwargs): + data = { + 'field_label': '', + 'help_text': '', + 'category': self.category.id, + 'multi': False, + } + data.update(kwargs) + + block = self.block.to_python(data) + + return self.block.get_field(block) + + def test_field_and_help_default(self): + field = self.get_field(field_label='', help_text='') + self.assertEqual(self.category.name, field.label) + self.assertEqual(self.category.help_text, field.help_text) + + def test_supplied_field_and_help(self): + values = {'field_label': 'LABEL', 'help_text': 'HELP'} + field = self.get_field(**values) + self.assertEqual(values['field_label'], field.label) + self.assertEqual(values['help_text'], field.help_text) + + def test_multi_select_enabled(self): + field = self.get_field(multi=True) + self.assertTrue(isinstance(field, forms.MultipleChoiceField)) + + def test_multi_select_disabled(self): + field = self.get_field(multi=True) + self.assertTrue(isinstance(field, forms.ChoiceField)) + + def test_options_included_in_choices(self): + # Don't assign to variable as the ordering wont match choices + OptionFactory.create_batch(3, category=self.category) + field = self.get_field() + self.assertEqual( + field.choices, + [(option.id, option.value) for option in self.category.options.all()] + )