def test_get_content_ids__returns_cached_ids_if_enough_in_cache(self, mock_queryset): stream = FollowedStream(user=self.user) stream.paginate_by = 1 with patch.object(stream, "get_queryset") as mock_queryset, \ patch.object(stream, "get_cached_content_ids") as mock_cached: mock_cached.return_value = [self.content1.id], {self.content1.id: self.content1.id} stream.get_content_ids() self.assertEqual(mock_queryset.call_count, 0)
def test_get_content_ids__returns_cached_ids_if_enough_in_cache(self, mock_queryset): stream = FollowedStream(user=self.user) stream.paginate_by = 1 with patch.object(stream, "get_queryset") as mock_queryset, \ patch.object(stream, "get_cached_content_ids") as mock_cached: mock_cached.return_value = [self.content1.id], {self.content1.id: self.content1.id} stream.get_content_ids() self.assertEqual(mock_queryset.call_count, 0)
def test_get_target_streams(self): self.assertEqual( len( FollowedStream.get_target_streams(self.public_content, self.user, self.public_content.author)), 1, )
def get_content(self): stream = FollowedStream(last_id=self.last_id, user=self.request.user) return stream.get_content()
def test_calls_add_to_redis(self, mock_add): add_to_stream_for_users(self.content.id, "FollowedStream") stream = FollowedStream(user=self.user) mock_add.assert_called_once_with(self.content, [stream.get_key()])
def setUp(self): super().setUp() self.stream = FollowedStream(user=self.user)
class TestFollowedStream(SocialhomeTestCase): @classmethod def setUpTestData(cls): super().setUpTestData() cls.create_local_and_remote_user() cls.remote_profile.followers.add(cls.profile) cls.create_content_set(author=cls.remote_profile) cls.other_public_content = PublicContentFactory() SiteContentFactory() SelfContentFactory() LimitedContentFactory() def setUp(self): super().setUp() self.stream = FollowedStream(user=self.user) def test_get_content_ids_uses_cached_ids(self): with patch.object(self.stream, "get_cached_content_ids") as mock_cached: self.stream.get_content_ids() mock_cached.assert_called_once_with() def test_get_content_ids_fills_in_non_cached_content_up_to_pagination_amount( self): with patch.object(self.stream, "get_cached_content_ids") as mock_cached: cached_ids = random.sample(range(10000, 100000), self.stream.paginate_by - 1) mock_cached.return_value = cached_ids # Fills up with one of the two that are available all_ids = set(cached_ids + [self.site_content.id]) self.assertEqual(set(self.stream.get_content_ids()), all_ids) def test_get_key(self): self.assertEqual(self.stream.get_key(), "socialhome:streams:followed:%s" % self.user.id) def test_only_followed_profile_content_returned(self): self.assertEqual( {self.public_content, self.site_content}, set(self.stream.get_content()), ) def test_raises_if_no_user(self): self.stream.user = None with self.assertRaises(AttributeError): self.stream.get_content() def test_should_cache_content(self): self.assertTrue(self.stream.should_cache_content(self.public_content)) self.assertTrue(self.stream.should_cache_content(self.site_content)) self.assertFalse(self.stream.should_cache_content( self.limited_content)) self.assertFalse(self.stream.should_cache_content(self.self_content)) self.assertFalse( self.stream.should_cache_content(self.other_public_content))
def test_get_target_streams(self): self.assertEqual( len(FollowedStream.get_target_streams(self.public_content, self.user, self.public_content.author)), 1, )
def setUp(self): super().setUp() self.stream = FollowedStream(user=self.user)
class TestFollowedStream(SocialhomeTestCase): @classmethod def setUpTestData(cls): super().setUpTestData() cls.create_local_and_remote_user() cls.remote_profile.followers.add(cls.profile) cls.create_content_set(author=cls.remote_profile) cls.other_public_content = PublicContentFactory() SiteContentFactory() SelfContentFactory() LimitedContentFactory() def setUp(self): super().setUp() self.stream = FollowedStream(user=self.user) def test_get_content_ids__uses_cached_ids(self): with patch.object(self.stream, "get_cached_content_ids", return_value=([], {})) as mock_cached: self.stream.get_content_ids() mock_cached.assert_called_once_with() def test_get_content_ids__fills_in_non_cached_content_up_to_pagination_amount(self): with patch.object(self.stream, "get_cached_content_ids") as mock_cached: cached_ids = random.sample(range(10000, 100000), self.stream.paginate_by - 1) throughs = dict(zip(cached_ids, cached_ids)) mock_cached.return_value = cached_ids, throughs # Fills up with one of the two that are available all_ids = set(cached_ids + [self.site_content.id]) self.assertEqual(set(self.stream.get_content_ids()[0]), all_ids) def test_get_target_streams(self): self.assertEqual( len(FollowedStream.get_target_streams(self.public_content, self.user, self.public_content.author)), 1, ) def test_get_throughs_key(self): self.assertEqual( self.stream.get_throughs_key(self.stream.key), "sh:streams:followed:%s:throughs" % self.user.id, ) def test_key(self): self.assertEqual(self.stream.key, "sh:streams:followed:%s" % self.user.id) def test_only_followed_profile_content_returned(self): qs, _throughs = self.stream.get_content() self.assertEqual( set(qs), {self.public_content, self.site_content}, ) def test_raises_if_no_user(self): self.stream.user = None with self.assertRaises(AttributeError): self.stream.get_content() def test_should_cache_content(self): self.assertTrue(self.stream.should_cache_content(self.public_content)) self.assertTrue(self.stream.should_cache_content(self.site_content)) self.assertFalse(self.stream.should_cache_content(self.limited_content)) self.assertFalse(self.stream.should_cache_content(self.self_content)) self.assertFalse(self.stream.should_cache_content(self.other_public_content))