From 867c2e43533869ac1b1e2ed11a21e109be8c8f7a Mon Sep 17 00:00:00 2001 From: Medhat Gayed Date: Tue, 23 Jan 2018 16:29:08 +1300 Subject: [PATCH] Added unit tests for when the queue has items and the queue size is changing vs not changing. --- tests/test_commands.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/tests/test_commands.py b/tests/test_commands.py index 82b62db..f3c4632 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -16,8 +16,8 @@ @mock.patch.object(settings, 'DEFAULT_FROM_EMAIL', 'sample_from@email.com') @mock.patch.object(settings, 'ADMINS', ['sample_admin@email.com']) class RQPulseCheckTestCase(TestCase): - def test_number_of_workers_is_not_expected(self, mock_queue, mock_worker, mock_send_mail, - mock_log, mock_time): + def test_number_of_workers_is_not_expected( + self, mock_queue, mock_worker, mock_send_mail, mock_log, mock_time): mock_worker.all.return_value = [] call_command('rq_pulse_check') mock_send_mail.assert_called_with( @@ -32,22 +32,34 @@ def test_number_of_workers_is_not_expected(self, mock_queue, mock_worker, mock_s 'The number of workers 1 does not equal the expected number 2. Workers maybe down.', 'sample_from@email.com', ['sample_admin@email.com']) - def test_number_of_workers_is_expected(self, mock_queue, mock_worker, mock_send_mail, - mock_log, mock_time): + def test_number_of_workers_is_expected( + self, mock_queue, mock_worker, mock_send_mail, mock_log, mock_time): mock_worker.all.return_value = [mock.MagicMock(), mock.MagicMock()] call_command('rq_pulse_check') self.assertFalse(mock_send_mail.called) - def test_queue_has_no_items(self, mock_queue, mock_worker, mock_send_mail, - mock_log, mock_time): + def test_queue_has_no_items( + self, mock_queue, mock_worker, mock_send_mail, mock_log, mock_time): mock_queue.return_value.__len__.return_value = 0 call_command('rq_pulse_check') self.assertFalse(mock_time.sleep.called) - def test_queue_has_items(self, mock_queue, mock_worker, mock_send_mail, - mock_log, mock_time): + def test_queue_has_items_and_queue_size_not_changing( + self, mock_queue, mock_worker, mock_send_mail, mock_log, mock_time): + mock_worker.all.return_value = [mock.MagicMock(), mock.MagicMock()] mock_queue.return_value.__len__.return_value = 1 call_command('rq_pulse_check') self.assertListEqual(mock_time.sleep.call_args_list, [mock.call(5), mock.call(5), mock.call(5), mock.call(5), mock.call(5)]) - \ No newline at end of file + mock_send_mail.assert_called_with( + 'WARNING: RQ Workers maybe down!', + 'The Q size is not changing, this is bad. Workers maybe down.', + 'sample_from@email.com', ['sample_admin@email.com']) + + def test_queue_has_items_and_queue_size_is_changing( + self, mock_queue, mock_worker, mock_send_mail, mock_log, mock_time): + mock_worker.all.return_value = [mock.MagicMock(), mock.MagicMock()] + mock_queue.return_value.__len__.side_effect = [10, 9] + call_command('rq_pulse_check') + self.assertListEqual(mock_time.sleep.call_args_list, [mock.call(5)]) + self.assertFalse(mock_send_mail.called) \ No newline at end of file