Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use MagicMock for unit testing WorkbenchConfig.py #697

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 30 additions & 27 deletions tests/unit_tests_workbench_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__))))
Expand All @@ -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:
Expand All @@ -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, \
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -108,7 +108,8 @@ 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

with patch('WorkbenchConfig.issue_request') as mocked_issue_request, \
patch('WorkbenchConfig.logging') as mocked_logging, \
Expand All @@ -133,7 +134,8 @@ 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

with patch('WorkbenchConfig.issue_request') as mocked_issue_request, \
patch('WorkbenchConfig.logging') as mocked_logging:
Expand All @@ -145,17 +147,18 @@ 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)

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:
Expand All @@ -167,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()
unittest.main()