示例#1
0
 def test_should_return_specific_form_for_project_without_unique_id(self):
     dbm = Mock()
     questionnaire_mock = Mock()
     field1 = Field(type='text',
                    name='name',
                    label='name',
                    code='code',
                    instruction='instruction',
                    constraints=[(self.mock_constraint())])
     questionnaire_mock.name = 'name'
     questionnaire_mock.fields = [field1]
     questionnaire_mock.form_code = 'form_code'
     questionnaire_mock.id = 'id'
     questionnaire_mock.unique_id_field = None
     questionnaire_mock.activeLanguages = ["en"]
     questionnaire_mock.xform = None
     with patch("mangrove.transport.xforms.xform.FormModel"
                ) as form_model_mock:
         form_model_mock.get.return_value = questionnaire_mock
         actual_response = xform_for(dbm, "someFormId", 'rep1')
         self.assertTrue(
             self.checker.check_output(
                 actual_response,
                 unicode(expected_xform_for_project_on_reporter), 0),
             actual_response)
示例#2
0
 def text_field(self, code):
     return Field(type='text',
                  name='name',
                  label='name',
                  code=code,
                  instruction='instruction',
                  constraints=[(self.mock_constraint())])
示例#3
0
 def test_should_set_new_instruction_for_field(self):
     new_instruction = "new_instruction"
     expected_json = {
         "label": "What is your age",
         "name": "name",
         "code": "Q2",
         "parent_field_code": None,
         "type": "integer",
         "required": True,
         "instruction": new_instruction,
         "appearance": None,
         "constraint_message": None,
         "default": None,
         "hint": None,
         "xform_constraint": None,
         "relevant": None
     }
     field = Field(type="integer",
                   name="name",
                   code="Q2",
                   label="What is your age",
                   instruction="instruction")
     field.set_instruction(new_instruction)
     actual_json = field._to_json()
     self.assertEqual(actual_json, expected_json)
示例#4
0
    def test_should_add_all_xform_constraints(self):
        first_constraint = Mock()
        first_constraint.xform_constraint.return_value = "first"
        second_constraint = Mock()
        second_constraint.xform_constraint.return_value = "second"

        field = Field(constraints=[first_constraint, second_constraint])
        self.assertEqual("first and second", field.xform_constraints())
示例#5
0
    def test_should_not_include_none_or_empty_constraints(self):
        first_constraint = Mock()
        first_constraint.xform_constraint.return_value = "first"
        second_constraint = Mock()
        second_constraint.xform_constraint.return_value = None
        third_constraint = Mock()
        third_constraint.xform_constraint.return_value = ""

        field = Field(constraints=[
            first_constraint, second_constraint, third_constraint
        ])
        self.assertEqual("first", field.xform_constraints())
示例#6
0
 def test_should_set_new_instruction_for_field(self):
     new_instruction = "new_instruction"
     expected_json = {
         "label": "What is your age",
         "name": "name",
         "code": "Q2",
         'parent_field_code': None,
         "type": "integer",
         "required": True,
         "instruction": new_instruction
     }
     field = Field(type="integer",
                   name="name",
                   code="Q2",
                   label="What is your age",
                   instruction="instruction")
     field.set_instruction(new_instruction)
     actual_json = field._to_json()
     self.assertEqual(actual_json, expected_json)
示例#7
0
    def _get_questionnaire(self,
                           group_label="Enter the outer group details",
                           group_name="group_outer",
                           field_label="Name please",
                           field_name="text2",
                           hint=None,
                           constraint_message=None,
                           appearance=None,
                           default=None,
                           required=False,
                           xform_constraint=None,
                           relevant=None,
                           field_choices=None):
        field = SelectField(field_name, field_name, field_label, field_choices) if field_choices is not None \
            else Field(code=field_name, name=field_name, label=field_label, parent_field_code=group_name, hint=hint,
                      constraint_message=constraint_message, appearance=appearance, default=default, required=required,
                      xform_constraint=xform_constraint, relevant=relevant)
        repeat = FieldSet(code="repeat_outer",
                          name="repeat_outer",
                          label="Enter the details you wanna repeat",
                          field_set=[field])

        xform = self._build_xform(group_label=group_label,
                                  fields=[field],
                                  field_type=field.type)
        if field.relevant:
            xform = _replace_node_name_with_xpath(field.relevant, xform)
        elif field.xform_constraint:
            xform = _replace_node_name_with_xpath(field.xform_constraint,
                                                  xform)

        doc = ProjectDocument()
        doc.xform = xform
        questionnaire = Project.new_from_doc(DatabaseManagerStub(), doc)
        questionnaire.name = "q1"
        questionnaire.form_code = "007"
        questionnaire.fields.append(
            FieldSet(code=group_name,
                     name=group_name,
                     label=group_label,
                     field_set=[repeat]))
        return questionnaire