def test_init_redis_connection(self, mock_redis, mock_queryset): stream = BaseStream() self.assertIsNone(stream.redis) stream.init_redis_connection() mock_redis.assert_called_once_with() self.assertEqual(stream.redis, "redis") mock_redis.reset_mock() stream.init_redis_connection() self.assertFalse(mock_redis.called)
def get_precache_trim_size(user_activities, key): """ Get user activity to decide what kind of trimming we need. :return: int """ # Local imports since we load tasks before apps are loaded fully from socialhome.streams.streams import BaseStream from socialhome.users.models import User user_id = BaseStream.get_key_user_id(key) if not user_id: # Anonymous, trim as inactive return settings.SOCIALHOME_STREAMS_PRECACHE_INACTIVE_SIZE user_active = user_activities.get(user_id) if user_active is not None: # Trim according to activity return (settings.SOCIALHOME_STREAMS_PRECACHE_SIZE if user_active else settings.SOCIALHOME_STREAMS_PRECACHE_INACTIVE_SIZE) # Get user try: user = User.objects.get(id=user_id) except User.DoesNotExist: # Trim all return 0 check_time = user.last_login if user.last_login else user.date_joined user_active = check_time >= now() - timedelta( days=settings.SOCIALHOME_STREAMS_PRECACHE_INACTIVE_DAYS) user_activities[user_id] = user_active # Trim according to activity return (settings.SOCIALHOME_STREAMS_PRECACHE_SIZE if user_active else settings.SOCIALHOME_STREAMS_PRECACHE_INACTIVE_SIZE)
def get_precache_trim_size(user_activities, key): """ Get user activity to decide what kind of trimming we need. :return: int """ # Local imports since we load tasks before apps are loaded fully from socialhome.streams.streams import BaseStream from socialhome.users.models import User user_id = BaseStream.get_key_user_id(key) if not user_id: # Anonymous, trim as inactive return settings.SOCIALHOME_STREAMS_PRECACHE_INACTIVE_SIZE user_active = user_activities.get(user_id) if user_active is not None: # Trim according to activity return ( settings.SOCIALHOME_STREAMS_PRECACHE_SIZE if user_active else settings.SOCIALHOME_STREAMS_PRECACHE_INACTIVE_SIZE ) # Get user try: user = User.objects.get(id=user_id) except User.DoesNotExist: # Trim all return 0 check_time = user.last_login if user.last_login else user.date_joined user_active = check_time >= now() - timedelta(days=settings.SOCIALHOME_STREAMS_PRECACHE_INACTIVE_DAYS) user_activities[user_id] = user_active # Trim according to activity return ( settings.SOCIALHOME_STREAMS_PRECACHE_SIZE if user_active else settings.SOCIALHOME_STREAMS_PRECACHE_INACTIVE_SIZE )
def test_get_cached_range(self, mock_get, mock_queryset): self.stream.stream_type = StreamType.PUBLIC mock_zrevrange = Mock( return_value=[str(self.content2.id), str(self.content1.id)]) mock_hmget = Mock( return_value=[str(self.content2.id), str(self.content1.id)]) mock_redis = Mock(zrevrange=mock_zrevrange, hmget=mock_hmget) mock_get.return_value = mock_redis ids, throughs = self.stream.get_cached_range(0) self.assertEqual(ids, [self.content2.id, self.content1.id]) self.assertEqual(throughs, { self.content2.id: self.content2.id, self.content1.id: self.content1.id }) mock_zrevrange.assert_called_once_with(self.stream.key, 0, 0 + self.stream.paginate_by) mock_hmget.assert_called_once_with(BaseStream.get_throughs_key( self.stream.key), keys=[ self.content2.id, self.content1.id, ]) # Non-zero index mock_zrevrange.reset_mock() self.stream.get_cached_range(5) mock_zrevrange.assert_called_once_with(self.stream.key, 5, 5 + self.stream.paginate_by)
def test_get_key_user_id(self, mock_queryset): self.assertEqual(BaseStream.get_key_user_id("spam:eggs:1"), 1) self.assertEqual(BaseStream.get_key_user_id("spam:eggs:1:throughs"), 1) self.assertEqual(BaseStream.get_key_user_id("spam:eggs:1:2"), 2) self.assertEqual(BaseStream.get_key_user_id("spam:eggs:1:2:throughs"), 2) self.assertIsNone(BaseStream.get_key_user_id("spam:eggs:anonymous")) self.assertIsNone(BaseStream.get_key_user_id("spam:eggs:anonymous:throughs"))
def test_init_redis_connection(self, mock_redis, mock_queryset): stream = BaseStream() self.assertIsNone(stream.redis) stream.init_redis_connection() mock_redis.assert_called_once_with() self.assertEqual(stream.redis, "redis") mock_redis.reset_mock() stream.init_redis_connection() self.assertFalse(mock_redis.called)
def test_get_cached_range(self, mock_get, mock_queryset): self.stream.stream_type = StreamType.PUBLIC mock_zrevrange = Mock(return_value=[str(self.content2.id), str(self.content1.id)]) mock_hmget = Mock(return_value=[str(self.content2.id), str(self.content1.id)]) mock_redis = Mock(zrevrange=mock_zrevrange, hmget=mock_hmget) mock_get.return_value = mock_redis ids, throughs = self.stream.get_cached_range(0) self.assertEqual(ids, [self.content2.id, self.content1.id]) self.assertEqual(throughs, {self.content2.id: self.content2.id, self.content1.id: self.content1.id}) mock_zrevrange.assert_called_once_with(self.stream.key, 0, 0 + self.stream.paginate_by) mock_hmget.assert_called_once_with(BaseStream.get_throughs_key(self.stream.key), keys=[ self.content2.id, self.content1.id, ]) # Non-zero index mock_zrevrange.reset_mock() self.stream.get_cached_range(5) mock_zrevrange.assert_called_once_with(self.stream.key, 5, 5 + self.stream.paginate_by)
def test_init(self, mock_queryset): stream = BaseStream(last_id=333, user="******") self.assertEqual(stream.last_id, 333) self.assertEqual(stream.user, "user")
def setUp(self): super().setUp() self.stream = BaseStream(user=self.user)
class TestBaseStream(SocialhomeTestCase): @classmethod def setUpTestData(cls): super().setUpTestData() cls.user = UserFactory() cls.content1 = ContentFactory() cls.content2 = ContentFactory() def setUp(self): super().setUp() self.stream = BaseStream(user=self.user) def test___str__(self, mock_queryset): self.assertEqual(str(self.stream), "BaseStream (%s)" % str(self.user)) @patch("socialhome.streams.streams.get_redis_connection") def test_get_cached_content_ids__calls(self, mock_get, mock_queryset): mock_redis = Mock() mock_get.return_value = mock_redis self.stream.stream_type = StreamType.PUBLIC self.stream.get_cached_content_ids() # Skips zrevrank if not last_id self.assertFalse(mock_redis.zrevrank.called) # Calls zrevrange with correct parameters mock_redis.zrevrange.assert_called_once_with(self.stream.get_key(), 0, self.stream.paginate_by) mock_redis.reset_mock() # Calls zrevrank with last_id self.stream.last_id = self.content2.id mock_redis.zrevrank.return_value = 3 self.stream.get_cached_content_ids() mock_redis.zrevrank.assert_called_once_with(self.stream.get_key(), self.content2.id) mock_redis.zrevrange.assert_called_once_with( self.stream.get_key(), 4, 4 + self.stream.paginate_by) @patch("socialhome.streams.streams.get_redis_connection") def test_get_cached_content_ids__returns_empty_list_if_outside_cached_ids( self, mock_get, mock_queryset): mock_redis = Mock(zrevrank=Mock(return_value=None)) mock_get.return_value = mock_redis self.stream.stream_type = StreamType.PUBLIC self.stream.last_id = 123 self.assertEqual(self.stream.get_cached_content_ids(), []) self.assertFalse(mock_redis.zrevrange.called) def test_get_content(self, mock_queryset): self.assertEqual([self.content2, self.content1], list(self.stream.get_content())) self.stream.last_id = self.content2.id self.assertEqual([self.content1], list(self.stream.get_content())) self.stream.last_id = self.content1.id self.assertEqual([], list(self.stream.get_content())) def test_get_content_ids_returns_right_ids_according_to_last_id_and_ordering( self, mock_queryset): self.assertEqual({self.content2.id, self.content1.id}, set(self.stream.get_content_ids())) self.stream.last_id = self.content2.id self.assertEqual({self.content1.id}, set(self.stream.get_content_ids())) self.stream.last_id = self.content1.id self.assertEqual(set(), set(self.stream.get_content_ids())) # Reverse self.stream.ordering = "created" self.stream.last_id = None self.assertEqual({self.content2.id, self.content1.id}, set(self.stream.get_content_ids())) self.stream.last_id = self.content1.id self.assertEqual({self.content2.id}, set(self.stream.get_content_ids())) self.stream.last_id = self.content2.id self.assertEqual(set(), set(self.stream.get_content_ids())) def test_get_content_ids_limits_by_paginate_by(self, mock_queryset): self.stream.paginate_by = 1 self.assertEqual({self.content2.id}, set(self.stream.get_content_ids())) def test_init(self, mock_queryset): stream = BaseStream(last_id=333, user="******") self.assertEqual(stream.last_id, 333) self.assertEqual(stream.user, "user") def test_should_cache_content(self, mock_queryset): self.assertTrue(self.stream.should_cache_content(self.content1)) self.assertTrue(self.stream.should_cache_content(self.content2)) mock_queryset.return_value = Content.objects.none() self.assertFalse(self.stream.should_cache_content(self.content1)) self.assertFalse(self.stream.should_cache_content(self.content2))
class TestBaseStream(SocialhomeTestCase): @classmethod def setUpTestData(cls): super().setUpTestData() cls.user = UserFactory() cls.content1 = ContentFactory() cls.content2 = ContentFactory() def setUp(self): super().setUp() self.stream = BaseStream(user=self.user) def test___str__(self, mock_queryset): self.assertEqual(str(self.stream), "BaseStream (%s)" % str(self.user)) @patch("socialhome.streams.streams.get_redis_connection") def test_get_cached_content_ids__calls(self, mock_get, mock_queryset): mock_redis = Mock(zrevrange=Mock(return_value=[])) mock_get.return_value = mock_redis self.stream.stream_type = StreamType.PUBLIC self.stream.get_cached_content_ids() # Skips zrevrank if not last_id self.assertFalse(mock_redis.zrevrank.called) # Calls zrevrange with correct parameters mock_redis.zrevrange.assert_called_once_with(self.stream.key, 0, self.stream.paginate_by) mock_redis.reset_mock() # Calls zrevrank with last_id self.stream.last_id = self.content2.id mock_redis.zrevrank.return_value = 3 self.stream.get_cached_content_ids() mock_redis.zrevrank.assert_called_once_with(self.stream.key, self.content2.id) mock_redis.zrevrange.assert_called_once_with(self.stream.key, 4, 4 + self.stream.paginate_by) @patch("socialhome.streams.streams.get_redis_connection") def test_get_cached_content_ids__returns_empty_list_if_outside_cached_ids(self, mock_get, mock_queryset): mock_redis = Mock(zrevrank=Mock(return_value=None)) mock_get.return_value = mock_redis self.stream.stream_type = StreamType.PUBLIC self.stream.last_id = 123 self.assertEqual(self.stream.get_cached_content_ids(), ([], {})) self.assertFalse(mock_redis.zrevrange.called) @patch("socialhome.streams.streams.get_redis_connection") def test_get_cached_range(self, mock_get, mock_queryset): self.stream.stream_type = StreamType.PUBLIC mock_zrevrange = Mock(return_value=[str(self.content2.id), str(self.content1.id)]) mock_hmget = Mock(return_value=[str(self.content2.id), str(self.content1.id)]) mock_redis = Mock(zrevrange=mock_zrevrange, hmget=mock_hmget) mock_get.return_value = mock_redis ids, throughs = self.stream.get_cached_range(0) self.assertEqual(ids, [self.content2.id, self.content1.id]) self.assertEqual(throughs, {self.content2.id: self.content2.id, self.content1.id: self.content1.id}) mock_zrevrange.assert_called_once_with(self.stream.key, 0, 0 + self.stream.paginate_by) mock_hmget.assert_called_once_with(BaseStream.get_throughs_key(self.stream.key), keys=[ self.content2.id, self.content1.id, ]) # Non-zero index mock_zrevrange.reset_mock() self.stream.get_cached_range(5) mock_zrevrange.assert_called_once_with(self.stream.key, 5, 5 + self.stream.paginate_by) def test_get_content(self, mock_queryset): qs, throughs = self.stream.get_content() self.assertEqual(set(qs), {self.content2, self.content1}) self.assertEqual(throughs, {self.content2.id: self.content2.id, self.content1.id: self.content1.id}) self.stream.last_id = self.content2.id qs, throughs = self.stream.get_content() self.assertEqual(set(qs), {self.content1}) self.assertEqual(throughs, {self.content1.id: self.content1.id}) self.stream.last_id = self.content1.id qs, throughs = self.stream.get_content() self.assertFalse(qs) self.assertFalse(throughs) def test_get_content_ids__returns_right_ids_according_to_last_id_and_ordering(self, mock_queryset): ids, throughs = self.stream.get_content_ids() self.assertEqual(ids, [self.content2.id, self.content1.id]) self.assertEqual(throughs, {self.content2.id: self.content2.id, self.content1.id: self.content1.id}) self.stream.last_id = self.content2.id ids, throughs = self.stream.get_content_ids() self.assertEqual(ids, [self.content1.id]) self.assertEqual(throughs, {self.content1.id: self.content1.id}) # Reverse self.stream.ordering = "created" self.stream.last_id = None ids, throughs = self.stream.get_content_ids() self.assertEqual(ids, [self.content1.id, self.content2.id]) self.assertEqual(throughs, {self.content1.id: self.content1.id, self.content2.id: self.content2.id}) self.stream.last_id = self.content1.id ids, throughs = self.stream.get_content_ids() self.assertEqual(ids, [self.content2.id]) self.assertEqual(throughs, {self.content2.id: self.content2.id}) self.stream.last_id = self.content2.id ids, throughs = self.stream.get_content_ids() self.assertFalse(ids) self.assertFalse(throughs) def test_get_content_ids__limits_by_paginate_by(self, mock_queryset): self.stream.paginate_by = 1 ids, throughs = self.stream.get_content_ids() self.assertEqual(ids, [self.content2.id]) self.assertEqual(throughs, {self.content2.id: self.content2.id}) 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_init(self, mock_queryset): stream = BaseStream(last_id=333, user="******") self.assertEqual(stream.last_id, 333) self.assertEqual(stream.user, "user") @patch("socialhome.streams.streams.get_redis_connection", return_value="redis") def test_init_redis_connection(self, mock_redis, mock_queryset): stream = BaseStream() self.assertIsNone(stream.redis) stream.init_redis_connection() mock_redis.assert_called_once_with() self.assertEqual(stream.redis, "redis") mock_redis.reset_mock() stream.init_redis_connection() self.assertFalse(mock_redis.called) def test_should_cache_content(self, mock_queryset): self.assertTrue(self.stream.should_cache_content(self.content1)) self.assertTrue(self.stream.should_cache_content(self.content2)) mock_queryset.return_value = Content.objects.none() self.assertFalse(self.stream.should_cache_content(self.content1)) self.assertFalse(self.stream.should_cache_content(self.content2))
def setUp(self): super().setUp() self.stream = BaseStream(user=self.user)
class TestBaseStream(SocialhomeTestCase): @classmethod def setUpTestData(cls): super().setUpTestData() cls.user = UserFactory() cls.content1 = ContentFactory() cls.content2 = ContentFactory() def setUp(self): super().setUp() self.stream = BaseStream(user=self.user) def test___str__(self, mock_queryset): self.assertEqual(str(self.stream), "BaseStream (%s)" % str(self.user)) @patch("socialhome.streams.streams.get_redis_connection") def test_get_cached_content_ids__calls(self, mock_get, mock_queryset): mock_redis = Mock(zrevrange=Mock(return_value=[])) mock_get.return_value = mock_redis self.stream.stream_type = StreamType.PUBLIC self.stream.get_cached_content_ids() # Skips zrevrank if not last_id self.assertFalse(mock_redis.zrevrank.called) # Calls zrevrange with correct parameters mock_redis.zrevrange.assert_called_once_with(self.stream.key, 0, self.stream.paginate_by) mock_redis.reset_mock() # Calls zrevrank with last_id self.stream.last_id = self.content2.id mock_redis.zrevrank.return_value = 3 self.stream.get_cached_content_ids() mock_redis.zrevrank.assert_called_once_with(self.stream.key, self.content2.id) mock_redis.zrevrange.assert_called_once_with(self.stream.key, 4, 4 + self.stream.paginate_by) @patch("socialhome.streams.streams.get_redis_connection") def test_get_cached_content_ids__returns_empty_list_if_outside_cached_ids(self, mock_get, mock_queryset): mock_redis = Mock(zrevrank=Mock(return_value=None)) mock_get.return_value = mock_redis self.stream.stream_type = StreamType.PUBLIC self.stream.last_id = 123 self.assertEqual(self.stream.get_cached_content_ids(), ([], {})) self.assertFalse(mock_redis.zrevrange.called) @patch("socialhome.streams.streams.get_redis_connection") def test_get_cached_range(self, mock_get, mock_queryset): self.stream.stream_type = StreamType.PUBLIC mock_zrevrange = Mock(return_value=[str(self.content2.id), str(self.content1.id)]) mock_hmget = Mock(return_value=[str(self.content2.id), str(self.content1.id)]) mock_redis = Mock(zrevrange=mock_zrevrange, hmget=mock_hmget) mock_get.return_value = mock_redis ids, throughs = self.stream.get_cached_range(0) self.assertEqual(ids, [self.content2.id, self.content1.id]) self.assertEqual(throughs, {self.content2.id: self.content2.id, self.content1.id: self.content1.id}) mock_zrevrange.assert_called_once_with(self.stream.key, 0, 0 + self.stream.paginate_by) mock_hmget.assert_called_once_with(BaseStream.get_throughs_key(self.stream.key), keys=[ self.content2.id, self.content1.id, ]) # Non-zero index mock_zrevrange.reset_mock() self.stream.get_cached_range(5) mock_zrevrange.assert_called_once_with(self.stream.key, 5, 5 + self.stream.paginate_by) def test_get_content(self, mock_queryset): qs, throughs = self.stream.get_content() self.assertEqual(set(qs), {self.content2, self.content1}) self.assertEqual(throughs, {self.content2.id: self.content2.id, self.content1.id: self.content1.id}) self.stream.last_id = self.content2.id qs, throughs = self.stream.get_content() self.assertEqual(set(qs), {self.content1}) self.assertEqual(throughs, {self.content1.id: self.content1.id}) self.stream.last_id = self.content1.id qs, throughs = self.stream.get_content() self.assertFalse(qs) self.assertFalse(throughs) def test_get_content_ids__returns_right_ids_according_to_last_id_and_ordering(self, mock_queryset): ids, throughs = self.stream.get_content_ids() self.assertEqual(ids, [self.content2.id, self.content1.id]) self.assertEqual(throughs, {self.content2.id: self.content2.id, self.content1.id: self.content1.id}) self.stream.last_id = self.content2.id ids, throughs = self.stream.get_content_ids() self.assertEqual(ids, [self.content1.id]) self.assertEqual(throughs, {self.content1.id: self.content1.id}) # Reverse self.stream.ordering = "created" self.stream.last_id = None ids, throughs = self.stream.get_content_ids() self.assertEqual(ids, [self.content1.id, self.content2.id]) self.assertEqual(throughs, {self.content1.id: self.content1.id, self.content2.id: self.content2.id}) self.stream.last_id = self.content1.id ids, throughs = self.stream.get_content_ids() self.assertEqual(ids, [self.content2.id]) self.assertEqual(throughs, {self.content2.id: self.content2.id}) self.stream.last_id = self.content2.id ids, throughs = self.stream.get_content_ids() self.assertFalse(ids) self.assertFalse(throughs) def test_get_content_ids__limits_by_paginate_by(self, mock_queryset): self.stream.paginate_by = 1 ids, throughs = self.stream.get_content_ids() self.assertEqual(ids, [self.content2.id]) self.assertEqual(throughs, {self.content2.id: self.content2.id}) 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_key_user_id(self, mock_queryset): self.assertEqual(BaseStream.get_key_user_id("spam:eggs:1"), 1) self.assertEqual(BaseStream.get_key_user_id("spam:eggs:1:throughs"), 1) self.assertEqual(BaseStream.get_key_user_id("spam:eggs:1:2"), 2) self.assertEqual(BaseStream.get_key_user_id("spam:eggs:1:2:throughs"), 2) self.assertIsNone(BaseStream.get_key_user_id("spam:eggs:anonymous")) self.assertIsNone(BaseStream.get_key_user_id("spam:eggs:anonymous:throughs")) def test_init(self, mock_queryset): stream = BaseStream(last_id=333, user="******") self.assertEqual(stream.last_id, 333) self.assertEqual(stream.user, "user") @patch("socialhome.streams.streams.get_redis_connection", return_value="redis") def test_init_redis_connection(self, mock_redis, mock_queryset): stream = BaseStream() self.assertIsNone(stream.redis) stream.init_redis_connection() mock_redis.assert_called_once_with() self.assertEqual(stream.redis, "redis") mock_redis.reset_mock() stream.init_redis_connection() self.assertFalse(mock_redis.called) def test_should_cache_content(self, mock_queryset): self.assertTrue(self.stream.should_cache_content(self.content1)) self.assertTrue(self.stream.should_cache_content(self.content2)) mock_queryset.return_value = Content.objects.none() self.assertFalse(self.stream.should_cache_content(self.content1)) self.assertFalse(self.stream.should_cache_content(self.content2))