23
23
Functional tests to test the AppLocation class and related methods.
26
from io import BytesIO
26
27
from unittest import TestCase
28
from openlp.core.common import add_actions, get_uno_instance, get_uno_command, delete_file, get_filesystem_encoding, \
29
split_filename, clean_filename
30
from tests.functional import MagicMock, patch
29
from openlp.core.common import add_actions, clean_filename, delete_file, get_file_encoding, get_filesystem_encoding, \
30
get_uno_command, get_uno_instance, split_filename
31
from tests.functional import MagicMock, PropertyMock, call, patch
31
32
from tests.helpers.testmixin import TestMixin
340
341
# THEN: delete_file should log and exception and return False
341
342
self.assertEqual(mocked_log.exception.call_count, 1)
342
343
self.assertFalse(result, 'delete_file should return False when os.remove raises an OSError')
345
def test_get_file_name_encoding_done_test(self):
347
Test get_file_encoding when the detector sets done to True
349
# GIVEN: A mocked UniversalDetector instance with done attribute set to True after first iteration
350
with patch('openlp.core.common.UniversalDetector') as mocked_universal_detector, \
351
patch('builtins.open', return_value=BytesIO(b"data" * 260)) as mocked_open:
352
encoding_result = {'encoding': 'UTF-8', 'confidence': 0.99}
353
mocked_universal_detector_inst = MagicMock(result=encoding_result)
354
type(mocked_universal_detector_inst).done = PropertyMock(side_effect=[False, True])
355
mocked_universal_detector.return_value = mocked_universal_detector_inst
357
# WHEN: Calling get_file_encoding
358
result = get_file_encoding('file name')
360
# THEN: The feed method of UniversalDetector should only br called once before returning a result
361
mocked_open.assert_called_once_with('file name', 'rb')
362
self.assertEqual(mocked_universal_detector_inst.feed.mock_calls, [call(b"data" * 256)])
363
mocked_universal_detector_inst.close.assert_called_once_with()
364
self.assertEqual(result, encoding_result)
366
def test_get_file_name_encoding_eof_test(self):
368
Test get_file_encoding when the end of the file is reached
370
# GIVEN: A mocked UniversalDetector instance which isn't set to done and a mocked open, with 1040 bytes of test
371
# data (enough to run the iterator twice)
372
with patch('openlp.core.common.UniversalDetector') as mocked_universal_detector, \
373
patch('builtins.open', return_value=BytesIO(b"data" * 260)) as mocked_open:
374
encoding_result = {'encoding': 'UTF-8', 'confidence': 0.99}
375
mocked_universal_detector_inst = MagicMock(mock=mocked_universal_detector,
376
**{'done': False, 'result': encoding_result})
377
mocked_universal_detector.return_value = mocked_universal_detector_inst
379
# WHEN: Calling get_file_encoding
380
result = get_file_encoding('file name')
382
# THEN: The feed method of UniversalDetector should have been called twice before returning a result
383
mocked_open.assert_called_once_with('file name', 'rb')
384
self.assertEqual(mocked_universal_detector_inst.feed.mock_calls, [call(b"data" * 256), call(b"data" * 4)])
385
mocked_universal_detector_inst.close.assert_called_once_with()
386
self.assertEqual(result, encoding_result)
388
def test_get_file_name_encoding_oserror_test(self):
390
Test get_file_encoding when the end of the file is reached
392
# GIVEN: A mocked UniversalDetector instance which isn't set to done and a mocked open, with 1040 bytes of test
393
# data (enough to run the iterator twice)
394
with patch('openlp.core.common.UniversalDetector'), \
395
patch('builtins.open', side_effect=OSError), \
396
patch('openlp.core.common.log') as mocked_log:
398
# WHEN: Calling get_file_encoding
399
result = get_file_encoding('file name')
401
# THEN: log.exception should be called and get_file_encoding should return None
402
mocked_log.exception.assert_called_once_with('Error detecting file encoding')
403
self.assertIsNone(result)