def test_discard_all(self): ''' Creat the posts way in the past and make sure we drop them all''' before = ChannelHotTopics.objects().count() DAY_10022011 = pytz.utc.localize(datetime(day=2, month=10, year=2011)) self._make_laptops_and_icecream(DAY_10022011) purge_stats(self.channel) after = ChannelHotTopics.objects().count() self.assertEqual(after, before)
def test_leave_months_only(self): before = ChannelHotTopics.objects().count() DAY_TWO_WEEKS_AGO = now() - timedelta(days=15) self._make_laptops_and_icecream(DAY_TWO_WEEKS_AGO) delta = ChannelHotTopics.objects().count() - before purge_stats(self.channel) after = ChannelHotTopics.objects().count() - before # The days should be all gone self.assertEqual(after, delta / 2)
def test_purge_none(self): TWO_DAYS_AGO = now() - timedelta(days=2) self._make_laptops_and_icecream(TWO_DAYS_AGO) stats = purge_stats(self.channel) last_purged = stats["last_purged"] days = stats["purge_days"] months = stats["purge_months"] self.channel.reload() self.assertEqual( datetime_to_timeslot(self.channel.last_purged, 'hour'), datetime_to_timeslot(last_purged, 'hour')) # Should have purged over 15 days for time slots since we never urged before self.assertEqual(len(days), 15) # Months purged depends on how far in we are to the month when we run the test self.assertTrue(len(months) in [2, 3]) import solariat_bottle.utils.purging class MockLocaltime(object): tm_mday = 6 solariat_bottle.utils.purging.localtime = MockLocaltime stats = purge_stats(self.channel) last_purged = stats["last_purged"] days = stats["purge_days"] months = stats["purge_months"] self.assertEqual(len(days), 1) self.assertEqual(days[0], decode_timeslot(Timeslot(level='day').timeslot)) self.assertEqual(len(months), 0) class MockLocaltime(object): tm_mday = 8 solariat_bottle.utils.purging.localtime = MockLocaltime stats = purge_stats(self.channel) last_purged = stats["last_purged"] days = stats["purge_days"] months = stats["purge_months"] self.assertEqual(len(days), 1) self.assertEqual(len(months), 1) self.assertEqual(months[0], decode_timeslot(Timeslot(level='month').timeslot))
def test_outdated_trends4(self): """ all existing hour stats should be kept """ date_now = now() date_old = now() - relativedelta( days=get_var('TOPIC_TRENDS_HOUR_STATS_KEEP_DAYS') - 1, hours=23) self._make_laptops_and_icecream(_created=date_old) total_trends = ChannelTopicTrends.objects().count() hour_trends = total_trends / 2 day_trends = total_trends / 2 stats = purge_stats(self.channel) self.assertEqual(day_trends, 6) self.assertEqual(hour_trends, 6) self.assertEqual(stats['discard_junk_stats']['trends_day_count'], 0) self.assertEqual(stats['discard_junk_stats']['trends_hour_count'], 0)
def test_outdated_trends2(self): """ all existing stats should be kept, cause it's not too old """ date_now = now() date_old = now() - relativedelta( months=get_var('TOPIC_TRENDS_DAY_STATS_KEEP_MONTHS')) self._make_laptops_and_icecream(_created=date_now) total_trends = ChannelTopicTrends.objects().count() hour_trends = total_trends / 2 day_trends = total_trends / 2 stats = purge_stats(self.channel) self.assertEqual(day_trends, 6) self.assertEqual(hour_trends, 6) self.assertEqual(stats['discard_junk_stats']['trends_day_count'], 0) self.assertEqual(stats['discard_junk_stats']['trends_hour_count'], 0)
def test_outdated_trends3(self): """ all existing hour stats should be removed, cause it's too old """ date_now = now() date_old = now() - relativedelta( days=get_var('TOPIC_TRENDS_HOUR_STATS_KEEP_DAYS'), hours=1) LOGGER.info( "11111111, %s, %s, %s" % (date_now, date_old, get_var('TOPIC_TRENDS_HOUR_STATS_KEEP_DAYS'))) self._make_laptops_and_icecream(_created=date_old) total_trends = ChannelTopicTrends.objects().count() hour_trends = total_trends / 2 day_trends = total_trends / 2 stats = purge_stats(self.channel) self.assertEqual(day_trends, 6) self.assertEqual(hour_trends, 6) self.assertEqual(stats['discard_junk_stats']['trends_day_count'], 0) self.assertEqual(stats['discard_junk_stats']['trends_hour_count'], 6)
def purge_all_channel_stats(channel): "Task to purge topics for current time" from solariat_bottle.utils.purging import purge_stats return purge_stats(channel)