def test_should_retain_value_when_calculated_value_is_not_NaN(self): field = TextField(name='height_calculate', code='height_calculate', label='My calculated question', is_calculated=True) field.set_value("calculate_value") self.assertEqual(field.value, "calculate_value")
def test_should_update_value_to_empty_string_when_calculated_value_is_NaN( self): field = TextField(name='height_calculate', code='height_calculate', label='My calculated question', is_calculated=True) field.set_value("NaN") self.assertEqual(field.value, "")
def test_should_create_multi_select_field_type_for_default_english_language( self): expected_json = { "label": "What is your favorite color", "name": "color", "choices": [{ "text": "RED", "val": 'a' }, { "text": "YELLOW", "val": 'b' }, { "text": 'green', 'val': 'green' }], "code": "Q3", "parent_field_code": None, "type": "select", "required": True, "instruction": "test_instruction", "appearance": None, "constraint_message": None, "default": None, "hint": None, "xform_constraint": None, "relevant": None, "is_cascade": False } field = SelectField(name="color", code="Q3", label="What is your favorite color", options=[("RED", 'a'), ("YELLOW", 'b'), ('green')], single_select_flag=False, instruction="test_instruction") actual_json = field._to_json() self.assertEqual(actual_json, expected_json) field.set_value(field.validate('ab')) self.assertEqual("RED,YELLOW", field.convert_to_unicode())
def test_should_create_integer_field_type_for_default_english_language( self): expected_json = { "label": "What is your age", "name": "Age", "code": "Q2", "type": "integer", 'parent_field_code': None, "required": True, "instruction": "test_instruction" } field = IntegerField(name="Age", code="Q2", label="What is your age", instruction="test_instruction") actual_json = field._to_json() self.assertEqual(actual_json, expected_json) field.set_value(123) self.assertEqual("123", field.convert_to_unicode())
def test_should_create_list_field_type_for_default_english_language(self): expected_json = { "label": "What is your location", "name": "loc", "instruction": "Answer is list", 'parent_field_code': None, "code": "Q1", "type": "list", "required": True, } field = HierarchyField(name="loc", code="Q1", label="What is your location", instruction="Answer is list") actual_json = field._to_json() self.assertEqual(actual_json, expected_json) field.set_value(["abc", "def"]) self.assertEqual("abc,def", field.convert_to_unicode())
def test_should_create_location_field_type_for_default_english_language( self): expected_json = { "label": "Where do you stay?", "name": "field1_Loc", "code": "Q1", "type": "geocode", 'parent_field_code': None, "required": True, "instruction": "test_instruction" } field = GeoCodeField( name="field1_Loc", code="Q1", label="Where do you stay?", instruction="test_instruction", ) actual_json = field._to_json() self.assertEqual(actual_json, expected_json) field.set_value(field.validate("23,23")) self.assertEqual("23.0, 23.0", field.convert_to_unicode())
def test_should_create_select_one_field_type_for_default_english_language( self): expected_json = { "label": "What is your favorite color", "name": "color", "choices": [{ "text": "RED", "val": 'a' }, { "text": "YELLOW", "val": 'b' }, { "text": 'green', "val": 'c' }], "code": "Q3", 'parent_field_code': None, "type": "select1", "required": True, "instruction": None } field = SelectField(name="color", code="Q3", label="What is your favorite color", options=[("RED", 'a'), ("YELLOW", 'b'), ('green', 'c')]) actual_json = field._to_json() self.assertEqual(actual_json, expected_json) field.set_value(field.validate('b')) self.assertEqual("YELLOW", field.convert_to_unicode())
def bind(self, submission): self.submission = submission for field in self.fields: answer = self._lookup_answer_for_field_code( self.submission, field.code) field.set_value(answer)