Skip to content

Commit

Permalink
added howdoi unittests. pegged requirement version numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
gleitz committed Feb 12, 2013
1 parent 8c7d0df commit 8103cea
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 9 deletions.
17 changes: 10 additions & 7 deletions howdoi/howdoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ def get_links(query):


def get_link_at_pos(links, pos):
pos = pos - 1
for link in links:
if is_question(link):
if pos == 0:
if pos == 1:
break
else:
pos = pos - 1
Expand Down Expand Up @@ -91,13 +90,12 @@ def get_instructions(args):
def howdoi(args):
args['query'] = ' '.join(args['query']).replace('?', '')
try:
instructions = get_instructions(args) or 'Sorry, couldn\'t find any help with that topic\n'
print(instructions)
return get_instructions(args) or 'Sorry, couldn\'t find any help with that topic\n'
except ConnectionError:
print('Failed to establish network connection\n')
return 'Failed to establish network connection\n'


def command_line_runner():
def get_parser():
parser = argparse.ArgumentParser(description='code search tool')
parser.add_argument('query', metavar='QUERY', type=str, nargs='+',
help='the question to answer')
Expand All @@ -107,8 +105,13 @@ def command_line_runner():
parser.add_argument('-l','--link', help='display only the answer link',
action='store_true')
parser.add_argument('-n','--num-answers', help='number of answers to return', default=1, type=int)
return parser


def command_line_runner():
parser = get_parser()
args = vars(parser.parse_args())
howdoi(args)
print(howdoi(args))

if __name__ == '__main__':
command_line_runner()
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
argparse==1.2.1
cssselect==0.7.1
lxml==3.0.1
pyquery==1.2.2
requests==1.1.0
wsgiref==0.1.2
argparse
requests
68 changes: 68 additions & 0 deletions test_howdoi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python

"""Tests for Howdoi."""

import unittest

from howdoi import howdoi


class HowdoiTestCase(unittest.TestCase):

def call_howdoi(self, query):
parser = howdoi.get_parser()
args = vars(parser.parse_args(query.split(' ')))
return howdoi.howdoi(args)

def setUp(self):
self.queries = ['format date bash',
'print stack trace python',
'convert mp4 to animated gif'
'create tar archive']

def tearDown(self):
pass

def test_get_link_at_pos(self):
self.assertEqual(howdoi.get_link_at_pos(['/questions/42/'], 1),
'/questions/42/')
self.assertEqual(howdoi.get_link_at_pos(['/questions/42/'], 2),
'/questions/42/')
self.assertEqual(howdoi.get_link_at_pos(['/howdoi', '/questions/42/'], 1),
'/questions/42/')
self.assertEqual(howdoi.get_link_at_pos(['/howdoi', '/questions/42/'], 2),
'/questions/42/')
self.assertEqual(howdoi.get_link_at_pos(['/questions/42/', '/questions/142/'], 1),
'/questions/42/')

def test_answers(self):
for query in self.queries:
self.assertTrue(self.call_howdoi(query))

def test_answer_links(self):
for query in self.queries:
self.assertTrue('http://' in self.call_howdoi(query + ' -l'))

def test_position(self):
query = self.queries[0]
first_answer = self.call_howdoi(query)
second_answer = self.call_howdoi(query + ' -p2')
self.assertNotEqual(first_answer, second_answer)

def test_all_text(self):
query = self.queries[0]
first_answer = self.call_howdoi(query)
second_answer = self.call_howdoi(query + ' -a')
self.assertNotEqual(first_answer, second_answer)

def test_multiple_answers(self):
query = self.queries[0]
first_answer = self.call_howdoi(query)
second_answer = self.call_howdoi(query + ' -n3')
self.assertNotEqual(first_answer, second_answer)

def test_unicode_answer(self):
assert self.call_howdoi('make a log scale d3')

if __name__ == '__main__':
unittest.main()

0 comments on commit 8103cea

Please sign in to comment.