From e2905e95a4562fddc698c387c12d4aba6933c1dc Mon Sep 17 00:00:00 2001 From: rjmurray Date: Mon, 18 Nov 2024 14:25:38 +0000 Subject: [PATCH] Starting point for initial unit test --- plugins/redis/requirements.txt | 1 + plugins/redis/unit_test/delete.py | 43 +++++++++++++++++++++++++++++++ plugins/redis/unit_test/util.py | 38 +++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 plugins/redis/unit_test/delete.py create mode 100644 plugins/redis/unit_test/util.py diff --git a/plugins/redis/requirements.txt b/plugins/redis/requirements.txt index e44bb3b808..b74557ab84 100755 --- a/plugins/redis/requirements.txt +++ b/plugins/redis/requirements.txt @@ -2,3 +2,4 @@ # All dependencies must be version-pinned, eg. requests==1.2.0 # See: https://pip.pypa.io/en/stable/user_guide/#requirements-files redis==4.3.6 +parameterized==0.9.0 \ No newline at end of file diff --git a/plugins/redis/unit_test/delete.py b/plugins/redis/unit_test/delete.py new file mode 100644 index 0000000000..cd1a3cce36 --- /dev/null +++ b/plugins/redis/unit_test/delete.py @@ -0,0 +1,43 @@ +import os +import sys + +sys.path.append(os.path.abspath("../")) + +from typing import Any, Dict +from unittest import TestCase +from unittest.mock import MagicMock, patch + +from insightconnect_plugin_runtime.exceptions import PluginException +from jsonschema import validate +from komand_redis.actions.delete import Delete +from parameterized import parameterized +import util + + +@patch("redis.StrictRedis", side_effect=util.mock_redis) +class TestForward(TestCase): + @classmethod + def setUpClass(cls) -> None: + cls.action = util.Util.default_connector(Delete()) + + @parameterized.expand( + [ + [ + "delete", + {"key": "user:1234"}, + {"count": 0} + ] + ] + ) + def test_delete( + self, + _mock_request: MagicMock, + _test_name: str, + input_params: Dict[str, Any], + expected: Dict[str, Any], + ) -> None: + validate(input_params, self.action.input.schema) + actual = self.action.run(input_params) + validate(actual, self.action.output.schema) + self.assertEqual(expected, actual) + diff --git a/plugins/redis/unit_test/util.py b/plugins/redis/unit_test/util.py new file mode 100644 index 0000000000..0556bab33a --- /dev/null +++ b/plugins/redis/unit_test/util.py @@ -0,0 +1,38 @@ +import json +import logging +import os +import sys +from typing import Any, Dict, List +sys.path.append(os.path.abspath("../")) +from insightconnect_plugin_runtime.action import Action +from komand_redis.connection.connection import Connection + +class Util(): + def __init__(self): + pass + + def default_connector(action: Action) -> Action: + default_connection = Connection() + default_connection.logger = logging.getLogger("connection logger") + params = {"host": "127.0.0.1", "port": 8080, "db": 0} + default_connection.connect(params) + action.connection = default_connection + action.logger = logging.getLogger("action logger") + return action + +class mock_redis(): + + def __init__(self): + pass + + def delete(self): + return 10 + + def get(self, params: str): + if params == "test": + return True + + + + +