-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* tasks and delayed update job status * tests * black/lint * docs * fix docs * Update django pin in docs --------- Co-authored-by: Nathan Swain <swainn@users.noreply.github.com>
- Loading branch information
Showing
15 changed files
with
465 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import unittest | ||
from unittest import mock | ||
|
||
import tethys_compute.tasks as tethys_compute_tasks | ||
|
||
|
||
async def noop(): | ||
pass | ||
|
||
|
||
def raise_error(): | ||
raise Exception() | ||
|
||
|
||
class TestTasks(unittest.IsolatedAsyncioTestCase): | ||
def setUp(self): | ||
pass | ||
|
||
def tearDown(self): | ||
pass | ||
|
||
@mock.patch("tethys_compute.tasks._run_after_delay", new_callable=mock.MagicMock) | ||
@mock.patch("tethys_compute.tasks.asyncio.create_task") | ||
def test_create_task(self, mock_aio_ct, mock_run_delay): | ||
mock_func = mock.MagicMock() | ||
mock_coro = mock.MagicMock() | ||
mock_run_delay.return_value = mock_coro | ||
tethys_compute_tasks.create_task(mock_func) | ||
mock_aio_ct.assert_called_with(mock_coro) | ||
mock_run_delay.assert_called_with( | ||
mock_func, delay=0, periodic=False, count=None | ||
) | ||
|
||
@mock.patch("tethys_compute.tasks.logger") | ||
async def test_run_after_delay(self, mock_log): | ||
await tethys_compute_tasks._run_after_delay( | ||
noop, delay=0, periodic=False, count=None | ||
) | ||
mock_log.info.assert_called() | ||
|
||
@mock.patch("tethys_compute.tasks.logger") | ||
@mock.patch("tethys_compute.tasks.asyncio.sleep") | ||
async def test_run_after_delay_periodic(self, mock_sleep, mock_log): | ||
await tethys_compute_tasks._run_after_delay( | ||
noop, delay=30, periodic=True, count=2 | ||
) | ||
mock_sleep.assert_called_with(30) | ||
mock_log.info.assert_called() | ||
|
||
@mock.patch("tethys_compute.tasks.logger") | ||
async def test_run_after_delay_exception(self, mock_log): | ||
await tethys_compute_tasks._run_after_delay( | ||
raise_error, delay=0, periodic=False, count=None | ||
) | ||
self.assertEqual(mock_log.info.call_count, 2) |
120 changes: 120 additions & 0 deletions
120
tests/unit_tests/test_tethys_compute/test_views/test_update_status.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import unittest | ||
from unittest import mock | ||
|
||
import tethys_compute.views.update_status as tethys_compute_update_status | ||
|
||
|
||
class TestUpdateStatus(unittest.IsolatedAsyncioTestCase): | ||
|
||
def setUp(self): | ||
pass | ||
|
||
def tearDown(self): | ||
pass | ||
|
||
@mock.patch("tethys_compute.views.update_status.TethysJob.objects.get_subclass") | ||
async def test_get_job(self, mock_tj): | ||
mock_user = mock.MagicMock(is_staff=False) | ||
mock_user.has_perm.return_value = False | ||
await tethys_compute_update_status.get_job("job_id", mock_user) | ||
mock_tj.assert_called_with(id="job_id", user=mock_user) | ||
|
||
@mock.patch("tethys_compute.views.update_status.TethysJob.objects.get_subclass") | ||
async def test_get_job_staff(self, mock_tj): | ||
mock_user = mock.MagicMock(is_staff=True) | ||
await tethys_compute_update_status.get_job("job_id", mock_user) | ||
mock_tj.assert_called_with(id="job_id") | ||
|
||
@mock.patch("tethys_compute.views.update_status.TethysJob.objects.get_subclass") | ||
async def test_get_job_has_permission(self, mock_tj): | ||
mock_user = mock.MagicMock(is_staff=False) | ||
mock_user.has_perm.return_value = True | ||
await tethys_compute_update_status.get_job("job_id", mock_user) | ||
mock_tj.assert_called_with(id="job_id") | ||
|
||
@mock.patch("tethys_compute.views.update_status.logger") | ||
@mock.patch("tethys_compute.views.update_status.JsonResponse") | ||
@mock.patch("tethys_compute.views.update_status.TethysJob") | ||
async def test_update_job_status(self, mock_tethysjob, mock_json_response, _): | ||
mock_request = mock.MagicMock(GET={}) | ||
mock_job_id = mock.MagicMock() | ||
mock_job1 = mock.MagicMock() | ||
mock_job1.status = True | ||
mock_tethysjob.objects.get_subclass.return_value = mock_job1 | ||
|
||
await tethys_compute_update_status.update_job_status(mock_request, mock_job_id) | ||
mock_tethysjob.objects.get_subclass.assert_called_once_with(id=mock_job_id) | ||
mock_json_response.assert_called_once_with({"success": True}) | ||
|
||
@mock.patch("tethys_compute.views.update_status.create_task") | ||
@mock.patch("tethys_compute.views.update_status.logger") | ||
@mock.patch("tethys_compute.views.update_status.JsonResponse") | ||
async def test_update_job_status_with_delay( | ||
self, mock_json_response, mock_log, mock_ct | ||
): | ||
mock_request = mock.MagicMock(GET={"delay": "1"}) | ||
mock_job_id = mock.MagicMock() | ||
|
||
await tethys_compute_update_status.update_job_status(mock_request, mock_job_id) | ||
mock_json_response.assert_called_once_with({"success": "scheduled"}) | ||
mock_log.debug.assert_called_once() | ||
mock_ct.assert_called_with( | ||
tethys_compute_update_status._update_job_status, mock_job_id, delay=1 | ||
) | ||
|
||
@mock.patch("tethys_compute.views.update_status.create_task") | ||
@mock.patch("tethys_compute.views.update_status.logger") | ||
@mock.patch("tethys_compute.views.update_status.JsonResponse") | ||
async def test_update_job_status_with_delay_exception( | ||
self, mock_json_response, mock_log, mock_ct | ||
): | ||
mock_request = mock.MagicMock(GET={"delay": "1"}) | ||
mock_job_id = mock.MagicMock() | ||
mock_ct.side_effect = Exception | ||
|
||
await tethys_compute_update_status.update_job_status(mock_request, mock_job_id) | ||
mock_json_response.assert_called_once_with({"success": False}) | ||
mock_log.warning.assert_called_once() | ||
|
||
@mock.patch("tethys_compute.views.update_status.logger") | ||
@mock.patch("tethys_compute.views.update_status.JsonResponse") | ||
@mock.patch("tethys_compute.views.update_status.TethysJob") | ||
async def test_update_job_statusException( | ||
self, mock_tethysjob, mock_json_response, mock_log | ||
): | ||
mock_request = mock.MagicMock(GET={}) | ||
mock_job_id = mock.MagicMock() | ||
mock_tethysjob.objects.get_subclass.side_effect = Exception | ||
|
||
await tethys_compute_update_status.update_job_status(mock_request, mock_job_id) | ||
mock_tethysjob.objects.get_subclass.assert_called_once_with(id=mock_job_id) | ||
mock_json_response.assert_called_once_with({"success": False}) | ||
mock_log.warning.assert_called_once() | ||
|
||
@mock.patch("tethys_compute.views.update_status.JsonResponse") | ||
@mock.patch("tethys_compute.views.update_status.DaskJob") | ||
def test_update_dask_job_status(self, mock_daskjob, mock_json_response): | ||
mock_request = mock.MagicMock() | ||
mock_job_key = mock.MagicMock() | ||
mock_job1 = mock.MagicMock() | ||
mock_job1.status = True | ||
mock_job2 = mock.MagicMock() | ||
mock_daskjob.objects.filter.return_value = [mock_job1, mock_job2] | ||
|
||
# Call the method | ||
tethys_compute_update_status.update_dask_job_status(mock_request, mock_job_key) | ||
|
||
# check results | ||
mock_daskjob.objects.filter.assert_called_once_with(key=mock_job_key) | ||
mock_json_response.assert_called_once_with({"success": True}) | ||
|
||
@mock.patch("tethys_compute.views.update_status.JsonResponse") | ||
@mock.patch("tethys_compute.views.update_status.DaskJob") | ||
def test_update_dask_job_statusException(self, mock_daskjob, mock_json_response): | ||
mock_request = mock.MagicMock() | ||
mock_job_key = mock.MagicMock() | ||
mock_daskjob.objects.filter.side_effect = Exception | ||
|
||
tethys_compute_update_status.update_dask_job_status(mock_request, mock_job_key) | ||
mock_daskjob.objects.filter.assert_called_once_with(key=mock_job_key) | ||
mock_json_response.assert_called_once_with({"success": False}) |
Oops, something went wrong.