def set_feed(self, obj, key, data, from_json): from pypump.models.feed import Feed if from_json: if data is not None: try: setattr(obj, key, Feed(pypump=self._pump).unserialize(data)) except Exception as e: _log.debug("Exception %s" % e) else: setattr(obj, key, [])
def setUp(self): super(FeedTest, self).setUp() self.response.data = { "displayName": "Followers for Testuser", "url": "https://example.com/api/user/testuser/followers", "objectTypes": [ "person" ], "items": [{"objectType": "person", "id": "acct:testuser%[email protected]" % i} for i in range(20)], "totalItems": 20, "author": {"objectType": "person", "id": "acct:[email protected]"}, "links": { "self": {"href": "https://example.com/api/user/testuser/followers?offset=0&count=20"}, "current": {"href": "https://example.com/api/user/testuser/followers"}, "prev": {"href": "https://example.com/api/user/testuser/followers?since=acct%3Atestuser0%40example.com"}, "next": {"href": "https://example.com/api/user/testuser/followers?before=acct%3Atestuser19%40example.com"}, } } self.feed = Feed(pypump=self.pump).unserialize(self.response.data)
def comments(self): """ A :class:`Feed <pypump.models.feed.Feed>` of the comments for the object. Example: >>> for comment in mynote.comments: ... print(comment) ... comment by [email protected] """ endpoint = self.links["replies"] self._comments = self._comments or Feed(endpoint, pypump=self._pump) return self._comments
def shares(self): """ A :class:`Feed <pypump.models.feed.Feed>` of the people who've shared the object. Example: >>> for person in mynote.shares: ... print(person.webfinger) ... [email protected] [email protected] """ endpoint = self.links["shares"] self._shares = self._shares or Feed(endpoint, pypump=self._pump) return self._shares
def likes(self): """ A :class:`Feed <pypump.models.feed.Feed>` of the people who've liked the object. Example: >>> for person in mynote.likes: ... print(person.webfinger) ... [email protected] [email protected] """ endpoint = self.links["likes"] if self._likes is None: self._likes = Feed(endpoint, pypump=self._pump) return self._likes
class FeedTest(PyPumpTest): def setUp(self): super(FeedTest, self).setUp() self.response.data = { "displayName": "Followers for Testuser", "url": "https://example.com/api/user/testuser/followers", "objectTypes": ["person"], "items": [{ "objectType": "person", "id": "acct:testuser%[email protected]" % i } for i in range(20)], "totalItems": 20, "author": { "objectType": "person", "id": "acct:[email protected]" }, "links": { "self": { "href": "https://example.com/api/user/testuser/followers?offset=0&count=20" }, "current": { "href": "https://example.com/api/user/testuser/followers" }, "prev": { "href": "https://example.com/api/user/testuser/followers?since=acct%3Atestuser0%40example.com" }, "next": { "href": "https://example.com/api/user/testuser/followers?before=acct%3Atestuser19%40example.com" }, } } self.feed = Feed(pypump=self.pump).unserialize(self.response.data) def test_before_since_offset_exceptions(self): with self.assertRaises(PyPumpException): self.feed.items(before="something", offset=12) with self.assertRaises(PyPumpException): self.feed.items(since="something", offset=12) with self.assertRaises(PyPumpException): self.feed.items(since="something", before="something") def test_feed(self): # is a Feed object self.assertTrue(isinstance(self.feed, Feed)) # object to string self.assertEqual(self.feed.__str__(), self.feed.display_name) self.feed.display_name = u'Followers for Testanvändare' if six.PY3: self.assertEqual(self.feed.__str__(), self.feed.display_name) else: self.assertEqual(self.feed.__str__(), self.feed.display_name.encode('utf-8')) def test_feed_attr_display_name(self): self.assertTrue(hasattr(self.feed, 'display_name')) self.assertEqual(self.feed.display_name, self.response["displayName"]) def test_feed_attr_url(self): self.assertTrue(hasattr(self.feed, 'url')) self.assertEqual(self.feed.url, self.response["url"]) def test_feed_attr_object_types(self): self.assertTrue(hasattr(self.feed, 'object_types')) self.assertEqual(self.feed.object_types, self.response["objectTypes"]) def test_feed_attr_items(self): self.assertTrue(hasattr(self.feed, '_items')) self.assertTrue(isinstance(self.feed._items, list)) def test_feed_attr_total_items(self): self.assertTrue(hasattr(self.feed, 'total_items')) self.assertEqual(self.feed.total_items, self.response["totalItems"]) def test_feed_attr_author(self): self.assertTrue(hasattr(self.feed, 'author')) self.assertTrue(isinstance(self.feed.author, type(self.pump.Person()))) def test_feed_attr_links(self): self.assertTrue(hasattr(self.feed, 'links')) # we should expand this test when we have settled on way to show links self.assertTrue(self.feed.links is not None) def test_feed_slice_0_to_5(self): sliceditems = self.feed[:5] self.assertEqual(len(sliceditems), 5) self.assertEqual(sliceditems[0].id, self.response['items'][0]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][4]['id']) def test_feed_slice_5_to_inf(self): sliceditems = self.feed[5:] self.assertEqual(len(sliceditems), 15) self.assertEqual(sliceditems[0].id, self.response['items'][5]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][19]['id']) def test_feed_slice_3_to_6(self): sliceditems = self.feed[3:6] self.assertEqual(len(sliceditems), 3) self.assertEqual(sliceditems[0].id, self.response['items'][3]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][5]['id']) def test_feed_slice_id_to_inf(self): sliceditems = self.feed[slice('acct:[email protected]', None)] self.assertEqual(len(sliceditems), 9) self.assertEqual(sliceditems[1].id, self.response['items'][12]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][19]['id']) def test_feed_slice_zero_to_id(self): sliceditems = self.feed[slice('acct:[email protected]')] self.assertEqual(len(sliceditems), 5) self.assertEqual(sliceditems[-1].id, self.response['items'][0]['id']) self.assertEqual(sliceditems[0].id, self.response['items'][4]['id']) def test_feed_items_before(self): # before and limit 3 items = self.feed.items(before='acct:[email protected]', limit=3) self.assertEqual(len(items), 3) self.assertEqual(items[0].id, self.response['items'][11]['id']) self.assertEqual(items[-1].id, self.response['items'][13]['id']) def test_feed_items_since(self): # since and limit 12 (limit more than items returned) items = self.feed.items(since='acct:[email protected]', limit=12) self.assertEqual(len(items), 10) self.assertEqual(items[0].id, self.response['items'][9]['id']) self.assertEqual(items[-1].id, self.response['items'][0]['id']) def test_feed_items_no_limit(self): # no limit items = self.feed.items(limit=None) self.assertEqual(len(items), 20) self.assertEqual(items[0].id, self.response['items'][0]['id']) self.assertEqual(items[-1].id, self.response['items'][19]['id']) def test_feed_items_offset(self): # offset items = self.feed.items(offset=18) self.assertEqual(len(items), 2) # offset and limit items = self.feed.items(offset=10, limit=5) self.assertEqual(len(items), 5) def test_slice_slice_zero_to_3(self): sliceditems = self.feed[3:10][:3] self.assertEqual(len(sliceditems), 3) self.assertEqual(sliceditems[0].id, self.response['items'][3]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][5]['id']) def test_slice_slice_zero_to_negative(self): sliceditems = self.feed[3:10][:-3] self.assertEqual(len(sliceditems), 4) self.assertEqual(sliceditems[0].id, self.response['items'][3]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][6]['id']) def test_slice_slice_3_to_end(self): sliceditems = self.feed[3:10][3:] self.assertEqual(len(sliceditems), 4) self.assertEqual(sliceditems[0].id, self.response['items'][6]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][9]['id']) def test_slice_slice_negative_to_end(self): sliceditems = self.feed[3:10][-3:] self.assertEqual(len(sliceditems), 3) self.assertEqual(sliceditems[0].id, self.response['items'][7]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][9]['id']) def test_slice_slice_with_id(self): sliceditems = self.feed[3:10] with self.assertRaises(TypeError): sliceditems[slice(self.response['items'][4]['id'])] def test_feed_sliced_by_id(self): sliceditems = self.feed[slice('acct:[email protected]', None)][:3] self.assertEqual(len(sliceditems), 3) self.assertEqual(sliceditems[0].id, self.response['items'][11]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][13]['id']) with self.assertRaises(PyPumpException): self.feed[slice('acct:[email protected]', None)][3:] with self.assertRaises(PyPumpException): self.feed[slice('acct:[email protected]', None)][:-3] def test_zero_to_negative_slicing(self): sliceditems = self.feed[:-18] self.assertEqual(len(sliceditems), 2) self.assertEqual(sliceditems[0].id, self.response['items'][0]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][1]['id']) def test_negative_to_inf_slicing(self): sliceditems = self.feed[-18:] self.assertEqual(len(sliceditems), 18) self.assertEqual(sliceditems[0].id, self.response['items'][2]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][19]['id'])
def shares(self): """ Fetches the people who've shared the model """ endpoint = self.links["shares"] self._shares = self._shares or Feed(endpoint, pypump=self._pump) return self._shares
def comments(self): """ Fetches the comment objects for the models """ endpoint = self.links["replies"] self._comments = self._comments or Feed(endpoint, pypump=self._pump) return self._comments
def likes(self): """ Gets who's liked this object """ endpoint = self.links["likes"] self._likes = self._likes or Feed(endpoint, pypump=self._pump) return self._likes
def members(self): self._members = self._members or Feed(self.links["members"], pypump=self._pump) return self._members
class FeedTest(PyPumpTest): def setUp(self): super(FeedTest, self).setUp() self.response.data = { "displayName": "Followers for Testuser", "url": "https://example.com/api/user/testuser/followers", "objectTypes": ["person"], "items": [{"objectType": "person", "id": "acct:testuser%[email protected]" % i} for i in range(20)], "totalItems": 20, "author": {"objectType": "person", "id": "acct:[email protected]"}, "links": { "self": {"href": "https://example.com/api/user/testuser/followers?offset=0&count=20"}, "current": {"href": "https://example.com/api/user/testuser/followers"}, "prev": { "href": "https://example.com/api/user/testuser/followers?since=acct%3Atestuser0%40example.com" }, "next": { "href": "https://example.com/api/user/testuser/followers?before=acct%3Atestuser19%40example.com" }, }, } self.feed = Feed(pypump=self.pump).unserialize(self.response.data) def test_feed(self): # is a Feed object self.assertTrue(isinstance(self.feed, Feed)) # object to string self.assertEqual(self.feed.__str__(), self.feed.display_name) self.feed.display_name = u"Followers for Testanvändare" if six.PY3: self.assertEqual(self.feed.__str__(), self.feed.display_name) else: self.assertEqual(self.feed.__str__(), self.feed.display_name.encode("utf-8")) def test_feed_attr_display_name(self): self.assertTrue(hasattr(self.feed, "display_name")) self.assertEqual(self.feed.display_name, self.response["displayName"]) def test_feed_attr_url(self): self.assertTrue(hasattr(self.feed, "url")) self.assertEqual(self.feed.url, self.response["url"]) def test_feed_attr_object_types(self): self.assertTrue(hasattr(self.feed, "object_types")) self.assertEqual(self.feed.object_types, self.response["objectTypes"]) def test_feed_attr_items(self): self.assertTrue(hasattr(self.feed, "_items")) self.assertTrue(isinstance(self.feed._items, list)) def test_feed_attr_total_items(self): self.assertTrue(hasattr(self.feed, "total_items")) self.assertEqual(self.feed.total_items, self.response["totalItems"]) def test_feed_attr_author(self): self.assertTrue(hasattr(self.feed, "author")) self.assertTrue(isinstance(self.feed.author, type(self.pump.Person()))) def test_feed_attr_links(self): self.assertTrue(hasattr(self.feed, "links")) # we should expand this test when we have settled on way to show links self.assertTrue(self.feed.links is not None) def test_feed_slice_0_to_5(self): sliceditems = list(self.feed[:5]) self.assertEqual(len(sliceditems), 5) self.assertEqual(sliceditems[0].id, self.response["items"][0]["id"]) self.assertEqual(sliceditems[-1].id, self.response["items"][4]["id"]) def test_feed_slice_3_to_6(self): sliceditems = list(self.feed[3:6]) self.assertEqual(len(sliceditems), 3) self.assertEqual(sliceditems[0].id, self.response["items"][3]["id"]) self.assertEqual(sliceditems[-1].id, self.response["items"][5]["id"]) def test_feed_slice_id_to_inf(self): sliceditems = list(self.feed[slice("acct:[email protected]", None)]) self.assertEqual(len(sliceditems), 9) self.assertEqual(sliceditems[0].id, self.response["items"][11]["id"]) self.assertEqual(sliceditems[-1].id, self.response["items"][19]["id"]) def test_feed_slice_zero_to_id(self): sliceditems = list(self.feed[slice("acct:[email protected]")]) self.assertEqual(len(sliceditems), 5) self.assertEqual(sliceditems[-1].id, self.response["items"][0]["id"]) self.assertEqual(sliceditems[0].id, self.response["items"][4]["id"]) def test_feed_items_before(self): # before and limit 3 items = list(self.feed.items(before="acct:[email protected]", limit=3)) self.assertEqual(len(items), 3) self.assertEqual(items[0].id, self.response["items"][11]["id"]) self.assertEqual(items[-1].id, self.response["items"][13]["id"]) def test_feed_items_since(self): # since and limit 12 (limit more than items returned) items = list(self.feed.items(since="acct:[email protected]", limit=12)) self.assertEqual(len(items), 10) self.assertEqual(items[0].id, self.response["items"][9]["id"]) self.assertEqual(items[-1].id, self.response["items"][0]["id"]) def test_feed_items_no_limit(self): # no limit items = list(self.feed.items(limit=None)) self.assertEqual(len(items), 20) self.assertEqual(items[0].id, self.response["items"][0]["id"]) self.assertEqual(items[-1].id, self.response["items"][19]["id"]) def test_feed_items_offset(self): # offset items = list(self.feed.items(offset=18)) self.assertEqual(len(items), 2) # offset and limit items = list(self.feed.items(offset=10, limit=5)) self.assertEqual(len(items), 5)
def members(self): """ :class:`Feed <pypump.models.feed.Feed>` of collection members. """ if self._members is None: self._members = Feed(self.links["members"], pypump=self._pump) return self._members
class FeedTest(PyPumpTest): def setUp(self): super(FeedTest, self).setUp() self.response.data = { "displayName": "Followers for Testuser", "url": "https://example.com/api/user/testuser/followers", "objectTypes": [ "person" ], "items": [{"objectType": "person", "id": "acct:testuser%[email protected]" % i} for i in range(20)], "totalItems": 20, "author": {"objectType": "person", "id": "acct:[email protected]"}, "links": { "self": {"href": "https://example.com/api/user/testuser/followers?offset=0&count=20"}, "current": {"href": "https://example.com/api/user/testuser/followers"}, "prev": {"href": "https://example.com/api/user/testuser/followers?since=acct%3Atestuser0%40example.com"}, "next": {"href": "https://example.com/api/user/testuser/followers?before=acct%3Atestuser19%40example.com"}, } } self.feed = Feed(pypump=self.pump).unserialize(self.response.data) def test_before_since_offset_exceptions(self): with self.assertRaises(PyPumpException): self.feed.items(before="something", offset=12) with self.assertRaises(PyPumpException): self.feed.items(since="something", offset=12) with self.assertRaises(PyPumpException): self.feed.items(since="something", before="something") def test_feed(self): # is a Feed object self.assertTrue(isinstance(self.feed, Feed)) # object to string self.assertEqual(self.feed.__str__(), self.feed.display_name) self.feed.display_name = u'Followers for Testanvändare' if six.PY3: self.assertEqual(self.feed.__str__(), self.feed.display_name) else: self.assertEqual(self.feed.__str__(), self.feed.display_name.encode('utf-8')) def test_feed_attr_display_name(self): self.assertTrue(hasattr(self.feed, 'display_name')) self.assertEqual(self.feed.display_name, self.response["displayName"]) def test_feed_attr_url(self): self.assertTrue(hasattr(self.feed, 'url')) self.assertEqual(self.feed.url, self.response["url"]) def test_feed_attr_object_types(self): self.assertTrue(hasattr(self.feed, 'object_types')) self.assertEqual(self.feed.object_types, self.response["objectTypes"]) def test_feed_attr_items(self): self.assertTrue(hasattr(self.feed, '_items')) self.assertTrue(isinstance(self.feed._items, list)) def test_feed_attr_total_items(self): self.assertTrue(hasattr(self.feed, 'total_items')) self.assertEqual(self.feed.total_items, self.response["totalItems"]) def test_feed_attr_author(self): self.assertTrue(hasattr(self.feed, 'author')) self.assertTrue(isinstance(self.feed.author, type(self.pump.Person()))) def test_feed_attr_links(self): self.assertTrue(hasattr(self.feed, 'links')) # we should expand this test when we have settled on way to show links self.assertTrue(self.feed.links is not None) def test_feed_slice_0_to_5(self): sliceditems = self.feed[:5] self.assertEqual(len(sliceditems), 5) self.assertEqual(sliceditems[0].id, self.response['items'][0]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][4]['id']) def test_feed_slice_5_to_inf(self): sliceditems = self.feed[5:] self.assertEqual(len(sliceditems), 15) self.assertEqual(sliceditems[0].id, self.response['items'][5]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][19]['id']) def test_feed_slice_3_to_6(self): sliceditems = self.feed[3:6] self.assertEqual(len(sliceditems), 3) self.assertEqual(sliceditems[0].id, self.response['items'][3]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][5]['id']) def test_feed_slice_id_to_inf(self): sliceditems = self.feed[slice('acct:[email protected]', None)] self.assertEqual(len(sliceditems), 9) self.assertEqual(sliceditems[1].id, self.response['items'][12]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][19]['id']) def test_feed_slice_zero_to_id(self): sliceditems = self.feed[slice('acct:[email protected]')] self.assertEqual(len(sliceditems), 5) self.assertEqual(sliceditems[-1].id, self.response['items'][0]['id']) self.assertEqual(sliceditems[0].id, self.response['items'][4]['id']) def test_feed_items_before(self): # before and limit 3 items = self.feed.items(before='acct:[email protected]', limit=3) self.assertEqual(len(items), 3) self.assertEqual(items[0].id, self.response['items'][11]['id']) self.assertEqual(items[-1].id, self.response['items'][13]['id']) def test_feed_items_since(self): # since and limit 12 (limit more than items returned) items = self.feed.items(since='acct:[email protected]', limit=12) self.assertEqual(len(items), 10) self.assertEqual(items[0].id, self.response['items'][9]['id']) self.assertEqual(items[-1].id, self.response['items'][0]['id']) def test_feed_items_no_limit(self): # no limit items = self.feed.items(limit=None) self.assertEqual(len(items), 20) self.assertEqual(items[0].id, self.response['items'][0]['id']) self.assertEqual(items[-1].id, self.response['items'][19]['id']) def test_feed_items_offset(self): # offset items = self.feed.items(offset=18) self.assertEqual(len(items), 2) # offset and limit items = self.feed.items(offset=10, limit=5) self.assertEqual(len(items), 5) def test_slice_slice_zero_to_3(self): sliceditems = self.feed[3:10][:3] self.assertEqual(len(sliceditems), 3) self.assertEqual(sliceditems[0].id, self.response['items'][3]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][5]['id']) def test_slice_slice_zero_to_negative(self): sliceditems = self.feed[3:10][:-3] self.assertEqual(len(sliceditems), 4) self.assertEqual(sliceditems[0].id, self.response['items'][3]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][6]['id']) def test_slice_slice_3_to_end(self): sliceditems = self.feed[3:10][3:] self.assertEqual(len(sliceditems), 4) self.assertEqual(sliceditems[0].id, self.response['items'][6]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][9]['id']) def test_slice_slice_negative_to_end(self): sliceditems = self.feed[3:10][-3:] self.assertEqual(len(sliceditems), 3) self.assertEqual(sliceditems[0].id, self.response['items'][7]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][9]['id']) def test_slice_slice_with_id(self): sliceditems = self.feed[3:10] with self.assertRaises(TypeError): sliceditems[slice(self.response['items'][4]['id'])] def test_feed_sliced_by_id(self): sliceditems = self.feed[slice('acct:[email protected]', None)][:3] self.assertEqual(len(sliceditems), 3) self.assertEqual(sliceditems[0].id, self.response['items'][11]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][13]['id']) with self.assertRaises(PyPumpException): self.feed[slice('acct:[email protected]', None)][3:] with self.assertRaises(PyPumpException): self.feed[slice('acct:[email protected]', None)][:-3] def test_zero_to_negative_slicing(self): sliceditems = self.feed[:-18] self.assertEqual(len(sliceditems), 2) self.assertEqual(sliceditems[0].id, self.response['items'][0]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][1]['id']) def test_negative_to_inf_slicing(self): sliceditems = self.feed[-18:] self.assertEqual(len(sliceditems), 18) self.assertEqual(sliceditems[0].id, self.response['items'][2]['id']) self.assertEqual(sliceditems[-1].id, self.response['items'][19]['id'])