def test_set(self): """set allows for changing count of threads""" session = {} counter = NewThreadsCount(self.user, session) counter.set(128) self.assertEqual(int(counter), 128) self.assertEqual(session[counter.name]['threads'], 128)
def test_cast_to_bool(self): """counter is castable to bool""" counter = NewThreadsCount(self.user, {}) self.assertFalse(counter) threads = [testutils.post_thread(self.forum) for t in xrange(42)] counter = NewThreadsCount(self.user, {}) self.assertTrue(counter)
def test_decrease_zero(self): """decrease is not allowing for negative threads counts""" session = {} counter = NewThreadsCount(self.user, session) counter.decrease() self.assertEqual(int(counter), 0) self.assertEqual(session[counter.name]['threads'], 0)
def test_cast_to_int(self): """counter is castable to int""" counter = NewThreadsCount(self.user, {}) self.assertEqual(int(counter), 0) threads = [testutils.post_thread(self.forum) for t in xrange(42)] counter = NewThreadsCount(self.user, {}) self.assertEqual(int(counter), 42)
def test_get_real_count(self): """get_real_count returns valid count of new threads""" counter = NewThreadsCount(self.user, {}) self.assertEqual(counter.count, 0) self.assertEqual(counter.get_real_count()['threads'], 0) # create 10 new threads threads = [testutils.post_thread(self.forum) for t in xrange(10)] self.assertEqual(counter.get_real_count()['threads'], 10) # create new counter counter = NewThreadsCount(self.user, {}) self.assertEqual(counter.count, 10) self.assertEqual(counter.get_real_count()['threads'], 10)
def process_request(self, request): if request.user.is_authenticated(): if request.user.acl['moderated_forums']: request.user.moderated_content = ModeratedCount( request.user, request.session) request.user.new_threads = NewThreadsCount(request.user, request.session) request.user.unread_threads = UnreadThreadsCount( request.user, request.session)
def test_is_cache_valid(self): """is_cache_valid returns valid value for different caches""" counter = NewThreadsCount(self.user, {}) self.assertTrue(counter.is_cache_valid({ 'expires': time() + 15, 'user': self.user.pk })) self.assertFalse(counter.is_cache_valid({ 'expires': time() - 15, 'user': self.user.pk })) self.assertFalse(counter.is_cache_valid({ 'expires': time() + 15, 'user': self.user.pk + 1 }))
def test_is_cache_valid(self): """is_cache_valid returns valid value for different caches""" counter = NewThreadsCount(self.user, {}) self.assertTrue( counter.is_cache_valid({ 'expires': time() + 15, 'user': self.user.pk })) self.assertFalse( counter.is_cache_valid({ 'expires': time() - 15, 'user': self.user.pk })) self.assertFalse( counter.is_cache_valid({ 'expires': time() + 15, 'user': self.user.pk + 1 }))
def process_request(self, request): if request.user.is_authenticated(): if request.user.acl['can_review_moderated_content']: request.user.moderated_content = ModeratedCount( request.user, request.session) request.user.new_threads = NewThreadsCount(request.user, request.session) request.user.unread_threads = UnreadThreadsCount( request.user, request.session) if request.user.acl['can_use_private_threads']: # special case: count unread threads sync_user_unread_private_threads_count(request.user)
def test_get_current_count_dict(self): """get_current_count_dict returns valid count of new threads""" counter = NewThreadsCount(self.user, {}) self.assertEqual(counter.count, 0) self.assertEqual(counter.get_current_count_dict()['threads'], 0) # create 10 new threads threads = [testutils.post_thread(self.forum) for t in xrange(10)] self.assertEqual(counter.get_current_count_dict()['threads'], 10) # create new counter counter = NewThreadsCount(self.user, {}) self.assertEqual(counter.count, 10) self.assertEqual(counter.get_current_count_dict()['threads'], 10)
def test_get_expiration_timestamp(self): """get_expiration_timestamp returns greater time than current one""" counter = NewThreadsCount(self.user, {}) self.assertTrue(counter.get_expiration_timestamp() > time())
def process_request(self, request): if request.user.is_authenticated(): request.user.new_threads = NewThreadsCount(request.user, request.session) request.user.unread_threads = UnreadThreadsCount( request.user, request.session)