Fix tests

This commit is contained in:
Tim Bentley 2017-12-09 16:11:45 +00:00
parent 595ae30cd4
commit 17b70f0c0a
4 changed files with 65 additions and 65 deletions

View File

@ -59,8 +59,8 @@ class TestCategoryActionList(TestCase):
self.list.append(self.action1) self.list.append(self.action1)
# THEN: The actions should (not) be in the list. # THEN: The actions should (not) be in the list.
self.assertTrue(self.action1 in self.list) assert self.action1 in self.list
self.assertFalse(self.action2 in self.list) assert self.action2 not in self.list
def test_len(self): def test_len(self):
""" """
@ -69,14 +69,14 @@ class TestCategoryActionList(TestCase):
# GIVEN: The list. # GIVEN: The list.
# WHEN: Do nothing. # WHEN: Do nothing.
# THEN: Check the length. # THEN: Check the length.
self.assertEqual(len(self.list), 0, "The length should be 0.") assert len(self.list) == 0, "The length should be 0."
# GIVEN: The list. # GIVEN: The list.
# WHEN: Append an action. # WHEN: Append an action.
self.list.append(self.action1) self.list.append(self.action1)
# THEN: Check the length. # THEN: Check the length.
self.assertEqual(len(self.list), 1, "The length should be 1.") assert len(self.list) == 1, "The length should be 1."
def test_append(self): def test_append(self):
""" """
@ -88,10 +88,10 @@ class TestCategoryActionList(TestCase):
self.list.append(self.action2) self.list.append(self.action2)
# THEN: Check if the actions are in the list and check if they have the correct weights. # THEN: Check if the actions are in the list and check if they have the correct weights.
self.assertTrue(self.action1 in self.list) assert self.action1 in self.list
self.assertTrue(self.action2 in self.list) assert self.action2 in self.list
self.assertEqual(self.list.actions[0], (0, self.action1)) assert self.list.actions[0] == (0, self.action1)
self.assertEqual(self.list.actions[1], (1, self.action2)) assert self.list.actions[1] == (1, self.action2)
def test_add(self): def test_add(self):
""" """
@ -106,11 +106,11 @@ class TestCategoryActionList(TestCase):
self.list.add(self.action2, action2_weight) self.list.add(self.action2, action2_weight)
# THEN: Check if they were added and have the specified weights. # THEN: Check if they were added and have the specified weights.
self.assertTrue(self.action1 in self.list) assert self.action1 in self.list
self.assertTrue(self.action2 in self.list) assert self.action2 in self.list
# Now check if action1 is second and action2 is first (due to their weights). # Now check if action1 is second and action2 is first (due to their weights).
self.assertEqual(self.list.actions[0], (41, self.action2)) assert self.list.actions[0] == (41, self.action2)
self.assertEqual(self.list.actions[1], (42, self.action1)) assert self.list.actions[1] == (42, self.action1)
def test_iterator(self): def test_iterator(self):
""" """
@ -121,11 +121,11 @@ class TestCategoryActionList(TestCase):
self.list.add(self.action2) self.list.add(self.action2)
# WHEN: Iterating over the list # WHEN: Iterating over the list
list = [a for a in self.list] local_list = [a for a in self.list]
# THEN: Make sure they are returned in correct order # THEN: Make sure they are returned in correct order
self.assertEquals(len(self.list), 2) assert len(self.list) == 2
self.assertIs(list[0], self.action1) assert local_list[0] == self.action1
self.assertIs(list[1], self.action2) assert local_list[1] == self.action2
def test_remove(self): def test_remove(self):
""" """
@ -138,7 +138,7 @@ class TestCategoryActionList(TestCase):
self.list.remove(self.action1) self.list.remove(self.action1)
# THEN: Now the element should not be in the list anymore. # THEN: Now the element should not be in the list anymore.
self.assertFalse(self.action1 in self.list) assert self.action1 not in self.list
# THEN: Check if an exception is raised when trying to remove a not present action. # THEN: Check if an exception is raised when trying to remove a not present action.
self.assertRaises(ValueError, self.list.remove, self.action2) self.assertRaises(ValueError, self.list.remove, self.action2)

View File

@ -48,7 +48,7 @@ class TestCommonFunctions(TestCase):
extension_loader('glob', ['file2.py', 'file3.py']) extension_loader('glob', ['file2.py', 'file3.py'])
# THEN: `extension_loader` should not try to import any files # THEN: `extension_loader` should not try to import any files
self.assertFalse(mocked_import_module.called) assert mocked_import_module.called is False
def test_extension_loader_files_found(self): def test_extension_loader_files_found(self):
""" """
@ -69,7 +69,8 @@ class TestCommonFunctions(TestCase):
# THEN: `extension_loader` should only try to import the files that are matched by the blob, excluding the # THEN: `extension_loader` should only try to import the files that are matched by the blob, excluding the
# files listed in the `excluded_files` argument # files listed in the `excluded_files` argument
mocked_import_module.assert_has_calls([call('openlp.import_dir.file1'), call('openlp.import_dir.file4')]) mocked_import_module.assert_has_calls([call('openlp.import_dir.file1'),
call('openlp.import_dir.file4')])
def test_extension_loader_import_error(self): def test_extension_loader_import_error(self):
""" """
@ -87,7 +88,7 @@ class TestCommonFunctions(TestCase):
extension_loader('glob') extension_loader('glob')
# THEN: The `ImportError` should be caught and logged # THEN: The `ImportError` should be caught and logged
self.assertTrue(mocked_logger.warning.called) assert mocked_logger.warning.called
def test_extension_loader_os_error(self): def test_extension_loader_os_error(self):
""" """
@ -105,7 +106,7 @@ class TestCommonFunctions(TestCase):
extension_loader('glob') extension_loader('glob')
# THEN: The `OSError` should be caught and logged # THEN: The `OSError` should be caught and logged
self.assertTrue(mocked_logger.warning.called) assert mocked_logger.warning.called
def test_de_hump_conversion(self): def test_de_hump_conversion(self):
""" """
@ -118,7 +119,7 @@ class TestCommonFunctions(TestCase):
new_string = de_hump(string) new_string = de_hump(string)
# THEN: the new string should be converted to python format # THEN: the new string should be converted to python format
self.assertEqual(new_string, "my_class", 'The class name should have been converted') assert new_string == "my_class", 'The class name should have been converted'
def test_de_hump_static(self): def test_de_hump_static(self):
""" """
@ -131,7 +132,7 @@ class TestCommonFunctions(TestCase):
new_string = de_hump(string) new_string = de_hump(string)
# THEN: the new string should be converted to python format # THEN: the new string should be converted to python format
self.assertEqual(new_string, "my_class", 'The class name should have been preserved') assert new_string == "my_class", 'The class name should have been preserved'
def test_path_to_module(self): def test_path_to_module(self):
""" """
@ -144,7 +145,7 @@ class TestCommonFunctions(TestCase):
result = path_to_module(path) result = path_to_module(path)
# THEN: path_to_module should return the module name # THEN: path_to_module should return the module name
self.assertEqual(result, 'openlp.core.ui.media.webkitplayer') assert result == 'openlp.core.ui.media.webkitplayer'
def test_trace_error_handler(self): def test_trace_error_handler(self):
""" """
@ -174,9 +175,9 @@ class TestCommonFunctions(TestCase):
mocked_sys.platform = 'win32' mocked_sys.platform = 'win32'
# THEN: The three platform functions should perform properly # THEN: The three platform functions should perform properly
self.assertTrue(is_win(), 'is_win() should return True') assert is_win(), 'is_win() should return True'
self.assertFalse(is_macosx(), 'is_macosx() should return False') assert is_macosx() is False, 'is_macosx() should return False'
self.assertFalse(is_linux(), 'is_linux() should return False') assert is_linux() is False, 'is_linux() should return False'
def test_is_macosx(self): def test_is_macosx(self):
""" """
@ -190,9 +191,9 @@ class TestCommonFunctions(TestCase):
mocked_sys.platform = 'darwin' mocked_sys.platform = 'darwin'
# THEN: The three platform functions should perform properly # THEN: The three platform functions should perform properly
self.assertTrue(is_macosx(), 'is_macosx() should return True') assert is_macosx(), 'is_macosx() should return True'
self.assertFalse(is_win(), 'is_win() should return False') assert is_win() is False, 'is_win() should return False'
self.assertFalse(is_linux(), 'is_linux() should return False') assert is_linux() is False, 'is_linux() should return False'
def test_is_linux(self): def test_is_linux(self):
""" """
@ -206,9 +207,9 @@ class TestCommonFunctions(TestCase):
mocked_sys.platform = 'linux3' mocked_sys.platform = 'linux3'
# THEN: The three platform functions should perform properly # THEN: The three platform functions should perform properly
self.assertTrue(is_linux(), 'is_linux() should return True') assert is_linux(), 'is_linux() should return True'
self.assertFalse(is_win(), 'is_win() should return False') assert is_win() is False, 'is_win() should return False'
self.assertFalse(is_macosx(), 'is_macosx() should return False') assert is_macosx() is False, 'is_macosx() should return False'
def test_clean_button_text(self): def test_clean_button_text(self):
""" """
@ -222,4 +223,4 @@ class TestCommonFunctions(TestCase):
actual_text = clean_button_text(input_text) actual_text = clean_button_text(input_text)
# THEN: The text should have been cleaned # THEN: The text should have been cleaned
self.assertEqual(expected_text, actual_text, 'The text should be clean') assert expected_text == actual_text, 'The text should be clean'

View File

@ -59,7 +59,7 @@ class TestHttpUtils(TestCase, TestMixin):
# THEN: The user agent is a Linux (or ChromeOS) user agent # THEN: The user agent is a Linux (or ChromeOS) user agent
result = 'Linux' in user_agent or 'CrOS' in user_agent result = 'Linux' in user_agent or 'CrOS' in user_agent
self.assertTrue(result, 'The user agent should be a valid Linux user agent') assert result, 'The user agent should be a valid Linux user agent'
def test_get_user_agent_windows(self): def test_get_user_agent_windows(self):
""" """
@ -74,7 +74,7 @@ class TestHttpUtils(TestCase, TestMixin):
user_agent = get_user_agent() user_agent = get_user_agent()
# THEN: The user agent is a Linux (or ChromeOS) user agent # THEN: The user agent is a Linux (or ChromeOS) user agent
self.assertIn('Windows', user_agent, 'The user agent should be a valid Windows user agent') assert 'Windows' in user_agent, 'The user agent should be a valid Windows user agent'
def test_get_user_agent_macos(self): def test_get_user_agent_macos(self):
""" """
@ -89,7 +89,7 @@ class TestHttpUtils(TestCase, TestMixin):
user_agent = get_user_agent() user_agent = get_user_agent()
# THEN: The user agent is a Linux (or ChromeOS) user agent # THEN: The user agent is a Linux (or ChromeOS) user agent
self.assertIn('Mac OS X', user_agent, 'The user agent should be a valid OS X user agent') assert 'Mac OS X' in user_agent, 'The user agent should be a valid OS X user agent'
def test_get_user_agent_default(self): def test_get_user_agent_default(self):
""" """
@ -104,7 +104,7 @@ class TestHttpUtils(TestCase, TestMixin):
user_agent = get_user_agent() user_agent = get_user_agent()
# THEN: The user agent is a Linux (or ChromeOS) user agent # THEN: The user agent is a Linux (or ChromeOS) user agent
self.assertIn('NetBSD', user_agent, 'The user agent should be the default user agent') assert 'NetBSD'in user_agent, 'The user agent should be the default user agent'
def test_get_web_page_no_url(self): def test_get_web_page_no_url(self):
""" """
@ -117,7 +117,7 @@ class TestHttpUtils(TestCase, TestMixin):
result = get_web_page(test_url) result = get_web_page(test_url)
# THEN: None should be returned # THEN: None should be returned
self.assertIsNone(result, 'The return value of get_web_page should be None') assert result is None, 'The return value of get_web_page should be None'
@patch('openlp.core.common.httputils.requests') @patch('openlp.core.common.httputils.requests')
@patch('openlp.core.common.httputils.get_user_agent') @patch('openlp.core.common.httputils.get_user_agent')

View File

@ -63,8 +63,8 @@ class TestInit(TestCase, TestMixin):
add_actions(mocked_target, empty_list) add_actions(mocked_target, empty_list)
# THEN: The add method on the mocked target is never called # THEN: The add method on the mocked target is never called
self.assertEqual(0, mocked_target.addSeparator.call_count, 'addSeparator method should not have been called') assert mocked_target.addSeparator.call_count == 0, 'addSeparator method should not have been called'
self.assertEqual(0, mocked_target.addAction.call_count, 'addAction method should not have been called') assert mocked_target.addAction.call_count == 0, 'addAction method should not have been called'
def test_add_actions_none_action(self): def test_add_actions_none_action(self):
""" """
@ -79,7 +79,7 @@ class TestInit(TestCase, TestMixin):
# THEN: The addSeparator method is called, but the addAction method is never called # THEN: The addSeparator method is called, but the addAction method is never called
mocked_target.addSeparator.assert_called_with() mocked_target.addSeparator.assert_called_with()
self.assertEqual(0, mocked_target.addAction.call_count, 'addAction method should not have been called') assert mocked_target.addAction.call_count == 0, 'addAction method should not have been called'
def test_add_actions_add_action(self): def test_add_actions_add_action(self):
""" """
@ -93,7 +93,7 @@ class TestInit(TestCase, TestMixin):
add_actions(mocked_target, action_list) add_actions(mocked_target, action_list)
# THEN: The addSeparator method is not called, and the addAction method is called # THEN: The addSeparator method is not called, and the addAction method is called
self.assertEqual(0, mocked_target.addSeparator.call_count, 'addSeparator method should not have been called') assert mocked_target.addSeparator.call_count == 0, 'addSeparator method should not have been called'
mocked_target.addAction.assert_called_with('action') mocked_target.addAction.assert_called_with('action')
def test_add_actions_action_and_none(self): def test_add_actions_action_and_none(self):
@ -150,9 +150,8 @@ class TestInit(TestCase, TestMixin):
result = get_uno_command() result = get_uno_command()
# THEN: The command 'libreoffice' should be called with the appropriate parameters # THEN: The command 'libreoffice' should be called with the appropriate parameters
self.assertEquals(result, assert result == 'libreoffice --nologo --norestore --minimized --nodefault --nofirststartwizard' \
'libreoffice --nologo --norestore --minimized --nodefault --nofirststartwizard' ' "--accept=pipe,name=openlp_pipe;urp;"'
' "--accept=pipe,name=openlp_pipe;urp;"')
def test_get_uno_command_only_soffice_command_exists(self): def test_get_uno_command_only_soffice_command_exists(self):
""" """
@ -169,8 +168,8 @@ class TestInit(TestCase, TestMixin):
result = get_uno_command() result = get_uno_command()
# THEN: The command 'soffice' should be called with the appropriate parameters # THEN: The command 'soffice' should be called with the appropriate parameters
self.assertEquals(result, 'soffice --nologo --norestore --minimized --nodefault --nofirststartwizard' assert result == 'soffice --nologo --norestore --minimized --nodefault --nofirststartwizard' \
' "--accept=pipe,name=openlp_pipe;urp;"') ' "--accept=pipe,name=openlp_pipe;urp;"'
def test_get_uno_command_when_no_command_exists(self): def test_get_uno_command_when_no_command_exists(self):
""" """
@ -198,8 +197,8 @@ class TestInit(TestCase, TestMixin):
result = get_uno_command('socket') result = get_uno_command('socket')
# THEN: The connection parameters should be set for socket # THEN: The connection parameters should be set for socket
self.assertEqual(result, 'libreoffice --nologo --norestore --minimized --nodefault --nofirststartwizard' assert result == 'libreoffice --nologo --norestore --minimized --nodefault --nofirststartwizard' \
' "--accept=socket,host=localhost,port=2002;urp;"') ' "--accept=socket,host=localhost,port=2002;urp;"'
def test_get_filesystem_encoding_sys_function_not_called(self): def test_get_filesystem_encoding_sys_function_not_called(self):
""" """
@ -215,8 +214,8 @@ class TestInit(TestCase, TestMixin):
# THEN: getdefaultencoding should have been called # THEN: getdefaultencoding should have been called
mocked_getfilesystemencoding.assert_called_with() mocked_getfilesystemencoding.assert_called_with()
self.assertEqual(0, mocked_getdefaultencoding.called, 'getdefaultencoding should not have been called') assert mocked_getdefaultencoding.called == 0, 'getdefaultencoding should not have been called'
self.assertEqual('cp1252', result, 'The result should be "cp1252"') assert 'cp1252' == result, 'The result should be "cp1252"'
def test_get_filesystem_encoding_sys_function_is_called(self): def test_get_filesystem_encoding_sys_function_is_called(self):
""" """
@ -234,7 +233,7 @@ class TestInit(TestCase, TestMixin):
# THEN: getdefaultencoding should have been called # THEN: getdefaultencoding should have been called
mocked_getfilesystemencoding.assert_called_with() mocked_getfilesystemencoding.assert_called_with()
mocked_getdefaultencoding.assert_called_with() mocked_getdefaultencoding.assert_called_with()
self.assertEqual('utf-8', result, 'The result should be "utf-8"') assert 'utf-8' == result, 'The result should be "utf-8"'
def test_clean_filename(self): def test_clean_filename(self):
""" """
@ -248,7 +247,7 @@ class TestInit(TestCase, TestMixin):
result = clean_filename(invalid_name) result = clean_filename(invalid_name)
# THEN: The file name should be cleaned. # THEN: The file name should be cleaned.
self.assertEqual(wanted_name, result, 'The file name should not contain any special characters.') assert wanted_name == result, 'The file name should not contain any special characters.'
def test_delete_file_no_path(self): def test_delete_file_no_path(self):
""" """
@ -259,7 +258,7 @@ class TestInit(TestCase, TestMixin):
result = delete_file(None) result = delete_file(None)
# THEN: delete_file should return False # THEN: delete_file should return False
self.assertFalse(result, "delete_file should return False when called with None") assert not result, "delete_file should return False when called with None"
def test_delete_file_path_success(self): def test_delete_file_path_success(self):
""" """
@ -272,7 +271,7 @@ class TestInit(TestCase, TestMixin):
result = delete_file(Path('path', 'file.ext')) result = delete_file(Path('path', 'file.ext'))
# THEN: delete_file should return True # THEN: delete_file should return True
self.assertTrue(result, 'delete_file should return True when it successfully deletes a file') assert result, 'delete_file should return True when it successfully deletes a file'
def test_delete_file_path_no_file_exists(self): def test_delete_file_path_no_file_exists(self):
""" """
@ -286,8 +285,8 @@ class TestInit(TestCase, TestMixin):
result = delete_file(Path('path', 'file.ext')) result = delete_file(Path('path', 'file.ext'))
# THEN: The function should not attempt to delete the file and it should return True # THEN: The function should not attempt to delete the file and it should return True
self.assertFalse(mocked_unlink.called) assert mocked_unlink.called is False
self.assertTrue(result, 'delete_file should return True when the file doesnt exist') assert result, 'delete_file should return True when the file doesnt exist'
def test_delete_file_path_exception(self): def test_delete_file_path_exception(self):
""" """
@ -303,8 +302,8 @@ class TestInit(TestCase, TestMixin):
result = delete_file(Path('path', 'file.ext')) result = delete_file(Path('path', 'file.ext'))
# THEN: The exception should be logged and `delete_file` should return False # THEN: The exception should be logged and `delete_file` should return False
self.assertTrue(mocked_log.exception.called) assert mocked_log.exception.called
self.assertFalse(result, 'delete_file should return False when an OSError is raised') assert result is False, 'delete_file should return False when an OSError is raised'
def test_get_file_encoding_done(self): def test_get_file_encoding_done(self):
""" """
@ -323,9 +322,9 @@ class TestInit(TestCase, TestMixin):
# THEN: The feed method of UniversalDetector should only br called once before returning a result # THEN: The feed method of UniversalDetector should only br called once before returning a result
mocked_open.assert_called_once_with('rb') mocked_open.assert_called_once_with('rb')
self.assertEqual(mocked_universal_detector_inst.feed.mock_calls, [call(b"data" * 256)]) assert mocked_universal_detector_inst.feed.mock_calls == [call(b"data" * 256)]
mocked_universal_detector_inst.close.assert_called_once_with() mocked_universal_detector_inst.close.assert_called_once_with()
self.assertEqual(result, encoding_result) assert result == encoding_result
def test_get_file_encoding_eof(self): def test_get_file_encoding_eof(self):
""" """
@ -345,9 +344,9 @@ class TestInit(TestCase, TestMixin):
# THEN: The feed method of UniversalDetector should have been called twice before returning a result # THEN: The feed method of UniversalDetector should have been called twice before returning a result
mocked_open.assert_called_once_with('rb') mocked_open.assert_called_once_with('rb')
self.assertEqual(mocked_universal_detector_inst.feed.mock_calls, [call(b"data" * 256), call(b"data" * 4)]) assert mocked_universal_detector_inst.feed.mock_calls == [call(b"data" * 256), call(b"data" * 4)]
mocked_universal_detector_inst.close.assert_called_once_with() mocked_universal_detector_inst.close.assert_called_once_with()
self.assertEqual(result, encoding_result) assert result, encoding_result
def test_get_file_encoding_oserror(self): def test_get_file_encoding_oserror(self):
""" """
@ -364,4 +363,4 @@ class TestInit(TestCase, TestMixin):
# THEN: log.exception should be called and get_file_encoding should return None # THEN: log.exception should be called and get_file_encoding should return None
mocked_log.exception.assert_called_once_with('Error detecting file encoding') mocked_log.exception.assert_called_once_with('Error detecting file encoding')
self.assertIsNone(result) assert not result