From ab03c45f53adb87c6998a2fe12845d532f83286c Mon Sep 17 00:00:00 2001 From: Mattia <5013654+mattiamatrix@users.noreply.github.com> Date: Wed, 3 Jan 2024 16:46:41 +0100 Subject: [PATCH] add test --- tests/test_credentials.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/test_credentials.py b/tests/test_credentials.py index 6e0a1ff8..85168741 100644 --- a/tests/test_credentials.py +++ b/tests/test_credentials.py @@ -1,9 +1,16 @@ +from unittest.mock import patch + import pytest from boto3.session import Session from botocore.client import BaseClient from moto import mock_s3 -from prefect_aws.credentials import AwsCredentials, ClientType, MinIOCredentials +from prefect_aws.credentials import ( + AwsCredentials, + ClientType, + MinIOCredentials, + _get_client_cached, +) def test_aws_credentials_get_boto3_session(): @@ -44,3 +51,27 @@ def test_minio_credentials_get_boto3_session(): def test_credentials_get_client(credentials, client_type): with mock_s3(): assert isinstance(credentials.get_client(client_type), BaseClient) + + +@patch("prefect_aws.credentials._get_client_cached") +def test_get_client_cached(mock_get_client_cached): + """ + Test to ensure that _get_client_cached function returns the same instance + for multiple calls with the same parameters and properly utilizes lru_cache. + """ + + # Create a mock AwsCredentials instance + aws_credentials_block = AwsCredentials() + + # Call _get_client_cached multiple times with the same parameters + _get_client_cached(aws_credentials_block, ClientType.S3) + _get_client_cached(aws_credentials_block, ClientType.S3) + + # Verify that _get_client_cached is called only once due to caching + mock_get_client_cached.assert_called_once_with(aws_credentials_block, ClientType.S3) + + # Test with different parameters to ensure they are cached separately + _get_client_cached(aws_credentials_block, ClientType.ECS) + assert ( + mock_get_client_cached.call_count == 2 + ), "Should be called twice with different parameters"