def test_should_give_fields_codes_for_multiple_geocode_questionnaire(self): GEO_CODE1 = "gc" form_model = self._get_form_model() form_model.add_field(GeoCodeField(None, GEO_CODE, "label")) form_model.add_field(GeoCodeField(None, GEO_CODE1, "label1")) self.assertEqual([GEO_CODE, GEO_CODE1], get_geo_code_fields_question_code(form_model))
def _create_geo_code_question(self, post_dict, ddtype, code): return GeoCodeField(name=self._get_name(post_dict), code=code, label=post_dict["title"], ddtype=ddtype, instruction=post_dict.get("instruction"), required=post_dict.get("required"))
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", "appearance": None, "constraint_message": None, "default": None, "hint": None, "xform_constraint": None, "relevant": None } 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_validate_location_with_whitespaces(self): expect_lat_long = (89.1, 100.1) field = GeoCodeField(name="field1_Loc", code="Q1", label="Where do you stay?") actual_lat_long = field.validate(lat_long_string="89.1 100.1") self.assertEqual(expect_lat_long, actual_lat_long)
def test_should_accept_submission_with_empty_fields(self): submission_data = u''' <hindimai-055> <name/> <age/> <dob/> <location/> <form_code>055</form_code> </hindimai-055> ''' expected_values = { 'name': None, 'age': None, 'dob': None, 'location': None } with patch("mangrove.transport.player.parser.get_form_model_by_code" ) as mock_get_form_model: mock_form_model = Mock() mock_form_model.fields = [ TextField('', 'name', ''), GeoCodeField('', 'location', '', {'': ''}), DateField('', 'dob', '', 'mm.yyyy'), IntegerField('', 'age', '') ] mock_get_form_model.return_value = mock_form_model self.assertEquals(self.parser.parse(submission_data), ('055', expected_values))
def test_should_return_geocode_format(self): type = DataDictType(Mock(DatabaseManager), name="date type") field = GeoCodeField(name="What is the place?", code="dat", label="naam", ddtype=type) preview = helper.get_preview_for_field(field) self.assertEqual("xx.xxxx yy.yyyy", preview["constraints"])
def _create_geo_code_question(self, post_dict, code): return GeoCodeField(name=self._get_name(post_dict, code), code=code, label=post_dict["title"], instruction=post_dict.get("instruction"), required=post_dict.get("required"), parent_field_code=post_dict.get('parent_field_code'), hint=post_dict.get('hint'), constraint_message=post_dict.get('constraint_message'), appearance=post_dict.get('appearance'), default=post_dict.get('default'), xform_constraint=post_dict.get('xform_constraint'), relevant=post_dict.get('relevant'))
def test_should_give_geo_code(self): form_model = self._get_form_model() form_model.add_field( GeoCodeField(None, GEO_CODE, "label", ddtype=Mock(spec=DataDictType))) self.assertEqual(GEO_CODE, get_geo_code_field_question_code(form_model))
def test_should_validate_location(self): expect_lat_long = (89.1, 100.1) field = GeoCodeField(name="field1_Loc", code="Q1", label="Where do you stay?", ddtype=self.ddtype, language="eng") actual_lat_long = field.validate(lat_long_string="89.1 100.1") self.assertEqual(expect_lat_long, actual_lat_long)
def test_gps_field(self): field = GeoCodeField("gps 1", "gps1", "gps of this") geo_code_field = FormField().create(field) self.assertEquals(1, len(geo_code_field.validators)) self.assertTrue( isinstance(geo_code_field.validators[0], GeoCodeValidator)) self.assertEquals(geo_code_field.widget.attrs["watermark"], "xx.xxxx,yy.yyyy") self.assertIsNone(geo_code_field.widget.attrs.get('class'))
def test_should_split_gps_field_based_on_comma(self): field = GeoCodeField( name="field1_Loc", code="Q1", label="Where do you stay?", instruction="test_instruction", ) expected = (12.32, 14.32) seperated_values = field.formatted_field_values_for_excel( '12.32,14.32') self.assertEqual(expected, seperated_values)
def test_convert_survey_response_values_to_dict_format(self): survey_response_doc = SurveyResponseDocument( values={ 'q1': '23', 'q2': 'sometext', 'q3': 'a', 'geo': '2.34,5.64', 'date': '12.12.2012' }) survey_response = SurveyResponse(Mock()) survey_response._doc = survey_response_doc int_field = IntegerField(name='question one', code='q1', label='question one', ddtype=Mock(spec=DataDictType)) text_field = TextField(name='question two', code='q2', label='question two', ddtype=Mock(spec=DataDictType)) single_choice_field = SelectField(name='question three', code='q3', label='question three', options=[("one", "a"), ("two", "b"), ("three", "c"), ("four", "d")], single_select_flag=True, ddtype=Mock(spec=DataDictType)) geo_field = GeoCodeField(name='geo', code='GEO', label='geo', ddtype=Mock()) date_field = DateField(name='date', code='DATE', label='date', date_format='dd.mm.yyyy', ddtype=Mock()) questionnaire_form_model = Mock(spec=FormModel) questionnaire_form_model.form_code = 'test_form_code' questionnaire_form_model.fields = [ int_field, text_field, single_choice_field, geo_field, date_field ] request_dict = construct_request_dict(survey_response, questionnaire_form_model) expected_dict = OrderedDict({ 'q1': '23', 'q2': 'sometext', 'q3': 'a', 'GEO': '2.34,5.64', 'DATE': '12.12.2012', 'form_code': 'test_form_code' }) self.assertEqual(expected_dict, request_dict)
def test_should_give_error_for_invalid_location(self): field = GeoCodeField(name="field1_Loc", code="Q1", label="Where do you stay?") with self.assertRaises(GeoCodeFormatException) as e: field.validate(lat_long_string="89.1") self.assertEquals(("89.1", ), e.exception.data) with self.assertRaises(RequiredFieldNotPresentException): field.validate(lat_long_string=" ") with self.assertRaises(RequiredFieldNotPresentException): field.validate(lat_long_string="") with self.assertRaises(RequiredFieldNotPresentException): field.validate(lat_long_string=None)
def form(self): manager = Mock(spec=DatabaseManager) question4 = HierarchyField(name=LOCATION_TYPE_FIELD_NAME, code=LOCATION_TYPE_FIELD_CODE, label=LOCATION_TYPE_FIELD_NAME) question5 = GeoCodeField( name=GEO_CODE_FIELD_NAME, code=GEO_CODE, label="What is the subject's GPS co-ordinates?") form_model = FormModel(manager, name="asd", form_code="asd", fields=[question4, question5]) return form_model
def setUp(self): self.dbm = Mock(spec=DatabaseManager) q1 = UniqueIdField('clinic', name="entity_question", code="ID", label="What is associated entity") q2 = TextField(name="question1_Name", code="Q1", label="What is your name", defaultValue="some default value", constraints=[TextLengthConstraint(5, 10)], required=False) q3 = IntegerField( name="Father's age", code="Q2", label="What is your Father's Age", constraints=[NumericRangeConstraint(min=15, max=120)], required=False) q4 = SelectField(name="Color", code="Q3", label="What is your favourite color", options=[("RED", 'a'), ("YELLOW", 'b')], required=False) q5 = TextField(name="Desc", code="Q4", label="Description", required=False) self.event_time_field_code = "Q6" q6 = DateField(name="Event time", code=self.event_time_field_code, label="Event time field", date_format="%m.%d.%Y", required=False) q7 = GeoCodeField(name="My Location", code="loc", label="Geo Location Field", required=False) self.form_model = FormModel(self.dbm, name="aids", label="Aids form_model", form_code="1", fields=[q1, q2, q3, q4, q5, q6, q7]) self.form_model_patch = patch( 'mangrove.form_model.form_model.FormModel') self.form_model_document_patch = patch( 'mangrove.form_model.form_model.FormModelDocument')
def test_should_create_submission_form_with_appropriate_fields(self): fields = [ IntegerField('field_name', 'integer_field_code', 'label', Mock), DateField('Date', 'date_field_code', 'date_label', 'dd.mm.yyyy', Mock), GeoCodeField('', 'geo_field_code', '', Mock), TextField('', 'text_field_code', '') ] type(self.project).fields = PropertyMock(return_value=fields) submission_form_create = SurveyResponseForm(self.project, {}) expected_field_keys = [ 'form_code', 'dsid', 'integer_field_code', 'date_field_code', 'geo_field_code', 'text_field_code' ] self.assertListEqual(submission_form_create.fields.keys(), expected_field_keys)
def test_gps_field(self): field = GeoCodeField( "gps 1", "gps1", "gps of this", "Answer must be GPS coordinates in the following format (latitude,longitude). Example: -18.1324,27.6547" ) geo_code_field = FormField().create(field) self.assertEquals(1, len(geo_code_field.validators)) self.assertTrue( isinstance(geo_code_field.validators[0], GeoCodeValidator)) self.assertEquals(geo_code_field.widget.attrs["watermark"], "xx.xxxx,yy.yyyy") self.assertIsNone(geo_code_field.widget.attrs.get('class')) self.assertEqual( field.instruction, "Answer must be GPS coordinates in the following format (latitude,longitude). Example: -18.1324,27.6547" )
def setUp(self): self.validator = AtLeastOneLocationFieldMustBeAnsweredValidator() self.field1 = HierarchyField( name=LOCATION_TYPE_FIELD_NAME, code=LOCATION_TYPE_FIELD_CODE, label="What is the subject's location?", instruction="Enter a region, district, or commune", required=False) self.field2 = GeoCodeField( name=GEO_CODE_FIELD_NAME, code=GEO_CODE, label="What is the subject's GPS co-ordinates?", instruction="Enter lat and long. Eg 20.6, 47.3", required=False) self.field3 = TextField('a', 'a', 'a') self.field4 = TextField('b', 'b', 'b') self.fields = [self.field1, self.field2, self.field3, self.field4]
def setUp(self): self.form_model = MagicMock(spec=FormModel, id="1212") self.field1 = UniqueIdField(unique_id_type='clinic', name='unique_id', code="q1", label='which clinic') self.field2 = TextField('text', "q2", "enter text") self.field3 = GeoCodeField('gps', "q3", "enter gps") self.field4 = SelectField('select', 'q4', 'enter one', [{ 'text': 'one', 'val': 'a' }, { 'text': 'two', 'val': 'b' }], single_select_flag=False) self.field5 = DateField('date', 'q5', 'enter date', 'mm.dd.yyyy')
def create_test_fields_and_survey_for_all_fields_type(self): name = TextField('name', 'name' ,'What is your name?') degree = TextField('degree', 'degree' ,'Degree name') completed_on = DateField('completed_on', 'completed_on','Degree completion year', 'dd.mm.yyyy') education = FieldSet('education', 'education', 'Education', field_set=[degree,completed_on]) age = IntegerField('age', 'age' ,'What is your age?') opt_fav_col = [('red','Red'), ('blue','Blue'),('c','Green')] fav_col = SelectField('fav_color', 'fav_color', 'Which colors you like?', opt_fav_col) opt_pizza_col = [('yes', 'Yes'),('no','No')] pizza_fan = SelectField('pizza_fan', 'pizza_fan', 'Do you like pizza?', opt_pizza_col) other = TextField('other', 'other' ,'What else you like?') pizza_type = TextField('pizza_type', 'pizza_type' ,'Which pizza type you like?') location = GeoCodeField('location', 'location' ,'Your location?') form_fields = [name, education, age, fav_col, pizza_fan, other, pizza_type, location] # todo how required will be handled survey_response_values = {'name': 'Santa', 'pizza_type': None, 'age': '30', 'other': 'Samosa', 'location': '4.9158,11.9531', 'education': [{'completed_on': u'10.02.2014', 'degree': 'SantaSSC'}], 'pizza_fan': 'yes', 'fav_color': 'red blue'} return form_fields, survey_response_values
def test_should_create_submission_form_with_appropriate_fields(self): fields = [ IntegerField('field_name', 'integer_field_code', 'label', Mock), DateField('Date', 'date_field_code', 'date_label', 'dd.mm.yyyy', Mock), GeoCodeField('', 'geo_field_code', '', Mock), TextField('', 'text_field_code', '', Mock) ] type(self.form_model).entity_question = PropertyMock(return_value=None) type(self.form_model).fields = PropertyMock(return_value=fields) submission_form_create = EditSubmissionForm(self.manager, self.project, self.form_model, {}) expected_field_keys = [ 'form_code', 'integer_field_code', 'date_field_code', 'geo_field_code', 'text_field_code' ] self.assertListEqual(submission_form_create.fields.keys(), expected_field_keys)
def _create_registration_form(manager, entity_name=None, form_code=None, entity_type=None): code_generator = question_code_generator() location_type = _get_or_create_data_dict(manager, name='Location Type', slug='location', primitive_type='string') geo_code_type = _get_or_create_data_dict(manager, name='GeoCode Type', slug='geo_code', primitive_type='geocode') name_type = _get_or_create_data_dict(manager, name='Name', slug='name', primitive_type='string') mobile_number_type = _get_or_create_data_dict(manager, name='Mobile Number Type', slug='mobile_number', primitive_type='string') question1 = TextField(name=FIRSTNAME_FIELD, code=code_generator.next(), label=_("What is the %(entity_type)s's first name?") % {'entity_type': entity_name}, defaultValue="some default value", ddtype=name_type, instruction=_("Enter a %(entity_type)s first name") % {'entity_type': entity_name}) question2 = TextField(name=NAME_FIELD, code=code_generator.next(), label=_("What is the %(entity_type)s's last name?") % {'entity_type': entity_name}, defaultValue="some default value", ddtype=name_type, instruction=_("Enter a %(entity_type)s last name") % {'entity_type': entity_name}) question3 = HierarchyField(name=LOCATION_TYPE_FIELD_NAME, code=code_generator.next(), label=_("What is the %(entity_type)s's location?") % {'entity_type': entity_name}, ddtype=location_type, instruction=unicode(_("Enter a region, district, or commune"))) question4 = GeoCodeField(name=GEO_CODE_FIELD_NAME, code=code_generator.next(), label=_("What is the %(entity_type)s's GPS co-ordinates?") % {'entity_type': entity_name}, ddtype=geo_code_type, instruction=unicode(_("Answer must be GPS co-ordinates in the following format: xx.xxxx,yy.yyyy Example: -18.1324,27.6547 "))) question5 = TelephoneNumberField(name=MOBILE_NUMBER_FIELD, code=code_generator.next(), label=_("What is the %(entity_type)s's mobile telephone number?") % {'entity_type': entity_name}, defaultValue="some default value", ddtype=mobile_number_type, instruction=_( "Enter the (%(entity_type)s)'s number with the country code and telephone number. Example: 261333745269") % { 'entity_type': entity_name}, constraints=( _create_constraints_for_mobile_number())) question6 = TextField(name=SHORT_CODE_FIELD, code=code_generator.next(), label=_("What is the %(entity_type)s's Unique ID Number?") % {'entity_type': entity_name}, defaultValue="some default value", ddtype=name_type, instruction=unicode(_("Enter an id, or allow us to generate it")), entity_question_flag=True, constraints=[TextLengthConstraint(max=20)], required=False) questions = [question1, question2, question3, question4, question5, question6] form_model = FormModel(manager, name=entity_name, form_code=form_code, fields=questions, is_registration_model=True, entity_type=entity_type) return form_model
def setUp(self): self.form_model = MagicMock(spec=FormModel, id="1212") self.field1 = UniqueIdField(unique_id_type='clinic', name='unique_id', code="q1", label='which clinic') self.field2 = TextField('text', "q2", "enter text") self.field3 = GeoCodeField('gps', "q3", "enter gps") self.field4 = SelectField('select', 'q4', 'select multple options', [{ 'text': 'one', 'val': 'a' }, { 'text': 'two', 'val': 'b' }], single_select_flag=False) self.field5 = DateField('date', 'q5', 'enter date', 'mm.dd.yyyy') self.field6 = SelectField('select', 'single-select', 'select one option', [{ 'text': 'one', 'val': 'a' }, { 'text': 'two', 'val': 'b' }], single_select_flag=True) self.repeat_question = FieldSet(name="repeat", code="repeat-code", label="repeat-label", fieldset_type="repeat", field_set=[self.field4, self.field6]) self.group_question = FieldSet(name="group", code="group-code", label="group-label", fieldset_type="group", field_set=[self.field4, self.field6]) self.dbm = MagicMock(spec=DatabaseManager)
def test_should_create_location_field_type_for_default_english_language( self): expected_json = { "label": { "eng": "Where do you stay?" }, "name": "field1_Loc", "code": "Q1", "type": "geocode", "ddtype": self.DDTYPE_JSON, "instruction": "test_instruction" } field = GeoCodeField( name="field1_Loc", code="Q1", label="Where do you stay?", ddtype=self.ddtype, language="eng", instruction="test_instruction", ) actual_json = field._to_json() self.assertEqual(actual_json, expected_json)
def init_form_model_fields(self): self.eid_field = TextField(label="What is associated entity?", code="EID", name="What is associatéd entity?", entity_question_flag=True, ddtype=Mock()) self.rp_field = DateField( label="Report date", code="RD", name="What is réporting date?", date_format="dd.mm.yyyy", event_time_field_flag=True, ddtype=Mock(), instruction= "Answer must be a date in the following format: day.month.year. Example: 25.12.2011" ) self.symptoms_field = SelectField(label="Zhat are symptoms?", code="SY", name="Zhat are symptoms?", options=[("Rapid weight loss", "a"), ("Dry cough", "2b"), ("Pneumonia", "c"), ("Memory loss", "d"), ("Neurological disorders ", "e")], single_select_flag=False, ddtype=Mock()) self.blood_type_field = SelectField(label="What is your blood group?", code="BG", name="What is your blood group?", options=[("O+", "a"), ("O-", "b"), ("AB", "c"), ("B+", "d")], single_select_flag=True, ddtype=Mock()) self.gps_field = GeoCodeField(name="field1_Loc", code="gps", label="Where do you stay?", ddtype=Mock())
def test_should_parse_input_and_return_submission_values(self): form_code = 'someFormCode' submission_values = { 'data': { 'form_code': form_code, 'q1': 'a b c', 'q2': 'lat long alt accuracy', 'q3': '1012-01-23', 'q4': '1' } } expected_values = { 'q1': 'a b c', 'q2': 'lat,long', 'q3': '01.1012', 'q4': '1' } with patch( "mangrove.transport.player.parser.xmltodict") as mock_xml_dict: mock_xml_dict.parse.return_value = submission_values with patch( "mangrove.transport.player.parser.get_form_model_by_code" ) as mock_get_form_model: mock_form_model = Mock() mock_form_model.fields = [ SelectField('', 'q1', '', {'': ''}, single_select_flag=False), GeoCodeField('', 'q2', '', {'': ''}), DateField('', 'q3', '', 'mm.yyyy'), IntegerField('', 'q4', '') ] mock_get_form_model.return_value = mock_form_model self.assertEquals(self.parser.parse(submission_values), (form_code, expected_values))
def _create_geo_code_question(self, post_dict, code): return GeoCodeField(name=self._get_name(post_dict), code=code, label=post_dict["title"], instruction=post_dict.get("instruction"), required=post_dict.get("required"), parent_field_code=post_dict.get('parent_field_code'))
def test_should_give_geo_code(self): form_model = self._get_form_model() form_model.add_field(GeoCodeField(None, GEO_CODE, "label")) self.assertEqual([GEO_CODE], get_geo_code_fields_question_code(form_model))
def _create_registration_form(manager, entity_name=None, form_code=None, entity_type=None): code_generator = question_code_generator() question1 = TextField( name=FIRSTNAME_FIELD, code=code_generator.next(), label=_("What is the %(entity_type)s's first name?") % {'entity_type': entity_name}, defaultValue="some default value", instruction=_("Enter a %(entity_type)s first name") % {'entity_type': entity_name}) question2 = TextField(name=NAME_FIELD, code=code_generator.next(), label=_("What is the %(entity_type)s's last name?") % {'entity_type': entity_name}, defaultValue="some default value", instruction=_("Enter a %(entity_type)s last name") % {'entity_type': entity_name}) question3 = HierarchyField( name=LOCATION_TYPE_FIELD_NAME, code=code_generator.next(), label=_("What is the %(entity_type)s's location?") % {'entity_type': entity_name}, instruction=unicode(_("Enter a region, district, or commune"))) question4 = GeoCodeField( name=GEO_CODE_FIELD_NAME, code=code_generator.next(), label=_("What is the %(entity_type)s's GPS co-ordinates?") % {'entity_type': entity_name}, instruction=unicode( _("Answer must be GPS coordinates in the following format (latitude,longitude). Example: -18.1324,27.6547" ))) question5 = TelephoneNumberField( name=MOBILE_NUMBER_FIELD, code=code_generator.next(), label=_("What is the %(entity_type)s's mobile telephone number?") % {'entity_type': entity_name}, defaultValue="some default value", instruction= _("Enter the (%(entity_type)s)'s number with the country code and telephone number. Example: 261333745269" ) % {'entity_type': entity_name}, constraints=(_create_constraints_for_mobile_number())) question6 = ShortCodeField( name=SHORT_CODE_FIELD, code=code_generator.next(), label=_("What is the %(entity_type)s's Unique ID Number?") % {'entity_type': entity_name}, defaultValue="some default value", instruction=unicode(_("Enter an id, or allow us to generate it")), required=False) questions = [ question1, question2, question3, question4, question5, question6 ] form_model = EntityFormModel(manager, name=entity_name, form_code=form_code, fields=questions, is_registration_model=True, entity_type=entity_type) return form_model
def test_should_return_geocode_format(self): field = GeoCodeField(name="What is the place?", code="dat", label="naam") preview = helper.get_preview_for_field(field) self.assertEqual("xx.xxxx yy.yyyy", preview["constraints"])