Skip to content

Commit

Permalink
Merge pull request #79 from cloudblue/lite-29138-fix-resourceset-bool
Browse files Browse the repository at this point in the history
LITE-29138 Fix bool method for ResourceSe
  • Loading branch information
bdjilka authored Dec 4, 2023
2 parents e6ba1ce + d66eec2 commit f8423e3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 71 deletions.
7 changes: 5 additions & 2 deletions connect/client/models/resourceset.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,11 @@ def __iter__(self):
return iter(self._results)

def __bool__(self):
self._fetch_all()
return bool(self._results)
if self._results is not None:
return bool(self._results)
copy = self._copy()
copy._fetch_all()
return bool(copy._results)

def __getitem__(self, key): # noqa: CCR001
self._validate_key(key)
Expand Down
69 changes: 0 additions & 69 deletions tests/client/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,37 +917,6 @@ def test_rs_values_list(mocker, rs_factory):
assert list(rs) == expected


def test_rs_values_list_evaluated(mocker, rs_factory):
mocker.patch(
'connect.client.models.resourceset.parse_content_range',
return_value=ContentRange(0, 9, 10),
)
return_value = [
{
'id': i,
'name': f'name {i}',
'inner': {
'title': f'title {i}',
},
}
for i in range(10)
]
expected = [
{
'id': i,
'inner.title': f'title {i}',
}
for i in range(10)
]
rs = rs_factory()
rs._client.get = mocker.MagicMock(return_value=return_value)

bool(rs)
values = rs.values_list('id', 'inner.title')

assert values == expected


def test_rs_pagination(mocker, rs_factory):
mocker.patch(
'connect.client.models.iterators.parse_content_range',
Expand Down Expand Up @@ -1127,20 +1096,6 @@ def test_rs_help(rs_factory):
assert rs2 == rs


def test_rs_bool_truthy_already_evaluated(mocker, rs_factory):
mocker.patch(
'connect.client.models.resourceset.parse_content_range',
return_value=ContentRange(0, 9, 10),
)
expected = [{'id': i} for i in range(10)]
rs = rs_factory()
mocked = mocker.patch.object(ResourceSet, '_execute_request', wraps=rs._execute_request)
rs._client.get = mocker.MagicMock(return_value=expected)
assert bool(rs) is True
assert bool(rs) is True
mocked.assert_called_once()


def test_rs_count_already_evaluated(mocker, rs_factory):
mocker.patch(
'connect.client.models.resourceset.parse_content_range',
Expand All @@ -1162,27 +1117,3 @@ def test_rs_slice_single_bound(mocker, rs_factory):
with pytest.raises(ValueError) as cv:
rs[:1]
assert str(cv.value) == 'Both start and stop indexes must be specified.'


def test_rs_slice_already_evaluated(mocker, rs_factory):
mocker.patch(
'connect.client.models.resourceset.parse_content_range',
return_value=ContentRange(0, 9, 10),
)
expected = [{'id': i} for i in range(10)]
rs = rs_factory()
rs._client.get = mocker.MagicMock(return_value=expected)
assert bool(rs) is True
assert rs[0:2] == expected[0:2]


def test_rs_iterate_already_evaluated(mocker, rs_factory):
mocker.patch(
'connect.client.models.resourceset.parse_content_range',
return_value=ContentRange(0, 9, 10),
)
expected = [{'id': i} for i in range(10)]
rs = rs_factory()
rs._client.get = mocker.MagicMock(return_value=expected)
assert bool(rs) is True
assert [item for item in rs] == expected

0 comments on commit f8423e3

Please sign in to comment.