Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added vcr support for consistent tests #109

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

extras_require = {
'docs': [
'sphinx~=3.2',
'sphinx~=3.3',
'sphinx-rtd-theme',
],
'checks': [
Expand All @@ -35,10 +35,13 @@
'flake8',
'flake8-bugbear',
'pygments',
],
'tests': [
'vcrpy~=4.1'
]
}
extras_require['dev'] = (
extras_require['docs'] + extras_require['checks']
extras_require['docs'] + extras_require['checks'] + extras_require['tests']
)

setup(
Expand Down
27 changes: 27 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os

import vcr

from lyricsgenius import Genius


# Import client access token from environment variable
access_token = os.environ.get("GENIUS_ACCESS_TOKEN", None)
assert access_token is not None, (
"Must declare environment variable: GENIUS_ACCESS_TOKEN")

# Genius client
genius = Genius(access_token, sleep_time=1.0, timeout=15)

cassettes_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'fixtures/cassettes'
)

# VCR Configs
test_vcr = vcr.VCR(
path_transformer=vcr.VCR.ensure_suffix('.yaml'),
serializer='yaml',
cassette_library_dir=cassettes_path,
filter_headers=['authorization']
)
11 changes: 7 additions & 4 deletions tests/test_album.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import unittest
import os
import warnings

try:
from .test_genius import genius
except ModuleNotFoundError:
from test_genius import genius
import vcr

from . import genius, test_vcr
from lyricsgenius.types import Album


class TestAlbum(unittest.TestCase):

@classmethod
@test_vcr.use_cassette(path_transformer=vcr.VCR.ensure_suffix(' album.yaml'),
serializer='yaml')
def setUpClass(cls):
print("\n---------------------\nSetting up Album tests...\n")
warnings.simplefilter("ignore", ResourceWarning)
cls.album_name = "The Party"
cls.artist_name = "Andy Shauf"
cls.num_songs = 10
Expand Down
11 changes: 7 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import unittest

try:
from .test_genius import genius
except ModuleNotFoundError:
from test_genius import genius

from . import genius, test_vcr


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

@test_vcr.use_cassette
def test_account(self):
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)

@test_vcr.use_cassette
def test_annotation(self):
msg = "Returned annotation API path is different than expected."
id_ = 10225840
Expand All @@ -26,6 +26,7 @@ def test_annotation(self):
expected = '/annotations/10225840'
self.assertEqual(real, expected, msg)

@test_vcr.use_cassette
def test_manage_annotation(self):
example_text = 'The annotation'
new_annotation = genius.create_annotation(
Expand Down Expand Up @@ -64,6 +65,7 @@ def test_manage_annotation(self):
r = genius.delete_annotation(new_annotation['id'])
self.assertEqual(r, 204, msg)

@test_vcr.use_cassette
def test_referents_web_page(self):
msg = "Returned referent API path is different than expected."
id_ = 10347
Expand All @@ -82,6 +84,7 @@ def test_referents_invalid_input(self):
with self.assertRaises(AssertionError):
genius.referents(song_id=1, web_page_id=1)

@test_vcr.use_cassette
def test_web_page(self):
msg = "Returned web page API path is different than expected."
url = "https://docs.genius.com"
Expand Down
21 changes: 17 additions & 4 deletions tests/test_artist.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import unittest
import os

try:
from .test_genius import genius
except ModuleNotFoundError:
from test_genius import genius

import vcr

from . import genius, test_vcr
from lyricsgenius.types import Artist
from lyricsgenius.utils import sanitize_filename


class TestArtist(unittest.TestCase):

@classmethod
@test_vcr.use_cassette(path_transformer=vcr.VCR.ensure_suffix(' artist.yaml'),
serializer='yaml')
def setUpClass(cls):
print("\n---------------------\nSetting up Artist tests...\n")

cls.artist_name = "The Beatles"
cls.new_song = "Paperback Writer"
cls.max_songs = 2
Expand All @@ -24,12 +27,15 @@ def test_artist(self):
msg = "The returned object is not an instance of the Artist class."
self.assertIsInstance(self.artist, Artist, msg)

@test_vcr.use_cassette(path_transformer=vcr.VCR.ensure_suffix('.yaml'),
serializer='yaml')
def test_correct_artist_name(self):
msg = "Returned artist name does not match searched artist."
name = "Queen"
result = genius.search_artist(name, max_songs=1).name
self.assertEqual(name, result, msg)

@test_vcr.use_cassette
def test_zero_songs(self):
msg = "Songs were downloaded even though 0 songs was requested."
name = "Queen"
Expand All @@ -40,21 +46,28 @@ def test_name(self):
msg = "The artist object name does not match the requested artist name."
self.assertEqual(self.artist.name, self.artist_name, msg)

@test_vcr.use_cassette(path_transformer=vcr.VCR.ensure_suffix('.yaml'),
serializer='yaml')
def test_add_song_from_same_artist(self):
msg = "The new song was not added to the artist object."
self.artist.add_song(genius.search_song(self.new_song, self.artist_name))
self.assertEqual(self.artist.num_songs, self.max_songs + 1, msg)

@test_vcr.use_cassette
def test_song(self):
msg = "Song was not in artist's songs."
song = self.artist.song(self.new_song)
self.assertIsNotNone(song, msg)

@test_vcr.use_cassette(path_transformer=vcr.VCR.ensure_suffix('.yaml'),
serializer='yaml')
def test_add_song_from_different_artist(self):
msg = "A song from a different artist was incorrectly allowed to be added."
self.artist.add_song(genius.search_song("These Days", "Jackson Browne"))
self.assertEqual(self.artist.num_songs, self.max_songs, msg)

@test_vcr.use_cassette(path_transformer=vcr.VCR.ensure_suffix('.yaml'),
serializer='yaml')
def test_artist_with_includes_features(self):
# The artist did not get songs returned that they were featured in.
name = "Swae Lee"
Expand Down
1 change: 1 addition & 0 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
from unittest.mock import MagicMock, patch


from lyricsgenius import OAuth2

client_id = os.environ["GENIUS_CLIENT_ID"]
Expand Down
6 changes: 2 additions & 4 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

from requests.exceptions import HTTPError

try:
from .test_genius import genius
except ModuleNotFoundError:
from test_genius import genius
from . import genius, test_vcr


class TestAPIBase(unittest.TestCase):
Expand All @@ -14,6 +11,7 @@ class TestAPIBase(unittest.TestCase):
def setUpClass(cls):
print("\n---------------------\nSetting up API base tests...\n")

@test_vcr.use_cassette
def test_http_error_handler(self):
status_code = None
try:
Expand Down
21 changes: 13 additions & 8 deletions tests/test_genius.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import os
import unittest

from lyricsgenius import Genius
import vcr


# Import client access token from environment variable
access_token = os.environ.get("GENIUS_ACCESS_TOKEN", None)
assert access_token is not None, (
"Must declare environment variable: GENIUS_ACCESS_TOKEN")
genius = Genius(access_token, sleep_time=1.0, timeout=15)
from . import genius, test_vcr


class TestEndpoints(unittest.TestCase):

@classmethod
@test_vcr.use_cassette(path_transformer=vcr.VCR.ensure_suffix(' endpoints.yaml'),
serializer='yaml')
def setUpClass(cls):
print("\n---------------------\nSetting up Endpoint tests...\n")

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

@test_vcr.use_cassette(path_transformer=vcr.VCR.ensure_suffix('.yaml'),
serializer='yaml')
def test_search_song(self):
artist = "Jay-Z"
# Empty response
Expand Down Expand Up @@ -50,6 +49,7 @@ def test_search_song(self):
response = genius.search_song(self.song_title_only, artist="Drake")
self.assertFalse(response.title.lower() == self.song_title_only.lower())

@test_vcr.use_cassette
def test_song_annotations(self):
msg = "Incorrect song annotation response."
r = sorted(genius.song_annotations(1))
Expand Down Expand Up @@ -86,6 +86,7 @@ class TestLyrics(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("\n---------------------\nSetting up lyrics tests...\n")

cls.song_url = "https://genius.com/Andy-shauf-begin-again-lyrics"
cls.song_id = 2885745
cls.lyrics_ending = (
Expand All @@ -95,10 +96,14 @@ def setUpClass(cls):
"\nI wonder who you’re thinking of"
)

@test_vcr.use_cassette(path_transformer=vcr.VCR.ensure_suffix('.yaml'),
serializer='yaml')
def test_lyrics_with_url(self):
lyrics = genius.lyrics(self.song_url)
self.assertTrue(lyrics.endswith(self.lyrics_ending))

@test_vcr.use_cassette(path_transformer=vcr.VCR.ensure_suffix('.yaml'),
serializer='yaml')
def test_lyrics_with_id(self):
lyrics = genius.lyrics(self.song_id)
self.assertTrue(lyrics.endswith(self.lyrics_ending))
Loading