class TestEntityShouldExistValidator(TestCase): def setUp(self): self.dbm = Mock(spec=DatabaseManager) self.get_by_short_code_patcher = patch( 'mangrove.contrib.delete_validators.get_by_short_code') self.get_by_short_code_mock = self.get_by_short_code_patcher.start() self.validator = EntityShouldExistValidator() self.field1 = HierarchyField('a', 'entity_type', 'a') self.field2 = ShortCodeField('b', 'entity_id', 'b') self.field1.set_value('clinic') self.field2.set_value('cli01') self.fields = [self.field1, self.field2] self.entity_type = 'clinic' self.entity_id = 'cli01' self.values = { 'entity_type': self.entity_type, 'entity_id': self.entity_id } def tearDown(self): self.get_by_short_code_patcher.stop() def test_should_give_error_if_entity_does_not_exist(self): self.get_by_short_code_mock.side_effect = DataObjectNotFound( 'Entity', "Unique Identification Number (ID)", self.entity_id) exception = DataObjectNotFound("Entity", "Unique Identification Number (ID)", self.entity_id) errors = OrderedDict() errors['entity_type'] = exception.message errors['entity_id'] = exception.message self.assertEqual( errors, self.validator.validate(self.values, self.fields, self.dbm)) self.get_by_short_code_mock.assert_called_once_with( self.dbm, self.entity_id, [self.entity_type]) def test_should_not_give_error_if_entity_exist(self): self.assertEqual( OrderedDict(), self.validator.validate(self.values, self.fields, self.dbm)) self.get_by_short_code_mock.assert_called_once_with( self.dbm, self.entity_id, [self.entity_type]) def test_should_create_entity_should_exist_validator_from_json(self): validator_json = {'cls': ValidatorTypes.ENTITY_SHOULD_EXIST} self.assertTrue( isinstance(validator_factory(validator_json), EntityShouldExistValidator)) def test_entity_should_exist_validators_should_be_serializable(self): expected_json = {'cls': ValidatorTypes.ENTITY_SHOULD_EXIST} self.assertEqual(expected_json, self.validator.to_json())
def test_location_field(self): field = HierarchyField(LOCATION_TYPE_FIELD_NAME, "some_code", "some label") location_field = FormField().create(field) self.assertEquals(location_field.widget.attrs['class'], 'location_field') self.assertEquals(location_field.widget.attrs['watermark'], '')
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, "appearance": None, "constraint_message": None, "default": None, "hint": None, "xform_constraint": None, "relevant": None } 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 _get_location_field(self): location_field = HierarchyField(name=LOCATION_TYPE_FIELD_NAME, code=LOCATION_TYPE_FIELD_CODE, label="anything", ddtype=Mock(spec=DataDictType)) return location_field
def _create_location_question(self, post_dict, code): return HierarchyField(name=LOCATION_TYPE_FIELD_NAME, code=code, label=post_dict["title"], instruction=post_dict.get("instruction"), 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 setUp(self): self.dbm = Mock(spec=DatabaseManager) self.get_by_short_code_patcher = patch( 'mangrove.contrib.delete_validators.get_by_short_code') self.get_by_short_code_mock = self.get_by_short_code_patcher.start() self.validator = EntityShouldExistValidator() self.field1 = HierarchyField('a', 'entity_type', 'a') self.field2 = ShortCodeField('b', 'entity_id', 'b') self.field1.set_value('clinic') self.field2.set_value('cli01') self.fields = [self.field1, self.field2] self.entity_type = 'clinic' self.entity_id = 'cli01' self.values = { 'entity_type': self.entity_type, 'entity_id': self.entity_id }
def _construct_global_deletion_form(manager): question1 = HierarchyField(name=ENTITY_TYPE_FIELD_NAME, code=ENTITY_TYPE_FIELD_CODE, label="What is the entity type" , instruction="Enter a type for the entity") question2 = ShortCodeField(name=SHORT_CODE_FIELD, code=SHORT_CODE, label="What is the entity's Unique ID Number", defaultValue="some default value" , instruction="Enter the id of the entity you want to delete", constraints=[]) form_model = EntityFormModel(manager, name=ENTITY_DELETION_FORM_CODE, form_code=ENTITY_DELETION_FORM_CODE, fields=[ question1, question2], entity_type=["deletion"], validators=[MandatoryValidator(), EntityShouldExistValidator()]) return form_model
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.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 test_should_create_list_field_type_for_default_english_language(self): expected_json = { "label": { "eng": "What is your location" }, "name": "loc", "instruction": "Answer is list", "code": "Q1", "type": "list", "ddtype": self.DDTYPE_JSON, } field = HierarchyField(name="loc", code="Q1", label="What is your location", language="eng", ddtype=self.ddtype, instruction="Answer is list") actual_json = field._to_json() self.assertEqual(actual_json, expected_json)
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 _create_location_question(self, post_dict, code): return HierarchyField(name=LOCATION_TYPE_FIELD_NAME, code=code, label=post_dict["title"], instruction=post_dict.get("instruction"), parent_field_code=post_dict.get('parent_field_code'))
def _get_location_field(self): location_field = HierarchyField(name=LOCATION_TYPE_FIELD_NAME, code=LOCATION_TYPE_FIELD_CODE, label="anything") return location_field
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 _construct_registration_form(manager): 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') description_type = get_or_create_data_dict(manager, name='description Type', slug='description', primitive_type='string') mobile_number_type = get_or_create_data_dict(manager, name='Mobile Number Type', slug='mobile_number', primitive_type='string') name_type = get_or_create_data_dict(manager, name='Name', slug='name', primitive_type='string') entity_id_type = get_or_create_data_dict(manager, name='Entity Id Type', slug='entity_id', primitive_type='string') #Create registration questionnaire question1 = HierarchyField(name=ENTITY_TYPE_FIELD_NAME, code=ENTITY_TYPE_FIELD_CODE, label="What is associated subject type?", language="eng", ddtype=entity_id_type, instruction="Enter a type for the subject") question2 = TextField(name=NAME_FIELD, code=NAME_FIELD_CODE, label="What is the subject's name?", defaultValue="some default value", language="eng", ddtype=name_type, instruction="Enter a subject name") question3 = TextField(name=SHORT_CODE_FIELD, code=SHORT_CODE, label="What is the subject's Unique ID Number", defaultValue="some default value", language="eng", ddtype=name_type, instruction="Enter a id, or allow us to generate it", entity_question_flag=True) question4 = HierarchyField( name=LOCATION_TYPE_FIELD_NAME, code=LOCATION_TYPE_FIELD_CODE, label="What is the subject's location?", language="eng", ddtype=location_type, instruction="Enter a region, district, or commune") question5 = GeoCodeField(name=GEO_CODE_FIELD, code=GEO_CODE, label="What is the subject's GPS co-ordinates?", language="eng", ddtype=geo_code_type, instruction="Enter lat and long. Eg 20.6, 47.3") question6 = TextField( name=DESCRIPTION_FIELD, code=DESCRIPTION_FIELD_CODE, label="Describe the subject", defaultValue="some default value", language="eng", ddtype=description_type, instruction="Describe your subject in more details (optional)") question7 = TextField( name=MOBILE_NUMBER_FIELD, code=MOBILE_NUMBER_FIELD_CODE, label="What is the mobile number associated with the subject?", defaultValue="some default value", language="eng", ddtype=mobile_number_type, instruction="Enter the subject's number") form_model = FormModel(manager, name="reg", form_code=REGISTRATION_FORM_CODE, fields=[ question1, question2, question3, question4, question5, question6, question7 ], entity_type=["Registration"]) return form_model
def _create_location_question(self, post_dict, ddtype, code): return HierarchyField(name=LOCATION_TYPE_FIELD_NAME, code=code, label=post_dict["title"], ddtype=ddtype, instruction=post_dict.get("instruction"))
def construct_global_registration_form(manager): question1 = HierarchyField(name=ENTITY_TYPE_FIELD_NAME, code=ENTITY_TYPE_FIELD_CODE, label="What is associated subject type?", instruction="Enter a type for the subject") question2 = TextField(name=NAME_FIELD, code=NAME_FIELD_CODE, label="What is the subject's name?", defaultValue="some default value", instruction="Enter a subject name", constraints=[TextLengthConstraint(max=80)], required=False) question3 = ShortCodeField( name=SHORT_CODE_FIELD, code=SHORT_CODE, label="What is the subject's Unique ID Number", defaultValue="some default value", instruction="Enter a id, or allow us to generate it", constraints=[ TextLengthConstraint(max=12), ShortCodeRegexConstraint(reg='^[a-zA-Z0-9]+$') ], required=False) question4 = 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) question5 = 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) question6 = TelephoneNumberField( name=MOBILE_NUMBER_FIELD, code=MOBILE_NUMBER_FIELD_CODE, label="What is the mobile number associated with the subject?", defaultValue="some default value", instruction="Enter the subject's number", constraints=(_create_constraints_for_mobile_number()), required=True) question7 = TextField(name=EMAIL_FIELD, code=EMAIL_FIELD, label="What is the subject's email", defaultValue="", instruction="Enter email id", constraints=[TextLengthConstraint(max=50)], required=False) question8 = BooleanField(name=IS_DATASENDER_FIELD_CODE, code=IS_DATASENDER_FIELD_CODE, label="Am I a data sender", defaultValue=True, required=False) form_model = EntityFormModel( manager, name=GLOBAL_REGISTRATION_FORM_CODE, form_code=REGISTRATION_FORM_CODE, fields=[ question1, question2, question3, question4, question5, question6, question7, question8 ], is_registration_model=True, entity_type=["registration"], validators=[ MandatoryValidator(), MobileNumberValidationsForReporterRegistrationValidator() ]) return form_model