|
4 | 4 | """Tests for `lyricsmaster` package.""" |
5 | 5 |
|
6 | 6 |
|
7 | | -import unittest |
| 7 | +from collections import Iterable |
| 8 | +import pytest |
8 | 9 | from click.testing import CliRunner |
9 | 10 |
|
| 11 | +from bs4 import BeautifulSoup, Tag |
| 12 | + |
10 | 13 | from lyricsmaster import lyricsmaster |
11 | 14 | from lyricsmaster import cli |
| 15 | +from lyricsmaster import lyricsprovider |
| 16 | + |
| 17 | +try: |
| 18 | + basestring |
| 19 | +except NameError: |
| 20 | + basestring = str |
| 21 | + |
| 22 | +@pytest.fixture(scope="module") |
| 23 | +def songs(): |
| 24 | + songs = [lyricsmaster.Song('Bad Love', 'Bad news is coming','Luther Alison', None), |
| 25 | + lyricsmaster.Song('Ragged and dirty', 'Bad news is coming', 'Luther Alison', None), |
| 26 | + lyricsmaster.Song('Red rooster', 'Bad news is coming', 'Luther Alison', None), |
| 27 | + lyricsmaster.Song('Life is bitch', 'Bad news is coming', 'Luther Alison', None)] |
| 28 | + return songs |
| 29 | + |
| 30 | +class TestSongs: |
| 31 | + """Tests for Song Class.""" |
| 32 | + song = lyricsmaster.Song('Bad Love', 'Bad news is coming','Luther Alison', None) |
| 33 | + |
| 34 | + def test_song(self): |
| 35 | + assert self.song.__repr__() == 'Song Object: Bad Love' |
| 36 | + |
| 37 | + |
| 38 | +class TestAlbums: |
| 39 | + """Tests for Album Class.""" |
| 40 | + |
| 41 | + songs = songs() |
| 42 | + album = lyricsmaster.Album('Bad news is coming', 'Luther Alison', songs) |
| 43 | + |
| 44 | + def test_album(self): |
| 45 | + assert self.album.idx == 0 |
| 46 | + assert self.album.title == 'Bad news is coming' |
| 47 | + assert self.album.author == 'Luther Alison' |
| 48 | + assert self.album.__repr__() == 'Album Object: Bad news is coming' |
| 49 | + |
| 50 | + def test_album_isiter(self): |
| 51 | + assert len(self.album) == 4 |
| 52 | + assert [elmt for elmt in self.album] == self.songs |
| 53 | + |
| 54 | + |
| 55 | +class TestDiscography: |
| 56 | + """Tests for Album Class.""" |
| 57 | + |
| 58 | + albums = [lyricsmaster.Album('Bad news is coming', 'Luther Alison', songs), |
| 59 | + lyricsmaster.Album('Bad news is coming', 'Luther Alison', songs)] |
| 60 | + discography = lyricsmaster.Discography('Luther Allison', albums) |
| 61 | + |
| 62 | + def test_discography(self): |
| 63 | + assert self.discography.__repr__() == 'Discography Object: Luther Allison' |
| 64 | + |
| 65 | + def test_discography_isiter(self): |
| 66 | + assert len(self.discography) == 2 |
| 67 | + assert [elmt for elmt in self.discography] == self.albums |
| 68 | + |
| 69 | +class TestLyricWiki: |
| 70 | + provider = lyricsprovider.LyricWiki() |
| 71 | + author = 'Reggie Watts' |
| 72 | + |
| 73 | + def test_clean_string(self): |
| 74 | + assert self.provider.clean_string('Reggie Watts {(#5)}') == 'Reggie_Watts_((Number_5))' |
| 75 | + |
| 76 | + def test_get_artist_page(self): |
| 77 | + page = self.provider.get_artist_page(self.author) |
| 78 | + assert isinstance(page, BeautifulSoup) |
| 79 | + |
| 80 | + def test_get_album_page(self): |
| 81 | + page = self.provider.get_album_page('Reggie Watts', 'Simplified (2004)') |
| 82 | + assert isinstance(page, BeautifulSoup) |
| 83 | + |
| 84 | + def test_get_lyrics_page(self): |
| 85 | + page = self.provider.get_lyrics_page('http://lyrics.wikia.com/wiki/Reggie_Watts:Your_Name') |
| 86 | + assert isinstance(page, BeautifulSoup) |
| 87 | + |
| 88 | + def test_extract_lyrics(self): |
| 89 | + page = self.provider.get_lyrics_page('http://lyrics.wikia.com/wiki/Reggie_Watts:Your_Name') |
| 90 | + lyrics = self.provider.extract_lyrics(page) |
| 91 | + assert isinstance(lyrics, basestring) |
| 92 | + assert 'I recall the day' in lyrics |
| 93 | + assert "And I hope you'll stay." in lyrics |
12 | 94 |
|
| 95 | + def test_get_songs(self): |
| 96 | + author_page = self.provider.get_artist_page(self.author) |
| 97 | + album = [tag for tag in author_page.find_all("span", {'class': 'mw-headline'}) if |
| 98 | + tag.attrs['id'] not in ('Additional_information', 'External_links')][0] |
| 99 | + song_links = self.provider.get_songs(album) |
| 100 | + for link in song_links: |
| 101 | + assert isinstance(link, Tag) |
13 | 102 |
|
14 | | -class TestLyricsmaster(unittest.TestCase): |
15 | | - """Tests for `lyricsmaster` package.""" |
| 103 | + def test_create_song(self): |
| 104 | + author_page = self.provider.get_artist_page(self.author) |
| 105 | + album = [tag for tag in author_page.find_all("span", {'class': 'mw-headline'}) if |
| 106 | + tag.attrs['id'] not in ('Additional_information', 'External_links')][0] |
| 107 | + song_links = self.provider.get_songs(album) |
| 108 | + fail_song = self.provider.create_song(song_links[0], self.author, "Simplified (2004)") |
| 109 | + assert fail_song is None |
| 110 | + good_song = self.provider.create_song(song_links[9], self.author, "Simplified (2004)") |
| 111 | + assert isinstance(good_song, lyricsmaster.Song) |
| 112 | + assert good_song.title == 'Reggie Watts:Your Name' |
| 113 | + assert good_song.album == "Simplified (2004)" |
| 114 | + assert good_song.author == self.author |
| 115 | + assert 'I recall the day' in good_song.lyrics |
| 116 | + assert "And I hope you'll stay." in good_song.lyrics |
16 | 117 |
|
17 | | - def setUp(self): |
18 | | - """Set up test fixtures, if any.""" |
| 118 | + def test_get_lyrics(self): |
| 119 | + discography = self.provider.get_lyrics(self.author) |
| 120 | + assert isinstance(discography, lyricsmaster.Discography) |
19 | 121 |
|
20 | | - def tearDown(self): |
21 | | - """Tear down test fixtures, if any.""" |
22 | 122 |
|
23 | | - def test_000_something(self): |
24 | | - """Test something.""" |
25 | 123 |
|
26 | | - def test_command_line_interface(self): |
27 | | - """Test the CLI.""" |
28 | | - runner = CliRunner() |
29 | | - result = runner.invoke(cli.main) |
30 | | - assert result.exit_code == 0 |
31 | | - assert 'lyricsmaster.cli.main' in result.output |
32 | | - help_result = runner.invoke(cli.main, ['--help']) |
33 | | - assert help_result.exit_code == 0 |
34 | | - assert '--help Show this message and exit.' in help_result.output |
| 124 | +def test_command_line_interface(): |
| 125 | + """Test the CLI.""" |
| 126 | + runner = CliRunner() |
| 127 | + result = runner.invoke(cli.main) |
| 128 | + assert result.exit_code == 0 |
| 129 | + assert 'lyricsmaster.cli.main' in result.output |
| 130 | + help_result = runner.invoke(cli.main, ['--help']) |
| 131 | + assert help_result.exit_code == 0 |
| 132 | + assert '--help Show this message and exit.' in help_result.output |
0 commit comments