Example #1
0
    def test_should_batch_get_form_models(self):
        fields = [ShortCodeField('name', 'eid', 'label')]
        form = EntityFormModel(
            self.manager,
            'test_form',
            'label',
            'form_code1',
            fields=fields,
            entity_type=['Clinic'],
            validators=[
                MandatoryValidator(),
                MobileNumberValidationsForReporterRegistrationValidator()
            ])
        form.save()

        fields = [ShortCodeField('name', 'eid', 'label')]
        form = EntityFormModel(
            self.manager,
            'test_form',
            'label',
            'form_code2',
            fields=fields,
            entity_type=['Clinic'],
            validators=[
                MandatoryValidator(),
                MobileNumberValidationsForReporterRegistrationValidator()
            ])
        form.save()

        forms = list_form_models_by_code(self.manager,
                                         ['form_code1', 'form_code2'])

        self.assertEqual(2, len(forms))
        self.assertEqual('form_code1', forms[0].form_code)
        self.assertEqual('form_code2', forms[1].form_code)
 def setUp(self):
     self.validator = MobileNumberValidationsForReporterRegistrationValidator(
     )
     self.field1 = ShortCodeField('t', 't', 't')
     self.field2 = TextField('m', 'm', 'm')
     self.fields = [self.field1, self.field2]
     self.dbm = Mock(spec=DatabaseManager)
     self.dbm.view = Mock()
     self.dbm.view.datasender_by_mobile = Mock(return_value=[{
         'doc': {
             "data": {
                 "mobile_number": {
                     "value": "123"
                 }
             },
             "short_code": "abc"
         }
     }])
Example #3
0
 def test_should_save_form_model_with_validators(self):
     fields = [ShortCodeField('name', 'eid', 'label')]
     form = EntityFormModel(
         self.manager,
         'test_form',
         'label',
         'foo',
         fields=fields,
         entity_type=['Clinic'],
         validators=[
             MandatoryValidator(),
             MobileNumberValidationsForReporterRegistrationValidator()
         ])
     form.save()
     form = get_form_model_by_code(self.manager, 'foo')
     self.assertEqual(2, len(form.validators))
     self.assertTrue(isinstance(form.validators[0], MandatoryValidator))
     self.assertTrue(
         isinstance(
             form.validators[1],
             MobileNumberValidationsForReporterRegistrationValidator))
class TestMobileNumberMandatoryValidationsForReporterRegistrationValidator(
        unittest.TestCase):
    def setUp(self):
        self.validator = MobileNumberValidationsForReporterRegistrationValidator(
        )
        self.field1 = ShortCodeField('t', 't', 't')
        self.field2 = TextField('m', 'm', 'm')
        self.fields = [self.field1, self.field2]
        self.dbm = Mock(spec=DatabaseManager)
        self.dbm.view = Mock()
        self.dbm.view.datasender_by_mobile = Mock(return_value=[{
            'doc': {
                "data": {
                    "mobile_number": {
                        "value": "123"
                    }
                },
                "short_code": "abc"
            }
        }])

    def test_should_return_error_dict_if_mobile_number_allready_exist(self):
        entity_mock = Mock()
        entity_mock.value.return_value = '123'
        values = dict(t='reporter', m='123')
        error_dict = self.validator.validate(values, self.fields, self.dbm)
        self.assertEqual(1, len(error_dict))
        self.assertTrue('m' in error_dict.keys())

    def test_should_create_mobile_number_mandatory_for_reporter_validator_from_json(
            self):
        validator_json = {
            'cls': ValidatorTypes.MOBILE_NUMBER_MANDATORY_FOR_REPORTER
        }

        self.assertTrue(
            isinstance(
                validator_factory(validator_json),
                MobileNumberValidationsForReporterRegistrationValidator))

    def test_mobile_number_mandatory_for_reporter_validator_should_be_serializable(
            self):
        expected_json = {
            'cls': ValidatorTypes.MOBILE_NUMBER_MANDATORY_FOR_REPORTER
        }
        self.assertEqual(expected_json, self.validator.to_json())

    def test_should_return_error_if_mobile_number_comes_in_epsilon_format_from_excel_file(
            self):
        entity_mock = Mock()
        entity_mock.value.return_value = '266123321435'
        values = dict(t='reporter', m='2.66123321435e+11')
        error_dict = self.validator.validate(values, self.fields, self.dbm)
        self.assertEqual(1, len(error_dict))
        self.assertTrue('m' in error_dict.keys())

    def test_should_return_error_if_mobile_number_has_hyphens_from_excel_file(
            self):
        entity_mock = Mock()
        entity_mock.value.return_value = '266123321435'
        values = dict(t='reporter', m='266-123321435')
        error_dict = self.validator.validate(values, self.fields, self.dbm)
        self.assertEqual(1, len(error_dict))
        self.assertTrue('m' in error_dict.keys())

    def test_should_return_error_if_mobile_number_comes_as_floating_point_number_from_excel_file(
            self):
        entity_mock = Mock()
        entity_mock.value.return_value = '266123321435'
        values = dict(t='reporter', m='266123321435.0')
        error_dict = self.validator.validate(values, self.fields, self.dbm)
        self.assertEqual(1, len(error_dict))
        self.assertTrue('m' in error_dict.keys())
Example #5
0
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