def test_unread_conversations(self): user = User('user', '123456') conv1 = Conversation(user, 'first conversation') conv1.messages.append(Message(user, 'first conversation message')) conv2 = Conversation(user, 'second conversation') conv2.messages.append(Message(user, 'second conversation message')) db.session.add(user) db.session.commit() user.set_last_read_message(conv2, conv2.messages[-1].id) db.session.commit() assert len(conv1.get_unread_messages(user)) == 1 assert len(conv2.get_unread_messages(user)) == 0 conversations = user.get_unread_conversations() assert len(conversations) == 1 assert conversations[0].title == 'first conversation'
class UnreadTests(Base): def setUp(self): super(UnreadTests, self).setUp() self.user1 = User('self.user1', '123456') self.user2 = User('self.user2', '123456') self.conv = Conversation(self.user1, 'some title') self.conv.messages.append(Message(self.user2, 'first message')) self.conv.messages.append(Message(self.user2, 'second message')) self.conv.messages.append(Message(self.user1, 'third message')) self.conv.messages.append(Message(self.user2, 'fourth message')) db.session.add_all([self.user1, self.user2]) db.session.commit() def test_all_unread(self): messages = self.conv.get_unread_messages(for_user=self.user1) assert len(messages) == 4 messages = self.conv.get_unread_messages(for_user=self.user2) assert len(messages) == 4 def test_no_unread(self): self.user1.set_last_read_message(self.conv, message_id=4) db.session.commit() messages = self.conv.get_unread_messages(for_user=self.user1) assert len(messages) == 0 def test_some_unread(self): self.user1.set_last_read_message(self.conv, message_id=2) self.user1.set_last_read_message(self.conv, message_id=3) user1messages = self.conv.get_unread_messages(for_user=self.user1) user2messages = self.conv.get_unread_messages(for_user=self.user2) [m.text for m in user1messages] == ['third message', 'fourth message'] [m.text for m in user2messages] == ['fourth message'] def test_mark_read(self): self.conv.mark_read(self.user1) db.session.commit() user1messages = self.conv.get_unread_messages(for_user=self.user1) assert len(user1messages) == 0