hackernews: handle unicode & html escapes

add an end-to-end test that also checks that the remote API works
This commit is contained in:
Ryan Hitchman 2015-07-24 13:26:36 -07:00
parent a3e1b452e7
commit 7eff869123
1 changed files with 23 additions and 2 deletions

View File

@ -1,3 +1,5 @@
import unittest
from util import http, hook from util import http, hook
@ -7,8 +9,27 @@ def hackernews(match):
entry = http.get_json(base_api + match.group(1) + ".json") entry = http.get_json(base_api + match.group(1) + ".json")
if entry['type'] == "story": if entry['type'] == "story":
return "{title} by {by} with {score} points and {descendants} comments ({url})".format(**entry) entry['title'] = http.unescape(entry['title'])
return u"{title} by {by} with {score} points and {descendants} comments ({url})".format(**entry)
if entry['type'] == "comment": if entry['type'] == "comment":
return '"{text}" -- {by}'.format(**entry) entry['text'] = http.unescape(entry['text'])
return u'"{text}" -- {by}'.format(**entry)
class Test(unittest.TestCase):
def news(self, inp):
re = hackernews._hook[0][1][1]['re']
return hackernews(re.search(inp))
def test_story(self):
assert 'Desalination' in self.news('https://news.ycombinator.com/item?id=9943431')
def test_comment(self):
res = self.news('https://news.ycombinator.com/item?id=9943987')
assert 'kilowatt hours' in res
assert 'oaktowner' in res
def test_comment_encoding(self):
res = self.news('https://news.ycombinator.com/item?id=9943897')
assert 'abominations' in res
assert '> ' in res # encoding