File: test_forum_post.py

package info (click to toggle)
odoo 18.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 878,716 kB
  • sloc: javascript: 927,937; python: 685,670; xml: 388,524; sh: 1,033; sql: 415; makefile: 26
file content (38 lines) | stat: -rw-r--r-- 2,049 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from odoo import fields
from odoo.addons.website_forum.tests.common import TestForumCommon


class TestForumPost(TestForumCommon):

    def test_get_related_posts(self):
        """Test the method returns 5 related posts based on tag similarity."""
        # Create forum posts and associate them tags
        forum_tags = self.env['forum.tag'].create([{
                'name': f'tag_{i}',
                'forum_id': self.forum.id,
            }
            for i in range(10)])
        forum_posts = self.env['forum.post'].create([{
                    'content': 'A post ...',
                    'forum_id': self.forum.id,
                    'name': 'Post...',
                    'tag_ids': forum_tags[:i],
                    'last_activity_date': fields.Datetime.subtract(fields.Datetime.now(), days=i)
                }
                for i in range(len(forum_tags) + 1)  # 11 posts with 0 to 10 tags
            ])
        # First post (no tags), should return an empty record set
        self.assertFalse(forum_posts[0].tag_ids)
        self.assertEqual(forum_posts[0]._get_related_posts(), self.env['forum.post'])
        # Second post, most similar posts should be the 5 following posts
        self.assertEqual(forum_posts[1]._get_related_posts(), forum_posts[2:7])
        # Last post, most similar posts should be the 5 preceding posts in descending order
        self.assertEqual(forum_posts[-1]._get_related_posts(),
                            forum_posts[len(forum_posts) - 6: -1][::-1])
        # A post with a unique tag, should return an empty record set
        self.assertEqual(self.post.tag_ids.post_ids, self.post)
        self.assertEqual(forum_posts[0]._get_related_posts(), self.env['forum.post'])
        # Related posts with the same list of tags are sorted by last activity date in descending order
        forum_posts.write({'tag_ids': forum_tags.ids})
        self.assertEqual(forum_posts[0]._get_related_posts(), forum_posts[1:6])
        self.assertEqual(forum_posts[-1]._get_related_posts(), forum_posts[:5])