11import pytest
2- from unittest .mock import Mock
32from openstack import exceptions
43from openstack_mcp_server .tools .keystone_tools import KeystoneTools , Region
4+ from unittest .mock import Mock
5+
56
67class TestKeystoneTools :
78 """Test cases for KeystoneTools class."""
89
910 def get_keystone_tools (self ) -> KeystoneTools :
1011 """Get an instance of KeystoneTools."""
1112 return KeystoneTools ()
12-
13+
1314 def test_get_regions_success (self , mock_get_openstack_conn_keystone ):
1415 """Test getting keystone regions successfully."""
1516 mock_conn = mock_get_openstack_conn_keystone
16-
17+
1718 # Create mock region objects
1819 mock_region1 = Mock ()
1920 mock_region1 .id = "RegionOne"
@@ -22,32 +23,34 @@ def test_get_regions_success(self, mock_get_openstack_conn_keystone):
2223 mock_region2 = Mock ()
2324 mock_region2 .id = "RegionTwo"
2425 mock_region2 .description = "Region Two description"
25-
26+
2627 # Configure mock region.regions()
2728 mock_conn .identity .regions .return_value = [mock_region1 , mock_region2 ]
2829
2930 # Test get_regions()
3031 keystone_tools = self .get_keystone_tools ()
3132 result = keystone_tools .get_regions ()
32-
33+
3334 # Verify results
34- assert result == [Region (id = "RegionOne" , description = "Region One description" ),
35- Region (id = "RegionTwo" , description = "Region Two description" )]
35+ assert result == [
36+ Region (id = "RegionOne" , description = "Region One description" ),
37+ Region (id = "RegionTwo" , description = "Region Two description" ),
38+ ]
3639
3740 # Verify mock calls
3841 mock_conn .identity .regions .assert_called_once ()
3942
4043 def test_get_regions_empty_list (self , mock_get_openstack_conn_keystone ):
4144 """Test getting keystone regions when there are no regions."""
4245 mock_conn = mock_get_openstack_conn_keystone
43-
46+
4447 # Empty region list
4548 mock_conn .identity .regions .return_value = []
46-
49+
4750 # Test get_regions()
4851 keystone_tools = self .get_keystone_tools ()
4952 result = keystone_tools .get_regions ()
50-
53+
5154 # Verify results
5255 assert result == []
5356
@@ -57,7 +60,7 @@ def test_get_regions_empty_list(self, mock_get_openstack_conn_keystone):
5760 def test_create_region_success (self , mock_get_openstack_conn_keystone ):
5861 """Test creating a keystone region successfully."""
5962 mock_conn = mock_get_openstack_conn_keystone
60-
63+
6164 # Create mock region object
6265 mock_region = Mock ()
6366 mock_region .id = "RegionOne"
@@ -68,15 +71,23 @@ def test_create_region_success(self, mock_get_openstack_conn_keystone):
6871
6972 # Test create_region()
7073 keystone_tools = self .get_keystone_tools ()
71- result = keystone_tools .create_region (id = "RegionOne" , description = "Region One description" )
72-
74+ result = keystone_tools .create_region (
75+ id = "RegionOne" , description = "Region One description"
76+ )
77+
7378 # Verify results
74- assert result == Region (id = "RegionOne" , description = "Region One description" )
79+ assert result == Region (
80+ id = "RegionOne" , description = "Region One description"
81+ )
7582
7683 # Verify mock calls
77- mock_conn .identity .create_region .assert_called_once_with (id = "RegionOne" , description = "Region One description" )
78-
79- def test_create_region_without_description (self , mock_get_openstack_conn_keystone ):
84+ mock_conn .identity .create_region .assert_called_once_with (
85+ id = "RegionOne" , description = "Region One description"
86+ )
87+
88+ def test_create_region_without_description (
89+ self , mock_get_openstack_conn_keystone
90+ ):
8091 """Test creating a keystone region without a description."""
8192 mock_conn = mock_get_openstack_conn_keystone
8293
@@ -95,22 +106,32 @@ def test_create_region_without_description(self, mock_get_openstack_conn_keyston
95106 # Verify results
96107 assert result == Region (id = "RegionOne" )
97108
98- def test_create_region_invalid_id_format (self , mock_get_openstack_conn_keystone ):
109+ def test_create_region_invalid_id_format (
110+ self , mock_get_openstack_conn_keystone
111+ ):
99112 """Test creating a keystone region with an invalid ID format."""
100113 mock_conn = mock_get_openstack_conn_keystone
101114
102115 # Configure mock region.create_region() to raise an exception
103- mock_conn .identity .create_region .side_effect = exceptions .BadRequestException (
104- "Invalid input for field 'id': Expected string, got integer"
116+ mock_conn .identity .create_region .side_effect = (
117+ exceptions .BadRequestException (
118+ "Invalid input for field 'id': Expected string, got integer"
119+ )
105120 )
106121
107122 # Test create_region()
108123 keystone_tools = self .get_keystone_tools ()
109124
110125 # Verify results
111- with pytest .raises (exceptions .BadRequestException , match = "Invalid input for field 'id': Expected string, got integer" ):
112- keystone_tools .create_region (id = 1 , description = "Region One description" )
126+ with pytest .raises (
127+ exceptions .BadRequestException ,
128+ match = "Invalid input for field 'id': Expected string, got integer" ,
129+ ):
130+ keystone_tools .create_region (
131+ id = 1 , description = "Region One description"
132+ )
113133
114134 # Verify mock calls
115- mock_conn .identity .create_region .assert_called_once_with (id = 1 , description = "Region One description" )
116-
135+ mock_conn .identity .create_region .assert_called_once_with (
136+ id = 1 , description = "Region One description"
137+ )
0 commit comments