예제 #1
0
def test_select_one_q_to_xform():
    """
    Test select_one floip question to XForm.
    """
    survey = Survey(name='floip')
    question = xform_from_floip_dict(
        survey, 'ae54d4', {
            "type": "select_one",
            "label": "Are you male or female?",
            "type_options": {
                "choices": ["male", "female", "not identified"]
            }
        })
    choices = [
        '<item><label>male</label><value>male</value></item>',
        '<item><label>female</label><value>female</value></item>',
        '<item><label>not identified</label><value>not identified</value>'
        '</item>'
    ]
    expected_data = {
        'name': question['name'],
        'label': question['label'],
        'choices': ''.join(choices)
    }
    body_xml = (
        u'<select1 ref="/floip/%(name)s"><label>%(label)s</label>%(choices)s'
        u'</select1>' % expected_data)
    assert question.xml_control().toxml() == body_xml
    bind_xml = (u'<bind nodeset="/floip/%(name)s" type="select1"/>' % question)
    assert question.xml_binding().toxml() == bind_xml
    assert len([child.xml() for child in question.children]) == 3
예제 #2
0
def test_select_many_q_to_xform():
    """
    Test select_many floip question to XForm.
    """
    survey = Survey(name='floip')
    question = xform_from_floip_dict(
        survey, 'ae54d5', {
            "type": "select_many",
            "label": "What is your favorite desert?",
            "type_options": {
                "choices": ["cake", "fruit", "ice cream"]
            }
        })
    choices = [
        '<item><label>cake</label><value>cake</value></item>',
        '<item><label>fruit</label><value>fruit</value></item>',
        '<item><label>ice cream</label><value>ice cream</value>'
        '</item>'
    ]
    expected_data = {
        'name': question['name'],
        'label': question['label'],
        'choices': ''.join(choices)
    }
    body_xml = (
        u'<select ref="/floip/%(name)s"><label>%(label)s</label>%(choices)s'
        u'</select>' % expected_data)
    assert question.xml_control().toxml() == body_xml
    bind_xml = (u'<bind nodeset="/floip/%(name)s" type="select"/>' % question)
    assert question.xml_binding().toxml() == bind_xml
    assert len([child.xml() for child in question.children]) == 3
예제 #3
0
    def __init__(self, descriptor=None, title=None, id_string=None):
        # Seek to begining of file if it has the seek attribute before loading
        # the file.
        if hasattr(descriptor, 'seek'):
            descriptor.seek(0)
        try:
            # descriptor is a file
            self.descriptor = json.load(descriptor)
        except AttributeError:
            try:
                # descriptor is a JSON string
                self.descriptor = json.loads(descriptor)
            except JSONDecodeError:
                # descriptor is a file path.
                self.descriptor = json.load(
                    codecs.open(descriptor, encoding='utf-8'))

        if self.descriptor['profile'] == FLOW_RESULTS_PROFILE:
            del self.descriptor['profile']

        self._package = Package(self.descriptor)
        self.descriptor = self._package.descriptor
        self._name = id_string or self._package.descriptor.get('name')
        assert self._name, "The 'name' property must be defined."
        title = title or self._package.descriptor.get('title') or self._name
        survey_dict = {
            constants.NAME: 'data',
            constants.ID_STRING: self._name,
            constants.TITLE: title,
            constants.TYPE: constants.SURVEY,
        }
        self._survey = Survey(**survey_dict)
        self.build()
예제 #4
0
    def test_answers_can_be_imported_from_xml(self):
        surv = Survey(name="data")

        surv.add_child(
            create_survey_element_from_dict({
                "type": "text",
                "name": "name",
                "label": "Name"
            }))
        surv.add_child(
            create_survey_element_from_dict({
                "type": "integer",
                "name": "users_per_month",
                "label": "Users per month",
            }))
        surv.add_child(
            create_survey_element_from_dict({
                "type": "gps",
                "name": "geopoint",
                "label": "gps"
            }))
        surv.add_child(
            create_survey_element_from_dict({
                "type": "imei",
                "name": "device_id"
            }))

        instance = surv.instantiate()
        import_xml = self.config.get(self.cls_name,
                                     "test_answers_can_be_imported_from_xml")
        instance.import_from_xml(import_xml)
예제 #5
0
def _load_registration_survey_object():
    """
    Loads a registration survey with all the values necessary
    to register a surveyor.
    """
    survey = Survey(name="registration", id_string="registration")
    survey.add_child(
        create_survey_element_from_dict({
            'type': 'text',
            'name': 'name',
            'label': 'Name'
        }))
    survey.add_child(
        create_survey_element_from_dict({
            'type': 'start time',
            'name': 'start'
        }))
    survey.add_child(
        create_survey_element_from_dict({
            'type': 'end time',
            'name': 'end'
        }))
    survey.add_child(
        create_survey_element_from_dict({
            'type': 'imei',
            'name': 'device_id'
        }))
    return survey
예제 #6
0
    def test_survey_can_be_created_in_a_verbose_manner(self):
        s = Survey()
        s.name = "simple_survey"

        q = MultipleChoiceQuestion()
        q.name = "cow_color"
        q.type = "select one"

        q.add_choice(label="Green", name="green")
        s.add_child(q)

        expected_dict = {
            "name":
            "simple_survey",
            "children": [{
                "name": "cow_color",
                "type": "select one",
                "children": [{
                    "label": "Green",
                    "name": "green"
                }],
            }],
        }
        self.maxDiff = None
        self.assertEqual(s.to_json_dict(), expected_dict)
예제 #7
0
 def setUp(self):
     survey_out = Survey(type=u"survey")
     question = InputQuestion(name=u"age")
     question.set(InputQuestion.TYPE, u"integer")
     question.set(InputQuestion.LABEL, u"How old are you?")
     survey_out.add_child(question)
     self.survey_out_dict = survey_out.to_dict()
     print_pyobj_to_json(self.survey_out_dict,
                         "pyxform/tests/how_old_are_you.json")
예제 #8
0
 def setUp(self):
     self.this_directory = os.path.dirname(__file__)
     survey_out = Survey(name=u"age", type=u"survey")
     question = InputQuestion(name=u"age")
     question.type = u"integer"
     question.label = u"How old are you?"
     survey_out.add_child(question)
     self.survey_out_dict = survey_out.to_json_dict()
     print_pyobj_to_json(self.survey_out_dict,
                         utils.path_to_text_fixture("how_old_are_you.json"))
예제 #9
0
def _load_simple_survey_object():
    """
    Returns a "watersimple" survey object,
    complete with questions.
    """
    survey = Survey(name="WaterSimple", id_string="WaterSimple")
    survey.add_child(
        create_survey_element_from_dict({
            'hint': {
                'English': 'What is this point named?'
            },
            'label': {
                'English': 'Water Point Name'
            },
            'type': 'text',
            'name': 'name'
        }))
    survey.add_child(
        create_survey_element_from_dict({
            'hint': {
                'English': 'How many people use this every month?'
            },
            'label': {
                'English': 'Monthly Usage'
            },
            'name': 'users_per_month',
            'type': 'integer'
        }))
    survey.add_child(
        create_survey_element_from_dict({
            'type': 'gps',
            'name': 'geopoint',
            'label': {
                'English': 'Location'
            }
        }))
    survey.add_child(
        create_survey_element_from_dict({
            'type': 'imei',
            'name': 'device_id'
        }))
    survey.add_child(
        create_survey_element_from_dict({
            'type': 'start time',
            'name': 'start'
        }))
    survey.add_child(
        create_survey_element_from_dict({
            'type': 'end time',
            'name': 'end'
        }))
    return survey
예제 #10
0
    def test_output_node_for_select_one_question_type(self):

        self.this_directory = os.path.dirname(__file__)
        survey_out = Survey(name="geopgraphy", sms_keyword="geography", type="survey")
        question = InputQuestion(name="counties")
        question.type = "select one external"
        question.label = "county"
        survey_out.add_child(question)

        expected = '<input ref="/geopgraphy/counties">'

        xml = survey_out.to_xml()
        self.assertEqual(1, xml.count(expected))
예제 #11
0
    def test_simple_survey_instantiation(self):
        surv = Survey(name="Simple")
        q = create_survey_element_from_dict({
            "type": "text",
            "name": "survey_question",
            "label": "Question"
        })
        surv.add_child(q)

        i = surv.instantiate()

        self.assertEquals(i.keys(), ["survey_question"])
        self.assertEquals(set(i.xpaths()),
                          {"/Simple", "/Simple/survey_question"})
예제 #12
0
def test_time_question_to_xform():
    """
    Test a floip time question to XForm.
    """
    survey = Survey(name='floip')
    question = xform_from_floip_dict(survey, 'ae54d10', {
        "type": "time",
        "label": "What is the time?",
        "type_options": {}
    })
    body_xml = (u'<input ref="/floip/%(name)s">'
                '<label>%(label)s</label></input>' % question)
    assert question.xml_control().toxml() == body_xml
    bind_xml = (u'<bind nodeset="/floip/%(name)s" type="time"/>' % question)
    assert question.xml_binding().toxml() == bind_xml
예제 #13
0
def test_video_upload_to_xform():
    """
    Test a video floip question to XForm.
    """
    survey = Survey(name='floip')
    question = xform_from_floip_dict(survey, 'ae54d9', {
        "type": "video",
        "label": "Upload a video recording",
        "type_options": {}
    })
    body_xml = (u'<upload mediatype="video/*" ref="/floip/%(name)s">'
                '<label>%(label)s</label></upload>' % question)
    assert question.xml_control().toxml() == body_xml
    bind_xml = (u'<bind nodeset="/floip/%(name)s" type="binary"/>' % question)
    assert question.xml_binding().toxml() == bind_xml
예제 #14
0
def test_time_question_to_floip():
    """
    Test XForm time question to FLOIP
    """
    survey = Survey(name='floip')
    element = xform_from_floip_dict(survey, 'ae54d8', {
        "type": "time",
        "label": "What is the time?",
        "type_options": {}
    })
    question = floip_dict_from_xform_dict(element.to_json_dict())
    assert question == {
        "type": "time",
        "label": "What is the time?",
        "type_options": {}
    }
예제 #15
0
def test_video_upload_to_floip():
    """
    Test XForm video question to FLOIP.
    """
    survey = Survey(name='floip')
    element = xform_from_floip_dict(survey, 'ae54d8', {
        "type": "video",
        "label": "Upload a video recording",
        "type_options": {}
    })
    question = floip_dict_from_xform_dict(element.to_json_dict())
    assert question == {
        "type": "video",
        "label": "Upload a video recording",
        "type_options": {}
    }
예제 #16
0
def test_text_question_to_xform():
    """
    Test text floip queston to XForm.
    """
    survey = Survey(name='floip')
    question = xform_from_floip_dict(survey, 'ae54d6', {
        "type": "text",
        "label": "What is your name?",
        "type_options": {}
    })
    body_xml = (
        u'<input ref="/floip/%(name)s"><label>%(label)s</label></input>' %
        question)
    assert question.xml_control().toxml() == body_xml
    bind_xml = (u'<bind nodeset="/floip/%(name)s" type="string"/>' % question)
    assert question.xml_binding().toxml() == bind_xml
예제 #17
0
def test_geopoint_question_to_floip():
    """
    Test geopoint question to FLOIP geo_point dictionary.
    """
    survey = Survey(name='floip')
    element = xform_from_floip_dict(survey, 'ae54db', {
        "type": "geo_point",
        "label": "Where are you?",
        "type_options": {}
    })
    question = floip_dict_from_xform_dict(element.to_json_dict())
    assert question == {
        "type": "geo_point",
        "label": "Where are you?",
        "type_options": {}
    }
예제 #18
0
def test_numeric_question_to_floip():
    """
    Test numeric floip queston to XForm.
    """
    survey = Survey(name='floip')
    element = xform_from_floip_dict(
        survey, 'ae54d8', {
            "type": "numeric",
            "label": "How much do you weigh, in lbs?",
            "type_options": {}
        })
    question = floip_dict_from_xform_dict(element.to_json_dict())
    assert question == {
        "type": "numeric",
        "label": "How much do you weigh, in lbs?",
        "type_options": {}
    }
예제 #19
0
def test_image_upload_to_floip():
    """
    Test an XForm image question to FLOIP.
    """
    survey = Survey(name='floip')
    element = xform_from_floip_dict(
        survey, 'ae54d8', {
            "type": "image",
            "label": "Upload an image of your location",
            "type_options": {}
        })
    question = floip_dict_from_xform_dict(element.to_json_dict())
    assert question == {
        "type": "image",
        "label": "Upload an image of your location",
        "type_options": {}
    }
예제 #20
0
def test_geopoint_question_to_xform():
    """
    Test geo_point floip queston to XForm.
    """
    survey = Survey(name='floip')
    question = xform_from_floip_dict(survey, 'ae54db', {
        "type": "geo_point",
        "label": "Where are you?",
        "type_options": {}
    })
    body_xml = (
        u'<input ref="/floip/%(name)s"><label>%(label)s</label></input>' %
        question)
    assert question.xml_control().toxml() == body_xml
    bind_xml = (u'<bind nodeset="/floip/%(name)s" type="geopoint"/>' %
                question)
    assert question.xml_binding().toxml() == bind_xml
예제 #21
0
def test_numeric_question_to_xform():
    """
    Test numeric floip queston to XForm.
    """
    survey = Survey(name='floip')
    question = xform_from_floip_dict(
        survey, 'ae54d8', {
            "type": "numeric",
            "label": "How much do you weigh, in lbs?",
            "type_options": {}
        })
    body_xml = (
        u'<input ref="/floip/%(name)s"><label>%(label)s</label></input>' %
        question)
    assert question.xml_control().toxml() == body_xml
    bind_xml = (u'<bind nodeset="/floip/%(name)s" type="int"/>' % question)
    assert question.xml_binding().toxml() == bind_xml
예제 #22
0
    def test_simple_registration_xml(self):
        reg_xform = Survey(name="Registration")
        name_question = create_survey_element_from_dict({
            "type": "text",
            "name": "name",
            "label": "Name"
        })
        reg_xform.add_child(name_question)

        reg_instance = reg_xform.instantiate()

        reg_instance.answer(name="name", value="bob")

        rx = reg_instance.to_xml()
        expected_xml = self.config.get(self.cls_name,
                                       "test_simple_registration_xml").format(
                                           reg_xform.id_string)
        self.assertEqual(rx, expected_xml)
예제 #23
0
def test_select_many_q_to_floip():
    """
    Test XForm select_many question to FLOIP.
    """
    survey = Survey(name='floip')
    element = xform_from_floip_dict(
        survey, 'ae54d8', {
            "type": "select_many",
            "label": "What is your favorite desert?",
            "type_options": {
                "choices": ["cake", "fruit", "ice cream"]
            }
        })
    question = floip_dict_from_xform_dict(element.to_json_dict())
    assert question == {
        "type": "select_many",
        "label": "What is your favorite desert?",
        "type_options": {
            "choices": ["cake", "fruit", "ice cream"]
        }
    }
    def test_dictionary_consolidates_duplicate_entries(self):

        yes_or_no_dict_array = [
            {"label": {"French": "Oui", "English": "Yes"}, "name": "yes"},
            {"label": {"French": "Non", "English": "No"}, "name": "no"},
        ]

        first_yesno_question = MultipleChoiceQuestion(
            name="yn_q1", options=yes_or_no_dict_array, type="select one"
        )
        second_yesno_question = MultipleChoiceQuestion(
            name="yn_q2", options=yes_or_no_dict_array, type="select one"
        )

        s = Survey(name="yes_or_no_tests")
        s.add_child(first_yesno_question)
        s.add_child(second_yesno_question)

        # begin the processes in survey.to_xml()
        # 1. validate()
        s.validate()
예제 #25
0
def test_numeric_range_q_to_floip():
    """
    Test XForm numeric range queston to FLOIP.
    """
    survey = Survey(name='floip')
    element = xform_from_floip_dict(
        survey, 'ae54d8', {
            "type": "numeric",
            "label": "How much do you weigh, in lbs?",
            "type_options": {
                "range": [1, 250]
            }
        })
    question = floip_dict_from_xform_dict(element.to_json_dict())
    assert question == {
        "type": "numeric",
        "label": "How much do you weigh, in lbs?",
        "type_options": {
            "range": [1, 250]
        }
    }
예제 #26
0
def test_select_one_q_to_floip():
    """
    Test XForm select_one question to FLOIP.
    """
    survey = Survey(name='floip')
    element = xform_from_floip_dict(
        survey, 'ae54d8', {
            "type": "select_one",
            "label": "Are you male or female?",
            "type_options": {
                "choices": ["male", "female", "not identified"]
            }
        })
    question = floip_dict_from_xform_dict(element.to_json_dict())
    assert question == {
        "type": "select_one",
        "label": "Are you male or female?",
        "type_options": {
            "choices": ["male", "female", "not identified"]
        }
    }
예제 #27
0
    def test_survey_can_be_created_in_a_slightly_less_verbose_manner(self):
        option_dict_array = [
            {
                "name": "red",
                "label": "Red"
            },
            {
                "name": "blue",
                "label": "Blue"
            },
        ]

        q = MultipleChoiceQuestion(name="Favorite_Color",
                                   choices=option_dict_array)
        q.type = "select one"
        s = Survey(name="Roses_are_Red", children=[q])

        expected_dict = {
            "name":
            "Roses_are_Red",
            "children": [{
                "name":
                "Favorite_Color",
                "type":
                "select one",
                "children": [
                    {
                        "label": "Red",
                        "name": "red"
                    },
                    {
                        "label": "Blue",
                        "name": "blue"
                    },
                ],
            }],
        }

        self.assertEqual(s.to_json_dict(), expected_dict)
예제 #28
0
    def test_simple_survey_answering(self):
        surv = Survey(name="Water")
        q = create_survey_element_from_dict({
            "type": "text",
            "name": "color",
            "label": "Color"
        })
        q2 = create_survey_element_from_dict({
            "type": "text",
            "name": "feeling",
            "label": "Feeling"
        })

        surv.add_child(q)
        surv.add_child(q2)
        i = SurveyInstance(surv)

        i.answer(name="color", value="blue")
        self.assertEquals(i.answers()["color"], "blue")

        i.answer(name="feeling", value="liquidy")
        self.assertEquals(i.answers()["feeling"], "liquidy")
예제 #29
0
 def test_one_section_cannot_have_two_conflicting_slugs(self):
     q1 = InputQuestion(name="YourName")
     q2 = InputQuestion(name="YourName")
     s = Survey(name="Roses are Red", children=[q1, q2])
     self.assertRaises(Exception, s, "validate")
예제 #30
0
 def setUp(self):
     self.s = Survey(name="test")