Example #1
0
def _run_sync_validation(resource_id,
                         upload=False,
                         new_resource=True,
                         user=None):

    try:
        t.get_action(u'resource_validation_run')({
            u'ignore_auth': True
        }, {
            u'resource_id': resource_id,
            u'async': False
        })
    except t.ValidationError as e:
        log.info(u'Could not run validation for resource {}: {}'.format(
            resource_id, str(e)))
        return

    validation = t.get_action(u'resource_validation_show')(
        {
            u'ignore_auth': True
        }, {
            u'resource_id': resource_id
        })

    report = validation['report']

    if not report['valid']:

        # Delete validation object
        t.get_action(u'resource_validation_delete')({
            u'ignore_auth': True
        }, {
            u'resource_id': resource_id
        })

        # Delete uploaded file
        if upload:
            delete_local_uploaded_file(resource_id)

        if new_resource:
            # Delete resource
            t.get_action(u'resource_delete')({
                u'ignore_auth': True,
                'user': user
            }, {
                u'id': resource_id
            })

        raise t.ValidationError({u'validation': [report]})
    def test_delete_passes_if_os_exeception(self, mock_open):

        resource_id = str(uuid.uuid4())
        path = '/doesnt_exist/resources/{}/{}/{}'.format(
            resource_id[0:3], resource_id[3:6], resource_id[6:])

        patcher = fake_filesystem_unittest.Patcher()
        patcher.setUp()
        patcher.fs.CreateFile(path)

        assert os.path.exists(path)
        with mock.patch('ckanext.validation.utils.os.remove',
                        side_effect=OSError):

            delete_local_uploaded_file(resource_id)

        patcher.tearDown()
    def test_delete_upload_file(self, mock_open):

        resource_id = str(uuid.uuid4())
        path = '/doesnt_exist/resources/{}/{}/{}'.format(
            resource_id[0:3], resource_id[3:6], resource_id[6:])

        patcher = fake_filesystem_unittest.Patcher()
        patcher.setUp()
        patcher.fs.CreateFile(path)

        assert os.path.exists(path)

        delete_local_uploaded_file(resource_id)

        assert not os.path.exists(path)

        patcher.tearDown()
    def test_delete_file_not_deleted_if_resources_second(self, mock_open):

        resource_id = str(uuid.uuid4())
        path = '/doesnt_exist/resources/data/{}'.format(resource_id)

        patcher = fake_filesystem_unittest.Patcher()
        patcher.setUp()
        patcher.fs.CreateFile(path)

        assert os.path.exists(path)
        with mock.patch('ckanext.validation.utils.get_local_upload_path',
                        return_value=path):
            delete_local_uploaded_file(resource_id)

        assert not os.path.exists(path)
        assert os.path.exists('/doesnt_exist/resources')

        patcher.tearDown()