Skip to content

Commit

Permalink
Fix relative import in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwmillr committed Dec 13, 2024
1 parent fc7e141 commit 67f900b
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 171 deletions.
32 changes: 13 additions & 19 deletions tests/test_album.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import warnings

from . import genius
from tests import genius
from lyricsgenius.types import Album


Expand All @@ -15,10 +15,7 @@ def setUpClass(cls):
cls.album_name = "The Party"
cls.artist_name = "Andy Shauf"
cls.num_tracks = 10
cls.album = genius.search_album(
cls.album_name,
cls.artist_name
)
cls.album = genius.search_album(cls.album_name, cls.artist_name)

def test_type(self):
self.assertIsInstance(self.album, Album)
Expand All @@ -33,13 +30,12 @@ def test_tracks(self):
self.assertEqual(len(self.album.tracks), self.num_tracks)

def test_saving_json_file(self):
print('\n')
extension = 'json'
print("\n")
extension = "json"
msg = "Could not save {} file.".format(extension)
expected_filename = ('Lyrics_'
+ self.album.name.replace(' ', '')
+ '.'
+ extension)
expected_filename = (
"Lyrics_" + self.album.name.replace(" ", "") + "." + extension
)

# Remove the test file if it already exists
if os.path.isfile(expected_filename):
Expand All @@ -57,13 +53,12 @@ def test_saving_json_file(self):
os.remove(expected_filename)

def test_saving_txt_file(self):
print('\n')
extension = 'txt'
print("\n")
extension = "txt"
msg = "Could not save {} file.".format(extension)
expected_filename = ('Lyrics_'
+ self.album.name.replace(' ', '')
+ '.'
+ extension)
expected_filename = (
"Lyrics_" + self.album.name.replace(" ", "") + "." + extension
)

# Remove the test file if it already exists
if os.path.isfile(expected_filename):
Expand All @@ -75,8 +70,7 @@ def test_saving_txt_file(self):

# Test overwriting txt file (now that file is written)
try:
self.album.save_lyrics(
extension=extension, overwrite=True)
self.album.save_lyrics(extension=extension, overwrite=True)
except Exception:
self.fail("Failed {} overwrite test".format(extension))
os.remove(expected_filename)
58 changes: 29 additions & 29 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest


from . import genius
from tests import genius


class TestAPI(unittest.TestCase):
Expand All @@ -11,63 +11,63 @@ def setUpClass(cls):
print("\n---------------------\nSetting up API tests...\n")

def test_account(self):
msg = ("No user detail was returned. "
"Are you sure you're using a user access token?")
msg = (
"No user detail was returned. "
"Are you sure you're using a user access token?"
)
r = genius.account()
self.assertTrue("user" in r, msg)

def test_annotation(self):
msg = "Returned annotation API path is different than expected."
id_ = 10225840
r = genius.annotation(id_)
real = r['annotation']['api_path']
expected = '/annotations/10225840'
real = r["annotation"]["api_path"]
expected = "/annotations/10225840"
self.assertEqual(real, expected, msg)

def test_manage_annotation(self):
example_text = 'The annotation'
example_text = "The annotation"
new_annotation = genius.create_annotation(
example_text,
'https://example.com',
'illustrative examples',
title='test')['annotation']
msg = 'Annotation text did not match the one that was passed.'
self.assertEqual(new_annotation['body']['plain'], example_text, msg)
example_text, "https://example.com", "illustrative examples", title="test"
)["annotation"]
msg = "Annotation text did not match the one that was passed."
self.assertEqual(new_annotation["body"]["plain"], example_text, msg)

try:
example_text_two = 'Updated annotation'
example_text_two = "Updated annotation"
r = genius.update_annotation(
new_annotation['id'],
new_annotation["id"],
example_text_two,
'https://example.com',
'illustrative examples',
title='test'
)['annotation']
msg = 'Updated annotation text did not match the one that was passed.'
self.assertEqual(r['body']['plain'], example_text_two, msg)
"https://example.com",
"illustrative examples",
title="test",
)["annotation"]
msg = "Updated annotation text did not match the one that was passed."
self.assertEqual(r["body"]["plain"], example_text_two, msg)

r = genius.upvote_annotation(11828417)
msg = 'Upvote was not registered.'
msg = "Upvote was not registered."
self.assertTrue(r is not None, msg)

r = genius.downvote_annotation(11828417)
msg = 'Downvote was not registered.'
msg = "Downvote was not registered."
self.assertTrue(r is not None, msg)

r = genius.unvote_annotation(11828417)
msg = 'Vote was not removed.'
msg = "Vote was not removed."
self.assertTrue(r is not None, msg)
finally:
msg = 'Annotation was not deleted.'
r = genius.delete_annotation(new_annotation['id'])
msg = "Annotation was not deleted."
r = genius.delete_annotation(new_annotation["id"])
self.assertEqual(r, 204, msg)

def test_referents_web_page(self):
msg = "Returned referent API path is different than expected."
id_ = 10347
r = genius.referents(web_page_id=id_)
real = r['referents'][0]['api_path']
expected = '/referents/11828416'
real = r["referents"][0]["api_path"]
expected = "/referents/11828416"
self.assertTrue(real == expected, msg)

def test_referents_no_inputs(self):
Expand All @@ -84,6 +84,6 @@ def test_web_page(self):
msg = "Returned web page API path is different than expected."
url = "https://docs.genius.com"
r = genius.web_page(raw_annotatable_url=url)
real = r['web_page']['api_path']
expected = '/web_pages/10347'
real = r["web_page"]["api_path"]
expected = "/web_pages/10347"
self.assertEqual(real, expected, msg)
41 changes: 17 additions & 24 deletions tests/test_artist.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import os

from . import genius
from tests import genius
from lyricsgenius.types import Artist
from lyricsgenius.utils import sanitize_filename

Expand All @@ -15,8 +15,7 @@ def setUpClass(cls):
cls.artist_name = "The Beatles"
cls.new_song = "Paperback Writer"
cls.max_songs = 2
cls.artist = genius.search_artist(
cls.artist_name, max_songs=cls.max_songs)
cls.artist = genius.search_artist(cls.artist_name, max_songs=cls.max_songs)

def test_artist(self):
msg = "The returned object is not an instance of the Artist class."
Expand Down Expand Up @@ -56,31 +55,27 @@ def test_add_song_from_different_artist(self):
def test_artist_with_includes_features(self):
# The artist did not get songs returned that they were featured in.
name = "Swae Lee"
result = genius.search_artist(
name,
max_songs=1,
include_features=True)
result = genius.search_artist(name, max_songs=1, include_features=True)
result = result.songs[0].artist
self.assertNotEqual(result, name)

def determine_filenames(self, extension):
expected_filenames = []
for song in self.artist.songs:
fn = "lyrics_{name}_{song}.{ext}".format(name=self.artist.name,
song=song.title,
ext=extension)
fn = "lyrics_{name}_{song}.{ext}".format(
name=self.artist.name, song=song.title, ext=extension
)
fn = sanitize_filename(fn.lower().replace(" ", ""))
expected_filenames.append(fn)
return expected_filenames

def test_saving_json_file(self):
print('\n')
extension = 'json'
print("\n")
extension = "json"
msg = "Could not save {} file.".format(extension)
expected_filename = ('Lyrics_'
+ self.artist.name.replace(' ', '')
+ '.'
+ extension)
expected_filename = (
"Lyrics_" + self.artist.name.replace(" ", "") + "." + extension
)

# Remove the test file if it already exists
if os.path.isfile(expected_filename):
Expand All @@ -98,13 +93,12 @@ def test_saving_json_file(self):
os.remove(expected_filename)

def test_saving_txt_file(self):
print('\n')
extension = 'txt'
print("\n")
extension = "txt"
msg = "Could not save {} file.".format(extension)
expected_filename = ('Lyrics_'
+ self.artist.name.replace(' ', '')
+ '.'
+ extension)
expected_filename = (
"Lyrics_" + self.artist.name.replace(" ", "") + "." + extension
)

# Remove the test file if it already exists
if os.path.isfile(expected_filename):
Expand All @@ -116,8 +110,7 @@ def test_saving_txt_file(self):

# Test overwriting txt file (now that file is written)
try:
self.artist.save_lyrics(
extension=extension, overwrite=True)
self.artist.save_lyrics(extension=extension, overwrite=True)
except Exception:
self.fail("Failed {} overwrite test".format(extension))
os.remove(expected_filename)
2 changes: 1 addition & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from requests.exceptions import HTTPError

from . import genius
from tests import genius


class TestAPIBase(unittest.TestCase):
Expand Down
26 changes: 13 additions & 13 deletions tests/test_genius.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from . import genius
from tests import genius


class TestEndpoints(unittest.TestCase):
Expand All @@ -11,12 +11,12 @@ def setUpClass(cls):

cls.search_term = "Ezra Furman"
cls.song_title_only = "99 Problems"
cls.tag = genius.tag('pop')
cls.tag = genius.tag("pop")

def test_search_song(self):
artist = "Jay-Z"
# Empty response
response = genius.search_song('')
response = genius.search_song("")
self.assertIsNone(response)

# Pass no title and ID
Expand Down Expand Up @@ -53,25 +53,25 @@ def test_song_annotations(self):
def test_tag_results(self):
r = self.tag

self.assertEqual(r['next_page'], 2)
self.assertEqual(len(r['hits']), 20)
self.assertEqual(r["next_page"], 2)
self.assertEqual(len(r["hits"]), 20)

def test_tag_first_result(self):
artists = ['Luis Fonsi', 'Daddy Yankee']
featured_artists = ['Justin Bieber']
artists = ["Luis Fonsi", "Daddy Yankee"]
featured_artists = ["Justin Bieber"]
song_title = "Despacito (Remix)"
title_with_artists = (
"Despacito (Remix) by Luis Fonsi & Daddy Yankee (Ft. Justin Bieber)"
)
url = "https://genius.com/Luis-fonsi-and-daddy-yankee-despacito-remix-lyrics"

first_song = self.tag['hits'][0]
first_song = self.tag["hits"][0]

self.assertEqual(artists, first_song['artists'])
self.assertEqual(featured_artists, first_song['featured_artists'])
self.assertEqual(song_title, first_song['title'])
self.assertEqual(title_with_artists, first_song['title_with_artists'])
self.assertEqual(url, first_song['url'])
self.assertEqual(artists, first_song["artists"])
self.assertEqual(featured_artists, first_song["featured_artists"])
self.assertEqual(song_title, first_song["title"])
self.assertEqual(title_with_artists, first_song["title_with_artists"])
self.assertEqual(url, first_song["url"])


class TestLyrics(unittest.TestCase):
Expand Down
Loading

0 comments on commit 67f900b

Please sign in to comment.