Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Test unsupported_precheck in __main__.py

Signed-off-by: Aadi Bajpai <[email protected]>
  • Loading branch information
aadibajpai committed Aug 14, 2019
1 parent 0bd4dce commit 9c538d2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
3 changes: 3 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SwagLyrics for Spotify
# Copyright (c) 2019 Aadi Bajpai
# The SwagLyrics Project
13 changes: 13 additions & 0 deletions tests/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# SwagLyrics for Spotify
# Copyright (c) 2019 Aadi Bajpai
# The SwagLyrics Project


class R:
"""
This is a fake class created to mock requests' status code
"""

def __init__(self, status_code=7355608, text='google this number'):
self.status_code = status_code
self.text = text
24 changes: 8 additions & 16 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,7 @@
from swaglyrics.cli import stripper, lyrics, get_lyrics, clear
from swaglyrics import unsupported_txt
from mock import patch
from time import sleep


class R:
"""
This is a fake class created to mock requests' status code
"""

def __init__(self, status_code=7355608, text='google this number'):
self.status_code = status_code
self.text = text
from tests.base import R


class Tests(unittest.TestCase):
Expand All @@ -33,7 +23,7 @@ def test_that_stripping_works(self):
Test that stripping works
"""
self.assertEqual(stripper('River (feat. Ed Sheeran)', 'Eminem'), 'Eminem-River')
self.assertEqual(stripper('Ain\'t My Fault - R3hab Remix', 'Zara Larsson'), 'Zara-Larsson-Aint-My-Fault')
self.assertEqual(stripper("Ain't My Fault - R3hab Remix", 'Zara Larsson'), 'Zara-Larsson-Aint-My-Fault')
self.assertEqual(stripper('1800-273-8255', 'Logic'), 'Logic-1800-273-8255')
self.assertEqual(stripper('Garota', 'Erlend Øye'), 'Erlend-ye-Garota')
self.assertEqual(stripper('Scream & Shout', 'will.i.am'), 'william-Scream-and-Shout')
Expand All @@ -52,10 +42,11 @@ def test_that_no_song_or_artist_does_not_break_stuff(self):
"""
Test that None parameters in lyrics function does not break stuff
"""
self.assertEqual(lyrics(None, 'lol'), 'Nothing playing at the moment.')
self.assertEqual(lyrics('', None), 'Nothing playing at the moment.')
self.assertEqual(lyrics(None, None), 'Nothing playing at the moment.')
self.assertEqual(lyrics('', 'lol'), 'Nothing playing at the moment.')
self.assertEqual(lyrics('lol', ''), 'Nothing playing at the moment.')
self.assertEqual(lyrics('', ''), 'Nothing playing at the moment.')

# Integration test
def test_that_get_lyrics_works(self):
"""
Test that get_lyrics function works
Expand All @@ -75,7 +66,7 @@ def test_that_get_lyrics_does_not_break_with_wrong_data(self, fake_get):
self.assertEqual(get_lyrics(
"xyzzy", "Yeet"), None)
self.assertEqual(get_lyrics("aifone", "Muhmello"), None)
self.assertEqual(get_lyrics("Pixel2XL", "Elgoog"), None)
self.assertEqual(get_lyrics("Pixel2XL", "Goog-el"), None)

@patch('swaglyrics.cli.get_lyrics')
def test_that_lyrics_works_for_unsupported_songs(self, fake_get_lyrics):
Expand Down Expand Up @@ -112,6 +103,7 @@ def test_that_lyrics_do_not_break_with_file_not_found(self, fake_get_lyrics):
resp = lyrics("Pixel2XL", "Elgoog", False)
self.assertEqual(resp, "Couldn't get lyrics for Pixel2XL by Elgoog.\n")

# Integration test
def test_database_for_unsupported_song(self):
"""
test that the database set on pythonanywhere is working and giving strippers for unsupported songs
Expand Down
40 changes: 31 additions & 9 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io
import sys
from swaglyrics.__main__ import main, unsupported_precheck, unsupported_txt
from tests.base import R
from mock import patch


Expand All @@ -24,22 +25,42 @@ class Tests(unittest.TestCase):
def setup(self):
pass

# @mock.patch('swaglyrics.__version__', '0.2.9')
@patch('swaglyrics.__main__.version', '0.2.6')
@patch('swaglyrics.__main__.requests.get')
def test_that_unsupported_precheck_works(self, fake_get):
# fake_txt = 'unsupported txt test'
# fake_ver = '0.2.8'
# fake_get.text = fake_ver
# fake_get.side_effect = [fake_ver, fake_txt]
def test_that_unsupported_precheck_works_normally(self, fake_get):
fake_txt = 'unsupported txt test'
fake_get.side_effect = [R(200, '0.2.9'), R(200, fake_txt)]
capturedOutput = io.StringIO()
sys.stdout = capturedOutput
unsupported_precheck()
sys.stdout = sys.__stdout__
self.assertIn("New version of SwagLyrics available: v0.2.9\nPlease update :)", capturedOutput.getvalue())
self.assertIn("Updated unsupported.txt successfully.", capturedOutput.getvalue())
data = unsupported_txt_data()
self.assertEqual(data, fake_txt)

@patch('swaglyrics.__main__.requests.get')
def test_that_unsupported_precheck_works_on_requests_errors(self, fake_get):
fake_get.side_effect = requests.exceptions.RequestException
capturedOutput = io.StringIO()
sys.stdout = capturedOutput
unsupported_precheck()
sys.stdout = sys.__stdout__
# self.assertIn("New version of SwagLyrics available: v0.2.9\nPlease update :)", capturedOutput.getvalue())
self.assertNotIn("New version of SwagLyrics available:", capturedOutput.getvalue())
self.assertIn("Could not update unsupported.txt successfully.", capturedOutput.getvalue())
# data = unsupported_txt_data()
# self.assertEqual(data, fake_txt)

@patch('swaglyrics.__main__.requests.get')
def test_that_unsupported_precheck_works_on_permission_error(self, fake_get):
fake_get.side_effect = [requests.exceptions.RequestException, PermissionError]
capturedOutput = io.StringIO()
sys.stdout = capturedOutput
with self.assertRaises(SystemExit) as se:
unsupported_precheck()
sys.stdout = sys.__stdout__
self.assertEqual(se.exception.code, 1)
self.assertNotIn("New version of SwagLyrics available:", capturedOutput.getvalue())
self.assertIn("You should install SwagLyrics as --user or use sudo to access unsupported.txt.",
capturedOutput.getvalue())

@patch('argparse.ArgumentParser.parse_args', return_value=argparse.Namespace(tab=False, cli=False))
def test_parser_prints_description(self, mock_argparse):
Expand All @@ -50,6 +71,7 @@ def test_parser_prints_description(self, mock_argparse):
sys.stdout = capturedOutput
main()
sys.stdout = sys.__stdout__
# the newline is necessary since argparse wraps it there in terminal
self.assertIn("Get lyrics for the currently playing song on Spotify. Either --tab or --cli is\nrequired.",
capturedOutput.getvalue())

Expand Down

0 comments on commit 9c538d2

Please sign in to comment.