Skip to content

Commit

Permalink
Add more tests for parser
Browse files Browse the repository at this point in the history
  • Loading branch information
martialblog committed Nov 6, 2017
1 parent 8274144 commit 9625bc5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
4 changes: 3 additions & 1 deletion getalltweets/parser.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/usr/bin/env python3


"""
Parses the HTML Response from the Twitter Search page
"""

from sys import stderr
from pyquery import PyQuery


Expand Down Expand Up @@ -47,7 +49,7 @@ def parse(html=None):
return final_tweets

except Exception as exception:
print('Failed to open parse HTML')
print('Failed to parse HTML', file=stderr)
print(exception)
return final_tweets

Expand Down
36 changes: 35 additions & 1 deletion tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pickle
import pytest
import pyquery
import lxml

# Package Imports
import getalltweets.parser as tp
Expand All @@ -23,6 +24,20 @@ def response():

return resp

@pytest.fixture
def failed_response():
"""
Example Twitter Response
"""

dir_path = os.path.dirname(os.path.realpath(__file__))
file_path = os.path.join(dir_path, 'fail.pickle')

with open(file_path, 'rb') as response_file:
resp = pickle.load(response_file)

return resp

def test_tweetcriteria_parse_typecheck(response):
"""
Test that parse type is OK and all tweets are returned
Expand All @@ -36,7 +51,6 @@ def test_tweetcriteria_parse_typecheck(response):
assert(isinstance(actual_parse, list))
assert(isinstance(actual_parse[0], dict))


def test_tweetcriteria_dict(response):
"""
Test format of parse
Expand All @@ -57,3 +71,23 @@ def test_tweetcriteria_dict(response):
'id': '870531084437606402'}

assert(excpected_dict == tweet_dict)

def test_tweetcriteria_empty(failed_response):
"""
Test format of parse
"""

parser = tp.TweetParser()
actual_parse = parser.parse(failed_response)

assert(actual_parse == [])

def test_tweetcriteria_exception(capsys):
"""
Test what happens when pyquery fails to parse
"""

parser = tp.TweetParser()

actual_parse = parser.parse('')
assert(actual_parse == [])

0 comments on commit 9625bc5

Please sign in to comment.