def test_build_view_context_for_question(self): # Given g.schema = schema = load_schema_from_params('test', 'titles') block = g.schema.get_block('single-title-block') full_routing_path = [ Location('group', 0, 'single-title-block'), Location('group', 0, 'who-is-answer-block'), Location('group', 0, 'multiple-question-versions-block'), Location('group', 0, 'Summary') ] current_location = Location('group', 0, 'single-title-block') schema_context = _get_schema_context(full_routing_path, current_location, {}, {}, AnswerStore(), g.schema) # When with self._application.test_request_context(): question_view_context = build_view_context('Question', {}, schema, AnswerStore(), schema_context, block, current_location, form=None) # Then self.assertEqual( question_view_context['question_titles']['single-title-question'], 'How are you feeling??')
def post_block( routing_path, schema, metadata, collection_metadata, answer_store, eq_id, form_type, collection_id, group_id, # pylint: disable=too-many-locals group_instance, block_id): current_location = Location(group_id, group_instance, block_id) completeness = get_completeness(current_user) router = Router(schema, routing_path, completeness, current_location) if not router.can_access_location(): next_location = router.get_next_location() return _redirect_to_location(collection_id, eq_id, form_type, next_location) block = _get_block_json(current_location, schema, answer_store, metadata) schema_context = _get_schema_context(routing_path, current_location, metadata, collection_metadata, answer_store, schema) rendered_block = renderer.render(block, **schema_context) form = _generate_wtf_form(request.form, rendered_block, current_location, schema) if 'action[save_sign_out]' in request.form: return _save_sign_out(routing_path, current_location, form, schema, answer_store, metadata) if form.validate(): _set_started_at_metadata_if_required(form, collection_metadata) _update_questionnaire_store(current_location, form, schema) next_location = path_finder.get_next_location( current_location=current_location) if _is_end_of_questionnaire(block, next_location): return submit_answers(routing_path, eq_id, form_type, schema) return redirect(_next_location_url(next_location)) context = build_view_context(block['type'], metadata, schema, answer_store, schema_context, rendered_block, current_location, form) return _render_page(block['type'], context, current_location, schema, answer_store, metadata, routing_path)
def test_build_view_context_for_answer_summary(self): primary_uuid = uuid.uuid4() repeating_0_uuid = uuid.uuid4() repeating_1_uuid = uuid.uuid4() self.answer_store.add( Answer('primary-first-name', 'Bob', primary_uuid, 0, 0)) self.answer_store.add( Answer('primary-last-name', 'Smith', primary_uuid, 0, 0)) self.answer_store.add( Answer('primary-anyone-else', 'Yes', primary_uuid, 0, 0)) self.answer_store.add( Answer('repeating-first-name', 'Mary', repeating_0_uuid, 0, 0)) self.answer_store.add( Answer('repeating-last-name', 'Smith', repeating_0_uuid, 0, 0)) self.answer_store.add( Answer('repeating-anyone-else', 'Yes', repeating_0_uuid, 0, 0)) self.answer_store.add( Answer('repeating-first-name', 'Sally', repeating_1_uuid, 1, 0)) self.answer_store.add( Answer('repeating-last-name', 'Smith', repeating_1_uuid, 1, 0)) current_location = Location( block_id='household-summary', group_id='household-summary-group', group_instance=0, ) with self.app_request_context('/'): context = build_view_context(self.block_type, self.metadata, self.schema, self.answer_store, None, None, current_location, None) self.check_context(context) self.assertEqual(len(context['summary']), 6) self.assertTrue('title' in context['summary']) self.assertTrue('icon' in context['summary']) self.assertEqual('person', context['summary']['icon']) self.assertEqual(1, len(context['summary']['groups'])) self.assertEqual( 'Bob Smith', context['summary']['groups'][0]['answers'][0]['label']) self.assertEqual( 'Mary Smith', context['summary']['groups'][0]['answers'][1]['label']) self.assertEqual( 'Sally Smith', context['summary']['groups'][0]['answers'][2]['label'])
def _get_context(full_routing_path, block, current_location, schema, form=None): metadata = get_metadata(current_user) answer_store = get_answer_store(current_user) schema_context = _get_schema_context(full_routing_path, current_location.group_instance, metadata, answer_store, schema) rendered_block = renderer.render(block, **schema_context) return build_view_context(block['type'], metadata, schema, answer_store, schema_context, rendered_block, current_location, form=form)
def test_build_view_context_for_calculation_summary(self): # Given schema = load_schema_from_params('test', 'calculated_summary') block = schema.get_block('currency-total-playback') metadata = { 'tx_id': '12345678-1234-5678-1234-567812345678', 'collection_exercise_sid': '789', 'form_type': 'calculated_summary', 'eq_id': 'test', } answers = [ { 'answer_instance': 0, 'group_instance': 0, 'answer_id': 'first-number-answer', 'value': 1 }, { 'answer_instance': 0, 'group_instance': 0, 'answer_id': 'second-number-answer', 'value': 2 }, { 'answer_instance': 0, 'group_instance': 0, 'answer_id': 'third-number-answer', 'value': 4 }, { 'answer_instance': 0, 'group_instance': 0, 'answer_id': 'fourth-number-answer', 'value': 6 }, ] schema_context = { 'answers': answers, 'group_instance': 0, 'metadata': metadata } current_location = Location('group', 0, 'currency-total-playback') # When with self._application.test_request_context(): view_context = build_view_context(block['type'], metadata, schema, AnswerStore(answers), schema_context, block, current_location, form=None) # Then self.assertTrue('summary' in view_context) self.assertTrue('calculated_question' in view_context['summary']) self.assertEqual( view_context['summary']['title'], 'We calculate the total of currency values entered to be £13.00. Is this correct? (With Fourth)' )