Skip to content

Commit 188ee56

Browse files
committed
docs(parameters): new section clearing cache
1 parent be2cb73 commit 188ee56

File tree

1 file changed

+75
-3
lines changed

1 file changed

+75
-3
lines changed

docs/utilities/parameters.md

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,9 @@ The **`config`** and **`boto3_session`** parameters enable you to pass in a cust
518518

519519
## Testing your code
520520

521-
For unit testing your applications, you can mock the calls to the parameters utility to avoid calling AWS APIs. This
522-
can be achieved in a number of ways - in this example, we use the [pytest monkeypatch fixture](https://docs.pytest.org/en/latest/how-to/monkeypatch.html)
523-
to patch the `parameters.get_parameter` method:
521+
### Mocking parameter values
522+
523+
For unit testing your applications, you can mock the calls to the parameters utility to avoid calling AWS APIs. This can be achieved in a number of ways - in this example, we use the [pytest monkeypatch fixture](https://docs.pytest.org/en/latest/how-to/monkeypatch.html) to patch the `parameters.get_parameter` method:
524524

525525
=== "tests.py"
526526
```python
@@ -588,3 +588,75 @@ object named `get_parameter_mock`.
588588
assert return_val.get('message') == 'mock_value'
589589

590590
```
591+
592+
593+
### Clearing cache
594+
595+
Parameters utility caches all parameter values for performance and cost reasons. However, this can have unintended interference in tests using the same parameter name.
596+
597+
Within your tests, you can use `clear_cache` method available in [every provider](#built-in-provider-class). When using multiple providers or higher level functions like `get_parameter`, use `clear_caches` standalone function to clear cache globally.
598+
599+
600+
=== "clear_cache method"
601+
```python hl_lines="9"
602+
import pytest
603+
604+
from src import app
605+
606+
607+
@pytest.fixture(scope="function", autouse=True)
608+
def clear_parameters_cache():
609+
yield
610+
app.ssm_provider.clear_cache() # This will clear SSMProvider cache
611+
612+
@pytest.fixture
613+
def mock_parameter_response(monkeypatch):
614+
def mockreturn(name):
615+
return "mock_value"
616+
617+
monkeypatch.setattr(app.ssm_provider, "get", mockreturn)
618+
619+
# Pass our fixture as an argument to all tests where we want to mock the get_parameter response
620+
def test_handler(mock_parameter_response):
621+
return_val = app.handler({}, {})
622+
assert return_val.get('message') == 'mock_value'
623+
```
624+
625+
=== "global clear_caches"
626+
```python hl_lines="10"
627+
import pytest
628+
629+
from aws_lambda_powertools.utilities import parameters
630+
from src import app
631+
632+
633+
@pytest.fixture(scope="function", autouse=True)
634+
def clear_parameters_cache():
635+
yield
636+
parameters.clear_caches() # This will clear all providers cache
637+
638+
@pytest.fixture
639+
def mock_parameter_response(monkeypatch):
640+
def mockreturn(name):
641+
return "mock_value"
642+
643+
monkeypatch.setattr(app.ssm_provider, "get", mockreturn)
644+
645+
# Pass our fixture as an argument to all tests where we want to mock the get_parameter response
646+
def test_handler(mock_parameter_response):
647+
return_val = app.handler({}, {})
648+
assert return_val.get('message') == 'mock_value'
649+
```
650+
651+
=== "app.py"
652+
```python
653+
from aws_lambda_powertools.utilities import parameters
654+
from botocore.config import Config
655+
656+
ssm_provider = parameters.SSMProvider(config=Config(region_name="us-west-1"))
657+
658+
659+
def handler(event, context):
660+
value = ssm_provider.get("/my/parameter")
661+
return {"message": value}
662+
```

0 commit comments

Comments
 (0)