Skip to content

Commit

Permalink
Unit test fixes
Browse files Browse the repository at this point in the history
Correct unit test for query manager
Add method of parse subset and unit test for user_runner
Fixes done based on errors raised by unit tests for user query
  • Loading branch information
meoflynn committed Aug 1, 2023
1 parent 009997a commit cb7d614
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/openstack_query/queries/user_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ def _get_server_side_handler(self) -> ServerSideHandler:
return ServerSideHandler(
{
QueryPresetsGeneric.EQUAL_TO: {
UserProperties.DOMAIN_ID: lambda value: {"domain_id": value},
UserProperties.NAME: lambda value: {"name": value},
UserProperties.USER_DOMAIN_ID: lambda value: {"domain_id": value},
UserProperties.USER_NAME: lambda value: {"name": value},
}
}
)
Expand Down
10 changes: 10 additions & 0 deletions lib/openstack_query/runners/user_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,13 @@ def _run_query(
if "domain_id" not in filter_kwargs.keys():
filter_kwargs.update({"domain_id": self.STFC_DOMAIN_ID})
return list(conn.identity.v3.users(**filter_kwargs))

def _parse_subset(self, _: OpenstackConnection, subset: List[User]) -> List[User]:
"""
This method is a helper function that will check a list of users to ensure that they are valid Server
objects
:param subset: A list of openstack Server objects
"""
if any(not isinstance(i, User) for i in subset):
raise ParseQueryError("'from_subset' only accepts User openstack objects")
return subset
8 changes: 4 additions & 4 deletions tests/lib/openstack_query/managers/test_query_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_search_by_datetime(
mock_build_and_run_query.assert_called_once_with(
preset_details=QueryPresetDetails(
preset=QueryPresetsDateTime.OLDER_THAN,
prop=self.prop_cls.SERVER_CREATION_DATE,
prop=self.prop_cls.from_string.return_value,
args={
"days": 10,
"hours": 10,
Expand Down Expand Up @@ -199,7 +199,7 @@ def test_search_by_property_with_single_value(
mock_build_and_run_query.assert_called_once_with(
preset_details=QueryPresetDetails(
preset=QueryPresetsGeneric.EQUAL_TO,
prop=self.prop_cls.RESOURCE_ID,
prop=self.prop_cls.from_string.return_value,
args={"value": "image-id1"},
),
output_details=mock_output_details,
Expand Down Expand Up @@ -236,7 +236,7 @@ def test_search_by_property_with_multiple_values(
mock_build_and_run_query.assert_called_once_with(
preset_details=QueryPresetDetails(
preset=QueryPresetsString.ANY_IN,
prop=self.prop_cls.RESOURCE_ID,
prop=self.prop_cls.from_string.return_value,
args={"values": ["image-id1", "image-id2"]},
),
output_details=mock_output_details,
Expand Down Expand Up @@ -276,7 +276,7 @@ def test_search_by_regex_valid(
mock_build_and_run_query.assert_called_once_with(
preset_details=QueryPresetDetails(
preset=QueryPresetsString.MATCHES_REGEX,
prop=self.prop_cls.RESOURCE_NAME,
prop=self.prop_cls.from_string.return_value,
args={"regex_string": "some-regex-pattern"},
),
output_details=mock_output_details,
Expand Down
30 changes: 29 additions & 1 deletion tests/lib/openstack_query/runners/test_user_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ def test_run_query(self, _, mock_filter_kwargs):
"user2",
"user3",
]
mock_user = self.conn.identity.v3.users
res = self.instance._run_query(self.conn, filter_kwargs=mock_filter_kwargs)
self.assertEqual(res, mock_user_list)
mock_user_list.assert_called_once_with(**mock_filter_kwargs)
mock_user.assert_called_once_with(**mock_filter_kwargs)

def test_parse_subset(self):
"""
Tests _parse_subset works expectedly
method simply checks each value in 'subset' param is of the User type and returns it
"""

# with one item
mock_user_1 = MagicMock()
mock_user_1.__class__ = User
res = self.instance._parse_subset(self.conn, [mock_user_1])
self.assertEqual(res, [mock_user_1])

# with two items
mock_user_2 = MagicMock()
mock_user_2.__class__ = User
res = self.instance._parse_subset(self.conn, [mock_user_1, mock_user_2])
self.assertEqual(res, [mock_user_1, mock_user_2])

@raises(ParseQueryError)
def test_parse_subset_invalid(self):
"""
Tests _parse_subset works expectedly
method raises error when provided value which is not of User type
"""
invalid_user = "invalid-user-obj"
self.instance._parse_subset(self.conn, [invalid_user])

0 comments on commit cb7d614

Please sign in to comment.