Skip to content

Commit 4d84e73

Browse files
committed
update some tests
1 parent 4612a7e commit 4d84e73

3 files changed

Lines changed: 57 additions & 57 deletions

File tree

python/example_code/neptune/tests/neptune_stubber.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,30 @@ def stub_delete_db_cluster(self, cluster_id, error_code=None):
242242
expected_params
243243
)
244244

245+
def stub_describe_all_db_clusters(self, pages, error_code=None):
246+
"""
247+
Stub for describe_db_clusters using a paginator simulation.
248+
:param pages: List of pages, where each page is a list of DBClusters.
249+
Example: [[{...}], [{...}]] simulates 2 pages.
250+
"""
251+
if error_code:
252+
self.stubber.add_client_error(
253+
"describe_db_clusters",
254+
service_error_code=error_code,
255+
service_message=f"{error_code} error",
256+
expected_params={}
257+
)
258+
else:
259+
for clusters in pages:
260+
response = {
261+
"DBClusters": clusters
262+
}
263+
self.stubber.add_response(
264+
"describe_db_clusters",
265+
response,
266+
expected_params={}
267+
)
268+
245269
def stub_delete_db_subnet_group(self, group_name, error_code=None):
246270
expected_params = {
247271
"DBSubnetGroupName": group_name
@@ -260,3 +284,4 @@ def stub_delete_db_subnet_group(self, group_name, error_code=None):
260284
{},
261285
expected_params
262286
)
287+
Lines changed: 30 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,38 @@
11
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
# SPDX-License-Identifier: Apache-2.0
33

4+
import boto3
45
import pytest
5-
from unittest.mock import MagicMock
6+
from botocore.stub import Stubber
67
from botocore.exceptions import ClientError
7-
8-
from hello_neptune import describe_db_clusters # replace with actual import
8+
from hello_neptune import describe_db_clusters
99

1010

1111
@pytest.fixture
12-
def mock_neptune_client():
13-
"""Return a mocked boto3 Neptune client."""
14-
return MagicMock()
15-
16-
17-
def test_describe_db_clusters_unit(mock_neptune_client, capsys):
18-
"""
19-
Unit test for describe_db_clusters with paginator.
20-
Mocks the Neptune client's paginator and verifies expected output is printed.
21-
"""
22-
23-
# Create a mock paginator
24-
mock_paginator = MagicMock()
25-
mock_neptune_client.get_paginator.return_value = mock_paginator
26-
27-
# Mock pages returned by paginate()
28-
mock_paginator.paginate.return_value = [
29-
{
30-
"DBClusters": [
31-
{
32-
"DBClusterIdentifier": "my-test-cluster",
33-
"Status": "available"
34-
}
35-
]
36-
},
37-
{
38-
"DBClusters": [
39-
{
40-
"DBClusterIdentifier": "my-second-cluster",
41-
"Status": "modifying"
42-
}
43-
]
44-
}
45-
]
46-
47-
try:
48-
describe_db_clusters(mock_neptune_client)
49-
captured = capsys.readouterr()
50-
51-
# Check that expected outputs from both pages were printed
52-
assert "my-test-cluster" in captured.out
53-
assert "available" in captured.out
54-
assert "my-second-cluster" in captured.out
55-
assert "modifying" in captured.out
56-
57-
mock_neptune_client.get_paginator.assert_called_once_with("describe_db_clusters")
58-
mock_paginator.paginate.assert_called_once()
59-
60-
except ClientError as e:
61-
pytest.fail(f"AWS ClientError occurred: {e.response['Error']['Message']}")
62-
except Exception as e:
63-
pytest.fail(f"Unexpected error: {str(e)}")
12+
def neptune_client_stub():
13+
client = boto3.client("neptune", region_name="us-east-1")
14+
stubber = Stubber(client)
15+
stubber.activate()
16+
yield client, stubber
17+
stubber.deactivate()
18+
19+
20+
def test_describe_db_clusters_with_stubber_single_page(neptune_client_stub, capsys):
21+
client, stubber = neptune_client_stub
22+
23+
# Simulate a single-page paginator result with both clusters in one call
24+
stubber.add_response("describe_db_clusters", {
25+
"DBClusters": [
26+
{"DBClusterIdentifier": "my-test-cluster", "Status": "available"},
27+
{"DBClusterIdentifier": "my-second-cluster", "Status": "modifying"}
28+
]
29+
})
30+
31+
describe_db_clusters(client)
32+
33+
captured = capsys.readouterr()
34+
35+
assert "my-test-cluster" in captured.out
36+
assert "available" in captured.out
37+
assert "my-second-cluster" in captured.out
38+
assert "modifying" in captured.out

scenarios/basics/neptune/SPECIFICATION.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ The Amazon Neptune Basics scenario executes the following operations.
3636

3737
5. **Show Neptune Cluster details**:
3838
- Description: Shows the details of the cluster by invoking `describeDBClusters`.
39-
- Exception Handling: Check to see if a `ResourceNotFoundException` is thrown. If so, display the message and end the program.
39+
- Exception Handling: Check to see if a `DBClusterNotFound` is thrown. If so, display the message and end the program.
4040

4141
6. **Stop the Cluster**:
4242
- Description: Stop the cluster by invoking `stopDBCluster`. Poll the cluster until it reaches a `stopped`state.
43-
- Exception Handling: Check to see if a `ResourceNotFoundException` is thrown. If so, display the message and end the program.
43+
- Exception Handling: Check to see if a `DBClusterNotFound` is thrown. If so, display the message and end the program.
4444

4545
7. **Start the cluster**:
4646
- Description: Start the cluster by invoking `startBCluster`. Poll the cluster until it reaches an `available`state.

0 commit comments

Comments
 (0)