Skip to content

Commit

Permalink
Add mocked tests for the main function, will revisit
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Tracy committed Jun 5, 2015
1 parent 0fa4eeb commit 4a84148
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion idonethis.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def main():
try:
submit_done(session, team, done_text)
except Exception as exception:
exit("Failed to record what you've done: {}".format(exception))
sys.exit("Failed to record what you've done: {}".format(exception))

print("Recorded what you've done, keep up the good work!")

Expand Down
49 changes: 49 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,52 @@ def test_stdin_available(self, stdin):
@mock.patch('idonethis.subprocess.call')
def test_bringing_up_an_editor(self, subprocess_call):
self.assertEqual('Today I did... ', idonethis.get_done_text())


class TestMainFunction(BetamaxMixn, unittest.TestCase):

@mock.patch('sys.exit')
@mock.patch('sys.stdin')
@mock.patch('idonethis.parser')
@mock.patch('idonethis.requests')
def test_no_done_text_provided_in_cli(self, requests, parser, stdin, exit_):
requests.Session.return_value = self.session
parser.parse_args.return_value = mock.Mock(
message=None,
team='aweber-be-bof',
token=os.environ.get('IDONETHIS_TOKEN', 'x' * 20))

stdin.isatty.return_value = False
stdin.read.return_value = 'testing'

with self.vcr.use_cassette('good_post_request'):
idonethis.main()

self.assertFalse(exit_.called)

@mock.patch('idonethis.requests')
@mock.patch('idonethis.parser')
@mock.patch('idonethis.get_done_text')
def test_done_text_provided_in_cli(self, get_done_text, parser, requests):
requests.Session.return_value = self.session
parser.parse_args.return_value = mock.Mock(
message='I did it!',
team='aweber-be-bof',
token=os.environ.get('IDONETHIS_TOKEN', 'x' * 20))

with self.vcr.use_cassette('good_post_request'):
idonethis.main()

self.assertFalse(get_done_text.called)

@mock.patch('sys.exit')
@mock.patch('idonethis.parser')
@mock.patch('idonethis.requests')
def test_unhandled_exception_occurs(self, requests, parser, exit_):
requests.Session.return_value = self.session
parser.parse_args.return_value = mock.Mock()

with self.vcr.use_cassette('good_post_request'):
idonethis.main()

self.assertTrue(exit_.called)

0 comments on commit 4a84148

Please sign in to comment.