|
| 1 | +from unittest import SkipTest |
| 2 | +from django.test import TestCase |
| 3 | + |
| 4 | +from .models import ( |
| 5 | + Block, BlockMarkdown, BlockRadioGroup, BlockRadioButton, |
| 6 | + Questionnaire, SurveyStep) |
| 7 | + |
| 8 | + |
| 9 | +try: |
| 10 | + from nested_admin.tests.nested_polymorphic.base import BaseNestedPolymorphicTestCase |
| 11 | +except ImportError: |
| 12 | + BaseNestedPolymorphicTestCase = TestCase |
| 13 | + has_polymorphic = False |
| 14 | +else: |
| 15 | + has_polymorphic = True |
| 16 | + |
| 17 | + |
| 18 | +class PolymorphicMixedNestingTestCase(BaseNestedPolymorphicTestCase): |
| 19 | + """ |
| 20 | + Tests for polymorphic inlines that are nested inside non-polymorphic inlines. |
| 21 | + """ |
| 22 | + root_model = Questionnaire |
| 23 | + |
| 24 | + @classmethod |
| 25 | + def setUpClass(cls): |
| 26 | + if not has_polymorphic: |
| 27 | + raise SkipTest('django-polymorphic unavailable') |
| 28 | + super(PolymorphicMixedNestingTestCase, cls).setUpClass() |
| 29 | + |
| 30 | + def test_polymorphic_child_formset_rendering(self): |
| 31 | + """The admin should only display one child inline for polymorphic instances""" |
| 32 | + obj = self.root_model.objects.create(title='test') |
| 33 | + self.load_admin(obj) |
| 34 | + step_indexes = self.add_inline(model=SurveyStep, title='Step 1') |
| 35 | + self.add_inline(step_indexes, model=BlockMarkdown, value="Some instructions") |
| 36 | + group_indexes = self.add_inline(step_indexes, model=BlockRadioGroup, label="Select one") |
| 37 | + self.add_inline(group_indexes, BlockRadioButton, label="Choice 1") |
| 38 | + self.add_inline(group_indexes, BlockRadioButton, label="Choice 2") |
| 39 | + |
| 40 | + inline_ids = [ |
| 41 | + el.get_attribute('id') for el in |
| 42 | + self.selenium.find_elements_by_css_selector( |
| 43 | + '.djn-group:not([id*="-empty-"]')] |
| 44 | + |
| 45 | + expected_inline_ids = [ |
| 46 | + "surveystep_set-group", |
| 47 | + "surveystep_set-0-block_set-group", |
| 48 | + "surveystep_set-0-block_set-1-blockradiobutton_set-group"] |
| 49 | + |
| 50 | + assert inline_ids == expected_inline_ids |
| 51 | + |
| 52 | + self.save_form() |
| 53 | + |
| 54 | + inline_ids = [ |
| 55 | + el.get_attribute('id') for el in |
| 56 | + self.selenium.find_elements_by_css_selector( |
| 57 | + '.djn-group:not([id*="-empty-"]')] |
| 58 | + |
| 59 | + assert "surveystep_set-0-block_set-0-blockradiobutton_set-group" not in inline_ids, ( |
| 60 | + "Nested polymorphic model erroneously has inlines for both child models") |
| 61 | + assert inline_ids == expected_inline_ids |
| 62 | + |
| 63 | + def test_add_step_to_empty_survey(self): |
| 64 | + survey = self.root_model.objects.create(title='test') |
| 65 | + self.load_admin(survey) |
| 66 | + self.add_inline(model=SurveyStep, title='Step 1') |
| 67 | + self.save_form() |
| 68 | + |
| 69 | + steps = survey.surveystep_set.all() |
| 70 | + |
| 71 | + assert len(steps) == 1 |
| 72 | + assert steps[0].title == "Step 1" |
| 73 | + |
| 74 | + def test_add_blocks_to_empty_survey(self): |
| 75 | + survey = self.root_model.objects.create(title='test') |
| 76 | + self.load_admin(survey) |
| 77 | + step_indexes = self.add_inline(model=SurveyStep, title='Step 1') |
| 78 | + self.add_inline(step_indexes, model=BlockMarkdown, value="Some instructions") |
| 79 | + group_indexes = self.add_inline(step_indexes, model=BlockRadioGroup, label="Select one") |
| 80 | + self.add_inline(group_indexes, BlockRadioButton, label="Choice 1") |
| 81 | + self.add_inline(group_indexes, BlockRadioButton, label="Choice 2") |
| 82 | + self.save_form() |
| 83 | + |
| 84 | + steps = survey.surveystep_set.all() |
| 85 | + |
| 86 | + assert len(steps) == 1 |
| 87 | + assert steps[0].title == "Step 1" |
| 88 | + |
| 89 | + blocks = Block.objects.filter(survey_step=steps[0]) |
| 90 | + assert len(blocks) == 2 |
| 91 | + assert isinstance(blocks[0], BlockMarkdown) |
| 92 | + assert isinstance(blocks[1], BlockRadioGroup) |
| 93 | + assert blocks[0].value == 'Some instructions' |
| 94 | + assert blocks[1].label == 'Select one' |
| 95 | + |
| 96 | + buttons = blocks[1].blockradiobutton_set.all() |
| 97 | + assert len(buttons) == 2 |
| 98 | + assert list(buttons.values_list('label', flat=True)) == ["Choice 1", "Choice 2"] |
| 99 | + |
| 100 | + def test_add_blocks_to_existing_survey(self): |
| 101 | + survey = self.root_model.objects.create(title='test') |
| 102 | + step = SurveyStep.objects.create(survey=survey, title='Step 1', position=0) |
| 103 | + BlockMarkdown.objects.create(survey_step=step, position=0, value='Some instructions') |
| 104 | + group = BlockRadioGroup.objects.create(survey_step=step, position=1, label='Select one') |
| 105 | + |
| 106 | + BlockRadioButton.objects.create(radio_group=group, position=0, label='Choice 1') |
| 107 | + BlockRadioButton.objects.create(radio_group=group, position=1, label='Choice 2') |
| 108 | + |
| 109 | + self.load_admin(survey) |
| 110 | + |
| 111 | + self.add_inline([[0, 0], [0, 1]], model=BlockRadioButton, label="Choice 3") |
| 112 | + self.add_inline([[0, 0]], model=BlockMarkdown, value="More instructions") |
| 113 | + |
| 114 | + step_indexes = self.add_inline(model=SurveyStep, title='Step 2') |
| 115 | + self.add_inline(step_indexes, model=BlockMarkdown, value="Other instructions") |
| 116 | + group_indexes = self.add_inline(step_indexes, model=BlockRadioGroup, label="Please choose") |
| 117 | + self.add_inline(group_indexes, BlockRadioButton, label="Choice A") |
| 118 | + self.add_inline(group_indexes, BlockRadioButton, label="Choice B") |
| 119 | + self.save_form() |
| 120 | + |
| 121 | + assert survey.serialize() == { |
| 122 | + "title": "test", |
| 123 | + "steps": [{ |
| 124 | + "title": "Step 1", |
| 125 | + "blocks": [{ |
| 126 | + "type": "markdown", |
| 127 | + "value": "Some instructions" |
| 128 | + }, { |
| 129 | + "type": "radiogroup", |
| 130 | + "label": "Select one", |
| 131 | + "buttons": ["Choice 1", "Choice 2", "Choice 3"], |
| 132 | + }, { |
| 133 | + "type": "markdown", |
| 134 | + "value": "More instructions" |
| 135 | + }], |
| 136 | + }, { |
| 137 | + "title": "Step 2", |
| 138 | + "blocks": [{ |
| 139 | + "type": "markdown", |
| 140 | + "value": "Other instructions" |
| 141 | + }, { |
| 142 | + "type": "radiogroup", |
| 143 | + "label": "Please choose", |
| 144 | + "buttons": ["Choice A", "Choice B"], |
| 145 | + }], |
| 146 | + }], |
| 147 | + } |
0 commit comments