Skip to content

Commit

Permalink
MAINT: change select function signature
Browse files Browse the repository at this point in the history
select now takes a list of props instead of *args
  • Loading branch information
anish-mudaraddi committed Jun 27, 2024
1 parent a988bc7 commit 09ff5e9
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 13 deletions.
6 changes: 3 additions & 3 deletions lib/openstack_query/api/query_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@ def __init__(self, query_components: QueryComponents):
self.chainer = query_components.chainer
self.results_container = None

def select(self, *props: Union[str, PropEnum]):
def select(self, props: Optional[List[Union[str, PropEnum]]] = None):
"""
Public method used to 'select' properties that the query will return the value of.
Mutually exclusive to returning objects using select_all()
:param props: one or more properties to collect described as enum or a string that can convert to enum
:param props: a list of one or more properties to collect described as enum or a string that can convert to enum
"""

# is an idempotent function
# can be called multiple times with should aggregate properties to select
if not props:
raise ParseQueryError("provide at least one property to select")

self.output.parse_select(*props, select_all=False)
self.output.parse_select(props, select_all=False)
logger.debug(
"selected props are now: %s",
[prop.name for prop in self.output.selected_props],
Expand Down
4 changes: 2 additions & 2 deletions lib/openstack_query/query_blocks/query_chainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def parse_then(
current_query.parser.group_by = None

# grab all link prop values - including duplicates
search_values = current_query.select(link_props[0]).to_props(flatten=True)[
search_values = current_query.select([link_props[0]]).to_props(flatten=True)[
link_props[0].name.lower()
]

Expand Down Expand Up @@ -149,7 +149,7 @@ def run_append_from_query(
query_type = QueryTypes.from_string(query_type)

new_query = current_query.then(query_type, keep_previous_results=False)
new_query.select(*props)
new_query.select(props)
new_query.run(cloud_account)

link_props = current_query.chainer.get_link_props(query_type)
Expand Down
7 changes: 5 additions & 2 deletions lib/openstack_query/query_blocks/query_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,16 @@ def _parse_select_inputs(self, props):
parsed_props.append(prop)
return parsed_props

def parse_select(self, *props: Union[str, PropEnum], select_all=False) -> None:
def parse_select(
self, props: Optional[List[Union[str, PropEnum]]] = None, select_all=False
) -> None:
"""
Method which is used to set which properties to output once results are gathered
This method checks that each Enum provided is valid and populates internal attribute which holds selected props
:param select_all: boolean flag to select all valid properties
:param props: one or more Enums/string aliases representing properties to show
:param select_all: boolean flag to select all valid properties
"""

if select_all:
self.selected_props = set(self._prop_enum_cls)
return
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/openstack_query/api/test_query_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def test_select_with_many_props(instance):
mock_query_output = MagicMock()
instance.output = mock_query_output

res = instance.select(MockProperties.PROP_1, MockProperties.PROP_2)
res = instance.select([MockProperties.PROP_1, MockProperties.PROP_2])
mock_query_output.parse_select.assert_called_once_with(
MockProperties.PROP_1, MockProperties.PROP_2, select_all=False
[MockProperties.PROP_1, MockProperties.PROP_2], select_all=False
)
assert res == instance

Expand Down
4 changes: 2 additions & 2 deletions tests/lib/openstack_query/query_blocks/test_query_chainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def _run_parse_then_query_valid(
mock_current_query.to_props.assert_called_once()

# test getting link prop values works
mock_current_query.select.assert_any_call(MockProperties.PROP_1)
mock_current_query.select.assert_any_call([MockProperties.PROP_1])
mock_current_query.select.return_value.to_props.assert_any_call(flatten=True)

if mock_keep_previous_results:
Expand Down Expand Up @@ -245,7 +245,7 @@ def test_run_append_from_query(mock_query_types, instance):
mock_current_query.then.assert_called_once_with(
mock_query_types.from_string.return_value, keep_previous_results=False
)
mock_new_query.select.assert_called_once_with(*mock_props)
mock_new_query.select.assert_called_once_with(mock_props)
mock_new_query.run.assert_called_once_with(mock_cloud_account)
mock_current_query.chainer.get_link_props.assert_called_once_with(
mock_query_types.from_string.return_value
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/openstack_query/query_blocks/test_query_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def test_parse_select_given_invalid(instance):

# server prop enums are invalid here and should be picked up
with pytest.raises(ParseQueryError):
instance.parse_select(MockProperties.PROP_1, ServerProperties.SERVER_ID)
instance.parse_select([MockProperties.PROP_1, ServerProperties.SERVER_ID])


def test_parse_select_overwrites_old(instance):
Expand All @@ -529,7 +529,7 @@ def test_parse_select_overwrites_old(instance):
method should overwrite internal attribute selected_props if already set
"""
instance.selected_props = [MockProperties.PROP_1]
instance.parse_select(MockProperties.PROP_2)
instance.parse_select([MockProperties.PROP_2])
assert instance.selected_props == [MockProperties.PROP_2]


Expand Down

0 comments on commit 09ff5e9

Please sign in to comment.