File: test_thumbnails.py

package info (click to toggle)
beets 2.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,536 kB
  • sloc: python: 45,608; javascript: 7,997; xml: 334; sh: 261; makefile: 119
file content (287 lines) | stat: -rw-r--r-- 10,181 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# This file is part of beets.
# Copyright 2016, Bruno Cauet
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.


import os.path
from shutil import rmtree
from tempfile import mkdtemp
from unittest.mock import Mock, call, patch

import pytest

from beets.test.helper import BeetsTestCase
from beets.util import bytestring_path, syspath
from beetsplug.thumbnails import (
    LARGE_DIR,
    NORMAL_DIR,
    GioURI,
    PathlibURI,
    ThumbnailsPlugin,
)


class ThumbnailsTest(BeetsTestCase):
    @patch("beetsplug.thumbnails.ArtResizer")
    @patch("beetsplug.thumbnails.ThumbnailsPlugin._check_local_ok", Mock())
    @patch("beetsplug.thumbnails.os.stat")
    def test_add_tags(self, mock_stat, mock_artresizer):
        plugin = ThumbnailsPlugin()
        plugin.get_uri = Mock(
            side_effect={b"/path/to/cover": "COVER_URI"}.__getitem__
        )
        album = Mock(artpath=b"/path/to/cover")
        mock_stat.return_value.st_mtime = 12345

        plugin.add_tags(album, b"/path/to/thumbnail")

        metadata = {"Thumb::URI": "COVER_URI", "Thumb::MTime": "12345"}
        mock_artresizer.shared.write_metadata.assert_called_once_with(
            b"/path/to/thumbnail",
            metadata,
        )
        mock_stat.assert_called_once_with(syspath(album.artpath))

    @patch("beetsplug.thumbnails.os")
    @patch("beetsplug.thumbnails.ArtResizer")
    @patch("beetsplug.thumbnails.GioURI")
    def test_check_local_ok(self, mock_giouri, mock_artresizer, mock_os):
        # test local resizing capability
        mock_artresizer.shared.local = False
        mock_artresizer.shared.can_write_metadata = False
        plugin = ThumbnailsPlugin()
        assert not plugin._check_local_ok()

        # test dirs creation
        mock_artresizer.shared.local = True
        mock_artresizer.shared.can_write_metadata = True

        def exists(path):
            if path == syspath(NORMAL_DIR):
                return False
            if path == syspath(LARGE_DIR):
                return True
            raise ValueError(f"unexpected path {path!r}")

        mock_os.path.exists = exists
        plugin = ThumbnailsPlugin()
        mock_os.makedirs.assert_called_once_with(syspath(NORMAL_DIR))
        assert plugin._check_local_ok()

        # test metadata writer function
        mock_os.path.exists = lambda _: True

        mock_artresizer.shared.local = True
        mock_artresizer.shared.can_write_metadata = False
        with pytest.raises(RuntimeError):
            ThumbnailsPlugin()

        mock_artresizer.shared.local = True
        mock_artresizer.shared.can_write_metadata = True
        assert ThumbnailsPlugin()._check_local_ok()

        # test URI getter function
        giouri_inst = mock_giouri.return_value
        giouri_inst.available = True
        assert ThumbnailsPlugin().get_uri == giouri_inst.uri

        giouri_inst.available = False
        assert ThumbnailsPlugin().get_uri.__self__.__class__ == PathlibURI

    @patch("beetsplug.thumbnails.ThumbnailsPlugin._check_local_ok", Mock())
    @patch("beetsplug.thumbnails.ArtResizer")
    @patch("beetsplug.thumbnails.util")
    @patch("beetsplug.thumbnails.os")
    @patch("beetsplug.thumbnails.shutil")
    def test_make_cover_thumbnail(
        self, mock_shutils, mock_os, mock_util, mock_artresizer
    ):
        thumbnail_dir = os.path.normpath(b"/thumbnail/dir")
        md5_file = os.path.join(thumbnail_dir, b"md5")
        path_to_art = os.path.normpath(b"/path/to/art")
        path_to_resized_art = os.path.normpath(b"/path/to/resized/artwork")

        mock_os.path.join = os.path.join  # don't mock that function
        plugin = ThumbnailsPlugin()
        plugin.add_tags = Mock()

        album = Mock(artpath=path_to_art)
        mock_util.syspath.side_effect = lambda x: x
        plugin.thumbnail_file_name = Mock(return_value=b"md5")
        mock_os.path.exists.return_value = False

        def os_stat(target):
            if target == syspath(md5_file):
                return Mock(st_mtime=1)
            elif target == syspath(path_to_art):
                return Mock(st_mtime=2)
            else:
                raise ValueError(f"invalid target {target}")

        mock_os.stat.side_effect = os_stat

        mock_resize = mock_artresizer.shared.resize
        mock_resize.return_value = path_to_resized_art

        plugin.make_cover_thumbnail(album, 12345, thumbnail_dir)

        mock_os.path.exists.assert_called_once_with(syspath(md5_file))

        mock_resize.assert_called_once_with(12345, path_to_art, md5_file)
        plugin.add_tags.assert_called_once_with(album, path_to_resized_art)
        mock_shutils.move.assert_called_once_with(
            syspath(path_to_resized_art), syspath(md5_file)
        )

        # now test with recent thumbnail & with force
        mock_os.path.exists.return_value = True
        plugin.force = False
        mock_resize.reset_mock()

        def os_stat(target):
            if target == syspath(md5_file):
                return Mock(st_mtime=3)
            elif target == syspath(path_to_art):
                return Mock(st_mtime=2)
            else:
                raise ValueError(f"invalid target {target}")

        mock_os.stat.side_effect = os_stat

        plugin.make_cover_thumbnail(album, 12345, thumbnail_dir)
        assert mock_resize.call_count == 0

        # and with force
        plugin.config["force"] = True
        plugin.make_cover_thumbnail(album, 12345, thumbnail_dir)
        mock_resize.assert_called_once_with(12345, path_to_art, md5_file)

    @patch("beetsplug.thumbnails.ThumbnailsPlugin._check_local_ok", Mock())
    def test_make_dolphin_cover_thumbnail(self):
        plugin = ThumbnailsPlugin()
        tmp = bytestring_path(mkdtemp())
        album = Mock(path=tmp, artpath=os.path.join(tmp, b"cover.jpg"))
        plugin.make_dolphin_cover_thumbnail(album)
        with open(os.path.join(tmp, b".directory"), "rb") as f:
            assert f.read().splitlines() == [
                b"[Desktop Entry]",
                b"Icon=./cover.jpg",
            ]

        # not rewritten when it already exists (yup that's a big limitation)
        album.artpath = b"/my/awesome/art.tiff"
        plugin.make_dolphin_cover_thumbnail(album)
        with open(os.path.join(tmp, b".directory"), "rb") as f:
            assert f.read().splitlines() == [
                b"[Desktop Entry]",
                b"Icon=./cover.jpg",
            ]

        rmtree(syspath(tmp))

    @patch("beetsplug.thumbnails.ThumbnailsPlugin._check_local_ok", Mock())
    @patch("beetsplug.thumbnails.ArtResizer")
    def test_process_album(self, mock_artresizer):
        get_size = mock_artresizer.shared.get_size

        plugin = ThumbnailsPlugin()
        make_cover = plugin.make_cover_thumbnail = Mock(return_value=True)
        make_dolphin = plugin.make_dolphin_cover_thumbnail = Mock()

        # no art
        album = Mock(artpath=None)
        plugin.process_album(album)
        assert get_size.call_count == 0
        assert make_dolphin.call_count == 0

        # cannot get art size
        album.artpath = b"/path/to/art"
        get_size.return_value = None
        plugin.process_album(album)
        get_size.assert_called_once_with(b"/path/to/art")
        assert make_cover.call_count == 0

        # dolphin tests
        plugin.config["dolphin"] = False
        plugin.process_album(album)
        assert make_dolphin.call_count == 0

        plugin.config["dolphin"] = True
        plugin.process_album(album)
        make_dolphin.assert_called_once_with(album)

        # small art
        get_size.return_value = 200, 200
        plugin.process_album(album)
        make_cover.assert_called_once_with(album, 128, NORMAL_DIR)

        # big art
        make_cover.reset_mock()
        get_size.return_value = 500, 500
        plugin.process_album(album)
        make_cover.assert_has_calls(
            [call(album, 128, NORMAL_DIR), call(album, 256, LARGE_DIR)],
            any_order=True,
        )

    @patch("beetsplug.thumbnails.ThumbnailsPlugin._check_local_ok", Mock())
    @patch("beetsplug.thumbnails.decargs")
    def test_invokations(self, mock_decargs):
        plugin = ThumbnailsPlugin()
        plugin.process_album = Mock()
        album = Mock()

        plugin.process_album.reset_mock()
        lib = Mock()
        album2 = Mock()
        lib.albums.return_value = [album, album2]
        plugin.process_query(lib, Mock(), None)
        lib.albums.assert_called_once_with(mock_decargs.return_value)
        plugin.process_album.assert_has_calls(
            [call(album), call(album2)], any_order=True
        )

    @patch("beetsplug.thumbnails.BaseDirectory")
    def test_thumbnail_file_name(self, mock_basedir):
        plug = ThumbnailsPlugin()
        plug.get_uri = Mock(return_value="file:///my/uri")
        assert (
            plug.thumbnail_file_name(b"idontcare")
            == b"9488f5797fbe12ffb316d607dfd93d04.png"
        )

    def test_uri(self):
        gio = GioURI()
        if not gio.available:
            self.skipTest("GIO library not found")

        import ctypes

        with pytest.raises(ctypes.ArgumentError):
            gio.uri("/foo")
        assert gio.uri(b"/foo") == "file:///foo"
        assert gio.uri(b"/foo!") == "file:///foo!"
        assert (
            gio.uri(b"/music/\xec\x8b\xb8\xec\x9d\xb4")
            == "file:///music/%EC%8B%B8%EC%9D%B4"
        )


class TestPathlibURI:
    """Test PathlibURI class"""

    def test_uri(self):
        test_uri = PathlibURI()

        # test it won't break if we pass it bytes for a path
        test_uri.uri(b"/")