Debian Codegraph @

test/plugins/test_importadded.py

raw
1 """Tests for the `importadded` plugin."""
2
3 from __future__ import annotations
4
5 import os
6 from pathlib import Path
7 from typing import TYPE_CHECKING
8
9 import pytest
10
11 from beets import importer
12 from beets.test.helper import AutotagImportTestCase, PluginMixin
13
14 if TYPE_CHECKING:
15 from collections.abc import Iterable
16
17
18 def modify_mtimes(paths: Iterable[Path], offset=-60000):
19 for i, path in enumerate(paths, start=1):
20 mstat = path.stat()
21 os.utime(path, (mstat.st_atime, mstat.st_mtime + offset * i))
22
23
24 class ImportAddedTest(PluginMixin, AutotagImportTestCase):
25 # The minimum mtime of the files to be imported
26 plugin = "importadded"
27 min_mtime = None
28
29 def setUp(self):
30 super().setUp()
31 self.prepare_album_for_import(2)
32 # Different mtimes on the files to be imported in order to test the
33 # plugin
34 paths = [Path(mfile.path) for mfile in self.import_media]
35 modify_mtimes(paths)
36 self.min_mtime = min(p.stat().st_mtime for p in paths)
37 self.importer = self.setup_importer()
38 self.importer.add_choice(importer.Action.APPLY)
39
40 def find_media_file_mtime(self, item) -> float:
41 """Find the pre-import MediaFile for an Item"""
42 for m in self.import_media:
43 if m.title.replace("Tag", "Applied") == item.title:
44 return Path(m.path).stat().st_mtime
45 raise AssertionError(f"No MediaFile found for Item {item.filepath}")
46
47 def test_import_album_with_added_dates(self):
48 self.importer.run()
49
50 album = self.lib.albums().get()
51 assert album.added == self.min_mtime
52 for item in album.items():
53 assert item.added == self.min_mtime
54
55 def test_import_album_inplace_with_added_dates(self):
56 self.config["import"]["copy"] = False
57
58 self.importer.run()
59
60 album = self.lib.albums().get()
61 assert album.added == self.min_mtime
62 for item in album.items():
63 assert item.added == self.min_mtime
64
65 def test_import_album_with_preserved_mtimes(self):
66 self.config["importadded"]["preserve_mtimes"] = True
67 self.importer.run()
68 album = self.lib.albums().get()
69 assert album.added == self.min_mtime
70 for item in album.items():
71 assert item.added == pytest.approx(self.min_mtime, rel=1e-4)
72 mediafile_mtime = self.find_media_file_mtime(item)
73 assert item.mtime == pytest.approx(mediafile_mtime, rel=1e-4)
74 assert item.filepath.stat().st_mtime == pytest.approx(
75 mediafile_mtime, rel=1e-4
76 )
77
78 def test_reimported_album_skipped(self):
79 # Import and record the original added dates
80 self.importer.run()
81 album = self.lib.albums().get()
82 album_added_before = album.added
83 items_added_before = {
84 item.filepath: item.added for item in album.items()
85 }
86 # Newer Item path mtimes as if Beets had modified them
87 modify_mtimes(items_added_before.keys(), offset=10000)
88 # Reimport
89 self.setup_importer(import_dir=self.lib_path)
90 self.importer.run()
91 # Verify the reimported items
92 album = self.lib.albums().get()
93 assert album.added == pytest.approx(album_added_before, rel=1e-4)
94 items_added_after = {
95 item.filepath: item.added for item in album.items()
96 }
97 for item_path, added_after in items_added_after.items():
98 assert items_added_before[item_path] == pytest.approx(
99 added_after, rel=1e-4
100 ), f"reimport modified Item.added for {item_path}"
101
102 def test_import_singletons_with_added_dates(self):
103 self.config["import"]["singletons"] = True
104 self.importer.run()
105 for item in self.lib.items():
106 assert item.added == pytest.approx(
107 self.find_media_file_mtime(item), rel=1e-4
108 )
109
110 def test_import_singletons_with_preserved_mtimes(self):
111 self.config["import"]["singletons"] = True
112 self.config["importadded"]["preserve_mtimes"] = True
113 self.importer.run()
114 for item in self.lib.items():
115 mediafile_mtime = self.find_media_file_mtime(item)
116 assert item.added == pytest.approx(mediafile_mtime, rel=1e-4)
117 assert item.mtime == pytest.approx(mediafile_mtime, rel=1e-4)
118 assert item.filepath.stat().st_mtime == pytest.approx(
119 mediafile_mtime, rel=1e-4
120 )
121
122 def test_reimported_singletons_skipped(self):
123 self.config["import"]["singletons"] = True
124 # Import and record the original added dates
125 self.importer.run()
126 items_added_before = {
127 item.filepath: item.added for item in self.lib.items()
128 }
129 # Newer Item path mtimes as if Beets had modified them
130 modify_mtimes(items_added_before.keys(), offset=10000)
131 # Reimport
132 self.setup_importer(import_dir=self.lib_path, singletons=True)
133 self.importer.run()
134 # Verify the reimported items
135 items_added_after = {
136 item.filepath: item.added for item in self.lib.items()
137 }
138 for item_path, added_after in items_added_after.items():
139 assert items_added_before[item_path] == pytest.approx(
140 added_after, rel=1e-4
141 ), f"reimport modified Item.added for {item_path}"