def add_stream_item(user_or_users, content, created_at=None): """ Add a single message to the stream of one or more users. """ backend = get_backend() users = create_iterable(user_or_users) created_at = created_at or datetime_now() backend.add_stream_item(users, content, created_at)
def test_message_ordering(self): user = User.objects.create() now = datetime_now() add_stream_item(user, 'Message 1', created_at=now) add_stream_item(user, 'Message 2', created_at=now + timedelta(minutes=1)) add_stream_item(user, 'Message 3', created_at=now + timedelta(minutes=2)) stream_items = get_stream_items(user) self.assertEqual(stream_items[0].content, 'Message 3') self.assertEqual(stream_items[1].content, 'Message 2') self.assertEqual(stream_items[2].content, 'Message 1')
def test_slicing(self): user = User.objects.create() now = datetime_now() for count in range(10): created_at = now + timedelta(minutes=count) add_stream_item(user, 'Message %s' % count, created_at=created_at) stream_items = get_stream_items(user) first_five = stream_items[:5] self.assertEqual(len(first_five), 5) self.assertEqual(first_five[0].content, 'Message 9') self.assertEqual(first_five[4].content, 'Message 5') middle = stream_items[3:7] self.assertEqual(len(middle), 4) self.assertEqual(middle[0].content, 'Message 6') self.assertEqual(middle[3].content, 'Message 3') end = stream_items[6:] self.assertEqual(len(end), 4) self.assertEqual(end[0].content, 'Message 3') self.assertEqual(end[3].content, 'Message 0')
def test_pagination(self): user = User.objects.create() now = datetime_now() for count in range(100): created_at = now + timedelta(minutes=count) add_stream_item(user, 'Message %s' % count, created_at=created_at) paginator = Paginator(get_stream_items(user), 10) self.assertEqual(paginator.num_pages, 10) page_1 = paginator.page(1) objects = page_1.object_list self.assertEqual(len(objects), 10) self.assertEqual(objects[0].content, 'Message 99') self.assertEqual(objects[9].content, 'Message 90') self.assertEqual(page_1.next_page_number(), 2) page_10 = paginator.page(10) objects = page_10.object_list self.assertEqual(len(objects), 10) self.assertEqual(objects[0].content, 'Message 9') self.assertEqual(objects[9].content, 'Message 0') self.assertFalse(page_10.has_next())