From 0fe7425410ebd863135a80e291306527fba62001 Mon Sep 17 00:00:00 2001 From: Prajwal <2017ugcs059@nitjsr.ac.in> Date: Thu, 16 May 2019 17:09:45 +0530 Subject: [PATCH] Added Tests for brainyquotes scrapper and fixed bugs in it - Added tests for brainyquotes scrapper - Fixed bugs where the scrapper returned quotes in form of (quote1, quote2, ..., author) instead of ((quote1,author), (quote2,author)... ) --- pyquotes/brainyquote.py | 23 +++++++------- pyquotes/test_brainyquotes.py | 56 +++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 pyquotes/test_brainyquotes.py diff --git a/pyquotes/brainyquote.py b/pyquotes/brainyquote.py index 1da25e1..121ff9a 100644 --- a/pyquotes/brainyquote.py +++ b/pyquotes/brainyquote.py @@ -38,6 +38,8 @@ def get_quotes(person, category): respone_author = requests.get(URL) soup_author = BeautifulSoup(respone_author.content, 'html5lib') categories = soup_author.find_all('div', class_='kw-box') + category = category.lower() + person = person.lower() check = False count = 0 for i in categories: @@ -51,16 +53,16 @@ def get_quotes(person, category): # Getting the quote of the related author get_quote = soup_author.find_all('a', attrs={'title': 'view quote'}) quote_list = [] - big_list = [] for i in range(count): - quote_list.append(get_quote[i].text) - big_list.append(quote_list) + quote_tuple = [] + quote_tuple.append(get_quote[i].text) + quote_tuple.append(person) + quote_list.append(tuple(quote_tuple)) + + error_message = '''Oops! It seems that there are no quotes of the author of that category. \nYou may consider changing the category or the author ''' if len(quote_list) == 0: - return('''Oops! It seems that there are no quotes of the author of that - category. - \nYou may consider changing the category or the author ''') - quote_list.append(person) + return(error_message) return(quote_list) @@ -76,14 +78,15 @@ def get_quote(person, category): """ quotes = get_quotes(person, category) length = len(quotes) - if(length == 0): + error_message = '''Oops! It seems that there are no quotes of the author of that category. \nYou may consider changing the category or the author ''' + + if(quotes == error_message): # In case no quote of the author exist for that category. - return("No quotes found of that category") + return(error_message) else: random_number = random.randint(0, length - 1) list = [] list.append(quotes[random_number]) - list.append(person) return(tuple(list)) diff --git a/pyquotes/test_brainyquotes.py b/pyquotes/test_brainyquotes.py new file mode 100644 index 0000000..ee731eb --- /dev/null +++ b/pyquotes/test_brainyquotes.py @@ -0,0 +1,56 @@ +import unittest +import brainyquote + +class SimpleTest(unittest.TestCase): + + def test_quote_of_the_day(self): + # Test case for quote of the day + quote_of_the_day = brainyquote.get_quote_of_the_day() + self.assertTrue(quote_of_the_day, "No quote generated") + length = len(quote_of_the_day) + self.assertEqual(length, 2, "Invalid return format") + quote = quote_of_the_day[0].replace(' ', '') + self.assertNotEqual(quote, "", "Quote is Empty") + author = quote_of_the_day[1].replace(' ', '') + self.assertNotEqual(author, "", "Author Name is Empty") + + def test_no_data(self): + # Test case for input which would give no valid output + error_message = '''Oops! It seems that there are no quotes of the author of that category. \nYou may consider changing the category or the author ''' + quote_list = brainyquote.get_quote("abc", "def") + self.assertEqual(quote_list, error_message) + quote_list = brainyquote.get_quotes("abc", "def") + self.assertEqual(quote_list, error_message) + + def test_for_quote(self): + # Test for checking if mock data is present in the list of quotes + quote = ["Everything has beauty, but not everyone sees it.", "confucius"] + quote_list = brainyquote.get_quotes("confucius", "beauty") + self.assertIn(quote, quote_list, "Wrong quotes are being generated") + + def test_for_single_quote(self): + # Test for checking single quote is from list of all quotes + quote_list = brainyquote.get_quotes("confucius", "brainy") + quote = brainyquote.get_quote("confucius", "brainy") + self.assertIn(quote[0], quote_list, "Wrong quotes are being generated") + + def test_for_author(self): + # Test for checking quote is of correct author + author = "confucius" + quote = brainyquote.get_quote(author, "long") + self.assertEqual(quote[0][1], author, "Wrong author's quote") + + def test_for_case_sensitiveness(self): + # Test for checking if input is not case sensitive + quote_list1 = brainyquote.get_quotes("confucius", "motivational") + quote_list2 = brainyquote.get_quotes("Confucius", "Motivational") + quote_list3 = brainyquote.get_quotes("confucius", "MotiVatIonal") + quote_list4 = brainyquote.get_quotes("CoNfuCius", "motivational") + quote_list5 = brainyquote.get_quotes("conFUCiuS", "moTIvaTIonal") + self.assertEqual(quote_list1, quote_list2, "Input is case sensitive") + self.assertEqual(quote_list1, quote_list3, "Input is case sensitive") + self.assertEqual(quote_list1, quote_list4, "Input is case sensitive") + self.assertEqual(quote_list1, quote_list5, "Input is case sensitive") + +if __name__ == '__main__': + unittest.main()