Add support for activity list filters
This commit is contained in:
parent
0f6cbd58e0
commit
c39c4f3d8c
4 changed files with 46 additions and 11 deletions
|
@ -21,10 +21,27 @@ class TestActivities(test_base.TestBase):
|
|||
|
||||
# Check that each activity is for a valid test photo
|
||||
activities = self.client.activities.list()
|
||||
self.assertEqual(len(activities), len(self.photos))
|
||||
self.assertEqual(len(activities), len(photos))
|
||||
for activity in activities:
|
||||
self.assertIn(activity.data.id, [photo.id for photo in photos])
|
||||
|
||||
def test_list_filter(self):
|
||||
"""
|
||||
Check that the activity list filter parameter works correctly
|
||||
"""
|
||||
self._delete_all()
|
||||
self._create_test_photos(tag=False)
|
||||
photos = self.client.photos.list()
|
||||
|
||||
# Dummy photo update activity
|
||||
photos[0].update(tags=photos[0].tags)
|
||||
|
||||
# Check that the activities can be filtered
|
||||
upload_activities = self.client.activities.list(filters={"type": "photo-upload"})
|
||||
update_activities = self.client.activities.list(filters={"type": "photo-update"})
|
||||
self.assertEqual(len(upload_activities), len(photos))
|
||||
self.assertEqual(len(update_activities), 1)
|
||||
|
||||
# The purge endpoint currently reports a 500: Internal Server Error
|
||||
@unittest.expectedFailure
|
||||
def test_purge(self):
|
||||
|
|
|
@ -68,6 +68,17 @@ class TestActivitiesList(TestActivities):
|
|||
mock_get.assert_called_with("/activities/list.json")
|
||||
self.assertEqual(result, [])
|
||||
|
||||
@mock.patch.object(trovebox.Trovebox, 'get')
|
||||
def test_filters(self, mock_get):
|
||||
"""Check that the activity list filters are applied properly"""
|
||||
mock_get.return_value = self._return_value(self.test_activities_dict)
|
||||
self.client.activities.list(filters={"foo": "bar",
|
||||
"test1": "test2"})
|
||||
# Dict element can be any order
|
||||
self.assertIn(mock_get.call_args[0],
|
||||
[("/activities/foo-bar/test1-test2/list.json",),
|
||||
("/activities/test1-test2/foo-bar/list.json",)])
|
||||
|
||||
class TestActivitiesPurge(TestActivities):
|
||||
@mock.patch.object(trovebox.Trovebox, 'post')
|
||||
def test_activity_purge(self, mock_get):
|
||||
|
|
|
@ -4,15 +4,15 @@ api_activity.py : Trovebox Activity API Classes
|
|||
from trovebox import http
|
||||
from trovebox.errors import TroveboxError
|
||||
from trovebox.objects.activity import Activity
|
||||
from .api_base import ApiBase
|
||||
|
||||
class ApiActivities(object):
|
||||
class ApiActivities(ApiBase):
|
||||
""" Definitions of /activities/ API endpoints """
|
||||
def __init__(self, client):
|
||||
self._client = client
|
||||
|
||||
def list(self, **kwds):
|
||||
def list(self, filters={}, **kwds):
|
||||
""" Returns a list of Activity objects """
|
||||
activities = self._client.get("/activities/list.json", **kwds)["result"]
|
||||
filter_string = self._build_filter_string(filters)
|
||||
activities = self._client.get("/activities/%slist.json" % filter_string,
|
||||
**kwds)["result"]
|
||||
activities = http.result_to_list(activities)
|
||||
return [Activity(self._client, activity) for activity in activities]
|
||||
|
||||
|
@ -22,11 +22,8 @@ class ApiActivities(object):
|
|||
raise TroveboxError("Purge response returned False")
|
||||
return True
|
||||
|
||||
class ApiActivity(object):
|
||||
class ApiActivity(ApiBase):
|
||||
""" Definitions of /activity/ API endpoints """
|
||||
def __init__(self, client):
|
||||
self._client = client
|
||||
|
||||
def view(self, activity, **kwds):
|
||||
"""
|
||||
View an activity's contents.
|
||||
|
|
|
@ -6,3 +6,13 @@ class ApiBase(object):
|
|||
def __init__(self, client):
|
||||
self._client = client
|
||||
|
||||
@staticmethod
|
||||
def _build_filter_string(filters):
|
||||
"""
|
||||
:param filters: dictionary containing the filters
|
||||
:returns: filter_string formatted for an API endpoint
|
||||
"""
|
||||
filter_string = ""
|
||||
for filter in filters:
|
||||
filter_string += "%s-%s/" % (filter, filters[filter])
|
||||
return filter_string
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue