-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathtest_album.py
More file actions
306 lines (255 loc) · 10.3 KB
/
Copy pathtest_album.py
File metadata and controls
306 lines (255 loc) · 10.3 KB
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
import json
import os
from pathlib import Path
from typing import Any
from unittest import mock
import pytest
from lyricsgenius import Genius
from lyricsgenius.types import Album, Artist, Song
from lyricsgenius.utils import sanitize_filename
@pytest.fixture
def mock_album_data() -> dict[str, Any]:
"""Return the contents of album_info_mocked.json as a dict."""
with open("tests/fixtures/album_info_mocked.json", "r") as f:
return json.load(f)
@pytest.fixture
def mock_album_artist_data(mock_album_data: dict[str, Any]) -> dict[str, Any]:
"""Mock data for the album's artist."""
return mock_album_data["artist"]
@pytest.fixture
def mock_track_data_list() -> list[dict[str, Any]]:
"""Load mock track data from song_info_mocked.json (first two songs)."""
with open("tests/fixtures/song_info_mocked.json", "r") as f:
songs = json.load(f)
return songs[:2]
@pytest.fixture
def mock_track_lyrics_list() -> list[str]:
"""Mock lyrics for the tracks."""
return [
"[Verse 1]\nArrange the mocks, prepare the scene,\nBefore the test, keep it clean.",
"[Verse 1]\nReset the state, undo the change,\nAfter the test, rearrange.",
]
@pytest.fixture
def mock_track_objects(
mock_track_lyrics_list: list[str], mock_track_data_list: list[dict[str, Any]]
) -> list[Song]:
"""Creates a list of mock Song objects for the album tracks."""
songs: list[Song] = []
for lyrics, data in zip(mock_track_lyrics_list, mock_track_data_list, strict=True):
songs.append(Song(lyrics, data))
return songs
@pytest.fixture
def album_object(
mock_album_data: dict[str, Any], mock_track_objects: list[Song]
) -> Album:
"""Creates the Album object populated with mock tracks."""
return Album(mock_album_data, mock_track_objects)
def test_album_instance(album_object: Album) -> None:
"""Test if the created object is an Album instance."""
assert isinstance(album_object, Album)
def test_album_name(album_object: Album, mock_album_data: dict[str, Any]) -> None:
"""Test if the album name is correct."""
assert album_object.name == mock_album_data["name"]
def test_album_artist(
album_object: Album, mock_album_artist_data: dict[str, Any]
) -> None:
"""Test if the album artist name is correct."""
assert album_object.artist["name"] == mock_album_artist_data["name"]
assert album_object.artist["id"] == mock_album_artist_data["id"]
def test_tracks_populated(album_object: Album, mock_track_objects: list[Song]) -> None:
"""Test if the tracks list is populated correctly."""
assert len(album_object.tracks) == len(mock_track_objects)
assert album_object.tracks[0][1].title == mock_track_objects[0].title
assert album_object.tracks[1][1].title == mock_track_objects[1].title
def test_get_track_by_position(
album_object: Album, mock_track_objects: list[Song]
) -> None:
"""Test retrieving a track by its position (1-based index)."""
# Find track by iterating and checking track.number
target_position_1 = 1
found_track_1 = None
for number, song in album_object.tracks:
if number == target_position_1:
found_track_1 = song # The test expects a Song object
break
assert found_track_1 is not None
assert found_track_1.title == mock_track_objects[0].title
target_position_2 = 2
found_track_2 = None
for number, song in album_object.tracks:
if number == target_position_2:
found_track_2 = song
break
assert found_track_2 is not None
assert found_track_2.title == mock_track_objects[1].title
# Test invalid positions (0 and out of bounds)
target_position_0 = 0
found_track_0 = None
for number, song in album_object.tracks:
if number == target_position_0:
found_track_0 = song
break
assert found_track_0 is None
target_position_invalid = len(mock_track_objects) + 1
found_track_invalid = None
for number, song in album_object.tracks:
if number == target_position_invalid:
found_track_invalid = song
break
assert found_track_invalid is None
def test_to_dict(
album_object: Album, mock_album_data: dict[str, Any], mock_track_objects: list[Song]
) -> None:
"""Test the to_dict method."""
album_dict = album_object.to_dict()
assert album_dict["name"] == mock_album_data["name"]
assert album_dict["artist"] == mock_album_data["artist"]["name"]
assert "release_date" in album_dict
assert isinstance(album_dict["release_date"], str)
assert len(album_dict["tracks"]) == len(mock_track_objects)
assert album_dict["tracks"][0]["song"]["title"] == mock_track_objects[0].title
assert "lyrics" in album_dict["tracks"][0]["song"]
def test_saving_json_file(album_object: Album, tmp_path: Path) -> None:
extension = "json"
filename_base = "Lyrics_" + album_object.name.replace(" ", "")
sanitized_base = sanitize_filename(filename_base)
expected_filepath = tmp_path / f"{sanitized_base}.{extension}"
album_object.save_lyrics(
filename=str(expected_filepath),
extension=extension,
overwrite=True,
sanitize=False,
verbose=False,
)
assert expected_filepath.is_file(), f"File not created at {expected_filepath}"
content = expected_filepath.read_text()
assert f'"name": "{album_object.name}"' in content, content
assert f'"artist": "{album_object.artist["name"]}"' in content, content
assert '"title": "Setup Serenade"' in content, content
assert '"lyrics": "[Verse 1]\\nArrange the mocks' in content, content
original_lyrics = album_object.tracks[0][1].lyrics
album_object.tracks[0][1].lyrics = "Overwritten JSON Test"
album_object.save_lyrics(
filename=str(expected_filepath),
extension=extension,
overwrite=True,
sanitize=False,
verbose=False,
)
assert expected_filepath.is_file(), (
f"Overwritten file not found at {expected_filepath}"
)
content_after_overwrite = expected_filepath.read_text()
assert "Overwritten JSON Test" in content_after_overwrite, content_after_overwrite
album_object.tracks[0][1].lyrics = original_lyrics
def test_saving_txt_file(album_object: Album, tmp_path: Path) -> None:
extension = "txt"
filename_base = "Lyrics_" + album_object.name.replace(" ", "")
sanitized_base = sanitize_filename(filename_base)
expected_filepath = tmp_path / f"{sanitized_base}.{extension}"
album_object.save_lyrics(
filename=str(expected_filepath),
extension=extension,
overwrite=True,
sanitize=False,
verbose=False,
)
assert expected_filepath.is_file(), f"File not created at {expected_filepath}"
content = expected_filepath.read_text()
assert "Track 1: Setup Serenade" in content, content
assert "[Verse 1]\nArrange the mocks" in content, content
assert "Track 2: Teardown Tango" in content, content
assert "[Verse 1]\nReset the state" in content, content
original_lyrics = album_object.tracks[0][1].lyrics
album_object.tracks[0][1].lyrics = "Overwritten TXT Test"
album_object.save_lyrics(
filename=str(expected_filepath),
extension=extension,
overwrite=True,
sanitize=False,
verbose=False,
)
assert expected_filepath.is_file(), (
f"Overwritten file not found at {expected_filepath}"
)
content_after_overwrite = expected_filepath.read_text()
assert "Overwritten TXT Test" in content_after_overwrite, content_after_overwrite
album_object.tracks[0][1].lyrics = original_lyrics
@pytest.fixture
def genius_client() -> Genius:
return Genius("dummy_access_token", verbose=False, sleep_time=0)
def _make_search_all_response(album_data: dict[str, Any]) -> dict[str, Any]:
hit = {"index": "album", "result": album_data}
return {
"sections": [
{"type": "top_hits", "hits": [hit]},
{"type": "album", "hits": [hit]},
]
}
def _make_album_tracks_response(songs: list[dict[str, Any]]) -> dict[str, Any]:
return {
"tracks": [{"song": s, "number": i + 1} for i, s in enumerate(songs)],
"next_page": None,
}
def test_fetch_lyrics_true_calls_lyrics(
genius_client: Genius,
mock_album_data: dict[str, Any],
mock_track_data_list: list[dict[str, Any]],
) -> None:
"""When fetch_lyrics=True (default), lyrics() should be called for each track."""
with (
mock.patch.object(
genius_client,
"search_all",
return_value=_make_search_all_response(mock_album_data),
),
mock.patch.object(
genius_client, "album", return_value={"album": mock_album_data}
),
mock.patch.object(
genius_client,
"album_tracks",
return_value=_make_album_tracks_response(mock_track_data_list),
),
mock.patch.object(
genius_client, "lyrics", return_value="some lyrics"
) as mock_lyrics,
):
result = genius_client.search_album(
name=mock_album_data["name"], fetch_lyrics=True
)
assert result is not None
assert mock_lyrics.call_count == len(mock_track_data_list)
for _, track in result.tracks:
assert track.lyrics == "some lyrics"
def test_fetch_lyrics_false_skips_lyrics(
genius_client: Genius,
mock_album_data: dict[str, Any],
mock_track_data_list: list[dict[str, Any]],
) -> None:
"""When fetch_lyrics=False, lyrics() is not called and tracks have empty lyrics."""
with (
mock.patch.object(
genius_client,
"search_all",
return_value=_make_search_all_response(mock_album_data),
),
mock.patch.object(
genius_client, "album", return_value={"album": mock_album_data}
),
mock.patch.object(
genius_client,
"album_tracks",
return_value=_make_album_tracks_response(mock_track_data_list),
),
mock.patch.object(
genius_client, "lyrics", return_value="some lyrics"
) as mock_lyrics,
):
result = genius_client.search_album(
name=mock_album_data["name"], fetch_lyrics=False
)
assert result is not None
assert mock_lyrics.call_count == 0
for _, track in result.tracks:
assert track.lyrics == ""