def test_getting_a_multiquestion_as_a_section_preserves_value_of_context_attribute(self):
        multiquestion_data = {
            "id": "example",
            "slug": "example",
            "question": "Example question",
            "type": "multiquestion",
            "questions": [
                {
                    "id": "example2",
                    "type": "text",
                },
                {
                    "id": "example3",
                    "type": "number",
                }
            ]
        }

        section = ContentSection(
            slug='section',
            name=TemplateField('Section'),
            prefill=True,
            editable=False,
            edit_questions=False,
            questions=[Multiquestion(multiquestion_data)]
        )

        assert section.get_question_as_section('example')._context is None
        assert section.filter(
            {'context': 'context1'}
        ).get_question_as_section('example')._context == {'context': 'context1'}
    def test_is_empty(self):
        questions = [
            Question({
                'id': 'q1',
                'name': 'q1',
                'type': 'unknown'
            }),
            Question({
                'id': 'q2',
                'name': 'q2',
                'type': 'unknown'
            })
        ]  # note that id and type are required as this function indirectly calls QuestionSummary.value

        section = ContentSection(slug='section',
                                 name=TemplateField('Section'),
                                 prefill=False,
                                 editable=False,
                                 edit_questions=False,
                                 questions=questions)
        answered = section.summary({'q1': 'a1', 'q2': 'a2'})
        assert not answered.is_empty
        half_answered = section.summary({'q1': 'a1', 'q2': ''})
        assert not half_answered.is_empty
        not_answered = section.summary({'q1': '', 'q2': ''})
        assert not_answered.is_empty
    def test_getting_a_multiquestion_as_a_section_preserves_value_of_context_attribute(
            self):
        multiquestion_data = {
            "id":
            "example",
            "slug":
            "example",
            "question":
            "Example question",
            "type":
            "multiquestion",
            "questions": [{
                "id": "example2",
                "type": "text",
            }, {
                "id": "example3",
                "type": "number",
            }]
        }

        section = ContentSection(slug='section',
                                 name=TemplateField('Section'),
                                 prefill=True,
                                 editable=False,
                                 edit_questions=False,
                                 questions=[Multiquestion(multiquestion_data)])

        assert section.get_question_as_section('example')._context is None
        assert section.filter({
            'context': 'context1'
        }).get_question_as_section('example')._context == {
            'context': 'context1'
        }
    def test_not_all_fields_are_templated(self):
        section = ContentSection(slug='# {{ section }}',
                                 name=TemplateField('Section'),
                                 prefill=True,
                                 editable=False,
                                 edit_questions=False,
                                 questions=[Question({})])

        assert section.filter({}).slug == '# {{ section }}'
    def test_question_fields_without_template_tags_are_unchanged(self):
        section = ContentSection(slug='section',
                                 name=TemplateField('Section'),
                                 prefill=False,
                                 editable=False,
                                 edit_questions=False,
                                 questions=[Question({'name': 'Question'})])

        assert section.filter({}).questions[0].name == 'Question'
    def test_section_description_is_not_set(self):
        section = ContentSection(slug='section',
                                 name=TemplateField('Section one'),
                                 prefill=False,
                                 editable=False,
                                 edit_questions=False,
                                 questions=[Question({})])

        assert section.filter({}).description is None
    def test_section_name_is_templated(self):
        section = ContentSection(slug='section',
                                 name=TemplateField('Section {{ name }}'),
                                 prefill=True,
                                 editable=False,
                                 edit_questions=False,
                                 questions=[Question({})])

        assert section.filter({'name': 'one'}).name == 'Section one'
    def test_missing_context_variable_raises_an_error(self):
        section = ContentSection(slug='section',
                                 name=TemplateField('Section {{ name }}'),
                                 prefill=False,
                                 editable=False,
                                 edit_questions=False,
                                 questions=[Question({})])

        with pytest.raises(ContentTemplateError):
            section.filter({}).name
    def test_section_description_is_not_set(self):
        section = ContentSection(
            slug='section',
            name=TemplateField('Section one'),
            prefill=False,
            editable=False,
            edit_questions=False,
            questions=[Question({})]
        )

        assert section.filter({}).description is None
    def test_question_fields_without_template_tags_are_unchanged(self):
        section = ContentSection(
            slug='section',
            name=TemplateField('Section'),
            prefill=False,
            editable=False,
            edit_questions=False,
            questions=[Question({'name': 'Question'})]
        )

        assert section.filter({}).questions[0].name == 'Question'
    def test_not_all_fields_are_templated(self):
        section = ContentSection(
            slug='# {{ section }}',
            name=TemplateField('Section'),
            prefill=True,
            editable=False,
            edit_questions=False,
            questions=[Question({})]
        )

        assert section.filter({}).slug == '# {{ section }}'
    def test_section_name_is_templated(self):
        section = ContentSection(
            slug='section',
            name=TemplateField('Section {{ name }}'),
            prefill=True,
            editable=False,
            edit_questions=False,
            questions=[Question({})]
        )

        assert section.filter({'name': 'one'}).name == 'Section one'
    def test_unused_context_variables_are_ignored(self):
        section = ContentSection(
            slug='section',
            name=TemplateField('Section {{ name }}'),
            prefill=True,
            editable=False,
            edit_questions=False,
            questions=[Question({})]
        )

        assert section.filter({'name': 'one', 'name2': 'ignored'}).name == 'Section one'
    def test_fields_without_template_tags_are_unchanged(self):
        section = ContentSection(slug='section',
                                 name=TemplateField('Section'),
                                 description=TemplateField('just a string'),
                                 prefill=True,
                                 editable=False,
                                 edit_questions=False,
                                 questions=[Question({})])

        assert section.filter({}).name == 'Section'
        assert section.filter({}).description == 'just a string'
        assert section.filter({}).prefill is True
Example #15
0
    def test_question_followup_get_data(self, filter_inplace_allowed, get_data_arg, expected_retval):
        section = ContentSection(
            slug='section',
            name=TemplateField('Section one'),
            prefill=False,
            editable=False,
            edit_questions=False,
            questions=[Question({'id': 'q1', 'followup': {'q2': [False]}, 'type': 'boolean'}),
                       Question({'id': 'q2', 'type': 'boolean'})]
        ).filter({}, inplace_allowed=filter_inplace_allowed)

        assert section.get_data(get_data_arg) == expected_retval
    def test_missing_context_variable_raises_an_error(self):
        section = ContentSection(
            slug='section',
            name=TemplateField('Section {{ name }}'),
            prefill=False,
            editable=False,
            edit_questions=False,
            questions=[Question({})]
        )

        with pytest.raises(ContentTemplateError):
            section.filter({}).name
    def test_unused_context_variables_are_ignored(self):
        section = ContentSection(slug='section',
                                 name=TemplateField('Section {{ name }}'),
                                 prefill=True,
                                 editable=False,
                                 edit_questions=False,
                                 questions=[Question({})])

        assert section.filter({
            'name': 'one',
            'name2': 'ignored'
        }).name == 'Section one'
    def test_section_description_is_templated(self):
        section = ContentSection(
            slug='section',
            name=TemplateField('Section one'),
            prefill=False,
            editable=False,
            edit_questions=False,
            questions=[Question({})],
            description=TemplateField("This is the {{ name }} section")
        )

        assert section.filter({'name': 'first'}).description == 'This is the first section'
    def test_section_description_is_templated(self):
        section = ContentSection(
            slug='section',
            name=TemplateField('Section one'),
            prefill=False,
            editable=False,
            edit_questions=False,
            questions=[Question({})],
            description=TemplateField("This is the {{ name }} section"))

        assert section.filter({
            'name': 'first'
        }).description == 'This is the first section'
    def test_fields_without_template_tags_are_unchanged(self):
        section = ContentSection(
            slug='section',
            name=TemplateField('Section'),
            description=TemplateField('just a string'),
            prefill=True,
            editable=False,
            edit_questions=False,
            questions=[Question({})]
        )

        assert section.filter({}).name == 'Section'
        assert section.filter({}).description == 'just a string'
        assert section.filter({}).prefill is True
    def test_copying_section_preserves_value_of_context_attribute(self):
        section = ContentSection(slug='section',
                                 name=TemplateField('Section'),
                                 prefill=False,
                                 editable=False,
                                 edit_questions=False,
                                 questions=[Question({'name': 'Question'})
                                            ]).filter({'context': 'context1'})
        assert section._context == {'context': 'context1'}

        copied_section = section.copy()
        assert copied_section._context == section._context
        assert copied_section.prefill is False
        assert copied_section.editable is False
    def test_filtering_a_section_with_a_description_template(self):
        section = ContentSection(
            slug='section',
            name=TemplateField('Section one'),
            prefill=False,
            editable=False,
            edit_questions=False,
            questions=[Question({})],
            description=TemplateField(
                "description for {% if lot == 'digital-specialists' %}specialists{% else %}outcomes{% endif %}"
            )
        )

        assert section.filter({"lot": "digital-specialists"}).description == "description for specialists"
        assert section.filter({"lot": "digital-outcomes"}).description == "description for outcomes"
    def test_copying_section_preserves_value_of_context_attribute(self):
        section = ContentSection(
            slug='section',
            name=TemplateField('Section'),
            prefill=False,
            editable=False,
            edit_questions=False,
            questions=[Question({'name': 'Question'})]
        ).filter({'context': 'context1'})
        assert section._context == {'context': 'context1'}

        copied_section = section.copy()
        assert copied_section._context == section._context
        assert copied_section.prefill is False
        assert copied_section.editable is False
Example #24
0
    def test_is_empty(self, summary_arg, expect_empty, summary_inplace_allowed):
        questions = [
            Question({'id': 'q1', 'name': 'q1', 'type': 'unknown'}),
            Question({'id': 'q2', 'name': 'q2', 'type': 'unknown'})
        ]  # note that id and type are required as this function indirectly calls QuestionSummary.value

        section = ContentSection(
            slug='section',
            name=TemplateField('Section'),
            prefill=False,
            editable=False,
            edit_questions=False,
            questions=questions
        )
        assert section.summary(summary_arg, inplace_allowed=summary_inplace_allowed).is_empty is expect_empty
Example #25
0
    def test_filtering_a_section_with_a_description_template(self, filter_inplace_allowed, lot, expected_description):
        section = ContentSection(
            slug='section',
            name=TemplateField('Section one'),
            prefill=False,
            editable=False,
            edit_questions=False,
            questions=[Question({})],
            description=TemplateField(
                "description for {% if lot == 'digital-specialists' %}specialists{% else %}outcomes{% endif %}"
            )
        )

        assert section.filter(
            {"lot": lot},
            inplace_allowed=filter_inplace_allowed,
        ).description == expected_description
    def test_filtering_a_section_with_a_description_template(self):
        section = ContentSection(
            slug='section',
            name=TemplateField('Section one'),
            prefill=False,
            editable=False,
            edit_questions=False,
            questions=[Question({})],
            description=TemplateField(
                "description for {% if lot == 'digital-specialists' %}specialists{% else %}outcomes{% endif %}"
            ))

        assert section.filter({
            "lot": "digital-specialists"
        }).description == "description for specialists"
        assert section.filter({
            "lot": "digital-outcomes"
        }).description == "description for outcomes"
    def test_question_followup_get_data(self):
        section = ContentSection(
            slug='section',
            name=TemplateField('Section one'),
            prefill=False,
            editable=False,
            edit_questions=False,
            questions=[Question({'id': 'q1', 'followup': {'q2': [False]}, 'type': 'boolean'}),
                       Question({'id': 'q2', 'type': 'boolean'})]
        ).filter({})

        assert section.get_data(MultiDict([('q1', 'false')])) == {'q1': False}
        assert section.get_data(MultiDict([('q1', 'true')])) == {'q1': True, 'q2': None}
        assert section.get_data(
            MultiDict([('q1', 'false'), ('q2', 'true')])
        ) == {'q1': False, 'q2': True}
        assert section.get_data(
            MultiDict([('q1', 'true'), ('q2', 'true')])
        ) == {'q1': True, 'q2': None}
def test_get_questions_unpacks_multiquestions():

    section = ContentSection.create(section=get_section(
        questions=['checkboxes', 'boolean'],
        multiquestions=['checkboxes', 'boolean']
    ))
    assert section.id == 'options'

    # should be a flat list with 4 entries
    questions = get_questions(section.questions)
    assert len(questions) == 4
    def test_is_empty(self):
        questions = [
            Question({'id': 'q1', 'name': 'q1', 'type': 'unknown'}),
            Question({'id': 'q2', 'name': 'q2', 'type': 'unknown'})
        ]  # note that id and type are required as this function indirectly calls QuestionSummary.value

        section = ContentSection(
            slug='section',
            name=TemplateField('Section'),
            prefill=False,
            editable=False,
            edit_questions=False,
            questions=questions
        )
        answered = section.summary({'q1': 'a1', 'q2': 'a2'})
        assert not answered.is_empty
        half_answered = section.summary({'q1': 'a1', 'q2': ''})
        assert not half_answered.is_empty
        not_answered = section.summary({'q1': '', 'q2': ''})
        assert not_answered.is_empty
def test_get_questions_unpacks_multiquestions():

    section = ContentSection.create(section=get_section(
        questions=['checkboxes', 'boolean'],
        multiquestions=['checkboxes', 'boolean']
    ))
    assert section.id == 'options'

    # should be a flat list with 4 entries
    questions = get_questions(section.questions)
    assert len(questions) == 4
def return_row_and_question(question_type, is_multiquestion=False):

    if is_multiquestion:
        section = ContentSection.create(section=get_section(
            multiquestions=[question_type],
        ))
    else:
        section = ContentSection.create(section=get_section(
            questions=[question_type],
        ))

    rows = return_rows_for_sections([section])
    question = get_question(question_type, '0')
    assert len(rows) == 2
    assert rows[0][0] == 'Options'
    assert rows[0][2] == question['question']
    if is_multiquestion:
        assert rows[0][1] == 'Please indicate your preferred options'

    return rows[0], question
def test_get_questions_sets_multiquestion_names_and_hints():

    section = ContentSection.create(section=get_section(
        multiquestions=['checkboxes', 'boolean']
    ))
    assert section.id == 'options'

    # hint and name of multiquestion should be preserved in nested_questions
    for question in get_questions(section.questions):
        if question.get('id').startswith('multiquestion_'):
            assert question.get('multiquestion_name') == 'Options'
            assert question.get('multiquestion_hint') == 'Please indicate your preferred options'
def test_get_questions_sets_multiquestion_names_and_hints():

    section = ContentSection.create(section=get_section(
        multiquestions=['checkboxes', 'boolean']
    ))
    assert section.id == 'options'

    # hint and name of multiquestion should be preserved in nested_questions
    for question in get_questions(section.questions):
        if question.get('id').startswith('multiquestion_'):
            assert question.get('parent').get('name') == 'Options'
            assert question.get('parent').get('hint') == 'Please indicate your preferred options'
    def test_question_followup_get_data(self):
        section = ContentSection(slug='section',
                                 name=TemplateField('Section one'),
                                 prefill=False,
                                 editable=False,
                                 edit_questions=False,
                                 questions=[
                                     Question({
                                         'id': 'q1',
                                         'followup': {
                                             'q2': [False]
                                         },
                                         'type': 'boolean'
                                     }),
                                     Question({
                                         'id': 'q2',
                                         'type': 'boolean'
                                     })
                                 ]).filter({})

        assert section.get_data(MultiDict([('q1', 'false')])) == {'q1': False}
        assert section.get_data(MultiDict([('q1', 'true')])) == {
            'q1': True,
            'q2': None
        }
        assert section.get_data(MultiDict([('q1', 'false'),
                                           ('q2', 'true')])) == {
                                               'q1': False,
                                               'q2': True
                                           }
        assert section.get_data(MultiDict([('q1', 'true'),
                                           ('q2', 'true')])) == {
                                               'q1': True,
                                               'q2': None
                                           }
def return_row_and_question(question_type, is_multiquestion=False):

    if is_multiquestion:
        section = ContentSection.create(section=get_section(
            multiquestions=[question_type],
        ))
    else:
        section = ContentSection.create(section=get_section(
            questions=[question_type],
        ))

    rows = return_rows_for_sections([section])
    question = get_question(question_type, '0')
    assert len(rows) == 2
    if is_multiquestion:
        assert rows[0][0] == 'Section name / Options'
    else:
        assert rows[0][0] == 'Section name'
    assert rows[0][2] == question['question']
    if is_multiquestion:
        assert rows[0][1] == 'Please indicate your preferred options'

    return rows[0], question
    def test_get_templatable_section_attributes_calls_render(self):
        section = ContentSection(
            slug='section',
            name=TemplateField(
                '{% if True %}Section one{% else %}Section two{% endif %}'),
            prefill=True,
            editable=False,
            edit_questions=False,
            questions=[Question({})],
            description=TemplateField(
                "description for {% if lot == 'digital-specialists' %}specialists{% else %}outcomes{% endif %}"
            ))

        assert section.name == 'Section one'
        with pytest.raises(ContentTemplateError):
            section.description

        assert section.slug == 'section'