Skip to content

Commit

Permalink
lyrics: assume bs4 is always available
Browse files Browse the repository at this point in the history
  • Loading branch information
snejus committed Jul 18, 2024
1 parent 6f7174f commit 31fba03
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 64 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ jobs:
gstreamer1.0-plugins-good \
libgirepository1.0-dev \
mp3gain
poetry install --extras replaygain --extras embedart
poetry install \
--extras embedart \
--extras lyrics \
--extras replaygain
- name: Install Python dependencies
run: poetry install --only=main,test
Expand Down
78 changes: 15 additions & 63 deletions test/plugins/test_lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,12 @@ def assertLyricsContentOk(self, title, text, msg=""): # noqa: N802
LYRICS_TEXTS = confuse.load_yaml(yaml_path)


class LyricsGoogleBaseTest(unittest.TestCase):
class LyricsTestCase(unittest.TestCase):
def setUp(self):
"""Set up configuration."""
try:
__import__("bs4")
except ImportError:
self.skipTest("Beautiful Soup 4 not available")
self.plugin = lyrics.LyricsPlugin()


class LyricsPluginSourcesTest(LyricsGoogleBaseTest, LyricsAssertions):
class LyricsPluginSourcesTest(LyricsTestCase, LyricsAssertions):
"""Check that beets google custom search engine sources are correctly
scraped.
"""
Expand Down Expand Up @@ -346,10 +342,6 @@ class LyricsPluginSourcesTest(LyricsGoogleBaseTest, LyricsAssertions):
),
]

def setUp(self):
LyricsGoogleBaseTest.setUp(self)
self.plugin = lyrics.LyricsPlugin()

@unittest.skipUnless(
os.environ.get("INTEGRATION_TEST", "0") == "1",
"integration testing not enabled",
Expand Down Expand Up @@ -383,7 +375,7 @@ def test_google_sources_ok(self):
self.assertLyricsContentOk(s["title"], res, url)


class LyricsGooglePluginMachineryTest(LyricsGoogleBaseTest, LyricsAssertions):
class LyricsGooglePluginMachineryTest(LyricsTestCase, LyricsAssertions):
"""Test scraping heuristics on a fake html page."""

source = dict(
Expand All @@ -393,11 +385,6 @@ class LyricsGooglePluginMachineryTest(LyricsGoogleBaseTest, LyricsAssertions):
path="/lyrics/beetssong",
)

def setUp(self):
"""Set up configuration"""
LyricsGoogleBaseTest.setUp(self)
self.plugin = lyrics.LyricsPlugin()

@patch.object(lyrics.Backend, "fetch_url", MockFetchUrl())
def test_mocked_source_ok(self):
"""Test that lyrics of the mocked page are correctly scraped"""
Expand Down Expand Up @@ -461,23 +448,9 @@ def test_is_page_candidate_special_chars(self):
# test Genius backend


class GeniusBaseTest(unittest.TestCase):
def setUp(self):
"""Set up configuration."""
try:
__import__("bs4")
except ImportError:
self.skipTest("Beautiful Soup 4 not available")


class GeniusScrapeLyricsFromHtmlTest(GeniusBaseTest):
class GeniusScrapeLyricsFromHtmlTest(LyricsTestCase):
"""tests Genius._scrape_lyrics_from_html()"""

def setUp(self):
"""Set up configuration"""
GeniusBaseTest.setUp(self)
self.plugin = lyrics.LyricsPlugin()

def test_no_lyrics_div(self):
"""Ensure we don't crash when the scraping the html for a genius page
doesn't contain <div class="lyrics"></div>
Expand Down Expand Up @@ -507,14 +480,9 @@ def test_good_lyrics_multiple_divs(self):
# TODO: find an example of a lyrics page with multiple divs and test it


class GeniusFetchTest(GeniusBaseTest):
class GeniusFetchTest(LyricsTestCase):
"""tests Genius.fetch()"""

def setUp(self):
"""Set up configuration"""
GeniusBaseTest.setUp(self)
self.plugin = lyrics.LyricsPlugin()

@patch.object(lyrics.Genius, "_scrape_lyrics_from_html")
@patch.object(lyrics.Backend, "fetch_url", return_value=True)
def test_json(self, mock_fetch_url, mock_scrape):
Expand Down Expand Up @@ -567,22 +535,12 @@ def test_json(self, mock_fetch_url, mock_scrape):
# test Tekstowo


class TekstowoBaseTest(unittest.TestCase):
def setUp(self):
"""Set up configuration."""
try:
__import__("bs4")
except ImportError:
self.skipTest("Beautiful Soup 4 not available")


class TekstowoExtractLyricsTest(TekstowoBaseTest):
class TekstowoExtractLyricsTest(LyricsTestCase):
"""tests Tekstowo.extract_lyrics()"""

def setUp(self):
"""Set up configuration"""
TekstowoBaseTest.setUp(self)
self.plugin = lyrics.LyricsPlugin()
super().setUp()
tekstowo.config = self.plugin.config

def test_good_lyrics(self):
Expand Down Expand Up @@ -628,14 +586,9 @@ def test_song_no_match(self):
)


class TekstowoParseSearchResultsTest(TekstowoBaseTest):
class TekstowoParseSearchResultsTest(LyricsTestCase):
"""tests Tekstowo.parse_search_results()"""

def setUp(self):
"""Set up configuration"""
TekstowoBaseTest.setUp(self)
self.plugin = lyrics.LyricsPlugin()

def test_multiple_results(self):
"""Ensure we are able to scrape a page with multiple search results"""
url = (
Expand All @@ -659,13 +612,12 @@ def test_no_results(self):
self.assertEqual(tekstowo.parse_search_results(mock(url)), None)


class TekstowoIntegrationTest(TekstowoBaseTest, LyricsAssertions):
class TekstowoIntegrationTest(LyricsTestCase, LyricsAssertions):
"""Tests Tekstowo lyric source with real requests"""

def setUp(self):
"""Set up configuration"""
TekstowoBaseTest.setUp(self)
self.plugin = lyrics.LyricsPlugin()
super().setUp()
tekstowo.config = self.plugin.config

@unittest.skipUnless(
Expand Down Expand Up @@ -693,9 +645,9 @@ def test_no_matching_results(self):
# test LRCLib backend


class LRCLibLyricsTest(unittest.TestCase):
class LRCLibLyricsTest(LyricsTestCase):
def setUp(self):
self.plugin = lyrics.LyricsPlugin()
super().setUp()
lrclib.config = self.plugin.config

@patch("beetsplug.lyrics.requests.get")
Expand Down Expand Up @@ -750,9 +702,9 @@ def test_fetch_exception(self, mock_get):
self.assertIsNone(lyrics)


class LRCLibIntegrationTest(LyricsAssertions, unittest.TestCase):
class LRCLibIntegrationTest(LyricsTestCase, LyricsAssertions):
def setUp(self):
self.plugin = lyrics.LyricsPlugin()
super().setUp()
lrclib.config = self.plugin.config

@unittest.skipUnless(
Expand Down

0 comments on commit 31fba03

Please sign in to comment.