gva/gnuviechadmin/taskresults/tests/test_models.py
Jan Dittberner d5bba7a22d asynchronous refactoring
- don't execute celery tasks directly
- introduce optional parameters to fileserver tasks to allow chaining
- handle user/group/key create and delete tasks in new osusers.signals
  class
- adapt unit tests
- change TaskResults model to store the task signatures
- generalize the local settings' logging configuration
2015-10-12 00:23:31 +02:00

62 lines
2.2 KiB
Python

from __future__ import absolute_import, unicode_literals
from django.test import TestCase
from mock import patch, MagicMock
from taskresults.models import TaskResult
TEST_TASK_UUID = '3120f6a8-2665-4fa3-a785-79efd28bfe92'
TEST_TASK_NAME = 'test.task'
TEST_TASK_RESULT = '4ll y0ur b453 4r3 b3l0ng t0 u5'
class TaskResultTest(TestCase):
@patch('taskresults.models.app')
def test_update_taskstatus_unfinished(self, app):
resultmock = MagicMock(task_id=TEST_TASK_UUID)
resultmock.ready.return_value = False
mock = MagicMock()
mock.apply_async.return_value = resultmock
tr = TaskResult.objects.create_task_result(TEST_TASK_NAME, mock)
self.assertFalse(tr.finished)
mymock = app.AsyncResult(TEST_TASK_UUID)
mymock.state = 'SUCCESS'
mymock.get.return_value = TEST_RESULT
tr.fetch_result()
mymock.get.assert_called_with()
self.assertTrue(tr.finished)
@patch('taskresults.models.app')
def test_update_taskstatus_finished(self, app):
resultmock = MagicMock(task_id=TEST_TASK_UUID)
resultmock.ready.return_value = True
resultmock.state = 'SUCCESS'
resultmock.result = TEST_RESULT
mock = MagicMock()
mock.apply_async.return_value = resultmock
mock.state = 'SUCCESS'
mock.result = TEST_RESULT
tr = TaskResult.objects.create_task_result(TEST_TASK_NAME, mock)
tr.fetch_result()
self.assertTrue(tr.finished)
mymock = app.AsyncResult(TEST_TASK_UUID)
tr.fetch_result()
self.assertEqual(mymock.get.call_count, 1)
self.assertTrue(tr.finished)
TEST_RESULT = MagicMock()
TEST_RESULT.task_id = TEST_TASK_UUID
TEST_RESULT.task_name = TEST_TASK_NAME
TEST_RESULT.ready.return_value = False
class TaskResultManagerTest(TestCase):
def test_create_task_result(self):
resultmock = MagicMock(task_id=TEST_TASK_UUID)
mock = MagicMock()
mock.apply_async.return_value = resultmock
tr = TaskResult.objects.create_task_result(TEST_TASK_NAME, mock)
self.assertIsInstance(tr, TaskResult)
self.assertEqual(tr.task_id, TEST_TASK_UUID)
self.assertEqual(tr.creator, TEST_TASK_NAME)