Пример #1
0
 def test_submit_csv_and_rollback(self):
     count = Instance.objects.count()
     csv_import.submit_csv(self.user.username, self.xform, self.good_csv)
     self.assertEqual(Instance.objects.count(),
                      count + 9, u'submit_csv test Failed!')
     # Check that correct # of submissions belong to our user
     self.assertEqual(Instance.objects.filter(user=self.user).count(),
                      count + 8, u'submit_csv username check failed!')
Пример #2
0
    def test_submit_csv_edits(self):
        csv_import.submit_csv(self.user.username, self.xform, self.good_csv)
        self.assertEqual(Instance.objects.count(),
                         9, u'submit_csv edits #1 test Failed!')

        edit_csv = open(os.path.join(self.fixtures_dir, 'edit.csv'))
        edit_csv_str = edit_csv.read()

        edit_csv = StringIO(edit_csv_str.format(
            *[x.get('uuid') for x in Instance.objects.values('uuid')]))

        count = Instance.objects.count()
        csv_import.submit_csv(self.user.username, self.xform, edit_csv)
        self.assertEqual(Instance.objects.count(), count,
                         u'submit_csv edits #2 test Failed!')
Пример #3
0
    def csv_import(self, request, *args, **kwargs):
        """ Endpoint for CSV data imports
        Calls :py:func:`formshare.libs.utils.csv_import.submit_csv` for POST
        requests passing the `request.FILES.get('csv_file')` upload
        for import and
        :py:func:formshare.libs.utils.csv_import.get_async_csv_submission_status
        for GET requests passing `job_uuid` query param for job progress
        polling
        """
        resp = {}
        if request.method == 'GET':
            resp.update(get_async_csv_submission_status(
                request.QUERY_PARAMS.get('job_uuid')))
        else:
            csv_file = request.FILES.get('csv_file', None)
            if csv_file is None:
                resp.update({u'error': u'csv_file field empty'})
            else:
                num_rows = sum(1 for row in csv_file) - 1
                if num_rows < settings.CSV_ROW_IMPORT_ASYNC_THRESHOLD:
                    resp.update(submit_csv(request.user.username,
                                           self.get_object(), csv_file))
                else:
                    resp.update(
                        {u'task_id': submit_csv_async.delay(
                            request.user.username, self.get_object(),
                            csv_file).task_id})

        return Response(
            data=resp,
            status=status.HTTP_200_OK if resp.get('error') is None else
            status.HTTP_400_BAD_REQUEST)
Пример #4
0
    def csv_import(self, request, *args, **kwargs):
        """ Endpoint for CSV data imports
        Calls :py:func:`formshare.libs.utils.csv_import.submit_csv` for POST
        requests passing the `request.FILES.get('csv_file')` upload
        for import and
        :py:func:formshare.libs.utils.csv_import.get_async_csv_submission_status
        for GET requests passing `job_uuid` query param for job progress
        polling
        """
        resp = {}
        if request.method == 'GET':
            resp.update(
                get_async_csv_submission_status(
                    request.QUERY_PARAMS.get('job_uuid')))
        else:
            csv_file = request.FILES.get('csv_file', None)
            if csv_file is None:
                resp.update({u'error': u'csv_file field empty'})
            else:
                num_rows = sum(1 for row in csv_file) - 1
                if num_rows < settings.CSV_ROW_IMPORT_ASYNC_THRESHOLD:
                    resp.update(
                        submit_csv(request.user.username, self.get_object(),
                                   csv_file))
                else:
                    resp.update({
                        u'task_id':
                        submit_csv_async.delay(request.user.username,
                                               self.get_object(),
                                               csv_file).task_id
                    })

        return Response(data=resp,
                        status=status.HTTP_200_OK if resp.get('error') is None
                        else status.HTTP_400_BAD_REQUEST)
Пример #5
0
 def test_reject_spaces_in_headers(self):
     non_utf8csv = open(os.path.join(self.fixtures_dir, 'header_space.csv'))
     result = csv_import.submit_csv(self.user.username,
                                    self.xform, non_utf8csv)
     self.assertEqual(result.get('error'),
                      u'CSV file fieldnames should not contain spaces',
                      u'Incorrect error message returned.')
Пример #6
0
    def test_submit_csv_xml_location_property_test(
            self, d2x, safe_create_instance):
        safe_create_instance.return_value = [None, ]
        single_csv = open(os.path.join(self.fixtures_dir, 'single.csv'))
        csv_import.submit_csv(self.user.username, self.xform, single_csv)

        test_location_val = '83.3595 -32.8601 0 1'
        test_location2_val = '21.22474 -10.5601 50000 200'

        self.assertNotEqual(d2x.call_args, None,
                            u'dict2xmlsubmission not called')

        call_dict = d2x.call_args[0][0]

        self.assertEqual(call_dict.get('test_location'),
                         test_location_val, u'Location prop test fail')
        self.assertEqual(call_dict.get('test_location2'),
                         test_location2_val, u'Location2 prop test fail')
Пример #7
0
 def test_import_non_utf8_csv(self):
     count = Instance.objects.count()
     non_utf8_csv = open(os.path.join(self.fixtures_dir, 'non_utf8.csv'))
     result = csv_import.submit_csv(self.user.username,
                                    self.xform, non_utf8_csv)
     self.assertEqual(result.get('error'),
                      u'CSV file must be utf-8 encoded',
                      u'Incorrect error message returned.')
     self.assertEqual(Instance.objects.count(), count,
                      u'Non unicode csv import rollback failed!')
Пример #8
0
    def test_submit_csv_xml_params(self, safe_create_instance):
        safe_create_instance.return_value = {}
        single_csv = open(os.path.join(self.fixtures_dir, 'single.csv'))
        csv_import.submit_csv(self.user.username, self.xform, single_csv)
        xml_file_param = StringIO(open(os.path.join(self.fixtures_dir,
                                                    'single.xml')).read())
        safe_create_args = list(safe_create_instance.call_args[0])

        self.assertEqual(safe_create_args[0],
                         self.user.username,
                         u'Wrong username passed')
        self.assertEqual(strip_xml_uuid(safe_create_args[1].getvalue()),
                         strip_xml_uuid(xml_file_param.getvalue()),
                         u'Wrong xml param passed')
        self.assertEqual(safe_create_args[2], [],
                         u'Wrong media array param passed')
        self.assertEqual(safe_create_args[3], self.xform.uuid,
                         u'Wrong xform uuid passed')
        self.assertEqual(safe_create_args[4], None)
Пример #9
0
 def test_submit_csv_param_sanity_check(self):
     resp = csv_import.submit_csv(u'userX', XForm(), 123456)
     self.assertIsNotNone(resp.get('error'))
Пример #10
0
 def test_nested_geo_paths_csv(self):
     good_csv = open(os.path.join(self.fixtures_dir, 'another_good.csv'))
     csv_import.submit_csv(self.user.username, self.xform, good_csv)
     self.assertEqual(Instance.objects.count(),
                      9, u'submit_csv edits #1 test Failed!')