11# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22# SPDX-License-Identifier: Apache-2.0
33
4+ import pytest
45from unittest .mock import MagicMock , patch
5- from NeptuneScenario import create_subnet_group # Replace with your actual module path
6+ from botocore .exceptions import ClientError
7+ from NeptuneScenario import create_subnet_group # Adjust the import path as necessary
68
9+ # Mocking external functions to isolate the unit test
710@patch ("NeptuneScenario.get_subnet_ids" )
811@patch ("NeptuneScenario.get_default_vpc_id" )
9- def test_create_subnet_group_success (mock_get_vpc , mock_get_subnets ):
12+ def test_create_subnet_group (mock_get_vpc , mock_get_subnets ):
1013 """
1114 Unit test for create_subnet_group().
1215 Verifies successful creation and correct parsing of name and ARN.
1316 """
17+ # --- Setup Mocks ---
1418 mock_get_vpc .return_value = "vpc-1234"
1519 mock_get_subnets .return_value = ["subnet-1" , "subnet-2" ]
1620
@@ -22,8 +26,31 @@ def test_create_subnet_group_success(mock_get_vpc, mock_get_subnets):
2226 }
2327 }
2428
29+ # --- Success Case ---
2530 name , arn = create_subnet_group (mock_neptune , "test-group" )
26-
2731 assert name == "test-group"
2832 assert arn == "arn:aws:neptune:us-east-1:123456789012:subnet-group:test-group"
29- mock_neptune .create_db_subnet_group .assert_called_once ()
33+ mock_neptune .create_db_subnet_group .assert_called_once_with (
34+ DBSubnetGroupName = "test-group" ,
35+ DBSubnetGroupDescription = "My Neptune subnet group" ,
36+ SubnetIds = ["subnet-1" , "subnet-2" ],
37+ Tags = [{"Key" : "Environment" , "Value" : "Dev" }]
38+ )
39+
40+ # --- Missing Name or ARN ---
41+ mock_neptune .create_db_subnet_group .return_value = {"DBSubnetGroup" : {}}
42+ with pytest .raises (RuntimeError , match = "Response missing subnet group name or ARN" ):
43+ create_subnet_group (mock_neptune , "missing-id-group" )
44+
45+ # --- ClientError Handling ---
46+ mock_neptune .create_db_subnet_group .side_effect = ClientError (
47+ {"Error" : {"Code" : "AccessDenied" , "Message" : "Permission denied" }},
48+ operation_name = "CreateDBSubnetGroup"
49+ )
50+ with pytest .raises (ClientError , match = "Failed to create subnet group 'denied-group'" ):
51+ create_subnet_group (mock_neptune , "denied-group" )
52+
53+ # --- Unexpected Exception ---
54+ mock_neptune .create_db_subnet_group .side_effect = Exception ("Unexpected failure" )
55+ with pytest .raises (RuntimeError , match = "Unexpected error creating subnet group 'fail-group'" ):
56+ create_subnet_group (mock_neptune , "fail-group" )
0 commit comments