Ejemplo n.º 1
0
    def _test_corpus(self, slug):
        xform_file = os.path.join(os.path.dirname(__file__), 'readable_forms',
                                  '{}.xform.xml'.format(slug))
        submission_file = os.path.join(os.path.dirname(__file__),
                                       'readable_forms',
                                       '{}.submission.json'.format(slug))
        result_file = os.path.join(os.path.dirname(__file__), 'readable_forms',
                                   '{}.result.yaml'.format(slug))
        with open(xform_file) as f:
            xform = f.read()
        with open(submission_file) as f:
            data = json.load(f)
        with open(result_file) as f:
            result = yaml.load(f)
        questions = get_questions_from_xform_node(XForm(xform), langs=['en'])
        questions = get_readable_form_data(data, questions)

        # Search for 'READABLE FORMS TEST' for more info
        # to bootstrap a test and have it print out your yaml result
        # uncomment this line. Ghetto but it works.
        # print yaml.safe_dump([json.loads(json.dumps(x.to_json()))
        #                       for x in questions])

        self.assertJSONEqual(
            json.dumps([x.to_json() for x in questions]),
            json.dumps(result),
            msg=
            "Search for \"READABLE FORMS TEST\" for more info on fixing this test"
        )
Ejemplo n.º 2
0
    def _test_corpus(self, slug):
        xform_file = os.path.join(
            os.path.dirname(__file__),
            'readable_forms', '{}.xform.xml'.format(slug))
        submission_file = os.path.join(
            os.path.dirname(__file__),
            'readable_forms', '{}.submission.json'.format(slug))
        result_file = os.path.join(
            os.path.dirname(__file__),
            'readable_forms', '{}.result.yaml'.format(slug))
        with open(xform_file) as f:
            xform = f.read()
        with open(submission_file) as f:
            data = json.load(f)
        with open(result_file) as f:
            result = yaml.load(f)
        questions = get_questions_from_xform_node(XForm(xform), langs=['en'])
        questions = get_readable_form_data(data, questions)

        # Search for 'READABLE FORMS TEST' for more info
        # to bootstrap a test and have it print out your yaml result
        # uncomment this line. Ghetto but it works.
        # print yaml.safe_dump([json.loads(json.dumps(x.to_json()))
        #                       for x in questions])

        self.assertJSONEqual(json.dumps([x.to_json() for x in questions]),
                             json.dumps(result),
                             msg="Search for \"READABLE FORMS TEST\" for more info on fixing this test")
 def test_corpus(self):
     slug = 'mismatched_group_hierarchy'
     xform_file = os.path.join(
         os.path.dirname(__file__),
         'readable_forms', '{}.xform.xml'.format(slug))
     submission_file = os.path.join(
         os.path.dirname(__file__),
         'readable_forms', '{}.submission.json'.format(slug))
     result_file = os.path.join(
         os.path.dirname(__file__),
         'readable_forms', '{}.result.yaml'.format(slug))
     with open(xform_file) as f:
         xform = f.read()
     with open(submission_file) as f:
         data = json.load(f)
     with open(result_file) as f:
         result = yaml.load(f)
     questions = get_questions_from_xform_node(XForm(xform), langs=['en'])
     questions = get_readable_form_data(data, questions)
     self.assertJSONEqual(json.dumps([x.to_json() for x in questions]),
                          json.dumps(result))
Ejemplo n.º 4
0
    def test_get_data_cleaning_data(self, questions_patch):
        builder = XFormBuilder()
        responses = OrderedDict()

        # Simple question
        builder.new_question('something', 'Something')
        responses['something'] = 'blue'

        # Skipped question - doesn't appear in repsonses, shouldn't appear in data cleaning data
        builder.new_question('skip', 'Skip me')

        # Simple group
        lights = builder.new_group('lights',
                                   'Traffic Lights',
                                   data_type='group')
        lights.new_question('red', 'Red means')
        lights.new_question('green', 'Green means')
        responses['lights'] = OrderedDict([('red', 'stop'), ('green', 'go')])

        # Simple repeat group, one response
        one_hit_wonders = builder.new_group('one_hit_wonders',
                                            'One-Hit Wonders',
                                            data_type='repeatGroup')
        one_hit_wonders.new_question('name', 'Name')
        responses['one_hit_wonders'] = [
            {
                'name': 'A-Ha'
            },
        ]

        # Simple repeat group, multiple responses
        snacks = builder.new_group('snacks', 'Snacks', data_type='repeatGroup')
        snacks.new_question('kind_of_snack', 'Kind of snack')
        responses['snacks'] = [
            {
                'kind_of_snack': 'samosa'
            },
            {
                'kind_of_snack': 'pakora'
            },
        ]

        # Repeat group with nested group
        cups = builder.new_group('cups_of_tea',
                                 'Cups of tea',
                                 data_type='repeatGroup')
        details = cups.new_group('details_of_cup',
                                 'Details',
                                 data_type='group')
        details.new_question('kind_of_cup', 'Flavor')
        details.new_question('secret', 'Secret', data_type=None)
        responses['cups_of_tea'] = [
            {
                'details_of_cup': {
                    'kind_of_cup': 'green',
                    'secret': 'g'
                }
            },
            {
                'details_of_cup': {
                    'kind_of_cup': 'black',
                    'secret': 'b'
                }
            },
            {
                'details_of_cup': {
                    'kind_of_cup': 'more green',
                    'secret': 'mg'
                }
            },
        ]

        xform = XForm(builder.tostring())
        questions_patch.return_value = get_questions_from_xform_node(
            xform, ['en'])
        xml = FormSubmissionBuilder(form_id='123',
                                    form_properties=responses).as_xml_string()
        submitted_xform = submit_form_locally(xml, self.domain).xform
        form_data, _ = get_readable_data_for_submission(submitted_xform)
        question_response_map, ordered_question_values = get_data_cleaning_data(
            form_data, submitted_xform)

        expected_question_values = [
            '/data/something',
            '/data/lights/red',
            '/data/lights/green',
            '/data/one_hit_wonders/name',
            '/data/snacks[1]/kind_of_snack',
            '/data/snacks[2]/kind_of_snack',
            '/data/cups_of_tea[1]/details_of_cup/kind_of_cup',
            '/data/cups_of_tea[1]/details_of_cup/secret',
            '/data/cups_of_tea[2]/details_of_cup/kind_of_cup',
            '/data/cups_of_tea[2]/details_of_cup/secret',
            '/data/cups_of_tea[3]/details_of_cup/kind_of_cup',
            '/data/cups_of_tea[3]/details_of_cup/secret',
        ]
        self.assertListEqual(ordered_question_values, expected_question_values)

        expected_response_map = {
            '/data/something': 'blue',
            '/data/lights/red': 'stop',
            '/data/lights/green': 'go',
            '/data/one_hit_wonders/name': 'A-Ha',
            '/data/snacks[1]/kind_of_snack': 'samosa',
            '/data/snacks[2]/kind_of_snack': 'pakora',
            '/data/cups_of_tea[1]/details_of_cup/kind_of_cup': 'green',
            '/data/cups_of_tea[1]/details_of_cup/secret': 'g',
            '/data/cups_of_tea[2]/details_of_cup/kind_of_cup': 'black',
            '/data/cups_of_tea[2]/details_of_cup/secret': 'b',
            '/data/cups_of_tea[3]/details_of_cup/kind_of_cup': 'more green',
            '/data/cups_of_tea[3]/details_of_cup/secret': 'mg',
        }
        self.assertDictEqual(
            {k: v['value']
             for k, v in question_response_map.items()}, expected_response_map)
Ejemplo n.º 5
0
    def test_get_data_cleaning_data(self, questions_patch):
        builder = XFormBuilder()
        responses = OrderedDict()

        # Simple question
        builder.new_question('something', 'Something')
        responses['something'] = 'blue'

        # Skipped question - doesn't appear in repsonses, shouldn't appear in data cleaning data
        builder.new_question('skip', 'Skip me')

        # Simple group
        lights = builder.new_group('lights', 'Traffic Lights', data_type='group')
        lights.new_question('red', 'Red means')
        lights.new_question('green', 'Green means')
        responses['lights'] = OrderedDict([('red', 'stop'), ('green', 'go')])

        # Simple repeat group, one response
        one_hit_wonders = builder.new_group('one_hit_wonders', 'One-Hit Wonders', data_type='repeatGroup')
        one_hit_wonders.new_question('name', 'Name')
        responses['one_hit_wonders'] = [
            {'name': 'A-Ha'},
        ]

        # Simple repeat group, multiple responses
        snacks = builder.new_group('snacks', 'Snacks', data_type='repeatGroup')
        snacks.new_question('kind_of_snack', 'Kind of snack')
        responses['snacks'] = [
            {'kind_of_snack': 'samosa'},
            {'kind_of_snack': 'pakora'},
        ]

        # Repeat group with nested group
        cups = builder.new_group('cups_of_tea', 'Cups of tea', data_type='repeatGroup')
        details = cups.new_group('details_of_cup', 'Details', data_type='group')
        details.new_question('kind_of_cup', 'Flavor')
        details.new_question('secret', 'Secret', data_type=None)
        responses['cups_of_tea'] = [
            {'details_of_cup': {'kind_of_cup': 'green', 'secret': 'g'}},
            {'details_of_cup': {'kind_of_cup': 'black', 'secret': 'b'}},
            {'details_of_cup': {'kind_of_cup': 'more green', 'secret': 'mg'}},
        ]

        xform = XForm(builder.tostring())
        questions_patch.return_value = get_questions_from_xform_node(xform, ['en'])
        xml = FormSubmissionBuilder(form_id='123', form_properties=responses).as_xml_string()
        submitted_xform = submit_form_locally(xml, self.domain).xform
        form_data, _ = get_readable_data_for_submission(submitted_xform)
        question_response_map, ordered_question_values = get_data_cleaning_data(form_data, submitted_xform)

        expected_question_values = [
            '/data/something',
            '/data/lights/red',
            '/data/lights/green',
            '/data/one_hit_wonders/name',
            '/data/snacks[1]/kind_of_snack',
            '/data/snacks[2]/kind_of_snack',
            '/data/cups_of_tea[1]/details_of_cup/kind_of_cup',
            '/data/cups_of_tea[1]/details_of_cup/secret',
            '/data/cups_of_tea[2]/details_of_cup/kind_of_cup',
            '/data/cups_of_tea[2]/details_of_cup/secret',
            '/data/cups_of_tea[3]/details_of_cup/kind_of_cup',
            '/data/cups_of_tea[3]/details_of_cup/secret',
        ]
        self.assertListEqual(ordered_question_values, expected_question_values)

        expected_response_map = {
            '/data/something': 'blue',
            '/data/lights/red': 'stop',
            '/data/lights/green': 'go',
            '/data/one_hit_wonders/name': 'A-Ha',
            '/data/snacks[1]/kind_of_snack': 'samosa',
            '/data/snacks[2]/kind_of_snack': 'pakora',
            '/data/cups_of_tea[1]/details_of_cup/kind_of_cup': 'green',
            '/data/cups_of_tea[1]/details_of_cup/secret': 'g',
            '/data/cups_of_tea[2]/details_of_cup/kind_of_cup': 'black',
            '/data/cups_of_tea[2]/details_of_cup/secret': 'b',
            '/data/cups_of_tea[3]/details_of_cup/kind_of_cup': 'more green',
            '/data/cups_of_tea[3]/details_of_cup/secret': 'mg',
        }
        self.assertDictEqual({k: v['value'] for k, v in question_response_map.items()}, expected_response_map)