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_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()])