File: test_slide_slide.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 (193 lines) | stat: -rw-r--r-- 8,459 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import psycopg2

from odoo.addons.website_slides.tests import common as slides_common
from odoo.tests.common import users
from odoo.tools import mute_logger


class TestSlideInternals(slides_common.SlidesCase):
    def test_compute_category_completion_time(self):
        """
            Check that we properly calculate the completion time of a course without error, after deleting a slide.
        """
        self.category2 = self.env['slide.slide'].with_user(self.user_officer).create({
            'name': 'Cooking Tips For Dieting',
            'channel_id': self.channel.id,
            'is_category': True,
            'is_published': True,
            'sequence': 5,
        })
        self.slide_4 = self.env['slide.slide'].with_user(self.user_officer).create({
            'name': 'Vegan Diet',
            'channel_id': self.channel.id,
            'slide_category': 'document',
            'is_published': True,
            'completion_time': 5.0,
            'sequence': 6,
        })
        self.slide_5 = self.env['slide.slide'].with_user(self.user_officer).create({
            'name': 'Normal Diet',
            'channel_id': self.channel.id,
            'slide_category': 'document',
            'is_published': True,
            'completion_time': 1.5,
            'sequence': 7,
        })

        before_unlink = self.category2.completion_time
        self.assertEqual(before_unlink, self.slide_4.completion_time + self.slide_5.completion_time)

        self.channel.slide_ids[6].sudo().unlink()
        self.category2._compute_category_completion_time()

        after_unlink = self.category2.completion_time
        self.assertEqual(after_unlink, self.slide_4.completion_time)

    @mute_logger('odoo.sql_db')
    @users('user_manager')
    def test_slide_create_vote_constraint(self):
        # test vote value must be 1, 0 and -1.
        with self.assertRaises(psycopg2.errors.CheckViolation), self.cr.savepoint():
            self.env['slide.slide.partner'].create({
                'slide_id': self.slide.id,
                'channel_id': self.channel.id,
                'partner_id': self.user_manager.partner_id.id,
                'vote': 2,
            })

    @users('user_manager')
    def test_slide_user_has_completed_category(self):
        # As an uncategorized slide doesn't have a category, the method should always return False
        uncategorized_slide = self.channel.slide_ids.filtered(lambda s: not s.is_category and not s.category_id)
        self.assertEqual(len(uncategorized_slide), 1)
        self.assertFalse(uncategorized_slide.user_has_completed)
        self.assertFalse(uncategorized_slide.user_has_completed_category)
        uncategorized_slide.user_has_completed = True
        self.assertFalse(uncategorized_slide.user_has_completed_category)

        category_slides = self.category.slide_ids
        self.assertEqual(len(category_slides), 2)
        # No slide completed in the category
        self.assertFalse(any(category_slides.mapped('user_has_completed')))
        self.assertFalse(category_slides[0].user_has_completed_category)
        # One slide completed in the category
        category_slides[0].user_has_completed = True
        self.assertFalse(category_slides[0].user_has_completed_category)
        # All slides completed in the category
        for slide in category_slides:
            slide.user_has_completed = True
        self.assertTrue(category_slides[0].user_has_completed_category)

    def test_change_content_type(self):
        """ To prevent constraint violation when changing type from video to article and vice-versa """
        slide = self.env['slide.slide'].with_context(website_slides_skip_fetch_metadata=True).create({
            'name': 'dummy',
            'channel_id': self.channel.id,
            'slide_category': 'video',
            'is_published': True,
            'url': 'https://youtu.be/W0JQcpGLSFw',
        })

        slide.write({'slide_category': 'article', 'html_content': '<p>Hello</p>'})
        self.assertTrue(slide.html_content)
        self.assertFalse(slide.url)

        slide.slide_category = 'document'
        self.assertFalse(slide.html_content)

class TestVideoFromURL(slides_common.SlidesCase):
    def test_video_youtube(self):
        youtube_urls = {
            'W0JQcpGLSFw': [
                'https://youtu.be/W0JQcpGLSFw',
                'https://www.youtube.com/watch?v=W0JQcpGLSFw',
                'https://www.youtube.com/watch?v=W0JQcpGLSFw&list=PL1-aSABtP6ACZuppkBqXFgzpNb2nVctZx',
            ],
            'vmhB-pt7EfA': [  # id starts with v, it is important
                'https://youtu.be/vmhB-pt7EfA',
                'https://www.youtube.com/watch?feature=youtu.be&v=vmhB-pt7EfA',
                'https://www.youtube.com/watch?v=vmhB-pt7EfA&list=PL1-aSABtP6ACZuppkBqXFgzpNb2nVctZx&index=7',
            ],
            'hlhLv0GN1hA': [
                'https://www.youtube.com/v/hlhLv0GN1hA',
                'https://www.youtube.com/embed/hlhLv0GN1hA',
                'https://www.youtube-nocookie.com/embed/hlhLv0GN1hA',
                'https://m.youtube.com/watch?v=hlhLv0GN1hA',
            ],
        }

        Slide = self.env['slide.slide'].with_context(website_slides_skip_fetch_metadata=True)

        # test various YouTube URL formats
        for youtube_id, urls in youtube_urls.items():
            for url in urls:
                with self.subTest(url=url, id=youtube_id):
                    slide = Slide.create({
                        'name': 'dummy',
                        'channel_id': self.channel.id,
                        'url': url,
                        'slide_category': 'video'
                    })
                    self.assertEqual('youtube', slide.video_source_type)
                    self.assertEqual(youtube_id, slide.youtube_id)

    def test_video_google_drive(self):
        google_drive_urls = {
            '1qU5nHVNbz_r84P_IS5kDzoCuC1h5ZAZR': [
                'https://drive.google.com/file/d/1qU5nHVNbz_r84P_IS5kDzoCuC1h5ZAZR/view?usp=sharing',
                'https://drive.google.com/file/d/1qU5nHVNbz_r84P_IS5kDzoCuC1h5ZAZR',
            ],
        }

        Slide = self.env['slide.slide'].with_context(website_slides_skip_fetch_metadata=True)

        # test various Google Drive URL formats
        for google_drive_id, urls in google_drive_urls.items():
            for url in urls:
                with self.subTest(url=url, id=google_drive_id):
                    slide = Slide.create({
                        'name': 'dummy',
                        'channel_id': self.channel.id,
                        'url': url,
                        'slide_category': 'video'
                    })
                    self.assertEqual('google_drive', slide.video_source_type)
                    self.assertEqual(google_drive_id, slide.google_drive_id)

    def test_video_vimeo(self):
        vimeo_urls = {
            # regular URL from Vimeo
            '545859999': [
                'https://vimeo.com/545859999',
                'https://vimeo.com/545859999?autoplay=1',
            ],
            # test channel URL from Vimeo
            '551979139': [
                'https://vimeo.com/channels/staffpicks/551979139',
                'https://vimeo.com/channels/staffpicks/551979139?autoplay=1',
            ],
            # test URL from Vimeo with setting 'with URL only'
            # we need to store both the ID and the token, see '_compute_embed_code' method for details
            '545859999/94dd03ddb0': [
                'https://vimeo.com/545859999/94dd03ddb0',
                'https://vimeo.com/545859999/94dd03ddb0?autoplay=1',
            ],
        }

        Slide = self.env['slide.slide'].with_context(website_slides_skip_fetch_metadata=True)

        # test various Vimeo URL formats
        for vimeo_id, urls in vimeo_urls.items():
            for url in urls:
                with self.subTest(url=url, id=vimeo_id):
                    slide = Slide.create({
                        'name': 'dummy',
                        'channel_id': self.channel.id,
                        'url': url,
                        'slide_category': 'video'
                    })
                    self.assertEqual('vimeo', slide.video_source_type)
                    self.assertEqual(vimeo_id, slide.vimeo_id)