diff --git a/addons/mail/tests/test_mail.py b/addons/mail/tests/test_mail.py index 9fdabca315c..7668d743eab 100644 --- a/addons/mail/tests/test_mail.py +++ b/addons/mail/tests/test_mail.py @@ -526,10 +526,25 @@ class test_mail(TestMailMockups): def test_30_message_read(self): """ Tests for message_read and expandables. """ + + def _print_debug(message, debug=True, prefix=''): + if debug: + print prefix, message + + def _print_msg(message, debug=True, prefix=''): + if not debug: + return + if message.get('type') == 'expandable': + print prefix, 'expandable', message + else: + print prefix, 'message', message.get('body'), message.get('id'), message.get('ancestor_id') + + _debug = False cr, uid, user_admin, group_pigs = self.cr, self.uid, self.user_admin, self.group_pigs pigs_domain = [('model', '=', 'mail.group'), ('res_id', '=', self.group_pigs_id)] # Data: create a discussion in Pigs (2 messages, one with 2 and one with 3 answers) + msg_id0 = self.group_pigs.message_post(body='0', subtype='mt_comment') msg_id1 = self.group_pigs.message_post(body='1', subtype='mt_comment') msg_id2 = self.group_pigs.message_post(body='2', subtype='mt_comment') msg_id3 = self.group_pigs.message_post(body='1-1', subtype='mt_comment', parent_id=msg_id1) @@ -540,40 +555,153 @@ class test_mail(TestMailMockups): msg_id8 = self.group_pigs.message_post(body='2-1-1', subtype='mt_comment', parent_id=msg_id4) msg_id9 = self.group_pigs.message_post(body='1-1-1', subtype='mt_comment', parent_id=msg_id3) msg_id10 = self.group_pigs.message_post(body='2-1-1', subtype='mt_comment', parent_id=msg_id4) - msg_ids = [msg_id1, msg_id2, msg_id3, msg_id4, msg_id5, msg_id6, msg_id7, msg_id8, msg_id9, msg_id10] + msg_ids = [msg_id0, msg_id1, msg_id2, msg_id3, msg_id4, msg_id5, msg_id6, msg_id7, msg_id8, msg_id9, msg_id10] # Test: read some specific ids read_msg_list = self.mail_message.message_read(cr, uid, ids=msg_ids[2:4], domain=[('body', 'like', 'dummy')]) read_msg_ids = [msg.get('id') for msg in read_msg_list] self.assertEqual(msg_ids[2:4], read_msg_ids, 'message_read with direct ids should read only the requested ids') - # Test: read messages of Pigs through a domain + # Test: read messages of Pigs through a domain, being thread or not threaded read_msg_list = self.mail_message.message_read(cr, uid, domain=pigs_domain, limit=200) read_msg_ids = [msg.get('id') for msg in read_msg_list] - self.assertEqual(msg_ids, read_msg_ids, 'message_read with domain on Pigs should equal all messages of Pigs') + self.assertEqual(msg_ids, read_msg_ids, 'message_read flat with domain on Pigs should equal all messages of Pigs') + read_msg_list = self.mail_message.message_read(cr, uid, domain=pigs_domain, limit=200, thread_level=1) + read_msg_ids = [msg.get('id') for msg in read_msg_list] + self.assertEqual(msg_ids, read_msg_ids, 'message_read threaded with domain on Pigs should equal all messages of Pigs') - # Test: read last message, check expandables - read_msg_list = self.mail_message.message_read(cr, uid, domain=pigs_domain, limit=1) + # ---------------------------------------- + # CASE1: message_read with domain, threaded + # We simulate an entire flow, using the expandables to test them + # ---------------------------------------- + _print_debug('\nCASE1: message_read with domain, threaded\n', _debug) + + # Do: read last message, threaded + read_msg_list = self.mail_message.message_read(cr, uid, domain=pigs_domain, limit=1, thread_level=1) read_msg_ids = [msg.get('id') for msg in read_msg_list if msg.get('type') != 'expandable'] - # Test: parent is added to the read messages + # Test: structure content, ancestor is added to the read messages, ordered by id, ancestor is set, 2 expandables + self.assertEqual(len(read_msg_list), 4, 'message_read on last Pigs message should return 2 messages and 2 expandables') self.assertEqual(set([msg_id2, msg_id10]), set(read_msg_ids), 'message_read on the last Pigs message should also get its parent') - print read_msg_ids - # Test: expandables - # 1. expandable for childs of Body2 before the last reply - # 2. expandable for messages before Body2 + self.assertEqual(read_msg_list[1].get('ancestor_id'), read_msg_list[0].get('id'), 'message_read should set the ancestor to the thread header') + # Data: get expandables + new_threads_exp, new_msg_exp = None, None for msg in read_msg_list: - if msg.get('type') == 'expandable': - print msg - # 'read more threads' expandable + _print_msg(msg, _debug) if msg.get('type') == 'expandable' and msg.get('nb_messages') == -1 and msg.get('id') == -1: - domain = msg.get('domain', []) - self.assertEqual(set(domain), set(pigs_domain), 'general expandable domain should equal the message_read domain parameter') - # 'read more messages' expandables + new_threads_exp = msg elif msg.get('type') == 'expandable': - domain = msg.get('domain', []) - self.assertIn(('id', 'child_of', msg_id2), domain, 'thread expandable domain should contain a child_of condition') - self.assertIn(('id', '>=', msg_id4), domain, 'thread expandable domain should contain a child_of condition') - self.assertIn(('id', '<=', msg_id8), domain, 'thread expandable domain should contain a child_of condition') + new_msg_exp = msg + + # Do: fetch new messages in first thread, domain from expandable + self.assertIsNotNone(new_msg_exp, 'message_read on last Pigs message should have returned a new messages expandable') + domain = new_msg_exp.get('domain', []) + # Test: expandable, conditions in domain + _print_debug('Executing %s' % domain, _debug) + self.assertIn(('id', 'child_of', msg_id2), domain, 'new messages expandable domain should contain a child_of condition') + self.assertIn(('id', '>=', msg_id4), domain, 'new messages expandable domain should contain an id greater than condition') + self.assertIn(('id', '<=', msg_id8), domain, 'new messages expandable domain should contain an id less than condition') + self.assertEqual(new_msg_exp.get('ancestor_id'), msg_id2, 'new messages expandable should have ancestor_id set to the thread header') + # Do: message_read with domain, thread_level=0, parent_id=msg_id2 (should be imposed by JS) + read_msg_list = self.mail_message.message_read(cr, uid, domain=domain, limit=200, thread_level=0, parent_id=msg_id2) + for msg in read_msg_list: + _print_msg(msg, _debug) + read_msg_ids = [msg.get('id') for msg in read_msg_list if msg.get('type') != 'expandable'] + # Test: other message in thread have been fetch + self.assertEqual(set([msg_id4, msg_id6, msg_id8]), set(read_msg_ids), 'message_read in Pigs thread should return all the previous messages') + + # Do: fetch a new thread, domain from expandable + self.assertIsNotNone(new_threads_exp, 'message_read on last Pigs message should have returned a new threads expandable') + domain = new_threads_exp.get('domain', []) + # Test: expandable, conditions in domain + _print_debug('Executing %s' % domain, _debug) + for condition in pigs_domain: + self.assertIn(condition, domain, 'new threads expandable domain should contain the message_read domain parameter') + self.assertFalse(new_threads_exp.get('ancestor_id'), 'new threads expandable should not have an ancestor_id') + # Do: message_read with domain, thread_level=1 (should be imposed by JS) + read_msg_list = self.mail_message.message_read(cr, uid, domain=domain, limit=1, thread_level=1) + read_msg_ids = [msg.get('id') for msg in read_msg_list if msg.get('type') != 'expandable'] + # Test: structure content, ancestor is added to the read messages, ordered by id, ancestor is set, 2 expandables + self.assertEqual(len(read_msg_list), 4, 'message_read on Pigs should return 2 messages and 2 expandables') + self.assertEqual(set([msg_id1, msg_id9]), set(read_msg_ids), 'message_read on a Pigs message should also get its parent') + self.assertEqual(read_msg_list[1].get('ancestor_id'), read_msg_list[0].get('id'), 'message_read should set the ancestor to the thread header') + # Data: get expandables + new_threads_exp, new_msg_exp = None, None + for msg in read_msg_list: + _print_msg(msg, _debug) + if msg.get('type') == 'expandable' and msg.get('nb_messages') == -1 and msg.get('id') == -1: + new_threads_exp = msg + elif msg.get('type') == 'expandable': + new_msg_exp = msg + + # Do: fetch new messages in second thread, domain from expandable + self.assertIsNotNone(new_msg_exp, 'message_read on Pigs message should have returned a new messages expandable') + domain = new_msg_exp.get('domain', []) + # Test: expandable, conditions in domain + _print_debug('Executing %s' % domain, _debug) + self.assertIn(('id', 'child_of', msg_id1), domain, 'new messages expandable domain should contain a child_of condition') + self.assertIn(('id', '>=', msg_id3), domain, 'new messages expandable domain should contain an id greater than condition') + self.assertIn(('id', '<=', msg_id7), domain, 'new messages expandable domain should contain an id less than condition') + self.assertEqual(new_msg_exp.get('ancestor_id'), msg_id1, 'new messages expandable should have ancestor_id set to the thread header') + # Do: message_read with domain, thread_level=0, parent_id=msg_id1 (should be imposed by JS) + read_msg_list = self.mail_message.message_read(cr, uid, domain=domain, limit=200, thread_level=0, parent_id=msg_id1) + for msg in read_msg_list: + _print_msg(msg, _debug) + read_msg_ids = [msg.get('id') for msg in read_msg_list if msg.get('type') != 'expandable'] + # Test: other message in thread have been fetch + self.assertEqual(set([msg_id3, msg_id5, msg_id7]), set(read_msg_ids), 'message_read on the last Pigs message should also get its parent') + + # Test: fetch a new thread, domain from expandable + self.assertIsNotNone(new_threads_exp, 'message_read should have returned a new threads expandable') + domain = new_threads_exp.get('domain', []) + # Test: expandable, conditions in domain + _print_debug('Executing %s' % domain, _debug) + for condition in pigs_domain: + self.assertIn(condition, domain, 'general expandable domain should contain the message_read domain parameter') + # Do: message_read with domain, thread_level=1 (should be imposed by JS) + read_msg_list = self.mail_message.message_read(cr, uid, domain=domain, limit=1, thread_level=1) + read_msg_ids = [msg.get('id') for msg in read_msg_list if msg.get('type') != 'expandable'] + # Test: structure content, ancestor is added to the read messages, ordered by id, ancestor is set, 2 expandables + self.assertEqual(len(read_msg_list), 1, 'message_read on Pigs should return 1 message because everything else has been fetched') + self.assertEqual([msg_id0], read_msg_ids, 'message_read after 2 More should return only 1 last message') + for msg in read_msg_list: + _print_msg(msg, _debug) + + # ---------------------------------------- + # CASE2: message_read with domain, flat + # ---------------------------------------- + _print_debug('\nCASE2: message_read with domain, flat\n', _debug) + + # Do: read 2 lasts message, flat + read_msg_list = self.mail_message.message_read(cr, uid, domain=pigs_domain, limit=2, thread_level=0) + read_msg_ids = [msg.get('id') for msg in read_msg_list if msg.get('type') != 'expandable'] + # Test: structure content, ancestor is added to the read messages, ordered by id, ancestor is not set, 1 expandable + self.assertEqual(len(read_msg_list), 3, 'message_read on last Pigs message should return 2 messages and 1 expandable') + self.assertEqual(set([msg_id9, msg_id10]), set(read_msg_ids), 'message_read flat on Pigs last messages should only return those messages') + self.assertFalse(read_msg_list[0].get('ancestor_id'), 'message_read flat should set the ancestor as False') + self.assertFalse(read_msg_list[1].get('ancestor_id'), 'message_read flat should set the ancestor as False') + # Data: get expandables + new_threads_exp, new_msg_exp = None, None + for msg in read_msg_list: + _print_msg(msg, _debug) + if msg.get('type') == 'expandable' and msg.get('nb_messages') == -1 and msg.get('id') == -1: + new_threads_exp = msg + + # Do: fetch new messages, domain from expandable + self.assertIsNotNone(new_threads_exp, 'message_read flat on the 2 last Pigs messages should have returns a new threads expandable') + domain = new_threads_exp.get('domain', []) + # Test: expandable, conditions in domain + _print_debug('Executing %s' % domain, _debug) + for condition in pigs_domain: + self.assertIn(condition, domain, 'new threads expandable domain should contain the message_read domain parameter') + # Do: message_read with domain, thread_level=0 (should be imposed by JS) + read_msg_list = self.mail_message.message_read(cr, uid, domain=domain, limit=20, thread_level=0) + read_msg_ids = [msg.get('id') for msg in read_msg_list if msg.get('type') != 'expandable'] + # Test: structure content, ancestor is added to the read messages, ordered by id, ancestor is set, 2 expandables + self.assertEqual(len(read_msg_list), 9, 'message_read on Pigs should return 9 messages and 0 expandable') + self.assertEqual([msg_id0, msg_id1, msg_id2, msg_id3, msg_id4, msg_id5, msg_id6, msg_id7, msg_id8], read_msg_ids, + 'message_read, More on flat, should return all remaning messages') + for msg in read_msg_list: + _print_msg(msg, _debug) def test_40_needaction(self): """ Tests for mail.message needaction. """