Skip to content

Commit

Permalink
Updated test cases for handle_user_input
Browse files Browse the repository at this point in the history
  • Loading branch information
elijahsawyers committed Nov 14, 2019
1 parent a320e2a commit 63d2f31
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
8 changes: 5 additions & 3 deletions autostack/error/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,12 @@ def handle_exception(query):
if user_input not in (True, False):
handle_exception(user_input)
return

# Error solved, break out of the loop.
elif user_input:
if user_input is True:
print_listening_for_errors()
return

# Otherwise, the question wasn't answered, keep looping.


Expand All @@ -136,7 +137,8 @@ def handle_user_input():

if user_input not in ('Y', 'n'):
return user_input
elif user_input == 'Y':

if user_input == 'Y':
return True

return False
Expand Down
38 changes: 18 additions & 20 deletions autostack/error/__tests__/test_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
parse_output_for_error,
get_error_from_traceback,
handle_exception,
error_solved,
handle_user_input,
print_listening_for_errors,
)
from autostack.error.__tests__.mock_pipe import MockPipe
Expand Down Expand Up @@ -224,9 +224,9 @@ def mock_print_accepted_post(*args):

return

def mock_error_solved():
def mock_handle_user_input():
'''
Mocks the error_solved function.
Mocks the handle_user_input function.
'''

return True
Expand All @@ -249,8 +249,8 @@ def mock_print_listening_for_errors():
)

monkeypatch.setattr(
'autostack.error.error_solved',
mock_error_solved
'autostack.error.handle_user_input',
mock_handle_user_input
)

monkeypatch.setattr(
Expand All @@ -266,9 +266,9 @@ def mock_print_listening_for_errors():
assert captured.out == u'\U0001F95E Listening for Python errors...\n'


def test_error_solved_y(monkeypatch):
def test_handle_user_input_y(monkeypatch):
'''
Ensures that when 'Y' is inputted, error_solved returns True.
Ensures that when 'Y' is inputted, handle_user_input returns True.
'''

# 1. Given.
Expand All @@ -283,15 +283,15 @@ def mock_input(*args):
monkeypatch.setattr('builtins.input', mock_input)

# 2. When.
is_error_solved = error_solved()
user_input = handle_user_input()

# 3. Then.
assert is_error_solved
assert user_input is True


def test_error_solved_n(monkeypatch):
def test_handle_user_input_n(monkeypatch):
'''
Ensures that when 'n' is inputted, error_solved returns False.
Ensures that when 'n' is inputted, handle_user_input returns False.
'''

# 1. Given.
Expand All @@ -306,15 +306,15 @@ def mock_input(*args):
monkeypatch.setattr('builtins.input', mock_input)

# 2. When.
is_error_solved = error_solved()
user_input = handle_user_input()

# 3. Then.
assert not is_error_solved
assert not user_input


def test_error_solved_invalid_input(capsys, monkeypatch):
def test_handle_user_input_custom_query(monkeypatch):
'''
Ensures that when input is invalid, error_solved prompts the user
Ensures that when input is invalid, handle_user_input prompts the user
to try again.
'''

Expand All @@ -336,19 +336,17 @@ def mock_input(*args):
nonlocal call_count
if call_count == 0:
call_count += 1
return 'a'
return 'Custom query'
return 'Y'
return mock_input

monkeypatch.setattr('builtins.input', make_mock_input())

# 2. When.
is_error_solved = error_solved()
user_input = handle_user_input()

# 3. Then.
captured = capsys.readouterr()
assert captured.out == 'a is not valid input! Please try again.\n'
assert is_error_solved
assert user_input == 'Custom query'


def test_print_listening_for_errors(capsys):
Expand Down

0 comments on commit 63d2f31

Please sign in to comment.