def test_build_schema_context_repeating_answers(self): aliases = { '_full_name': { 'answer_id': 'full_name_answer', 'repeats': True, }, } answers = [ { 'answer_id': 'full_name_answer', 'answer_instance': 0, 'value': 'Person One', }, { 'answer_id': 'full_name_answer', 'answer_instance': 1, 'value': 'Person Two', }, { 'answer_id': 'full_name_answer', 'answer_instance': 2, 'value': 'Person Three', } ] self.answer_store.filter = MagicMock(return_value=answers) schema_context = build_schema_context(self.metadata, aliases, self.answer_store) context_answers = schema_context['answers'] self.assertIsInstance(context_answers['_full_name'], list) self.assertEqual(len(context_answers['_full_name']), 3) self.assertEqual(len(context_answers), 1)
def test_respondent_display_name_is_trading_as_when_trading_as_supplied( self): schema_context = build_schema_context(self.metadata, {}, self.answer_store) self.assertEqual(schema_context['respondent']['trad_as_or_ru_name'], self.metadata['trad_as'])
def test_respondent_display_name_is_ru_name_as_when_trading_as_empty(self): metadata = self.metadata.copy() metadata['trad_as'] = "" schema_context = build_schema_context(metadata, {}, self.answer_store) self.assertEqual(schema_context['respondent']['trad_as_or_ru_name'], metadata['ru_name'])
def _render_schema(current_location): metadata = get_metadata(current_user) answer_store = get_answer_store(current_user) block_json = SchemaHelper.get_block_for_location(g.schema_json, current_location) block_json = _evaluate_skip_conditions(block_json, current_location, answer_store, metadata) aliases = SchemaHelper.get_aliases(g.schema_json) block_context = build_schema_context(metadata, aliases, answer_store, current_location.group_instance) return renderer.render(block_json, **block_context)
def _get_schema_context(full_routing_path, group_instance, metadata, answer_store, schema): answer_ids_on_path = get_answer_ids_on_routing_path( schema, full_routing_path) return build_schema_context(metadata=metadata, schema=schema, answer_store=answer_store, group_instance=group_instance, answer_ids_on_path=answer_ids_on_path)
def test_given_quotes_in_ru_name_when_create_context_then_quotes_are_escaped( self): # Given metadata = self.metadata.copy() metadata['ru_name'] = "\"ru name\"" # When schema_context = build_schema_context(metadata, {}, self.answer_store) # Then self.assertEqual(schema_context['respondent']['ru_name'], r'\"ru name\"')
def test_given_backslash_in_ru_name_or_trading_name_when_create_context_then_backslash_are_escaped( self): # Given metadata = self.metadata.copy() metadata['trad_as'] = "\\trading name\\" # When schema_context = build_schema_context(metadata, {}, self.answer_store) # Then self.assertEqual(schema_context['respondent']['trad_as_or_ru_name'], r'\\trading name\\')
def test_build_schema_context(self): # Given with patch('app.templating.schema_context.build_schema_metadata', return_value='schema_metadata'), \ patch('app.templating.schema_context._build_answers', return_value='answer_context'): # When schema_context = build_schema_context(self.metadata, self.schema, self.answer_store, []) # Then self.assertIn('metadata', schema_context) self.assertEqual(schema_context['metadata'], 'schema_metadata') self.assertIn('answers', schema_context) self.assertEqual(schema_context['answers'], 'answer_context')
def get_summary(eq_id, form_type, collection_id): # pylint: disable=unused-argument answer_store = get_answer_store(current_user) path_finder = PathFinder(g.schema_json, answer_store, get_metadata(current_user)) latest_location = path_finder.get_latest_location(get_completed_blocks(current_user)) metadata = get_metadata(current_user) if latest_location.block_id is 'summary': answers = get_answer_store(current_user) schema_context = build_schema_context(metadata, g.schema.aliases, answers) rendered_schema_json = renderer.render(g.schema_json, **schema_context) summary_context = build_summary_rendering_context(rendered_schema_json, answer_store, metadata) return _build_template(current_location=latest_location, context=summary_context) return redirect(latest_location.url(metadata))
def _get_schema_context(full_routing_path, location, metadata, collection_metadata, answer_store, schema): answer_ids_on_path = get_answer_ids_on_routing_path( schema, full_routing_path) group_instance_id = get_group_instance_id(schema, answer_store, location) if location else None return build_schema_context( metadata=metadata, collection_metadata=collection_metadata, schema=schema, answer_store=answer_store, group_instance=location.group_instance if location else 0, group_instance_id=group_instance_id, answer_ids_on_path=answer_ids_on_path)
def test_build_schema_context(self): aliases = { 'first_name': { 'answer_id': 'answer_id', 'repeats': False, }, } answers = [] self.answer_store.filter = MagicMock(return_value=answers) schema_context = build_schema_context(self.metadata, aliases, self.answer_store) self.assertTrue('exercise' in schema_context) self.assertTrue('answers' in schema_context)
def test_build_schema_context_no_answers_should_return_empty_alias_value(self): aliases = { 'alias_with_no_matching_answer': { 'answer_id': 'answer_not_provided', 'repeats': False, }, } answers = [] self.answer_store.filter = MagicMock(return_value=answers) schema_context = build_schema_context(self.metadata, aliases, self.answer_store) context_answers = schema_context['answers'] self.assertEqual(len(context_answers), 1) self.assertEqual(context_answers['alias_with_no_matching_answer'], '')
def test_maximum_answers_must_be_limited_to_system_max(self): aliases = { 'repeating_answer_alias': { 'answer_id': 'answer_id', 'repeats': True, }, } answers = [{ 'answer_id': 'answer_id', 'answer_instance': instance, 'value': 'Some Value', } for instance in range(26)] schema_context = build_schema_context(self.metadata, aliases, AnswerStore(answers)) context_answers = schema_context['answers'] self.assertEqual(len(context_answers['repeating_answer_alias']), 25)
def test_build_exercise(self): aliases = { 'first_name': { 'answer_id': 'answer_id', 'repeats': False, }, } answers = [] self.answer_store.filter = MagicMock(return_value=answers) schema_context = build_schema_context(self.metadata, aliases, self.answer_store) exercise = schema_context['exercise'] self.assertEqual('2016-10-11', exercise['start_date'].date().isoformat()) self.assertEqual('2016-10-12', exercise['end_date'].date().isoformat()) self.assertEqual('2016-10-09', exercise['employment_date'].date().isoformat()) self.assertEqual('2016-10-10', exercise['return_by'].date().isoformat()) self.assertEqual('GB-GBN', exercise['region_code'])
def test_alias_for_repeating_answer_returns_list(self): aliases = { 'repeating_answer_alias': { 'answer_id': 'answer_id', 'repeats': True, }, } answers = [{ 'answer_id': 'answer_id', 'answer_instance': 0, 'value': 'Some Value', }] self.answer_store.filter = MagicMock(return_value=answers) schema_context = build_schema_context(self.metadata, aliases, self.answer_store) context_answers = schema_context['answers'] self.assertIsInstance(context_answers['repeating_answer_alias'], list)
def test_build_answers(self): aliases = { 'first_name': { 'answer_id': 'answer_id', 'repeats': False, }, } answers = [{ 'answer_id': 'answer_id', 'answer_instance': 0, 'value': 'Joe Bloggs', }] self.answer_store.filter = MagicMock(return_value=answers) schema_context = build_schema_context(self.metadata, aliases, self.answer_store) context_answers = schema_context['answers'] self.assertEqual(len(context_answers), 1) self.assertEqual(context_answers['first_name'], 'Joe Bloggs')
def test_build_schema_context_single_answer_should_not_return_list(self): aliases = { 'full_name': { 'answer_id': 'full_name_answer', 'repeats': False, }, } answers = [{ 'answer_id': 'full_name_answer', 'answer_instance': 0, 'value': 'Person One', }] self.answer_store.filter = MagicMock(return_value=answers) schema_context = build_schema_context(self.metadata, aliases, self.answer_store) context_answers = schema_context['answers'] self.assertEqual(len(answers), 1) self.assertEqual(context_answers['full_name'], 'Person One')
def _get_context(block, current_location, answer_store): error_messages = SchemaHelper.get_messages(g.schema_json) form, template_params = get_form_for_location(block, current_location, answer_store, error_messages) content = {'form': form, 'block': block} if template_params: content.update(template_params) if block['type'] == 'Summary': metadata = get_metadata(current_user) aliases = SchemaHelper.get_aliases(g.schema_json) schema_context = build_schema_context(metadata, aliases, answer_store) rendered_schema_json = renderer.render(g.schema_json, **schema_context) content.update({ 'summary': build_summary_rendering_context(rendered_schema_json, answer_store, metadata) }) return content
def test_given_backslash_in_answers_when_create_context_then_backslash_are_escaped( self): aliases = { 'first_name': { 'answer_id': 'answer_id', 'repeats': False, }, } answers = [{ 'answer_id': 'answer_id', 'answer_instance': 0, 'value': '\\', }] self.answer_store.filter = MagicMock(return_value=answers) schema_context = build_schema_context(self.metadata, aliases, self.answer_store) context_answers = schema_context['answers'] self.assertEqual(len(context_answers), 1) self.assertEqual(context_answers['first_name'], r'\\')
def _render_schema(current_location): metadata = get_metadata(current_user) answer_store = get_answer_store(current_user) schema_item = g.schema.get_item_by_id(current_location.block_id) schema_context = build_schema_context(metadata, g.schema.aliases, answer_store, current_location.group_instance) renderer.render_schema_items(schema_item, schema_context)