Skip to content

Commit

Permalink
Add tests for settings_menu.
Browse files Browse the repository at this point in the history
Add tests for call_set_default_chart_save_location, return_to_main_menu,
settings_menu_options.

Signed-off-by: David <toonarmycaptain@hotmail.com>
  • Loading branch information
toonarmycaptain committed Feb 21, 2019
1 parent 75ae12b commit 6046e12
Showing 1 changed file with 71 additions and 21 deletions.
92 changes: 71 additions & 21 deletions test_suite/UI_menus_tests/test_settings_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from unittest import TestCase, mock
from unittest.mock import patch

from dionysus_app.UI_menus.settings_menu import (run_settings_menu,
from dionysus_app.UI_menus.settings_menu import (call_set_default_chart_save_location,
return_to_main_menu,
run_settings_menu,
settings_menu_options,
take_settings_menu_input,
)

Expand All @@ -29,13 +32,33 @@ def test_run_settings_menu(self,
assert mocked_take_settings_menu_input.call_args_list == main_menu_calls


class TestSettingsMenuOptions(TestCase):
def setUp(self):
self.expected_print_stmts = [
"Dionysus - Settings\n",
("Please select an option by entering the corresponding number, and press return:\n"
" 1. Change default chart save location.\n"
" \n"
" 0. Return to main menu."
),
]

@patch('dionysus_app.UI_menus.settings_menu.print')
def test_settings_menu_options(self, mocked_print):
assert settings_menu_options() is None

mocked_print_call_args_list = [mock.call(print_stmt)
for print_stmt in self.expected_print_stmts]
assert mocked_print.call_args_list == mocked_print_call_args_list


class TestTakeSettingsMenuInput(TestCase):
def setUp(self):
self.return_to_main_menu_input = '0'

self.valid_input_option_mock = [
('1', 'dionysus_app.UI_menus.settings_menu.call_set_default_chart_save_location'),
('0', 'dionysus_app.UI_menus.settings_menu.return_to_main_menu'),
('1', 'dionysus_app.UI_menus.settings_menu.call_set_default_chart_save_location', None),
# ('0', 'dionysus_app.UI_menus.settings_menu.return_to_main_menu', True),
]

self.invalid_inputs = ['', # No input, return pressed.
Expand All @@ -59,29 +82,56 @@ def test_take_settings_menu_input_return_to_main_menu(self,
mocked_return_to_main_menu):
with self.subTest(msg=f'Return to main menu input: '
f'{self.return_to_main_menu_input} should return True.'):
mocked_input.return_value = '0' #self.return_to_main_menu_input

# mocked_return_to_main_menu.return_value = True
mocked_input.return_value = '0' # self.return_to_main_menu_input

assert take_settings_menu_input() is True

assert mocked_return_to_main_menu.called_once_with()

"""
@patch('dionysus_app.UI_menus.settings_menu.input')
def test_take_main_menu_input_all_valid_inputs(self, mocked_input):
for user_input, called_function, returned_value in self.valid_input_option_mock:
with self.subTest(msg=f'input={user_input}, should call {called_function}'
), patch(called_function) as mock_called_option:
mocked_input.return_value = user_input

assert take_settings_menu_input() is returned_value

mock_called_option.assert_called()

mocked_input.reset_mock(return_value=True)

possible_options = {
'1': call_set_default_chart_save_location,
'0': return_to_main_menu,
}
@patch('dionysus_app.UI_menus.settings_menu.print')
@patch('dionysus_app.UI_menus.settings_menu.input')
def test_take_settings_menu_input_bad_input(self, mocked_input, mocked_print):
mocked_input.side_effect = self.bad_input_user_inputs

assert take_settings_menu_input() is True # Final return valid quit input.
assert mocked_print.call_args_list == [mock.call(self.invalid_input_print_stmt)
for invalid_input in self.invalid_inputs]


class TestCallSetDefaultChartSaveLocation(TestCase):
def setUp(self):
self.after_call_print_stmt = '\n\n'

@patch('dionysus_app.UI_menus.settings_menu.print')
@patch('dionysus_app.UI_menus.settings_menu.set_default_chart_save_location')
def test_call_set_default_chart_save_location(self,
mocked_set_default_chart_save_location,
mocked_print):
assert call_set_default_chart_save_location() is None

mocked_set_default_chart_save_location.assert_called_once_with(user_set=True)
mocked_print.assert_called_once_with(self.after_call_print_stmt)


class TestReturnToMainMenu(TestCase):
def setUp(self):
self.return_to_main_menu_print_stmt = 'Returning to main menu...\n\n\n'

while True:
chosen_option = input('>>> ')
@patch('dionysus_app.UI_menus.settings_menu.print')
def test_return_to_main_menu(self, mocked_print):
assert return_to_main_menu() is False

if chosen_option in possible_options:
possible_options[chosen_option]()
break # Exit loop when chosen action finishes. Returns None.
if chosen_option == '0': # User selects to return to main menu.
return True
# else:
print("Invalid input.")
"""
mocked_print.assert_called_once_with(self.return_to_main_menu_print_stmt)

0 comments on commit 6046e12

Please sign in to comment.