diff --git a/lambda/config_handler/__init__.py b/lambda/config_handler/__init__.py index d267014..5ed71a3 100644 --- a/lambda/config_handler/__init__.py +++ b/lambda/config_handler/__init__.py @@ -1,7 +1,9 @@ import os import boto3 import json +import functools +@functools.lru_cache() def get(topic: str, parameter: str, default=None) -> str: """ Get's a configuration parameter. diff --git a/lambda/config_handler/__main__.py b/lambda/config_handler/__main__.py index 93b884b..313f324 100644 --- a/lambda/config_handler/__main__.py +++ b/lambda/config_handler/__main__.py @@ -19,6 +19,9 @@ secret_call = { } class TestConfigHandler(unittest.TestCase): + def setUp(self) -> None: + config_handler.get.cache_clear() + def test_env(self): with patch.dict(config_handler.os.environ,{ "MQTT_PASSWORD": "test_password" }, clear=True): return_value = config_handler.get("MQTT", "PASSWORD") @@ -30,6 +33,14 @@ class TestConfigHandler(unittest.TestCase): return_value = config_handler.get("MQTT", "PASSWORD") MockApiCall.assert_called() self.assertEqual(return_value, "test_password") + + @patch('botocore.client.BaseClient._make_api_call', return_value=secret_call) + def test_cache(self, MockApiCall): + with patch.dict(config_handler.os.environ,{}, clear=True): #ensure that local env variables don't influence the tests + return_value = config_handler.get("MQTT", "PASSWORD") + return_value = config_handler.get("MQTT", "PASSWORD") + MockApiCall.assert_called_once() + self.assertEqual(return_value, "test_password") @patch('botocore.client.BaseClient._make_api_call', return_value=secret_call) def test_not_found(self, MockApiCall):