Example #1
0
 def _create_form_model(self):
     self.entity_type = ["HealthFacility", "Clinic"]
     question1 = UniqueIdField('clinic',
                               name="entity_question",
                               code="ID",
                               label="What is associated entity")
     question2 = TextField(
         name="question1_Name",
         code="Q1",
         label="What is your name",
         defaultValue="some default value",
         constraints=[TextLengthConstraint(5, 10),
                      RegexConstraint("\w+")])
     question3 = IntegerField(
         name="Father's age",
         code="Q2",
         label="What is your Father's Age",
         constraints=[NumericRangeConstraint(min=15, max=120)])
     question4 = SelectField(name="Color",
                             code="Q3",
                             label="What is your favourite color",
                             options=[("RED", 'a'), ("YELLOW", 'b')])
     self.form_model = FormModelBuilder(
         self.manager, self.entity_type,
         "1").label("Aids form_model").name("aids").add_fields(
             question1, question2, question3, question4).build()
     self.form_model_id = self.form_model.id
Example #2
0
    def test_should_convert_field_with_constraints_to_json(self):
        constraints = [
            TextLengthConstraint(min=10, max=12),
            RegexConstraint("^[A-Za-z0-9]+$")
        ]
        field = TextField(name="test",
                          code='MC',
                          label='question',
                          constraints=constraints)
        expected_json = {
            "code": "MC",
            "name": "test",
            "defaultValue": "",
            "instruction": None,
            "label": "question",
            "type": "text",
            'parent_field_code': None,
            "length": {
                'max': 12,
                'min': 10
            },
            "regex": "^[A-Za-z0-9]+$",
            "required": True
        }

        self.assertEqual(expected_json, field_to_json(field))
Example #3
0
 def test_phone_number_field(self):
     field = TelephoneNumberField(
         'phone',
         'phone_code',
         'phone',
         constraints=[
             TextLengthConstraint(min=10, max=12),
             RegexConstraint(reg='^[0-9]+$')
         ],
         instruction=
         "Answer must be country code plus telephone number. Example: 261333745269"
     )
     phone_field = FormField().create(field)
     self.assertTrue(isinstance(phone_field, PhoneNumberField))
     self.assertEqual(len(phone_field.validators), 3)
     self.assertEqual(phone_field.widget.attrs['watermark'],
                      'Between 10 -- 12 characters')
     validator_types = []
     for validator in phone_field.validators:
         validator_types.append(type(validator))
     self.assertTrue(MinLengthValidator in validator_types)
     self.assertTrue(MaxLengthValidator in validator_types)
     self.assertTrue(RegexValidator in validator_types)
     self.assertEqual(
         field.instruction,
         "Answer must be country code plus telephone number. Example: 261333745269"
     )
Example #4
0
 def test_telephone_number_field_should_return_expected_json(self):
     mobile_number_length = TextLengthConstraint(max=15)
     mobile_number_pattern = RegexConstraint(reg='^[0-9]+$')
     field = TelephoneNumberField(
         name="test",
         code='MC',
         label='question',
         constraints=[mobile_number_length, mobile_number_pattern],
         instruction='')
     expected_json = {
         "label": "question",
         "name": "test",
         "code": "MC",
         'parent_field_code': None,
         "type": "telephone_number",
         "instruction": "",
         "constraints": [('length', {
             'max': 15
         }), ('regex', '^[0-9]+$')],
         "defaultValue": '',
         "required": True,
         "appearance": None,
         "constraint_message": None,
         "default": None,
         "hint": None,
         "xform_constraint": None,
         "relevant": None
     }
     self.assertEqual(expected_json, field._to_json())
Example #5
0
 def _create_summary_form_model(self):
     question1 = TextField(
         name="question1_Name",
         code="Q1",
         label="What is your name",
         defaultValue="some default value",
         constraints=[TextLengthConstraint(5, 10),
                      RegexConstraint("\w+")],
         ddtype=self.default_ddtype)
     question2 = IntegerField(
         name="Father's age",
         code="Q2",
         label="What is your Father's Age",
         constraints=[NumericRangeConstraint(min=15, max=120)],
         ddtype=self.default_ddtype)
     question3 = SelectField(name="Color",
                             code="Q3",
                             label="What is your favourite color",
                             options=[("RED", 1), ("YELLOW", 2)],
                             ddtype=self.default_ddtype)
     self.summary_form_model = FormModel(
         self.manager,
         entity_type=["reporter"],
         name="aids",
         label="Aids form_model",
         form_code=FORM_CODE_2,
         type="survey",
         fields=[question1, question2, question3])
     self.summary_form_model__id = self.summary_form_model.save()
Example #6
0
    def test_should_create_text_field_with_multiple_constraints(self):
        length_constraint = TextLengthConstraint(min=10, max=12)
        regex_constraint = RegexConstraint("^[A-Za-z0-9]+$")
        constraints = [length_constraint, regex_constraint]
        field = TextField(name="test",
                          code='MC',
                          label='question',
                          constraints=constraints)

        self.assertEqual(constraints, field.constraints)
Example #7
0
    def test_should_convert_text_in_epsilon_format_to_expanded_text(self):
        mobile_number_length = TextLengthConstraint(max=15)
        mobile_number_pattern = RegexConstraint(reg='^[0-9]+$')
        field = TelephoneNumberField(
            name="test",
            code='MC',
            label='question',
            constraints=[mobile_number_length, mobile_number_pattern],
            instruction='')

        self.assertEqual(u'266123321435', field._clean(u'2.66123321435e+11'))
Example #8
0
 def _get_telephone_number_field(self):
     form_model = self._get_form_model()
     phone_number_field = TelephoneNumberField(
         name=self.field_name,
         code='m',
         label=self.field_name,
         ddtype=Mock(spec=DataDictType),
         constraints=[
             TextLengthConstraint(max=15),
             RegexConstraint(reg='^[0-9]+$')
         ])
     return phone_number_field
Example #9
0
 def test_should_raise_regex_mismatch_exception_if_invalid_phone_number(
         self):
     mobile_number_length = TextLengthConstraint(max=15)
     mobile_number_pattern = RegexConstraint(reg='^[0-9]+$')
     field = TelephoneNumberField(
         name="test",
         code='MC',
         label='question',
         constraints=[mobile_number_length, mobile_number_pattern],
         instruction='')
     with self.assertRaises(RegexMismatchException):
         field.validate(u'020321dsa')
Example #10
0
    def test_telephone_number_should_clean_value_to_remove_only_hyphen_from_the_given_value(
            self):
        mobile_number_length = TextLengthConstraint(max=15)
        mobile_number_pattern = RegexConstraint(reg='^[0-9]+$')
        field = TelephoneNumberField(
            name="test",
            code='MC',
            label='question',
            constraints=[mobile_number_length, mobile_number_pattern],
            instruction='')

        self.assertEqual('1234321122', field._clean('123-4321122'))
        self.assertEqual('123dsasa4321122', field._clean('123dsasa4321122'))
Example #11
0
    def test_telephone_number_should_clean_before_validate(self):
        mobile_number_length = TextLengthConstraint(max=15)
        mobile_number_pattern = RegexConstraint(reg='^[0-9]+$')
        field = TelephoneNumberField(
            name="test",
            code='MC',
            label='question',
            constraints=[mobile_number_length, mobile_number_pattern],
            instruction='')

        self.assertEqual(u'266123321435', field.validate(u'2.66123321435e+11'))
        self.assertEqual(u'266123321435', field.validate(u'266-123321435'))
        self.assertEqual(u'266123321435', field.validate(u'266123321435.0'))
Example #12
0
    def test_should_validate_text_data_based_on_list_of_constraints(self):
        length_constraint = TextLengthConstraint(min=10, max=12)
        regex_constraint = RegexConstraint("^[A-Za-z0-9]+$")
        constraints = [length_constraint, regex_constraint]
        field = TextField(name="test",
                          code='MC',
                          label='question',
                          constraints=constraints)

        self.assertEqual('validatable', field.validate('validatable'))
        self.assertRaises(RegexMismatchException, field.validate,
                          '!alidatabl!')
        self.assertRaises(AnswerTooShortException, field.validate, 'val')
        self.assertRaises(AnswerTooLongException, field.validate,
                          'val11111111111111')
 def _create_form_model(cls):
     question1 = UniqueIdField(unique_id_type='clinic',
                               name="entity_question",
                               code="clinic ID",
                               label="What is associated clinic")
     question2 = TextField(
         name="question1_Name",
         code="Q1",
         label="What is your name",
         defaultValue="some default value",
         constraints=[TextLengthConstraint(5, 10),
                      RegexConstraint("\w+")])
     cls.form_model = FormModel(cls.manager,
                                name="aids",
                                label="Aids form_model",
                                form_code=FORM_CODE_1,
                                fields=[question1, question2])
     cls.form_model__id = cls.form_model.id
Example #14
0
 def test_phone_number_field(self):
     field = TelephoneNumberField('phone',
                                  'phone_code',
                                  'phone',
                                  self.ddtype,
                                  constraints=[
                                      TextLengthConstraint(min=10, max=12),
                                      RegexConstraint(reg='^[0-9]+$')
                                  ])
     phone_field = FormField().create(field)
     self.assertTrue(isinstance(phone_field, PhoneNumberField))
     self.assertEqual(len(phone_field.validators), 3)
     self.assertEqual(phone_field.widget.attrs['watermark'],
                      'Between 10 -- 12 characters')
     validator_types = []
     for validator in phone_field.validators:
         validator_types.append(type(validator))
     self.assertTrue(MinLengthValidator in validator_types)
     self.assertTrue(MaxLengthValidator in validator_types)
     self.assertTrue(RegexValidator in validator_types)
Example #15
0
 def _create_form_model(self):
     question1 = TextField(name="entity_question",
                           code="ID",
                           label="What is associated entity",
                           entity_question_flag=True,
                           ddtype=self.default_ddtype)
     question2 = TextField(
         name="question1_Name",
         code="Q1",
         label="What is your name",
         defaultValue="some default value",
         constraints=[TextLengthConstraint(5, 10),
                      RegexConstraint("\w+")],
         ddtype=self.default_ddtype)
     self.form_model = FormModel(self.manager,
                                 entity_type=self.entity_type,
                                 name="aids",
                                 label="Aids form_model",
                                 form_code=FORM_CODE_1,
                                 type='survey',
                                 fields=[question1, question2])
     self.form_model__id = self.form_model.save()
Example #16
0
def _create_constraints_for_mobile_number():
    #constraints on questionnaire
    mobile_number_length = TextLengthConstraint(max=15, min=5)
    mobile_number_pattern = RegexConstraint(reg='^[0-9]+$')
    mobile_constraints = [mobile_number_length, mobile_number_pattern]
    return mobile_constraints
Example #17
0
 def _create_constraints_for_mobile_number(s_create_short_code_fieldelf):
     mobile_number_length = TextLengthConstraint(max=15)
     mobile_number_pattern = RegexConstraint(reg='^[0-9]+$')
     mobile_constraints = [mobile_number_length, mobile_number_pattern]
     return mobile_constraints
Example #18
0
    def _create_project(self):
        registration_form = construct_global_registration_form(self.manager)
        registration_form.save()

        self.check_uniqueness_patch = patch.object(
            Project, '_check_if_project_name_unique')
        self.check_uniqueness_patch.start()
        clinic_form_model = EntityFormModel(
            self.manager,
            name='clinic',
            label='Entity Form Model',
            form_code='clin',
            fields=[TextField('name', 'code', 'label')],
            language="en",
            is_registration_model=True,
            enforce_unique_labels=True,
            entity_type=['clinic'])
        clinic_form_model.save()

        hf_form_model = EntityFormModel(self.manager,
                                        name='health',
                                        label='Entity Form Model',
                                        form_code='hf1',
                                        fields=[
                                            TextField('name', 'code', 'label'),
                                            TextField('location', 'loc',
                                                      'Where is it?')
                                        ],
                                        language="en",
                                        is_registration_model=True,
                                        enforce_unique_labels=True,
                                        entity_type=['healthfacility'])
        hf_form_model.save()

        entity_type = ["HealthFacility", "Clinic"]
        question1 = UniqueIdField('clinic',
                                  name="entity_question",
                                  code="ID",
                                  label="What is associated Clinic")
        question2 = TextField(
            name="question1_Name",
            code="Q1",
            label="What is your name",
            defaultValue="some default value",
            constraints=[TextLengthConstraint(5, 10),
                         RegexConstraint("\w+")])
        question3 = IntegerField(
            name="Father's age",
            code="Q2",
            label="What is your Father's Age",
            constraints=[NumericRangeConstraint(min=15, max=120)])
        question4 = SelectField(name="Color",
                                code="Q3",
                                label="What is your favourite color",
                                options=[("RED", 'a'), ("YELLOW", 'b')])
        question5 = UniqueIdField('healthfacility',
                                  name="health facility",
                                  code="hf",
                                  label="For which HF is it?")
        fields = [question1, question2, question3, question4, question5]
        project = Project(self.manager,
                          name='test project',
                          form_code='test01',
                          fields=fields)

        project.save()
        return project
Example #19
0
 def test_should_return_valid_regex_json(self):
     pattern = "^[A-Za-z0-9]+$"
     constraint = RegexConstraint(reg=pattern)
     self.assertEqual(("regex", pattern), constraint._to_json())
Example #20
0
 def test_should_throw_error_on_invalid_value(self):
     constraint = RegexConstraint(reg="^[A-Za-z0-9]+$")
     with self.assertRaises(RegexMismatchException):
         constraint.validate("Hello 1")
Example #21
0
 def test_should_validate_values_within_regex(self):
     constraint = RegexConstraint(reg="^[A-Za-z0-9]+$")
     self.assertEqual("Hello1", constraint.validate("Hello1"))