Skip to content

Commit

Permalink
Merge pull request #44 from cloudblue/LITE-23925
Browse files Browse the repository at this point in the history
LITE-23925: Extended the action capability to the Collection object
  • Loading branch information
marcserrat authored Jun 7, 2022
2 parents 6fd0b44 + 9d85564 commit 1fc33b9
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
35 changes: 35 additions & 0 deletions connect/client/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ def __getitem__(self, resource_id):
"""
return self.resource(resource_id)

def __call__(self, name):
return self.action(name)

def all(self):
"""
Return a ResourceSet instance.
Expand Down Expand Up @@ -258,6 +261,29 @@ def resource(self, resource_id):
f'{self._path}/{resource_id}',
)

def action(self, name):
"""
Returns the action called ``name``.
:param name: The name of the action.
:type name: str
:raises TypeError: if the ``name`` is not a string.
:raises ValueError: if the ``name`` is blank.
:raises NotFoundError: if the ``name`` does not exist.
:return: The action called ``name``.
:rtype: Action
"""
if not isinstance(name, str):
raise TypeError('`name` must be a string.')

if not name:
raise ValueError('`name` must not be blank.')

return self._get_action_class()(
self._client,
f'{self._path}/{name}',
)

def help(self):
"""
Output the collection documentation to the console.
Expand All @@ -274,6 +300,9 @@ def _get_resource_class(self):
def _get_resourceset_class(self):
return NotImplementedError() # pragma: no cover

def _get_action_class(self):
raise NotImplementedError() # pragma: no cover


class Collection(_CollectionBase, CollectionMixin):
def _get_resource_class(self):
Expand All @@ -282,6 +311,9 @@ def _get_resource_class(self):
def _get_resourceset_class(self):
return ResourceSet

def _get_action_class(self):
return Action


class AsyncCollection(_CollectionBase, AsyncCollectionMixin):
def _get_resource_class(self):
Expand All @@ -290,6 +322,9 @@ def _get_resource_class(self):
def _get_resourceset_class(self):
return AsyncResourceSet

def _get_action_class(self):
return AsyncAction


class _ResourceBase:
"""Represent a generic resource."""
Expand Down
39 changes: 39 additions & 0 deletions tests/async_client/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,45 @@ def test_collection_help(async_col_factory):
assert col1 == col


def test_collection_action_invalid_type(async_col_factory):
collection = async_col_factory()
with pytest.raises(TypeError) as cv:
collection.action(None)

assert str(cv.value) == '`name` must be a string.'

with pytest.raises(TypeError) as cv:
collection.action(3)

assert str(cv.value) == '`name` must be a string.'


def test_collection_action_invalid_value(async_col_factory):
collection = async_col_factory()
with pytest.raises(ValueError) as cv:
collection.action('')

assert str(cv.value) == '`name` must not be blank.'


def test_collection_action(async_col_factory):
collection = async_col_factory()
action = collection.action('action')

assert isinstance(action, AsyncAction)
assert action._client == collection._client
assert action.path == f'{collection.path}/action'


def test_collection_action_call(async_col_factory):
collection = async_col_factory()
action = collection('action')

assert isinstance(action, AsyncAction)
assert action._client == collection._client
assert action.path == f'{collection.path}/action'


def test_resource_getattr(async_res_factory):
res = async_res_factory()
col = res.resources
Expand Down
39 changes: 39 additions & 0 deletions tests/client/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,45 @@ def test_collection_help(col_factory):
assert col1 == col


def test_collection_action_invalid_type(col_factory):
collection = col_factory()
with pytest.raises(TypeError) as cv:
collection.action(None)

assert str(cv.value) == '`name` must be a string.'

with pytest.raises(TypeError) as cv:
collection.action(3)

assert str(cv.value) == '`name` must be a string.'


def test_collection_action_invalid_value(col_factory):
collection = col_factory()
with pytest.raises(ValueError) as cv:
collection.action('')

assert str(cv.value) == '`name` must not be blank.'


def test_collection_action(col_factory):
collection = col_factory()
action = collection.action('action')

assert isinstance(action, Action)
assert action._client == collection._client
assert action.path == f'{collection.path}/action'


def test_collection_action_call(col_factory):
collection = col_factory()
action = collection('action')

assert isinstance(action, Action)
assert action._client == collection._client
assert action.path == f'{collection.path}/action'


def test_resource_getattr(res_factory):
res = res_factory()
col = res.resources
Expand Down

0 comments on commit 1fc33b9

Please sign in to comment.