From a68d7900378ca9db9f0bd80224bf03b97116b731 Mon Sep 17 00:00:00 2001 From: Yamil Suarez Date: Wed, 11 Oct 2023 21:26:49 -0400 Subject: [PATCH 1/2] Update code to use MagicMock --- tests/unit_tests_workbench_config.py | 41 ++++++++++++++++------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/tests/unit_tests_workbench_config.py b/tests/unit_tests_workbench_config.py index 14f8e7df..7426781a 100644 --- a/tests/unit_tests_workbench_config.py +++ b/tests/unit_tests_workbench_config.py @@ -2,7 +2,7 @@ import sys import unittest from unittest.mock import patch -import argparse +from unittest.mock import MagicMock from collections import namedtuple sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) @@ -12,23 +12,20 @@ class TestWorkbenchConfig(unittest.TestCase): def setUp(self) -> None: - parser = argparse.ArgumentParser() - parser.add_argument('--config', required=True, help='Configuration file to use.') - parser.add_argument('--check', help='Check input data and exit without creating/updating/etc.', - action='store_true') - parser.add_argument('--get_csv_template', - help='Generate a CSV template using the specified configuration file.', action='store_true') - parser.add_argument('--quick_delete_node', - help='Delete the node (and all attached media) identified by the URL).') - parser.add_argument('--quick_delete_media', help='Delete the media (and attached file) identified by the URL).') - parser.add_argument('--contactsheet', help='Generate a contact sheet.', action='store_true') - parser.add_argument('--version', action='version', version='Islandora Workbench 0.0.0') - self.parser = parser + mock_argparse_parser = MagicMock() + + mock_argparse_parser.config = '' + mock_argparse_parser.check = False + mock_argparse_parser.get_csv_template = False + mock_argparse_parser.contactsheet = False + + self.parser = mock_argparse_parser def test_init_path_check_invalid_file(self): test_file_name = '/file/does/not/exist.yml' - args = self.parser.parse_args(['--config', test_file_name]) + self.parser.config = test_file_name + args = self.parser with self.assertRaises(SystemExit) as exit_return, \ patch('WorkbenchConfig.logging') as mocked_logging: @@ -45,7 +42,8 @@ def test_init_path_check_invalid_file(self): def test_init_path_check_valid_file(self): test_file_name = 'tests/assets/execute_bootstrap_script_test/config.yml' - args = self.parser.parse_args(['--config', test_file_name]) + self.parser.config = test_file_name + args = self.parser with patch('sys.exit', side_effect=lambda x: None) as mock_exit, \ patch('WorkbenchConfig.WorkbenchConfig.validate') as mocked_validate, \ @@ -63,7 +61,8 @@ def test_init_path_check_valid_file(self): def test_get_config_valid_config_file_01(self): test_file_name = 'tests/assets/WorkbenchConfig_test/config_01_create_short_valid.yml' - args = self.parser.parse_args(['--config', test_file_name]) + self.parser.config = test_file_name + args = self.parser with patch('WorkbenchConfig.WorkbenchConfig.validate') as mocked_validate, \ patch('WorkbenchConfig.logging') as mocked_logging: @@ -88,7 +87,8 @@ def test_get_config_valid_config_file_01(self): def test_init_validate_valid(self): test_file_name = 'tests/assets/WorkbenchConfig_test/config_01_create_short_valid.yml' - args = self.parser.parse_args(['--config', test_file_name]) + self.parser.config = test_file_name + args = self.parser with patch('WorkbenchConfig.issue_request') as mocked_issue_request, \ patch('WorkbenchConfig.logging') as mocked_logging: @@ -109,6 +109,8 @@ def test_init_validate_invalid_content_type(self): test_file_name = 'tests/assets/WorkbenchConfig_test/config_02_01_create_short_invalid.yml' args = self.parser.parse_args(['--config', test_file_name]) + self.parser.config = test_file_name + args = self.parser with patch('WorkbenchConfig.issue_request') as mocked_issue_request, \ patch('WorkbenchConfig.logging') as mocked_logging, \ @@ -134,6 +136,8 @@ def test_init_validate_invalid_mutators_01(self): test_file_name = 'tests/assets/WorkbenchConfig_test/config_02_02_create_short_invalid.yml' args = self.parser.parse_args(['--config', test_file_name]) + self.parser.config = test_file_name + args = self.parser with patch('WorkbenchConfig.issue_request') as mocked_issue_request, \ patch('WorkbenchConfig.logging') as mocked_logging: @@ -155,7 +159,8 @@ def test_init_validate_invalid_mutators_01(self): def test_init_validate_invalid_mutators_02(self): test_file_name = 'tests/assets/WorkbenchConfig_test/config_02_03_create_short_invalid.yml' - args = self.parser.parse_args(['--config', test_file_name]) + self.parser.config = test_file_name + args = self.parser with patch('WorkbenchConfig.issue_request') as mocked_issue_request, \ patch('WorkbenchConfig.logging') as mocked_logging: From a7ace12ee84dcd9f3e3d1b0a0ee267536b44dce1 Mon Sep 17 00:00:00 2001 From: Yamil Suarez Date: Wed, 11 Oct 2023 21:27:50 -0400 Subject: [PATCH 2/2] Fix small errors --- tests/unit_tests_workbench_config.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tests/unit_tests_workbench_config.py b/tests/unit_tests_workbench_config.py index 7426781a..d593ed49 100644 --- a/tests/unit_tests_workbench_config.py +++ b/tests/unit_tests_workbench_config.py @@ -108,7 +108,6 @@ def test_init_validate_valid(self): def test_init_validate_invalid_content_type(self): test_file_name = 'tests/assets/WorkbenchConfig_test/config_02_01_create_short_invalid.yml' - args = self.parser.parse_args(['--config', test_file_name]) self.parser.config = test_file_name args = self.parser @@ -135,7 +134,6 @@ def test_init_validate_invalid_content_type(self): def test_init_validate_invalid_mutators_01(self): test_file_name = 'tests/assets/WorkbenchConfig_test/config_02_02_create_short_invalid.yml' - args = self.parser.parse_args(['--config', test_file_name]) self.parser.config = test_file_name args = self.parser @@ -149,9 +147,9 @@ def test_init_validate_invalid_mutators_01(self): mocked_issue_request.return_value = fake_response # Error text should only be this line, therefore use ^ and $ at the start and end of the message respectively - error_message = "^Error: You may only select one of \['use_node_title_for_media', " \ - + "'use_nid_in_media_title', 'field_for_media_title'\].\n - This config has selected " \ - + "\['use_node_title_for_media', 'use_nid_in_media_title'\].\n$" + error_message = r"^Error: You may only select one of \['use_node_title_for_media', " \ + + r"'use_nid_in_media_title', 'field_for_media_title'\].\n - This config has selected " \ + + r"\['use_node_title_for_media', 'use_nid_in_media_title'\].\n$" with self.assertRaisesRegex(SystemExit, error_message) as exit_return: test_config_obj = WorkbenchConfig(args) @@ -172,13 +170,13 @@ def test_init_validate_invalid_mutators_02(self): mocked_issue_request.return_value = fake_response # Error text should only be this line, therefore use ^ and $ at the start and end of the message respectively - error_message = "^Error: You may only select one of \['use_node_title_for_media', " \ - + "'use_nid_in_media_title', 'field_for_media_title'\].\n - This config has selected " \ - + "\['use_node_title_for_media', 'field_for_media_title'\].\n$" + error_message = r"^Error: You may only select one of \['use_node_title_for_media', " \ + + r"'use_nid_in_media_title', 'field_for_media_title'\].\n - This config has selected " \ + + r"\['use_node_title_for_media', 'field_for_media_title'\].\n$" with self.assertRaisesRegex(SystemExit, error_message) as exit_return: test_config_obj = WorkbenchConfig(args) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main()