|
14 | 14 | def test_client_initialization():
|
15 | 15 | """Test client initialization."""
|
16 | 16 | with patch("boto3.client") as mock_boto_client:
|
17 |
| - # Test gamma environment |
18 |
| - client = MemoryClient(region_name="us-west-2", environment="gamma") |
| 17 | + # Setup the mock to return a consistent region_name |
| 18 | + mock_client_instance = MagicMock() |
| 19 | + mock_client_instance.meta.region_name = "us-west-2" |
| 20 | + mock_boto_client.return_value = mock_client_instance |
19 | 21 |
|
| 22 | + client = MemoryClient(region_name="us-west-2") |
| 23 | + |
| 24 | + # Check that the region was set correctly and boto3.client was called twice |
20 | 25 | assert client.region_name == "us-west-2"
|
21 | 26 | assert mock_boto_client.call_count == 2
|
22 | 27 |
|
23 | 28 |
|
24 | 29 | def test_client_initialization_region_mismatch():
|
25 | 30 | """Test client initialization with region mismatch warning."""
|
26 |
| - with patch("boto3.client"): |
27 |
| - with patch("os.getenv") as mock_getenv: |
28 |
| - # Mock AWS_REGION environment variable |
29 |
| - mock_getenv.return_value = "us-east-1" |
30 |
| - |
31 |
| - with warnings.catch_warnings(record=True) as w: |
32 |
| - warnings.simplefilter("always") |
33 |
| - |
34 |
| - # Initialize client with different region than AWS_REGION |
35 |
| - client = MemoryClient(region_name="us-west-2", environment="prod") |
36 | 31 |
|
37 |
| - # Verify warning was issued |
38 |
| - assert len(w) >= 1 |
39 |
| - warning_messages = [str(warning.message) for warning in w] |
40 |
| - assert any( |
41 |
| - "AWS_REGION environment variable (us-east-1) differs from provided region_name (us-west-2)" in msg |
42 |
| - for msg in warning_messages |
43 |
| - ) |
44 |
| - |
45 |
| - # Verify client uses provided region_name, not environment variable |
46 |
| - assert client.region_name == "us-west-2" |
| 32 | + with patch("boto3.client") as mock_boto_client: |
| 33 | + # First test - environment variable takes precedence |
| 34 | + with patch("boto3.Session") as mock_session: |
| 35 | + # Mock the session instance to simulate AWS_REGION=us-east-1 |
| 36 | + mock_session_instance = MagicMock() |
| 37 | + mock_session_instance.region_name = "us-east-1" |
| 38 | + mock_session.return_value = mock_session_instance |
| 39 | + |
| 40 | + # Mock the boto client |
| 41 | + mock_client_instance = MagicMock() |
| 42 | + mock_client_instance.meta.region_name = "us-east-1" |
| 43 | + mock_boto_client.return_value = mock_client_instance |
| 44 | + |
| 45 | + # When region_name is provided, environment variable should still take precedence |
| 46 | + client1 = MemoryClient(region_name="us-west-2") |
| 47 | + assert client1.region_name == "us-east-1" # Environment wins over explicit param |
| 48 | + |
| 49 | + # Second test - no environment variable, explicit param is used |
| 50 | + with patch("boto3.Session") as mock_session: |
| 51 | + # Mock the session instance to simulate no AWS_REGION set |
| 52 | + mock_session_instance = MagicMock() |
| 53 | + mock_session_instance.region_name = None |
| 54 | + mock_session.return_value = mock_session_instance |
| 55 | + |
| 56 | + # Mock the boto client |
| 57 | + mock_client_instance = MagicMock() |
| 58 | + mock_client_instance.meta.region_name = "us-west-2" |
| 59 | + mock_boto_client.return_value = mock_client_instance |
| 60 | + |
| 61 | + # When AWS_REGION is not set, explicitly provided region should be used |
| 62 | + client2 = MemoryClient(region_name="us-west-2") |
| 63 | + assert client2.region_name == "us-west-2" |
47 | 64 |
|
48 | 65 |
|
49 | 66 | def test_namespace_defaults():
|
|
0 commit comments