From 9d5908f8fbf6e9f0b4f4567f61098653109c436c Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Fri, 12 Feb 2021 21:12:47 +0300 Subject: Add new procedure for queueing a command * gn3/commands.py (queue_cmd): New procedure. * tests/unit/test_commands.py: New test-cases for ^^. --- tests/unit/test_commands.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'tests/unit') diff --git a/tests/unit/test_commands.py b/tests/unit/test_commands.py index a319332..a1ac6eb 100644 --- a/tests/unit/test_commands.py +++ b/tests/unit/test_commands.py @@ -2,8 +2,21 @@ import os import unittest +from dataclasses import dataclass +from datetime import datetime +from typing import Callable from unittest import mock from gn3.commands import compose_gemma_cmd +from gn3.commands import queue_cmd +from gn3.exceptions import RedisConnectionError + + +@dataclass +class MockRedis: + """Mock Redis connection""" + ping: Callable + hset: mock.MagicMock + rpush: mock.MagicMock class TestCommands(unittest.TestCase): @@ -27,3 +40,33 @@ class TestCommands(unittest.TestCase): "-g /tmp/genofile.txt " "-p /tmp/gf13Ad0tRX/phenofile.txt" " -gk")) + + def test_queue_cmd_exception_raised_when_redis_is_down(self): + """Test that the correct error is raised when Redis is unavailable""" + self.assertRaises(RedisConnectionError, + queue_cmd, + "ls", + MockRedis(ping=lambda: False, + hset=mock.MagicMock(), + rpush=mock.MagicMock())) + + @mock.patch("gn3.commands.datetime") + @mock.patch("gn3.commands.uuid4") + def test_queue_cmd_right_correct_calls_to_redis(self, mock_uuid4, + mock_datetime): + """Test that the cmd is queued properly""" + mock_uuid4.return_value = 1234 + mock_datetime.now.return_value = datetime.fromisoformat('2021-02-12 ' + '17:32:24.' + '859097') + mock_redis_conn = MockRedis(ping=lambda: True, + hset=mock.MagicMock(), + rpush=mock.MagicMock()) + actual_unique_id = "cmd::2021-02-1217-3224-3224-1234" + self.assertEqual(queue_cmd("ls", mock_redis_conn), actual_unique_id) + mock_redis_conn.hset.assert_has_calls( + [mock.call("cmd", "ls", actual_unique_id), + mock.call("result", "", actual_unique_id), + mock.call("status", "queued", actual_unique_id)]) + mock_redis_conn.rpush.assert_has_calls( + [mock.call("GN2::job-queue", actual_unique_id)]) -- cgit v1.2.3