2016-04-01 17:28:40 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
|
2019-04-13 13:00:22 +00:00
|
|
|
##########################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
|
|
# Copyright (c) 2008-2019 OpenLP Developers #
|
|
|
|
# ---------------------------------------------------------------------- #
|
|
|
|
# This program is free software: you can redistribute it and/or modify #
|
|
|
|
# it under the terms of the GNU General Public License as published by #
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or #
|
|
|
|
# (at your option) any later version. #
|
|
|
|
# #
|
|
|
|
# This program is distributed in the hope that it will be useful, #
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
|
|
|
|
# GNU General Public License for more details. #
|
|
|
|
# #
|
|
|
|
# You should have received a copy of the GNU General Public License #
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
|
|
|
|
##########################################################################
|
2016-04-01 17:28:40 +00:00
|
|
|
"""
|
|
|
|
Functional tests to test the AppLocation class and related methods.
|
|
|
|
"""
|
2016-08-09 06:24:04 +00:00
|
|
|
from io import BytesIO
|
2016-04-01 17:28:40 +00:00
|
|
|
from unittest import TestCase
|
2017-04-24 05:17:55 +00:00
|
|
|
from unittest.mock import MagicMock, PropertyMock, call, patch
|
2016-04-01 17:28:40 +00:00
|
|
|
|
2017-12-28 08:22:55 +00:00
|
|
|
from openlp.core.common import add_actions, clean_filename, delete_file, get_file_encoding, get_filesystem_encoding, \
|
2017-11-18 23:14:28 +00:00
|
|
|
get_uno_command, get_uno_instance
|
2017-08-25 20:03:25 +00:00
|
|
|
from openlp.core.common.path import Path
|
2016-04-04 20:47:33 +00:00
|
|
|
from tests.helpers.testmixin import TestMixin
|
2016-04-01 17:28:40 +00:00
|
|
|
|
|
|
|
|
2016-04-04 20:47:33 +00:00
|
|
|
class TestInit(TestCase, TestMixin):
|
2016-04-01 17:28:40 +00:00
|
|
|
"""
|
|
|
|
A test suite to test out various methods around the common __init__ class.
|
|
|
|
"""
|
|
|
|
|
2016-04-04 20:41:08 +00:00
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Create an instance and a few example actions.
|
|
|
|
"""
|
|
|
|
self.build_settings()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""
|
|
|
|
Clean up
|
|
|
|
"""
|
|
|
|
self.destroy_settings()
|
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_add_actions_empty_list(self):
|
2016-04-01 17:28:40 +00:00
|
|
|
"""
|
|
|
|
Test that no actions are added when the list is empty
|
|
|
|
"""
|
|
|
|
# GIVEN: a mocked action list, and an empty list
|
|
|
|
mocked_target = MagicMock()
|
|
|
|
empty_list = []
|
|
|
|
|
|
|
|
# WHEN: The empty list is added to the mocked target
|
|
|
|
add_actions(mocked_target, empty_list)
|
|
|
|
|
|
|
|
# THEN: The add method on the mocked target is never called
|
2017-12-09 16:11:45 +00:00
|
|
|
assert mocked_target.addSeparator.call_count == 0, 'addSeparator method should not have been called'
|
|
|
|
assert mocked_target.addAction.call_count == 0, 'addAction method should not have been called'
|
2016-04-01 17:28:40 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_add_actions_none_action(self):
|
2016-04-01 17:28:40 +00:00
|
|
|
"""
|
|
|
|
Test that a separator is added when a None action is in the list
|
|
|
|
"""
|
|
|
|
# GIVEN: a mocked action list, and a list with None in it
|
|
|
|
mocked_target = MagicMock()
|
|
|
|
separator_list = [None]
|
|
|
|
|
|
|
|
# WHEN: The list is added to the mocked target
|
|
|
|
add_actions(mocked_target, separator_list)
|
|
|
|
|
|
|
|
# THEN: The addSeparator method is called, but the addAction method is never called
|
|
|
|
mocked_target.addSeparator.assert_called_with()
|
2017-12-09 16:11:45 +00:00
|
|
|
assert mocked_target.addAction.call_count == 0, 'addAction method should not have been called'
|
2016-04-01 17:28:40 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_add_actions_add_action(self):
|
2016-04-01 17:28:40 +00:00
|
|
|
"""
|
|
|
|
Test that an action is added when a valid action is in the list
|
|
|
|
"""
|
|
|
|
# GIVEN: a mocked action list, and a list with an action in it
|
|
|
|
mocked_target = MagicMock()
|
|
|
|
action_list = ['action']
|
|
|
|
|
|
|
|
# WHEN: The list is added to the mocked target
|
|
|
|
add_actions(mocked_target, action_list)
|
|
|
|
|
|
|
|
# THEN: The addSeparator method is not called, and the addAction method is called
|
2017-12-09 16:11:45 +00:00
|
|
|
assert mocked_target.addSeparator.call_count == 0, 'addSeparator method should not have been called'
|
2016-04-01 17:28:40 +00:00
|
|
|
mocked_target.addAction.assert_called_with('action')
|
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_add_actions_action_and_none(self):
|
2016-04-01 17:28:40 +00:00
|
|
|
"""
|
|
|
|
Test that an action and a separator are added when a valid action and None are in the list
|
|
|
|
"""
|
|
|
|
# GIVEN: a mocked action list, and a list with an action and None in it
|
|
|
|
mocked_target = MagicMock()
|
|
|
|
action_list = ['action', None]
|
|
|
|
|
|
|
|
# WHEN: The list is added to the mocked target
|
|
|
|
add_actions(mocked_target, action_list)
|
|
|
|
|
|
|
|
# THEN: The addSeparator method is called, and the addAction method is called
|
|
|
|
mocked_target.addSeparator.assert_called_with()
|
|
|
|
mocked_target.addAction.assert_called_with('action')
|
2016-04-04 20:27:33 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_get_uno_instance_pipe(self):
|
2016-04-04 20:27:33 +00:00
|
|
|
"""
|
|
|
|
Test that when the UNO connection type is "pipe" the resolver is given the "pipe" URI
|
|
|
|
"""
|
|
|
|
# GIVEN: A mock resolver object and UNO_CONNECTION_TYPE is "pipe"
|
|
|
|
mock_resolver = MagicMock()
|
|
|
|
|
|
|
|
# WHEN: get_uno_instance() is called
|
|
|
|
get_uno_instance(mock_resolver)
|
|
|
|
|
|
|
|
# THEN: the resolve method is called with the correct argument
|
|
|
|
mock_resolver.resolve.assert_called_with('uno:pipe,name=openlp_pipe;urp;StarOffice.ComponentContext')
|
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_get_uno_instance_socket(self):
|
2016-04-04 20:27:33 +00:00
|
|
|
"""
|
|
|
|
Test that when the UNO connection type is other than "pipe" the resolver is given the "socket" URI
|
|
|
|
"""
|
|
|
|
# GIVEN: A mock resolver object and UNO_CONNECTION_TYPE is "socket"
|
|
|
|
mock_resolver = MagicMock()
|
|
|
|
|
|
|
|
# WHEN: get_uno_instance() is called
|
|
|
|
get_uno_instance(mock_resolver, 'socket')
|
|
|
|
|
|
|
|
# THEN: the resolve method is called with the correct argument
|
|
|
|
mock_resolver.resolve.assert_called_with('uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext')
|
2016-04-04 20:41:08 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_get_uno_command_libreoffice_command_exists(self):
|
2016-04-04 20:41:08 +00:00
|
|
|
"""
|
|
|
|
Test the ``get_uno_command`` function uses the libreoffice command when available.
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
|
|
|
# GIVEN: A patched 'which' method which returns a path when called with 'libreoffice'
|
2016-04-04 21:01:24 +00:00
|
|
|
with patch('openlp.core.common.which',
|
2016-04-04 20:41:08 +00:00
|
|
|
**{'side_effect': lambda command: {'libreoffice': '/usr/bin/libreoffice'}[command]}):
|
|
|
|
# WHEN: Calling get_uno_command
|
|
|
|
result = get_uno_command()
|
|
|
|
|
|
|
|
# THEN: The command 'libreoffice' should be called with the appropriate parameters
|
2017-12-09 16:11:45 +00:00
|
|
|
assert result == 'libreoffice --nologo --norestore --minimized --nodefault --nofirststartwizard' \
|
|
|
|
' "--accept=pipe,name=openlp_pipe;urp;"'
|
2016-04-04 20:41:08 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_get_uno_command_only_soffice_command_exists(self):
|
2016-04-04 20:41:08 +00:00
|
|
|
"""
|
|
|
|
Test the ``get_uno_command`` function uses the soffice command when the libreoffice command is not available.
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
|
|
|
# GIVEN: A patched 'which' method which returns None when called with 'libreoffice' and a path when called with
|
|
|
|
# 'soffice'
|
2016-04-04 21:01:24 +00:00
|
|
|
with patch('openlp.core.common.which',
|
2016-04-04 20:41:08 +00:00
|
|
|
**{'side_effect': lambda command: {'libreoffice': None, 'soffice': '/usr/bin/soffice'}[
|
|
|
|
command]}):
|
|
|
|
# WHEN: Calling get_uno_command
|
|
|
|
result = get_uno_command()
|
|
|
|
|
|
|
|
# THEN: The command 'soffice' should be called with the appropriate parameters
|
2017-12-09 16:11:45 +00:00
|
|
|
assert result == 'soffice --nologo --norestore --minimized --nodefault --nofirststartwizard' \
|
|
|
|
' "--accept=pipe,name=openlp_pipe;urp;"'
|
2016-04-04 20:41:08 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_get_uno_command_when_no_command_exists(self):
|
2016-04-04 20:41:08 +00:00
|
|
|
"""
|
|
|
|
Test the ``get_uno_command`` function raises an FileNotFoundError when neither the libreoffice or soffice
|
|
|
|
commands are available.
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
|
|
|
# GIVEN: A patched 'which' method which returns None
|
2016-04-04 21:01:24 +00:00
|
|
|
with patch('openlp.core.common.which', **{'return_value': None}):
|
2016-04-04 20:41:08 +00:00
|
|
|
# WHEN: Calling get_uno_command
|
|
|
|
|
|
|
|
# THEN: a FileNotFoundError exception should be raised
|
|
|
|
self.assertRaises(FileNotFoundError, get_uno_command)
|
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_get_uno_command_connection_type(self):
|
2016-04-04 20:41:08 +00:00
|
|
|
"""
|
|
|
|
Test the ``get_uno_command`` function when the connection type is anything other than pipe.
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
|
|
|
# GIVEN: A patched 'which' method which returns 'libreoffice'
|
2016-04-04 21:01:24 +00:00
|
|
|
with patch('openlp.core.common.which', **{'return_value': 'libreoffice'}):
|
2016-04-04 20:41:08 +00:00
|
|
|
# WHEN: Calling get_uno_command with a connection type other than pipe
|
|
|
|
result = get_uno_command('socket')
|
|
|
|
|
|
|
|
# THEN: The connection parameters should be set for socket
|
2017-12-09 16:11:45 +00:00
|
|
|
assert result == 'libreoffice --nologo --norestore --minimized --nodefault --nofirststartwizard' \
|
|
|
|
' "--accept=socket,host=localhost,port=2002;urp;"'
|
2016-04-04 20:41:08 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_get_filesystem_encoding_sys_function_not_called(self):
|
2016-04-05 18:11:42 +00:00
|
|
|
"""
|
|
|
|
Test the get_filesystem_encoding() function does not call the sys.getdefaultencoding() function
|
|
|
|
"""
|
|
|
|
# GIVEN: sys.getfilesystemencoding returns "cp1252"
|
|
|
|
with patch('openlp.core.common.sys.getfilesystemencoding') as mocked_getfilesystemencoding, \
|
|
|
|
patch('openlp.core.common.sys.getdefaultencoding') as mocked_getdefaultencoding:
|
|
|
|
mocked_getfilesystemencoding.return_value = 'cp1252'
|
|
|
|
|
|
|
|
# WHEN: get_filesystem_encoding() is called
|
|
|
|
result = get_filesystem_encoding()
|
|
|
|
|
|
|
|
# THEN: getdefaultencoding should have been called
|
|
|
|
mocked_getfilesystemencoding.assert_called_with()
|
2017-12-09 16:11:45 +00:00
|
|
|
assert mocked_getdefaultencoding.called == 0, 'getdefaultencoding should not have been called'
|
|
|
|
assert 'cp1252' == result, 'The result should be "cp1252"'
|
2016-04-05 18:11:42 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_get_filesystem_encoding_sys_function_is_called(self):
|
2016-04-05 18:11:42 +00:00
|
|
|
"""
|
|
|
|
Test the get_filesystem_encoding() function calls the sys.getdefaultencoding() function
|
|
|
|
"""
|
|
|
|
# GIVEN: sys.getfilesystemencoding returns None and sys.getdefaultencoding returns "utf-8"
|
|
|
|
with patch('openlp.core.common.sys.getfilesystemencoding') as mocked_getfilesystemencoding, \
|
|
|
|
patch('openlp.core.common.sys.getdefaultencoding') as mocked_getdefaultencoding:
|
|
|
|
mocked_getfilesystemencoding.return_value = None
|
|
|
|
mocked_getdefaultencoding.return_value = 'utf-8'
|
|
|
|
|
|
|
|
# WHEN: get_filesystem_encoding() is called
|
|
|
|
result = get_filesystem_encoding()
|
|
|
|
|
|
|
|
# THEN: getdefaultencoding should have been called
|
|
|
|
mocked_getfilesystemencoding.assert_called_with()
|
|
|
|
mocked_getdefaultencoding.assert_called_with()
|
2017-12-09 16:11:45 +00:00
|
|
|
assert 'utf-8' == result, 'The result should be "utf-8"'
|
2016-04-05 18:11:42 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_clean_filename(self):
|
2016-04-05 18:11:42 +00:00
|
|
|
"""
|
|
|
|
Test the clean_filename() function
|
|
|
|
"""
|
|
|
|
# GIVEN: A invalid file name and the valid file name.
|
2018-07-04 20:42:55 +00:00
|
|
|
invalid_name = 'A_file_with_invalid_characters_[\\/:*?"<>|+[]%].py'
|
|
|
|
wanted_name = 'A_file_with_invalid_characters________________.py'
|
2016-04-05 18:11:42 +00:00
|
|
|
|
|
|
|
# WHEN: Clean the name.
|
|
|
|
result = clean_filename(invalid_name)
|
|
|
|
|
|
|
|
# THEN: The file name should be cleaned.
|
2017-12-09 16:11:45 +00:00
|
|
|
assert wanted_name == result, 'The file name should not contain any special characters.'
|
2016-04-05 18:11:42 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_delete_file_no_path(self):
|
2016-04-05 18:11:42 +00:00
|
|
|
"""
|
|
|
|
Test the delete_file function when called with out a valid path
|
|
|
|
"""
|
|
|
|
# GIVEN: A blank path
|
|
|
|
# WEHN: Calling delete_file
|
2017-08-12 17:45:56 +00:00
|
|
|
result = delete_file(None)
|
2016-04-05 18:11:42 +00:00
|
|
|
|
|
|
|
# THEN: delete_file should return False
|
2017-12-15 16:19:42 +00:00
|
|
|
assert result is False, "delete_file should return False when called with None"
|
2016-04-05 18:11:42 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_delete_file_path_success(self):
|
2016-04-05 18:11:42 +00:00
|
|
|
"""
|
|
|
|
Test the delete_file function when it successfully deletes a file
|
|
|
|
"""
|
|
|
|
# GIVEN: A mocked os which returns True when os.path.exists is called
|
|
|
|
with patch('openlp.core.common.os', **{'path.exists.return_value': False}):
|
|
|
|
|
|
|
|
# WHEN: Calling delete_file with a file path
|
2017-08-12 17:45:56 +00:00
|
|
|
result = delete_file(Path('path', 'file.ext'))
|
2016-04-05 18:11:42 +00:00
|
|
|
|
|
|
|
# THEN: delete_file should return True
|
2017-12-15 16:19:42 +00:00
|
|
|
assert result is True, 'delete_file should return True when it successfully deletes a file'
|
2016-04-05 18:11:42 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_delete_file_path_no_file_exists(self):
|
2016-04-05 18:11:42 +00:00
|
|
|
"""
|
2017-08-12 17:45:56 +00:00
|
|
|
Test the `delete_file` function when the file to remove does not exist
|
2016-04-05 18:11:42 +00:00
|
|
|
"""
|
2017-08-12 17:45:56 +00:00
|
|
|
# GIVEN: A patched `exists` methods on the Path object, which returns False
|
|
|
|
with patch.object(Path, 'exists', return_value=False), \
|
|
|
|
patch.object(Path, 'unlink') as mocked_unlink:
|
2016-04-05 18:11:42 +00:00
|
|
|
|
2017-08-12 17:45:56 +00:00
|
|
|
# WHEN: Calling `delete_file with` a file path
|
|
|
|
result = delete_file(Path('path', 'file.ext'))
|
2016-04-05 18:11:42 +00:00
|
|
|
|
2017-08-12 17:45:56 +00:00
|
|
|
# THEN: The function should not attempt to delete the file and it should return True
|
2017-12-09 16:11:45 +00:00
|
|
|
assert mocked_unlink.called is False
|
2017-12-16 08:43:33 +00:00
|
|
|
assert result is True, 'delete_file should return True when the file doesnt exist'
|
2016-04-05 18:11:42 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_delete_file_path_exception(self):
|
2016-04-05 18:11:42 +00:00
|
|
|
"""
|
2017-08-12 17:45:56 +00:00
|
|
|
Test the delete_file function when an exception is raised
|
2016-04-05 18:11:42 +00:00
|
|
|
"""
|
2017-08-12 17:45:56 +00:00
|
|
|
# GIVEN: A test `Path` object with a patched exists method which raises an OSError
|
2016-04-05 18:11:42 +00:00
|
|
|
# called.
|
2017-08-12 17:45:56 +00:00
|
|
|
with patch.object(Path, 'exists') as mocked_exists, \
|
2016-04-05 18:11:42 +00:00
|
|
|
patch('openlp.core.common.log') as mocked_log:
|
2017-08-12 17:45:56 +00:00
|
|
|
mocked_exists.side_effect = OSError
|
2016-04-05 18:11:42 +00:00
|
|
|
|
2017-08-12 17:45:56 +00:00
|
|
|
# WHEN: Calling delete_file with a the test Path object
|
|
|
|
result = delete_file(Path('path', 'file.ext'))
|
2016-04-05 18:11:42 +00:00
|
|
|
|
2017-08-12 17:45:56 +00:00
|
|
|
# THEN: The exception should be logged and `delete_file` should return False
|
2017-12-09 16:11:45 +00:00
|
|
|
assert mocked_log.exception.called
|
|
|
|
assert result is False, 'delete_file should return False when an OSError is raised'
|
2016-08-09 06:24:04 +00:00
|
|
|
|
2017-10-29 06:01:25 +00:00
|
|
|
def test_get_file_encoding_done(self):
|
2016-08-09 06:24:04 +00:00
|
|
|
"""
|
|
|
|
Test get_file_encoding when the detector sets done to True
|
|
|
|
"""
|
|
|
|
# GIVEN: A mocked UniversalDetector instance with done attribute set to True after first iteration
|
|
|
|
with patch('openlp.core.common.UniversalDetector') as mocked_universal_detector, \
|
2019-03-03 09:49:01 +00:00
|
|
|
patch.object(Path, 'open', return_value=BytesIO(b'data' * 260)) as mocked_open:
|
2016-08-09 06:24:04 +00:00
|
|
|
encoding_result = {'encoding': 'UTF-8', 'confidence': 0.99}
|
2019-03-03 09:49:01 +00:00
|
|
|
mocked_universal_detector_inst = MagicMock(**{'close.return_value': encoding_result})
|
2016-08-09 06:24:04 +00:00
|
|
|
type(mocked_universal_detector_inst).done = PropertyMock(side_effect=[False, True])
|
|
|
|
mocked_universal_detector.return_value = mocked_universal_detector_inst
|
|
|
|
|
|
|
|
# WHEN: Calling get_file_encoding
|
2017-08-12 17:45:56 +00:00
|
|
|
result = get_file_encoding(Path('file name'))
|
2016-08-09 06:24:04 +00:00
|
|
|
|
|
|
|
# THEN: The feed method of UniversalDetector should only br called once before returning a result
|
2017-08-12 17:45:56 +00:00
|
|
|
mocked_open.assert_called_once_with('rb')
|
2019-03-03 09:49:01 +00:00
|
|
|
assert mocked_universal_detector_inst.feed.mock_calls == [call(b'data' * 256)]
|
2016-08-09 06:24:04 +00:00
|
|
|
mocked_universal_detector_inst.close.assert_called_once_with()
|
2017-12-09 16:11:45 +00:00
|
|
|
assert result == encoding_result
|
2016-08-09 06:24:04 +00:00
|
|
|
|
2017-10-29 06:01:25 +00:00
|
|
|
def test_get_file_encoding_eof(self):
|
2016-08-09 06:24:04 +00:00
|
|
|
"""
|
|
|
|
Test get_file_encoding when the end of the file is reached
|
|
|
|
"""
|
|
|
|
# GIVEN: A mocked UniversalDetector instance which isn't set to done and a mocked open, with 1040 bytes of test
|
|
|
|
# data (enough to run the iterator twice)
|
|
|
|
with patch('openlp.core.common.UniversalDetector') as mocked_universal_detector, \
|
2019-03-03 09:49:01 +00:00
|
|
|
patch.object(Path, 'open', return_value=BytesIO(b'data' * 260)) as mocked_open:
|
2016-08-09 06:24:04 +00:00
|
|
|
encoding_result = {'encoding': 'UTF-8', 'confidence': 0.99}
|
|
|
|
mocked_universal_detector_inst = MagicMock(mock=mocked_universal_detector,
|
2019-03-03 09:49:01 +00:00
|
|
|
**{'done': False, 'close.return_value': encoding_result})
|
2016-08-09 06:24:04 +00:00
|
|
|
mocked_universal_detector.return_value = mocked_universal_detector_inst
|
|
|
|
|
|
|
|
# WHEN: Calling get_file_encoding
|
2017-08-12 17:45:56 +00:00
|
|
|
result = get_file_encoding(Path('file name'))
|
2016-08-09 06:24:04 +00:00
|
|
|
|
|
|
|
# THEN: The feed method of UniversalDetector should have been called twice before returning a result
|
2017-08-12 17:45:56 +00:00
|
|
|
mocked_open.assert_called_once_with('rb')
|
2019-03-03 09:49:01 +00:00
|
|
|
assert mocked_universal_detector_inst.feed.mock_calls == [call(b'data' * 256), call(b'data' * 4)]
|
2016-08-09 06:24:04 +00:00
|
|
|
mocked_universal_detector_inst.close.assert_called_once_with()
|
2017-12-16 08:43:33 +00:00
|
|
|
assert result == encoding_result
|
2016-08-09 06:24:04 +00:00
|
|
|
|
2017-10-29 06:01:25 +00:00
|
|
|
def test_get_file_encoding_oserror(self):
|
2016-08-09 06:24:04 +00:00
|
|
|
"""
|
|
|
|
Test get_file_encoding when the end of the file is reached
|
|
|
|
"""
|
|
|
|
# GIVEN: A mocked UniversalDetector instance which isn't set to done and a mocked open, with 1040 bytes of test
|
|
|
|
# data (enough to run the iterator twice)
|
2019-03-03 09:49:01 +00:00
|
|
|
with patch('openlp.core.common.UniversalDetector') as mocked_universal_detector, \
|
2016-08-09 06:24:04 +00:00
|
|
|
patch('builtins.open', side_effect=OSError), \
|
|
|
|
patch('openlp.core.common.log') as mocked_log:
|
2019-03-03 09:49:01 +00:00
|
|
|
encoding_result = {'encoding': 'UTF-8', 'confidence': 0.99}
|
|
|
|
mocked_universal_detector_inst = MagicMock(mock=mocked_universal_detector,
|
|
|
|
**{'done': False, 'close.return_value': encoding_result})
|
|
|
|
mocked_universal_detector.return_value = mocked_universal_detector_inst
|
2016-08-09 06:24:04 +00:00
|
|
|
|
|
|
|
# WHEN: Calling get_file_encoding
|
2017-08-12 17:45:56 +00:00
|
|
|
result = get_file_encoding(Path('file name'))
|
2016-08-09 06:24:04 +00:00
|
|
|
|
|
|
|
# THEN: log.exception should be called and get_file_encoding should return None
|
|
|
|
mocked_log.exception.assert_called_once_with('Error detecting file encoding')
|
2019-03-03 09:49:01 +00:00
|
|
|
mocked_universal_detector_inst.feed.assert_not_called()
|
|
|
|
mocked_universal_detector_inst.close.assert_called_once_with()
|
|
|
|
assert result == encoding_result
|