Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EdgeIterator.total() should raise if _total_count is None #53

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions facebookads/exceptions.py
Expand Up @@ -126,3 +126,8 @@ def api_blame_field_specs(self):
class FacebookBadObjectError(FacebookError):
"""Raised when a guarantee about the object validity fails."""
pass


class FacebookUnavailablePropertyException(FacebookError):
"""Raised when an object's property or method is not available."""
pass
10 changes: 9 additions & 1 deletion facebookads/objects.py
Expand Up @@ -23,7 +23,10 @@
Ads API.
"""

from facebookads.exceptions import FacebookBadObjectError, FacebookError
from facebookads.exceptions import (
FacebookBadObjectError, FacebookError,
FacebookUnavailablePropertyException
)
from facebookads.api import FacebookAdsApi
from facebookads.session import FacebookSession
from facebookads.mixins import (
Expand Down Expand Up @@ -117,6 +120,11 @@ def __getitem__(self, index):
return self._queue[index]

def total(self):
if self._total_count is None:
raise FacebookUnavailablePropertyException(
"Couldn't retrieve theobject total "
"count for that type of request."
)
return self._total_count

def load_next_page(self):
Expand Down
18 changes: 18 additions & 0 deletions facebookads/test/unit.py
Expand Up @@ -112,6 +112,24 @@ def test_builds_from_object(self):
obj = ei.build_objects_from_response(response)
assert len(obj) == 1 and obj[0]['id'] == "601957/targetingsentencelines"

def test_total_is_none(self):
ei = objects.EdgeIterator(
objects.AdAccount(fbid='123'),
objects.AdGroup,
)
self.assertRaises(
exceptions.FacebookUnavailablePropertyException,
ei.total
)

def test_total_is_defined(self):
ei = objects.EdgeIterator(
objects.AdAccount(fbid='123'),
objects.AdGroup,
)
ei._total_count = 32
self.assertEqual(ei.total(), 32)


class AbstractCrudObjectTestCase(unittest.TestCase):
def test_all_aco_has_id_field(self):
Expand Down