2015-12-06 18:47:11 +01:00
|
|
|
"""
|
|
|
|
This module provides tests for the
|
|
|
|
:py:mod:`taskresults.management.commands.fetch_taskresults` Django management
|
|
|
|
command.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
from unittest.mock import MagicMock, patch
|
2015-12-06 18:47:11 +01:00
|
|
|
|
|
|
|
from django.test import TestCase
|
|
|
|
|
|
|
|
from taskresults.management.commands.fetch_taskresults import Command
|
2019-01-30 21:27:25 +01:00
|
|
|
from taskresults.models import TaskResult
|
2015-12-06 18:47:11 +01:00
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
TEST_TASK_UUID = "3120f6a8-2665-4fa3-a785-79efd28bfe92"
|
|
|
|
TEST_TASK_NAME = "test.task"
|
|
|
|
TEST_TASK_RESULT = "4ll y0ur b453 4r3 b3l0ng t0 u5"
|
2015-12-06 18:47:11 +01:00
|
|
|
|
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
@patch("taskresults.models.app.AsyncResult")
|
2015-12-06 18:47:11 +01:00
|
|
|
class FetchTaskResultsCommandTest(TestCase):
|
|
|
|
def test_handle_unfinished(self, asyncresult):
|
|
|
|
resultmock = MagicMock(task_id=TEST_TASK_UUID)
|
|
|
|
sigmock = MagicMock()
|
|
|
|
sigmock.apply_async.return_value = resultmock
|
|
|
|
tr = TaskResult.objects.create_task_result(TEST_TASK_NAME, sigmock)
|
|
|
|
self.assertFalse(tr.finished)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(tr.result, "")
|
|
|
|
self.assertEqual(tr.state, "")
|
2015-12-06 18:47:11 +01:00
|
|
|
|
|
|
|
aresult = asyncresult.return_value
|
2019-01-30 21:27:25 +01:00
|
|
|
aresult.state = "PENDING"
|
2015-12-06 18:47:11 +01:00
|
|
|
aresult.ready.return_value = False
|
|
|
|
|
|
|
|
Command().handle()
|
|
|
|
|
|
|
|
tr = TaskResult.objects.get(task_id=TEST_TASK_UUID)
|
|
|
|
self.assertTrue(asyncresult.called_with(TEST_TASK_UUID))
|
|
|
|
self.assertTrue(aresult.ready.called_with())
|
|
|
|
self.assertFalse(tr.finished)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(tr.result, "")
|
|
|
|
self.assertEqual(tr.state, "PENDING")
|
2015-12-06 18:47:11 +01:00
|
|
|
|
|
|
|
def test_handle_finished(self, asyncresult):
|
|
|
|
resultmock = MagicMock(task_id=TEST_TASK_UUID)
|
|
|
|
sigmock = MagicMock()
|
|
|
|
sigmock.apply_async.return_value = resultmock
|
|
|
|
tr = TaskResult.objects.create_task_result(TEST_TASK_NAME, sigmock)
|
|
|
|
self.assertFalse(tr.finished)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(tr.result, "")
|
|
|
|
self.assertEqual(tr.state, "")
|
2015-12-06 18:47:11 +01:00
|
|
|
|
|
|
|
aresult = asyncresult.return_value
|
2019-01-30 21:27:25 +01:00
|
|
|
aresult.state = "SUCCESS"
|
2015-12-06 18:47:11 +01:00
|
|
|
aresult.ready.return_value = True
|
|
|
|
aresult.get.return_value = TEST_TASK_RESULT
|
|
|
|
|
|
|
|
Command().handle()
|
|
|
|
|
|
|
|
tr = TaskResult.objects.get(task_id=TEST_TASK_UUID)
|
|
|
|
self.assertTrue(asyncresult.called_with(TEST_TASK_UUID))
|
|
|
|
self.assertTrue(aresult.ready.called_with())
|
|
|
|
self.assertTrue(aresult.get.called_with())
|
|
|
|
self.assertTrue(tr.finished)
|
|
|
|
self.assertEqual(tr.result, TEST_TASK_RESULT)
|
2019-01-30 21:27:25 +01:00
|
|
|
self.assertEqual(tr.state, "SUCCESS")
|