Beispiel #1
0
def test_blank_json_returns_error_json():
    with pytest.raises(KeyError):
        question_order(empty_input_json)
Beispiel #2
0
def test_invalid_json_returns_type_error_json():
    with pytest.raises(TypeError):
        question_order(invalid_json)
Beispiel #3
0
def test_question_order_returns_expected_format():

    expected_output = {
        'form_validation_outputs': [{
            'questioncode': '1001',
            'response': '',
            'displayquestionnumber': 'Q2',
            'displaytext': 'This is another numeric question - Internet Sales',
            'displayorder': 2,
            'validation_info': [],
            'panel': ''
        }, {
            'questioncode': '2000',
            'response': '',
            'displayquestionnumber': 'Q3',
            'displaytext': 'This is a checkbox question',
            'displayorder': 3,
            'validation_info': [],
            'panel': ''
        }, {
            'questioncode': '3000',
            'response': '',
            'displayquestionnumber': 'Q4',
            'displaytext': 'This is a text question',
            'displayorder': 4,
            'validation_info': [],
            'panel': ''
        }, {
            'questioncode': '4000',
            'response': '',
            'displayquestionnumber': 'Q5',
            'displaytext': 'This is a postive derived question',
            'displayorder': 5,
            'validation_info': [],
            'panel': ''
        }, {
            'questioncode': '4001',
            'response': '',
            'displayquestionnumber': 'Q6',
            'displaytext': 'This is a subtracted derived question',
            'displayorder': 6,
            'validation_info': [],
            'panel': ''
        }, {
            'questioncode': '5000',
            'response': '',
            'displayquestionnumber': 'Q7',
            'displaytext': 'Comment Question (!= Blank)',
            'displayorder': 7,
            'validation_info': [],
            'panel': ''
        }, {
            'questioncode': '5001',
            'response': '',
            'displayquestionnumber': 'Q8',
            'displaytext': 'Comment Question (= 2)',
            'displayorder': 8,
            'validation_info': [],
            'panel': ''
        }, {
            'questioncode': '6000',
            'response': '',
            'displayquestionnumber': 'Q9',
            'displaytext': 'Value is Blank Question (== Blank)',
            'displayorder': 9,
            'validation_info': [],
            'panel': ''
        }, {
            'questioncode': '7000',
            'response': '',
            'displayquestionnumber': 'Q10',
            'displaytext': 'Value is Zero question',
            'displayorder': 10,
            'validation_info': [],
            'panel': ''
        }, {
            'questioncode': '8000',
            'response': '',
            'displayquestionnumber': 'Q11',
            'displaytext': 'Question vs Question (Total turnover)',
            'displayorder': 11,
            'validation_info': [],
            'panel': ''
        }, {
            'questioncode': '8001',
            'response': '',
            'displayquestionnumber': 'Q12',
            'displaytext': 'Question vs Question (Internet sales)',
            'displayorder': 12,
            'validation_info': [],
            'panel': ''
        }, {
            'questioncode': '8100',
            'response': '',
            'displayquestionnumber': 'Q15',
            'displaytext': 'Internet Sales (POPRRM validation)',
            'displayorder': 13,
            'validation_info': [],
            'panel': ''
        }, {
            'questioncode': '8101',
            'response': '',
            'displayquestionnumber': 'Q16',
            'displaytext': 'Total turnover (POPRRM validation)',
            'displayorder': 14,
            'validation_info': [],
            'panel': ''
        }, {
            'questioncode': '9000',
            'response': '',
            'displayquestionnumber': 'Q13',
            'displaytext': 'VPSIC question',
            'displayorder': 15,
            'validation_info': [],
            'panel': ''
        }, {
            'questioncode': '9001',
            'response': '',
            'displayquestionnumber': 'Q14',
            'displaytext':
            'Previous Period Value is Blank Question (Internet sales PP = Blank)',
            'displayorder': 16,
            'validation_info': [],
            'panel': ''
        }, {
            'questioncode': '1000',
            'response': '',
            'displayquestionnumber': 'Q1',
            'displaytext': 'This is a numeric question - Total turnover',
            'displayorder': 20,
            'validation_info': [],
            'panel': ''
        }]
    }

    assert question_order(data_output) == expected_output
Beispiel #4
0
def test_missing_key_display_order():
    with pytest.raises(KeyError):
        question_order(data_missing_key)
def view_form(inqcode, period, ruref):
    if (
        request.form
        and request.form['action'] == 'save-and-validate'
        and not current_app.auth.has_permission(["dev", "survey.*.write", "survey.*.manager"])
    ):
        abort(403)
    log.info("View_Form -- START --")

    log.info("Request.form: %s", request.form)

    try:
        status_message = ""
        url_parameters = dict(
            zip(["survey", "period", "reference"], [inqcode, period, ruref]))
        parameters = build_uri(url_parameters)

        contributor_details = api_caller.contributor_search(parameters=parameters)
        contributor_data = json.loads(contributor_details)
        log.info("Contributor Details: %s", contributor_data)
        log.info("Contributor Details[0]: %s", contributor_data['data'][0])

        validation_outputs = api_caller.validation_outputs(parameters=parameters)
        validations = json.loads(validation_outputs)
        log.info("Validations output: %s", validations)

        view_forms = api_caller.view_form_responses(parameters=parameters)
        view_form_data = json.loads(view_forms)
        log.info("View Form Data: %s", view_form_data)

        historic_data = api_caller.get_historic_data(parameters=parameters)
        historic_data_json = json.loads(historic_data)
        log.info("History Data: %s", historic_data_json)

        grouped_historic_data = group_historic_data(historic_data_json)
        log.info("Grouped Historic Data by question : %s", grouped_historic_data)

        status = contributor_data['data'][0]['status']
        status_colour = check_status(status)
        log.info("status colour: %s", status_colour)

        filtered_validations = filter_validations(validations)
        log.info("filtered validations: %s", filtered_validations)
        response_and_validations = combine_responses_and_validations(view_form_data, filtered_validations)
        log.info("response_and_validations: %s", response_and_validations)
        ordered_response_and_validations = question_order(response_and_validations)
        log.info("Combined Response and Validation Info Data: %s", ordered_response_and_validations)

        override_button = override_all_button(ordered_response_and_validations)
    except Exception as error:
        log.info("Error %s", error)

    if request.form and request.form['action'] == 'save-and-validate':
        save_form(parameters, request.form, inqcode, period, ruref)
        validate(inqcode, period, ruref)
        time.sleep(6)
        return redirect(url_for('view_form.view_form', inqcode=inqcode, period=period, ruref=ruref))

    return render_template(
        template_name_or_list=form_view_template_HTML,
        survey=inqcode,
        period=period,
        ruref=ruref,
        data=ordered_response_and_validations,
        override_button=override_button,
        status_message=json.dumps(status_message),
        contributor_details=contributor_data['data'][0],
        validation=filter_validations(validations),
        user=get_user(),
        status_colour=status_colour,
        historic_data=historic_data_json,
        grouped_historic_data=grouped_historic_data)