From 9c538d2fc3c21e30982a61dce912e9df2e962393 Mon Sep 17 00:00:00 2001 From: Aadi Bajpai Date: Wed, 14 Aug 2019 23:39:10 +0530 Subject: [PATCH] Add tests Test unsupported_precheck in __main__.py Signed-off-by: Aadi Bajpai --- tests/__init__.py | 3 +++ tests/base.py | 13 +++++++++++++ tests/test_cli.py | 24 ++++++++---------------- tests/test_main.py | 40 +++++++++++++++++++++++++++++++--------- 4 files changed, 55 insertions(+), 25 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/base.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..112cf323 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,3 @@ +# SwagLyrics for Spotify +# Copyright (c) 2019 Aadi Bajpai +# The SwagLyrics Project diff --git a/tests/base.py b/tests/base.py new file mode 100644 index 00000000..08b751cc --- /dev/null +++ b/tests/base.py @@ -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 diff --git a/tests/test_cli.py b/tests/test_cli.py index 55760963..ff39c21d 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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): @@ -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') @@ -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 @@ -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): @@ -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 diff --git a/tests/test_main.py b/tests/test_main.py index 3b6734f7..c532ca6d 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -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 @@ -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): @@ -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())