def test_result(self, listdir):
        self.config.add_route('result_file', '/{script}/{taskid}/{filename}')
        self.request.matchdict['script'] = 'plot'
        self.request.matchdict['taskid'] = 'mytaskid'
        views = Views(self.request)
        task_result = Mock(AsyncResult)
        task_result.id = 'mytaskid'
        task_result.ready.return_value = True
        task_result.failed.return_value = False
        views.celery.AsyncResult = Mock(return_value=task_result)
        task = PythonTask()
        task.name = 'plot'
        views.celery.tasks = {'plot': task}
        listdir.return_value = ['stderr.txt', 'stdout.txt']

        result = views.result()

        eresult = {'result': task_result,
                   'files': {'stderr.txt': '/plot/mytaskid/stderr.txt',
                             'stdout.txt': '/plot/mytaskid/stdout.txt',
                             },
                   'task': task,
                   'result_html': None,
                   }
        self.assertEqual(result, eresult)
        listdir.assert_called_with('/tmp/results/mytaskid')
    def test_jsform(self):
        from tempfile import NamedTemporaryFile
        formjs = NamedTemporaryFile(suffix='.js')
        task = PythonTask()
        task.js_form_location = Mock(return_value=formjs.name)
        self.request.matchdict['script'] = 'plot'
        views = Views(self.request)
        views.celery.tasks = {'plot': task}

        result = views.jsform()

        self.assertIsInstance(result, FileResponse)
        self.assertEqual(result.content_type, 'application/javascript')
        formjs.close()
    def test_result_template(self):
        self.request.matchdict['script'] = 'plot'
        self.request.matchdict['taskid'] = 'mytaskid'
        views = Views(self.request)
        task_result = Mock(AsyncResult)
        views.task_result = Mock(return_value=task_result)
        task = PythonTask()
        task.render_result = Mock(return_value='mytemplate')
        views.task = Mock(return_value=task)
        files = {'result.csv': '/plot/mytaskid/result.csv', }
        views.result_files = Mock(return_value=files)

        result = views.result()

        eresult = {'result': task_result,
                   'files': files,
                   'task': task,
                   'result_html': 'mytemplate',
                   }
        self.assertEqual(result, eresult)
    def test_result_nofiles(self, listdir):
        self.request.matchdict['script'] = 'plot'
        self.request.matchdict['taskid'] = 'mytaskid'
        views = Views(self.request)
        task_result = Mock(AsyncResult)
        task_result.ready.return_value = True
        task_result.failed.return_value = False
        views.celery.AsyncResult = Mock(return_value=task_result)
        task = PythonTask()
        task.name = 'plot'
        views.celery.tasks = {'plot': task}
        listdir.side_effect = OSError('[Errno 2] No such file or directory: /tmp/results/mytaskid')

        result = views.result()

        eresult = {'result': task_result,
                   'files': {},
                   'task': task,
                   'result_html': None,
                   }
        self.assertEqual(result, eresult)