-
Notifications
You must be signed in to change notification settings - Fork 4
/
conftest.py
357 lines (293 loc) · 10.6 KB
/
conftest.py
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
"""Shared pytest configuration."""
import datetime
import random
import shutil
import sys
import textwrap
from pathlib import Path
from typing import Any, Callable, Iterator, Optional
from unittest.mock import MagicMock
import pytest
import sqlalchemy as sa
import sqlalchemy.exc
import sqlalchemy.orm
from moe import config
from moe.config import Config, ExtraPlugin, MoeSession, session_factory
from moe.library import Album, Extra, Track
from moe.plugins import write as moe_write
__all__ = ["album_factory", "extra_factory", "track_factory"]
RESOURCE_DIR = Path(__file__).parent / "resources"
EMPTY_MP3_FILE = RESOURCE_DIR / "empty.mp3"
SUPPORTED_PLATFORMS = {"darwin", "linux", "win32"}
def pytest_runtest_setup(item):
"""Only run tests on their appropriate platform.
Use the following markers to mark tests for specific platforms:
@pytest.mark.darwin
@pytest.mark.linux
@pytest.mark.win32
See Also:
pytest docs: https://docs.pytest.org/en/latest/example/markers.html#marking-platform-specific-tests-with-pytest
""" # noqa: E501
platforms = SUPPORTED_PLATFORMS.intersection(
mark.name for mark in item.iter_markers()
)
platform = sys.platform
if platforms and platform not in platforms:
pytest.skip(f"cannot run on platform {platform}")
@pytest.fixture(autouse=True, scope="session")
def _tmp_library_path(tmp_path_factory):
"""Creates a temporary music library directory for all test tracks and albums."""
global LIBRARY_PATH
LIBRARY_PATH = tmp_path_factory.mktemp("music")
@pytest.fixture
def tmp_config(
tmp_path_factory,
) -> Iterator[Callable[[str, bool, bool, list[ExtraPlugin]], Config]]:
"""Instantiates a temporary configuration.
This fixture must be declared, like a factory. If you want to use specific config
settings, pass them in your declaration.
Example::
settings = f"library_path = '''~/Music'''"
config = tmp_config(settings)
Note:
Any paths should be surrounded with triple single quotes ('''). This tells
toml to treat the path as a raw string, and prevents it from thinking Windows
paths are full of escape characters.
Args:
settings: Settings string to use. This has the same format as a normal
``config.toml`` file.
init_db: Whether or not to initialize the database.
tmp_db: Whether or not to use a temporary (in-memory) database. If ``True``,
the database will be initialized regardless of ``init_db``.
extra_plugins: Any additional plugins to enable.
config_dir: Optionally specifiy a config directory to use.
Yields:
The configuration instance.
"""
def _tmp_config(
settings: str = "",
init_db: bool = False,
tmp_db: bool = False,
extra_plugins: Optional[list[ExtraPlugin]] = None,
config_dir: Optional[Path] = None,
) -> Config:
config_dir = config_dir or tmp_path_factory.mktemp("config")
assert config_dir
if "library_path" not in settings:
settings += f"\nlibrary_path = '{LIBRARY_PATH.resolve()}'"
settings_path = config_dir / "config.toml"
settings_path.write_text(textwrap.dedent(settings))
engine: Optional[sa.engine.base.Engine]
if tmp_db:
engine = sa.create_engine("sqlite:///:memory:")
init_db = True
else:
engine = None
return Config(
config_dir=config_dir,
settings_filename="config.toml",
extra_plugins=extra_plugins,
engine=engine,
init_db=init_db,
)
yield _tmp_config
session_factory.configure(bind=None) # reset the database in between tests
@pytest.fixture
def tmp_session(tmp_config) -> Iterator[sqlalchemy.orm.session.Session]:
"""Creates a temporary session.
If you are also using `tmp_config` in your test, ensure to specify `tmp_db=True`
when creating the `tmp_config` instance.
Yields:
The temporary session.
"""
try:
MoeSession().get_bind()
except sqlalchemy.exc.UnboundExecutionError:
MoeSession.remove()
tmp_config("default_plugins = []", tmp_db=True)
session = MoeSession()
with session.begin():
yield session
MoeSession.remove()
@pytest.fixture(autouse=True)
def _clean_moe():
"""Ensure we aren't sharing sessions or configs between tests."""
MoeSession.remove()
config.CONFIG = MagicMock()
def track_factory(
album: Optional[Album] = None,
exists: bool = False,
dup_track: Optional[Track] = None,
custom_fields: Optional[dict[str, Any]] = None,
**kwargs,
):
"""Creates a track.
Args:
album: Optional album to assign the track to.
exists: Whether the track should exist on the filesystem. Note, this option
requires the write plugin.
dup_track: If given, the new track created will be a duplicate of `dup_track`.
custom_fields: Dict of custom_fields to values to assign to the track.
**kwargs: Any other fields to assign to the Track.
Returns:
Unique Track.
"""
album = album or album_factory(num_tracks=0, num_extras=0, exists=exists)
track_num = kwargs.pop("track_num", random.randint(1, 10000))
title = kwargs.pop("title", "Jazzy Belle")
disc = kwargs.pop("disc", 1)
if album.disc_total > 1:
disc_dir = f"Disc {disc:02}"
else:
disc_dir = ""
path = kwargs.pop(
"path", album.path / disc_dir / f"{disc}.{track_num} - {title}.mp3"
)
track = Track(
album=album,
path=path,
title=title,
track_num=track_num,
disc=disc,
**kwargs,
)
if custom_fields:
for field, value in custom_fields.items():
track.custom_fields.add(field)
setattr(track, field, value)
if dup_track:
for field in dup_track.fields:
value = getattr(dup_track, field)
if field == "genre":
print(value)
try:
setattr(track, field, value)
except AttributeError:
pass
return track
if exists:
track.path.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(EMPTY_MP3_FILE, track.path)
moe_write.write_tags(track)
return track
def extra_factory(
album: Optional[Album] = None,
path: Optional[Path] = None,
exists: bool = False,
dup_extra: Optional[Extra] = None,
custom_fields: Optional[dict[str, Any]] = None,
**kwargs,
) -> Extra:
"""Creates an extra for testing.
Args:
album: Album to assign the extra to.
path: Path to assign to the extra. Will create a random path if not given.
exists: Whether the extra should actually exist on the filesystem.
dup_extra: If given, the new extra created will be a duplicate of `dup_extra`.
custom_fields: Dict of custom_fields to values to assign to the extra.
**kwargs: Any other fields to assign to the extra.
Returns:
Created extra.
"""
album = album or album_factory(num_tracks=0, num_extras=0, exists=exists)
path = path or album.path / f"{random.randint(1,10000)}.txt"
extra = Extra(album=album, path=path, **kwargs)
if custom_fields:
for field, value in custom_fields.items():
extra.custom_fields.add(field)
setattr(extra, field, value)
if dup_extra:
for field in dup_extra.fields:
value = getattr(dup_extra, field)
try:
setattr(extra, field, value)
except AttributeError:
pass
return extra
if exists:
extra.path.touch()
return extra
def album_factory(
num_tracks: int = 2,
num_extras: int = 2,
num_discs: int = 1,
exists: bool = False,
dup_album: Optional[Album] = None,
custom_fields: Optional[dict[str, Any]] = None,
**kwargs,
) -> Album:
"""Creates an album.
Args:
num_tracks: Number of tracks to add to the album.
num_extras: Number of extras to add to the album.
num_discs: Number of discs on the album. Will have disc sub dirs.
exists: Whether the album should exist on the filesystem. Note, this option
requires the write plugin.
dup_album: If given, the new album created will be a duplicate of `dup_album`.
custom_fields: Dict of custom_fields to values to assign to the album.
**kwargs: Any other fields to assign to the album.
Returns:
Created album.
"""
artist = kwargs.pop("artist", "Outkast")
title = kwargs.pop("title", "ATLiens")
date = kwargs.pop(
"date", datetime.date(random.randint(datetime.MINYEAR, datetime.MAXYEAR), 1, 1)
)
path = kwargs.pop("path", LIBRARY_PATH / f"{artist}" / f"{title} ({date.year})")
album = Album(
path=path,
artist=artist,
title=title,
date=date,
disc_total=num_discs,
track_total=num_tracks,
**kwargs,
)
if custom_fields:
for field, value in custom_fields.items():
album.custom_fields.add(field)
setattr(album, field, value)
if dup_album:
for field in dup_album.fields:
if field not in ["tracks", "extras"]:
value = getattr(dup_album, field)
try:
setattr(album, field, value)
except AttributeError:
pass
dup_tracks = dup_album.tracks.copy()
for track in dup_tracks:
album.tracks.append(
track_factory(
album=None,
exists=exists,
dup_track=track,
)
)
dup_extras = dup_album.extras.copy()
for extra in dup_extras:
album.extras.append(
extra_factory(
album=None,
exists=exists,
dup_extra=extra,
)
)
return album
if exists:
album.path.mkdir(exist_ok=True, parents=True)
if num_tracks < num_discs and num_tracks != 0:
num_tracks = num_discs
tracks_per_disc = int(num_tracks / num_discs)
for disc in range(1, num_discs + 1):
for track_num in range(1, tracks_per_disc + 1):
track_factory(
album=album,
exists=exists,
track_num=track_num,
disc=disc,
)
for _ in range(1, num_extras):
extra_factory(album=album, exists=exists)
return album