~openlp-core/openlp/trunk

« back to all changes in this revision

Viewing changes to tests/functional/openlp_core_common/test_init.py

  • Committer: Tim Bentley
  • Author(s): phill.ridout at gmail
  • Date: 2016-08-12 17:26:54 UTC
  • mfrom: (2685.1.15 more-bible-refactors)
  • Revision ID: tim.bentley@gmail.com-20160812172654-8rh9bq8nz61pauph
Some more bible refactors. Mainly focusing on xml parsing and the csv importer

lp:~phill-ridout/openlp/more-bible-refactors (revision 2699)
[SUCCESS] https://ci.openlp.io/job/Branch-01-Pull/1708/
[SUCCESS] https://ci.openlp.io/job/Branch-02-Functional-Tests/1619/
[SUCCESS] https://ci.openlp.io/job/Branch-03-Interface-Tests/1557/
[SUCCESS] https://ci.openlp.io/job/Branch-04a-Windows_Functional_Tests/1319/
[SUCCESS] https://ci.openlp.io/job/Branch-04b-Windows_Interface_Tests/909/
[SUCCESS] htt...

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
Functional tests to test the AppLocation class and related methods.
24
24
"""
25
25
import os
 
26
from io import BytesIO
26
27
from unittest import TestCase
27
28
 
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
32
33
 
33
34
 
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')
 
344
 
 
345
    def test_get_file_name_encoding_done_test(self):
 
346
        """
 
347
        Test get_file_encoding when the detector sets done to True
 
348
        """
 
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
 
356
 
 
357
            # WHEN: Calling get_file_encoding
 
358
            result = get_file_encoding('file name')
 
359
 
 
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)
 
365
 
 
366
    def test_get_file_name_encoding_eof_test(self):
 
367
        """
 
368
        Test get_file_encoding when the end of the file is reached
 
369
        """
 
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
 
378
 
 
379
            # WHEN: Calling get_file_encoding
 
380
            result = get_file_encoding('file name')
 
381
 
 
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)
 
387
 
 
388
    def test_get_file_name_encoding_oserror_test(self):
 
389
        """
 
390
        Test get_file_encoding when the end of the file is reached
 
391
        """
 
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:
 
397
 
 
398
            # WHEN: Calling get_file_encoding
 
399
            result = get_file_encoding('file name')
 
400
 
 
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)