Ejemplo n.º 1
0
    def test_kml_export_data(self):
        """
        Test kml_export_data(id_string, user, xform=None).
        """
        kml_md = """
        | survey |
        |        | type              | name  | label |
        |        | geopoint          | gps   | GPS   |
        |        | select one fruits | fruit | Fruit |

        | choices |
        |         | list name | name   | label  |
        |         | fruits    | orange | Orange |
        |         | fruits    | mango  | Mango  |
        """
        xform1 = self._publish_markdown(kml_md, self.user, id_string='a')
        xform2 = self._publish_markdown(kml_md, self.user, id_string='b')
        xml = '<data id="a"><gps>-1.28 36.83</gps><fruit>orange</fruit></data>'
        Instance(xform=xform1, xml=xml).save()
        xml = '<data id="b"><gps>32.85 13.04</gps><fruit>mango</fruit></data>'
        Instance(xform=xform2, xml=xml).save()
        data = {
            'xforms': [
                "http://testserver/api/v1/forms/%s" % xform1.pk,
                "http://testserver/api/v1/forms/%s" % xform2.pk,
            ],
            'name': 'Merged Dataset',
            'project':
            "http://testserver/api/v1/projects/%s" % xform1.project.pk,
        }  # yapf: disable
        request = self.factory.post('/')
        request.user = self.user
        serializer = MergedXFormSerializer(data=data,
                                           context={'request': request})
        self.assertTrue(serializer.is_valid())
        serializer.save()
        xform = XForm.objects.filter(pk__gt=xform2.pk,
                                     is_merged_dataset=True).first()
        expected_data = [{
            'name': u'a',
            'image_urls': [],
            'lat': -1.28,
            'table': u'<table border="1"><a href="#"><img width="210" class="thumbnail" src="" alt=""></a><tr><td>GPS</td><td>-1.28 36.83</td></tr><tr><td>Fruit</td><td>orange</td></tr></table>',  # noqa pylint: disable=C0301
            'lng': 36.83,
            'id': xform1.instances.all().first().pk
        }, {
            'name': u'b',
            'image_urls': [],
            'lat': 32.85,
            'table':
            u'<table border="1"><a href="#"><img width="210" class="thumbnail" src="" alt=""></a><tr><td>GPS</td><td>32.85 13.04</td></tr><tr><td>Fruit</td><td>mango</td></tr></table>',  # noqa pylint: disable=C0301
            'lng': 13.04,
            'id': xform2.instances.all().first().pk
        }]  # yapf: disable
        self.assertEqual(kml_export_data(xform.id_string, xform.user),
                         expected_data)
Ejemplo n.º 2
0
    def test_kml_export_data(self):
        """
        Test kml_export_data(id_string, user, xform=None).
        """
        kml_md = """
        | survey |
        |        | type              | name  | label |
        |        | geopoint          | gps   | GPS   |
        |        | select one fruits | fruit | Fruit |

        | choices |
        |         | list name | name   | label  |
        |         | fruits    | orange | Orange |
        |         | fruits    | mango  | Mango  |
        """
        xform1 = self._publish_markdown(kml_md, self.user, id_string='a')
        xform2 = self._publish_markdown(kml_md, self.user, id_string='b')
        xml = '<data id="a"><gps>-1.28 36.83</gps><fruit>orange</fruit></data>'
        Instance(xform=xform1, xml=xml).save()
        xml = '<data id="b"><gps>32.85 13.04</gps><fruit>mango</fruit></data>'
        Instance(xform=xform2, xml=xml).save()
        data = {
            'xforms': [
                "http://testserver/api/v1/forms/%s" % xform1.pk,
                "http://testserver/api/v1/forms/%s" % xform2.pk,
            ],
            'name': 'Merged Dataset',
            'project':
            "http://testserver/api/v1/projects/%s" % xform1.project.pk,
        }  # yapf: disable
        request = self.factory.post('/')
        request.user = self.user
        serializer = MergedXFormSerializer(
            data=data, context={'request': request})
        self.assertTrue(serializer.is_valid())
        serializer.save()
        xform = XForm.objects.filter(
            pk__gt=xform2.pk, is_merged_dataset=True).first()
        expected_data = [{
            'name': u'a',
            'image_urls': [],
            'lat': -1.28,
            'table': u'<table border="1"><a href="#"><img width="210" class="thumbnail" src="" alt=""></a><tr><td>GPS</td><td>-1.28 36.83</td></tr><tr><td>Fruit</td><td>orange</td></tr></table>',  # noqa pylint: disable=C0301
            'lng': 36.83,
            'id': xform1.instances.all().first().pk
        }, {
            'name': u'b',
            'image_urls': [],
            'lat': 32.85,
            'table':
            u'<table border="1"><a href="#"><img width="210" class="thumbnail" src="" alt=""></a><tr><td>GPS</td><td>32.85 13.04</td></tr><tr><td>Fruit</td><td>mango</td></tr></table>',  # noqa pylint: disable=C0301
            'lng': 13.04,
            'id': xform2.instances.all().first().pk
        }]  # yapf: disable
        self.assertEqual(
            kml_export_data(xform.id_string, xform.user), expected_data)
Ejemplo n.º 3
0
    def test_create_merged_xform(self):
        """Test creating a merged dataset with the MergedXFormSerializer"""
        serializer = MergedXFormSerializer(data={})
        self.assertFalse(serializer.is_valid(raise_exception=False))

        # project is required
        self.assertEqual(serializer.errors['project'],
                         [u'This field is required.'])

        # name is required
        self.assertEqual(serializer.errors['name'],
                         [u'This field is required.'])

        # At least 2 *different* xforms
        # 0 xforms
        self.assertEqual(serializer.errors['xforms'],
                         [u'This field is required.'])

        self.project = get_user_default_project(self.user)
        xform1 = self._publish_markdown(MD, self.user, id_string='a')
        data = {
            'xforms': [],
            'name':
            'Merged Dataset',
            'project':
            "http://testserver.com/api/v1/projects/%s" % self.project.pk,
        }
        serializer = MergedXFormSerializer(data=data)
        self.assertFalse(serializer.is_valid(raise_exception=False))
        self.assertNotIn('name', serializer.errors)
        self.assertNotIn('project', serializer.errors)
        self.assertEqual(serializer.errors['xforms'],
                         [u'This list may not be empty.'])

        # 1 xform
        data['xforms'] = ["http://testserver.com/api/v1/forms/%s" % xform1.pk]
        serializer = MergedXFormSerializer(data=data)
        self.assertFalse(serializer.is_valid(raise_exception=False))
        self.assertIn(u'This field should have at least two unique xforms.',
                      serializer.errors['xforms'])

        # same xform twice
        data['xforms'] = [
            "http://testserver.com/api/v1/forms/%s" % xform1.pk,
            "http://testserver.com/api/v1/forms/%s" % xform1.pk
        ]
        serializer = MergedXFormSerializer(data=data)
        self.assertFalse(serializer.is_valid(raise_exception=False))
        self.assertIn(u'This field should have unique xforms',
                      serializer.errors['xforms'])

        # xform with no matching fields
        xform3 = self._publish_markdown(A_MD, self.user, id_string='c')
        data['xforms'] = [
            "http://testserver.com/api/v1/forms/%s" % xform1.pk,
            "http://testserver.com/api/v1/forms/%s" % xform3.pk
        ]
        serializer = MergedXFormSerializer(data=data)
        self.assertFalse(serializer.is_valid(raise_exception=False))
        self.assertEqual(serializer.errors['xforms'],
                         [u'No matching fields in xforms.'])

        # two different xforms
        xform2 = self._publish_markdown(MD, self.user, id_string='b')
        data['xforms'] = [
            "http://testserver.com/api/v1/forms/%s" % xform1.pk,
            "http://testserver.com/api/v1/forms/%s" % xform2.pk
        ]
        serializer = MergedXFormSerializer(data=data)
        self.assertTrue(serializer.is_valid(raise_exception=False))
        self.assertNotIn('xforms', serializer.errors)