add LRU cache

This commit is contained in:
xss 2023-10-23 15:23:59 +11:00
parent ce95ff37ad
commit 86029b5347
2 changed files with 13 additions and 0 deletions

View File

@ -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.

View File

@ -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):