def test_should_not_validate_no_values_sent_for_choice(self): with self.assertRaises(AnswerHasNoValuesException): constraint = ChoiceConstraint( single_select_constraint=True, list_of_valid_choices=["village", "urban"], code="Q1") constraint.validate("")
def test_should_invalidate_special_characters_sent_for_choice(self): with self.assertRaises(AnswerNotInListException): constraint = ChoiceConstraint( single_select_constraint=False, list_of_valid_choices=["village", "urban", "city", "country"], code="Q1") constraint.validate("a!b")
def test_should_validate_multiple_choice(self): constraint = ChoiceConstraint( single_select_constraint=False, list_of_valid_choices=["village", "urban"], code="Q1") v_data = constraint.validate("ab") self.assertEquals(v_data, ["village", "urban"])
def test_should_not_validate_wrong_choice(self): with self.assertRaises(AnswerNotInListException): constraint = ChoiceConstraint( single_select_constraint=True, list_of_valid_choices=["village", "urban"], code="Q1") constraint.validate("c")
def test_should_invalidate_answer_with_2_numbers(self): valid_choices = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1a", "1b", "1c" ] with self.assertRaises(AnswerNotInListException): constraint = ChoiceConstraint(single_select_constraint=False, list_of_valid_choices=valid_choices, code="Q1") constraint.validate("abc1b341c")
def test_should_not_validate_wrong_choice(self): with self.assertRaises(AnswerNotInListException): valid_choices = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1a", "1b", "1c" ] constraint = ChoiceConstraint(single_select_constraint=True, list_of_valid_choices=valid_choices, code="Q1") constraint.validate("1d")
def test_should_not_validate_answer_with_one_letter_followed_by_one_number_on_a_single_choice( self): valid_choices = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1a", "1b", "1c" ] with self.assertRaises(AnswerNotInListException): constraint = ChoiceConstraint(single_select_constraint=True, list_of_valid_choices=valid_choices, code="Q1") constraint.validate("a1")
def test_should_not_validate_multiple_values_sent_for_single_choice(self): with self.assertRaises(AnswerHasTooManyValuesException): valid_choices = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "1a", "1b", "1c" ] constraint = ChoiceConstraint(single_select_constraint=True, list_of_valid_choices=valid_choices, code="Q1") self.assertEqual(constraint.validate("1b"), ['1b']) constraint.validate("a1a")
def __init__(self, name, code, label, options, ddtype, instruction=None, language=field_attributes.DEFAULT_LANGUAGE, single_select_flag=True): assert len(options) > 0 type = field_attributes.SELECT_FIELD if single_select_flag else field_attributes.MULTISELECT_FIELD self.SINGLE_SELECT_FLAG = single_select_flag Field.__init__(self, type=type, name=name, code=code, label=label, language=language, ddtype=ddtype, instruction=instruction) self._dict[self.OPTIONS] = [] valid_choices = self._dict[self.OPTIONS] if options is not None: for option in options: if isinstance(option, tuple): single_language_specific_option = { 'text': { language: option[0] }, 'val': option[1] } elif isinstance(option, dict): single_language_specific_option = option else: single_language_specific_option = { 'text': { language: option } } valid_choices.append(single_language_specific_option) self.constraint = ChoiceConstraint( list_of_valid_choices=[ each.get('text').get(language) for each in valid_choices ], single_select_constraint=single_select_flag, code=code)
def __init__(self, name, code, label, options, instruction=None, single_select_flag=True, required=True, parent_field_code=None, has_other=False): assert len(options) > 0 type = field_attributes.SELECT_FIELD if single_select_flag else field_attributes.MULTISELECT_FIELD self.single_select_flag = single_select_flag Field.__init__(self, type=type, name=name, code=code, label=label, instruction=instruction, required=required, parent_field_code=parent_field_code) self._dict[self.OPTIONS] = [] valid_choices = self._dict[self.OPTIONS] if has_other: self._dict['has_other'] = has_other if options is not None: for option in options: if isinstance(option, tuple): single_language_specific_option = { 'text': option[0], 'val': option[1] } elif isinstance(option, dict): single_language_specific_option = option else: single_language_specific_option = { 'text': option, 'val': option } valid_choices.append(single_language_specific_option) self.constraint = ChoiceConstraint( list_of_valid_choices=valid_choices, single_select_constraint=single_select_flag, code=code, has_other=has_other)