def test_should_repeat_for_answer_until(self): questionnaire = { 'survey_id': '021', 'data_version': '0.0.1', 'sections': [{ 'id': 'section1', 'groups': [ { 'id': 'group-1', 'blocks': [ { 'id': 'block-1', 'questions': [{ 'id': 'question-2', 'answers': [ { 'id': 'my_answer', 'type': 'TextField' } ] }] } ] } ] }] } schema = QuestionnaireSchema(questionnaire) # Given repeat = { 'type': 'until', 'when': [ { 'id': 'my_answer', 'condition': 'equals', 'value': 'Done' } ] } answer_store = AnswerStore({}) current_path = [Location('group-1', 0, 'block-1')] answer_on_path = get_answer_ids_on_routing_path(schema, current_path) self.assertEqual(evaluate_repeat(repeat, answer_store, answer_on_path), 1) answer_store.add(Answer(answer_id='my_answer', value='Not Done', group_instance=0)) self.assertEqual(evaluate_repeat(repeat, answer_store, answer_on_path), 2) answer_store.add(Answer(answer_id='my_answer', value='Done', group_instance=1)) self.assertEqual(evaluate_repeat(repeat, answer_store, answer_on_path), 2)
def get_blocks(self): blocks = [] for group in SchemaHelper.get_groups(self.survey_json): if 'skip_condition' in group: skip = evaluate_skip_condition(group['skip_condition'], self.metadata, self.answer_store) if skip: continue no_of_repeats = 1 repeating_rule = SchemaHelper.get_repeat_rule(group) if repeating_rule: no_of_repeats = evaluate_repeat(repeating_rule, self.answer_store) for i in range(0, no_of_repeats): for block in group['blocks']: if 'skip_condition' in block: skip = evaluate_skip_condition(block['skip_condition'], self.metadata, self.answer_store) if skip: continue blocks.append({ "group_id": group['id'], "group_instance": i, "block": block, }) return blocks
def _build_repeating_navigation(self, repeating_rule, section, current_group_id, current_group_instance): groups = section['groups'] answer_ids_on_path = get_answer_ids_on_routing_path( self.schema, self.routing_path) no_of_repeats = evaluate_repeat(repeating_rule, self.answer_store, answer_ids_on_path) repeating_nav = [] if repeating_rule['type'] == 'answer_count': link_names = self._generate_link_names( section['title_from_answers']) for group in groups: is_current_group = group['id'] == current_group_id repeating_nav += self._generate_repeated_items( link_names, group, no_of_repeats, is_current_group, current_group_instance) elif no_of_repeats > 0: target_location = self._get_location_for_section(section, groups) item = self._build_single_repeating_navigation( section, current_group_id, target_location) repeating_nav.append(item) return repeating_nav
def test_should_repeat_for_answer_answer_value(self): # Given repeat = {'answer_id': 'my_answer', 'type': 'answer_value'} answer_store = AnswerStore() answer_store.add(Answer(answer_id='my_answer', value='3')) # When number_of_repeats = evaluate_repeat(repeat, answer_store) self.assertEqual(number_of_repeats, 3)
def test_should_repeat_for_answer_answer_count_minus_one(self): # Given repeat = {'answer_id': 'my_answer', 'type': 'answer_count_minus_one'} answer_store = AnswerStore() answer_store.add(Answer(answer_id='my_answer', value='3')) answer_store.add( Answer(answer_id='my_answer', value='4', answer_instance=1)) # When number_of_repeats = evaluate_repeat(repeat, answer_store) self.assertEqual(number_of_repeats, 1)
def test_should_minus_one_from_maximum_repeats(self): # Given repeat = {'answer_id': 'my_answer', 'type': 'answer_count_minus_one'} answer_store = AnswerStore() for i in range(27): answer_store.add( Answer(answer_id='my_answer', value='3', answer_instance=i)) # When number_of_repeats = evaluate_repeat(repeat, answer_store) self.assertEqual(number_of_repeats, 24)
def test_should_minus_one_from_maximum_repeats(self): questionnaire = { 'survey_id': '021', 'data_version': '0.0.1', 'sections': [{ 'id': 'section1', 'groups': [ { 'id': 'group-1', 'blocks': [ { 'id': 'block-1', 'questions': [{ 'id': 'question-2', 'answers': [ { 'id': 'my_answer', 'type': 'TextField' } ] }] } ] } ] }] } schema = QuestionnaireSchema(questionnaire) # Given repeat = { 'answer_id': 'my_answer', 'type': 'answer_count_minus_one' } answer_store = AnswerStore({}) for i in range(27): answer_store.add(Answer(answer_id='my_answer', value='3', answer_instance=i)) current_path = [Location('group-1', 0, 'block-1')] # When answer_on_path = get_answer_ids_on_routing_path(schema, current_path) number_of_repeats = evaluate_repeat(repeat, answer_store, answer_on_path) self.assertEqual(number_of_repeats, 24)
def test_should_repeat_for_answer_answer_count_minus_one(self): questionnaire = { 'survey_id': '021', 'data_version': '0.0.1', 'sections': [{ 'id': 'section1', 'groups': [{ 'id': 'group-1', 'blocks': [{ 'id': 'block-1', 'questions': [{ 'id': 'question-2', 'answers': [{ 'id': 'my_answer', 'type': 'TextField' }] }] }] }] }] } schema = QuestionnaireSchema(questionnaire) # Given repeat = {'answer_id': 'my_answer', 'type': 'answer_count_minus_one'} answer_store = AnswerStore({}) answer_store.add_or_update(Answer(answer_id='my_answer', value='3')) answer_store.add_or_update( Answer(answer_id='my_answer', value='4', answer_instance=1)) current_path = [Location('group-1', 0, 'block-1')] # When number_of_repeats = evaluate_repeat(repeat, answer_store, schema, current_path) self.assertEqual(number_of_repeats, 1)
def _build_repeating_navigation(self, repeating_rule, group, current_group_id, current_group_instance): first_location = Location(group['id'], 0, group['blocks'][0]['id']) no_of_repeats = evaluate_repeat(repeating_rule, self.answer_store) repeating_nav = [] if repeating_rule['type'] == 'answer_count': completed_id = group['completed_id'] if 'completed_id' in group else group['blocks'][-1]['id'] is_current_group = group['id'] == current_group_id link_names = self._generate_link_names(repeating_rule) repeating_nav = self._generate_repeated_items(link_names, first_location, completed_id, no_of_repeats, is_current_group, current_group_instance) elif no_of_repeats > 0: item = self._build_single_navigation(group, current_group_id, first_location) repeating_nav.append(item) return repeating_nav
def test_should_repeat_until(self): questionnaire = { 'survey_id': '021', 'data_version': '0.0.1', 'sections': [{ 'id': 'section1', 'groups': [{ 'id': 'group-1', 'blocks': [{ 'id': 'block-1', 'questions': [{ 'id': 'question-2', 'answers': [{ 'id': 'my_answer', 'type': 'TextField' }] }] }] }] }] } schema = QuestionnaireSchema(questionnaire) # Given repeat = { 'type': 'until', 'when': [{ 'id': 'my_answer', 'condition': 'equals', 'value': 'Done' }] } answer_store = AnswerStore({}) current_path = [Location('group-1', 0, 'block-1')] # The schema doesn't actually contain a repeat clause, so fake it. with patch.object(schema, 'answer_is_in_repeating_group', return_value=True): self.assertEqual( evaluate_repeat(repeat, answer_store, schema, current_path), 1) answer_store.add_or_update( Answer(answer_id='my_answer', value='Not Done', group_instance=0, group_instance_id=None)) self.assertEqual( evaluate_repeat(repeat, answer_store, schema, current_path), 2) answer_store.add_or_update( Answer(answer_id='my_answer', value='Done', group_instance=1, group_instance_id=None)) self.assertEqual( evaluate_repeat(repeat, answer_store, schema, current_path), 2)
def test_repeat_on_answer_count_when_not_all_answers_on_path(self): schema = load_schema_from_params('test', 'routing_repeat_until') primary_group_instance_id = str(uuid.uuid4()) repeating_group_1_instance_id = str(uuid.uuid4()) repeating_group_2_instance_id = str(uuid.uuid4()) answer_store = AnswerStore({}) answer_store.add_or_update( Answer(answer_id='primary-name', value='Jon', group_instance=0, group_instance_id=primary_group_instance_id)) answer_store.add_or_update( Answer( answer_id='repeating-anyone-else', answer_instance=0, value='Yes', group_instance=0, )) answer_store.add_or_update( Answer(answer_id='repeating-name', answer_instance=0, value='Adam', group_instance=0, group_instance_id=repeating_group_1_instance_id)) answer_store.add_or_update( Answer( answer_id='repeating-anyone-else', answer_instance=0, value='No', group_instance=1, )) answer_store.add_or_update( Answer(answer_id='repeating-name', answer_instance=0, value='Ben', group_instance=1, group_instance_id=repeating_group_2_instance_id)) answer_store.add_or_update( Answer( answer_id='repeating-anyone-else', answer_instance=0, value='No', group_instance=2, )) routing_path = [ Location('primary-group', 0, 'primary-name-block'), Location('repeating-group', 0, 'repeating-anyone-else-block'), Location('repeating-group', 0, 'repeating-name-block'), Location('repeating-group', 1, 'repeating-anyone-else-block'), ] repeat = { 'type': 'answer_count', 'answer_ids': ['primary-name', 'repeating-name'] } # When number_of_repeats = evaluate_repeat(repeat, answer_store, schema, routing_path) self.assertEqual(number_of_repeats, 2)