From 6046e12e01eb648d7c38d97f3948b911599ba020 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 21 Feb 2019 10:01:29 -0600 Subject: [PATCH] Add tests for settings_menu. Add tests for call_set_default_chart_save_location, return_to_main_menu, settings_menu_options. Signed-off-by: David --- .../UI_menus_tests/test_settings_menu.py | 92 ++++++++++++++----- 1 file changed, 71 insertions(+), 21 deletions(-) diff --git a/test_suite/UI_menus_tests/test_settings_menu.py b/test_suite/UI_menus_tests/test_settings_menu.py index 7ec757f0..8bebbb1f 100644 --- a/test_suite/UI_menus_tests/test_settings_menu.py +++ b/test_suite/UI_menus_tests/test_settings_menu.py @@ -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, ) @@ -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. @@ -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.") -""" \ No newline at end of file + mocked_print.assert_called_once_with(self.return_to_main_menu_print_stmt)