1
1
"""Shared pytest configuration."""
2
+
2
3
import datetime
3
4
import importlib
4
5
import random
5
6
import shutil
6
7
import sys
7
8
import textwrap
8
9
from pathlib import Path
9
- from typing import Any , Callable , Iterator , Optional
10
+ from typing import Callable , Iterator , Optional
10
11
from unittest .mock import MagicMock
11
12
13
+ import moe .write
12
14
import pytest
13
15
import sqlalchemy as sa
14
16
import sqlalchemy .exc
15
17
import sqlalchemy .orm
16
18
from moe import config
17
- from moe .config import Config , ExtraPlugin , MoeSession , session_factory
19
+ from moe .config import Config , ExtraPlugin , moe_sessionmaker
18
20
from moe .library import Album , Extra , Track
19
- from moe .plugins import write as moe_write
20
21
21
22
__all__ = ["album_factory" , "extra_factory" , "track_factory" ]
22
23
@@ -73,9 +74,7 @@ def tmp_config(
73
74
Args:
74
75
settings: Settings string to use. This has the same format as a normal
75
76
``config.toml`` file.
76
- init_db: Whether or not to initialize the database.
77
- tmp_db: Whether or not to use a temporary (in-memory) database. If ``True``,
78
- the database will be initialized regardless of ``init_db``.
77
+ tmp_db: Whether or not to use a temporary (in-memory) database.
79
78
extra_plugins: Any additional plugins to enable.
80
79
config_dir: Optionally specifiy a config directory to use.
81
80
@@ -112,18 +111,16 @@ def _tmp_config(
112
111
ExtraPlugin (importlib .import_module ("moe_transcode" ), "transcode" )
113
112
)
114
113
115
- config = Config (
114
+ return Config (
116
115
config_dir = config_dir ,
117
116
settings_filename = "config.toml" ,
118
117
extra_plugins = extra_plugins ,
119
118
engine = engine ,
120
119
init_db = init_db ,
121
120
)
122
121
123
- return config
124
-
125
122
yield _tmp_config
126
- session_factory .configure (bind = None ) # reset the database in between tests
123
+ moe_sessionmaker .configure (bind = None ) # reset the database in between tests
127
124
128
125
129
126
@pytest .fixture
@@ -137,22 +134,17 @@ def tmp_session(tmp_config) -> Iterator[sqlalchemy.orm.session.Session]:
137
134
The temporary session.
138
135
"""
139
136
try :
140
- MoeSession ().get_bind ()
137
+ moe_sessionmaker ().get_bind ()
141
138
except sqlalchemy .exc .UnboundExecutionError :
142
- MoeSession .remove ()
143
139
tmp_config ("default_plugins = []" , tmp_db = True )
144
140
145
- session = MoeSession ()
146
- with session .begin ():
141
+ with moe_sessionmaker .begin () as session :
147
142
yield session
148
143
149
- MoeSession .remove ()
150
-
151
144
152
145
@pytest .fixture (autouse = True )
153
146
def _clean_moe ():
154
- """Ensure we aren't sharing sessions or configs between tests."""
155
- MoeSession .remove ()
147
+ """Ensure we aren't sharing configs between tests."""
156
148
config .CONFIG = MagicMock ()
157
149
158
150
@@ -161,7 +153,6 @@ def track_factory(
161
153
exists : bool = False ,
162
154
dup_track : Optional [Track ] = None ,
163
155
audio_format : str = "mp3" ,
164
- custom_fields : Optional [dict [str , Any ]] = None ,
165
156
** kwargs ,
166
157
):
167
158
"""Creates a track.
@@ -171,7 +162,6 @@ def track_factory(
171
162
exists: Whether the track should exist on the filesystem. Note, this option
172
163
requires the write plugin.
173
164
dup_track: If given, the new track created will be a duplicate of `dup_track`.
174
- custom_fields: Dict of custom_fields to values to assign to the track.
175
165
audio_format: Audio format of the track.
176
166
**kwargs: Any other fields to assign to the Track.
177
167
@@ -201,11 +191,6 @@ def track_factory(
201
191
** kwargs ,
202
192
)
203
193
204
- if custom_fields :
205
- for field , value in custom_fields .items ():
206
- track .custom_fields .add (field )
207
- setattr (track , field , value )
208
-
209
194
if dup_track :
210
195
for field in dup_track .fields :
211
196
value = getattr (dup_track , field )
@@ -221,7 +206,7 @@ def track_factory(
221
206
shutil .copyfile (EMPTY_FLAC_FILE , track .path )
222
207
else :
223
208
shutil .copyfile (EMPTY_MP3_FILE , track .path )
224
- moe_write .write_tags (track )
209
+ moe . write .write_tags (track )
225
210
226
211
return track
227
212
@@ -231,7 +216,6 @@ def extra_factory(
231
216
path : Optional [Path ] = None ,
232
217
exists : bool = False ,
233
218
dup_extra : Optional [Extra ] = None ,
234
- custom_fields : Optional [dict [str , Any ]] = None ,
235
219
** kwargs ,
236
220
) -> Extra :
237
221
"""Creates an extra for testing.
@@ -241,7 +225,6 @@ def extra_factory(
241
225
path: Path to assign to the extra. Will create a random path if not given.
242
226
exists: Whether the extra should actually exist on the filesystem.
243
227
dup_extra: If given, the new extra created will be a duplicate of `dup_extra`.
244
- custom_fields: Dict of custom_fields to values to assign to the extra.
245
228
**kwargs: Any other fields to assign to the extra.
246
229
247
230
Returns:
@@ -252,11 +235,6 @@ def extra_factory(
252
235
253
236
extra = Extra (album = album , path = path , ** kwargs )
254
237
255
- if custom_fields :
256
- for field , value in custom_fields .items ():
257
- extra .custom_fields .add (field )
258
- setattr (extra , field , value )
259
-
260
238
if dup_extra :
261
239
for field in dup_extra .fields :
262
240
value = getattr (dup_extra , field )
@@ -279,7 +257,6 @@ def album_factory(
279
257
exists : bool = False ,
280
258
dup_album : Optional [Album ] = None ,
281
259
audio_format : str = "mp3" ,
282
- custom_fields : Optional [dict [str , Any ]] = None ,
283
260
** kwargs ,
284
261
) -> Album :
285
262
"""Creates an album.
@@ -292,7 +269,6 @@ def album_factory(
292
269
requires the write plugin.
293
270
dup_album: If given, the new album created will be a duplicate of `dup_album`.
294
271
audio_format: Audio format of the tracks in the album.
295
- custom_fields: Dict of custom_fields to values to assign to the album.
296
272
**kwargs: Any other fields to assign to the album.
297
273
298
274
Returns:
@@ -314,10 +290,6 @@ def album_factory(
314
290
track_total = num_tracks ,
315
291
** kwargs ,
316
292
)
317
- if custom_fields :
318
- for field , value in custom_fields .items ():
319
- album .custom_fields .add (field )
320
- setattr (album , field , value )
321
293
322
294
if dup_album :
323
295
for field in dup_album .fields :
0 commit comments