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 Tests for brainyquotes scrapper and fixed bugs in it #17

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 13 additions & 10 deletions pyquotes/brainyquote.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)

Expand All @@ -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))

Expand Down
56 changes: 56 additions & 0 deletions pyquotes/test_brainyquotes.py
Original file line number Diff line number Diff line change
@@ -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()