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 6 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
8 changes: 6 additions & 2 deletions tests/test_album.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import unittest
import os

import vcr

try:
from .test_genius import genius
from .test_genius import genius, test_vcr
except ModuleNotFoundError:
from test_genius import genius
from test_genius import genius, test_vcr
johnwmillr marked this conversation as resolved.
Show resolved Hide resolved
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")
cls.album_name = "The Party"
Expand Down
9 changes: 7 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import unittest

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


class TestAPI(unittest.TestCase):
Expand All @@ -12,12 +12,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 +28,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 +67,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 +86,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
18 changes: 16 additions & 2 deletions tests/test_artist.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import unittest
import os

import vcr

try:
from .test_genius import genius
from .test_genius import genius, test_vcr
except ModuleNotFoundError:
from test_genius import genius
from test_genius 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"
Expand All @@ -24,12 +28,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 +47,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
5 changes: 3 additions & 2 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from requests.exceptions import HTTPError

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


class TestAPIBase(unittest.TestCase):
Expand All @@ -14,6 +14,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
18 changes: 18 additions & 0 deletions tests/test_genius.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import unittest

import vcr

from lyricsgenius import Genius


Expand All @@ -10,16 +12,27 @@
"Must declare environment variable: GENIUS_ACCESS_TOKEN")
genius = Genius(access_token, sleep_time=1.0, timeout=15)

test_vcr = vcr.VCR(
path_transformer=vcr.VCR.ensure_suffix('.yaml'),
serializer='yaml',
cassette_library_dir='tests/fixtures/cassettes',
filter_headers=['authorization']
)


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 +63,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 @@ -95,10 +109,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