Example #1
0
    def test_parse_csv_file_oserror(self):
        """
        Test the parse_csv_file() handles an OSError correctly
        """
        # GIVEN: Mocked a mocked open object which raises an OSError
        with patch('openlp.plugins.bibles.lib.importers.csvbible.get_file_encoding',
                   return_value={'encoding': 'utf-8', 'confidence': 0.99}),\
                patch('openlp.plugins.bibles.lib.importers.csvbible.Path.open', side_effect=OSError, create=True):

            # WHEN: Calling CSVBible.parse_csv_file
            # THEN: A ValidationError should be raised
            with self.assertRaises(ValidationError) as context:
                CSVBible.parse_csv_file(Path('file.csv'), None)
            assert context.exception.msg == 'Parsing "file.csv" failed'
Example #2
0
    def do_import_success_test(self):
        """
        Test do_import when the import succeeds
        """
        # GIVEN: An instance of CSVBible
        mocked_manager = MagicMock()
        with patch('openlp.plugins.bibles.lib.db.BibleDB._setup'):
            importer = CSVBible(mocked_manager,
                                path='.',
                                name='.',
                                booksfile='books.csv',
                                versefile='verses.csv')
            importer.get_language = MagicMock(return_value=10)
            importer.parse_csv_file = MagicMock(
                side_effect=[['Book 1'], ['Verse 1']])
            importer.process_books = MagicMock(return_value=['Book 1'])
            importer.process_verses = MagicMock(return_value=['Verse 1'])
            importer.session = MagicMock()
            importer.stop_import_flag = False
            importer.wizard = MagicMock()

            # WHEN: Calling do_import
            result = importer.do_import('Bible Name')

            # THEN: parse_csv_file should be called twice,
            # and True should be returned.
            self.assertEqual(
                importer.parse_csv_file.mock_calls,
                [call('books.csv', Book),
                 call('verses.csv', Verse)])
            importer.process_books.assert_called_once_with(['Book 1'])
            importer.process_verses.assert_called_once_with(['Verse 1'],
                                                            ['Book 1'])
            self.assertTrue(result)
Example #3
0
    def parse_csv_file_csverror_test(self):
        """
        Test the parse_csv_file() handles an csv.Error correctly
        """
        # GIVEN: Mocked a csv.reader which raises an csv.Error
        with patch('openlp.plugins.bibles.lib.importers.csvbible.get_file_encoding',
                   return_value={'encoding': 'utf-8', 'confidence': 0.99}),\
                patch('openlp.plugins.bibles.lib.importers.csvbible.open', create=True),\
                patch('openlp.plugins.bibles.lib.importers.csvbible.csv.reader', side_effect=csv.Error):

            # WHEN: Calling CSVBible.parse_csv_file
            # THEN: A ValidationError should be raised
            with self.assertRaises(ValidationError) as context:
                CSVBible.parse_csv_file('file.csv', None)
            self.assertEqual(context.exception.msg,
                             'Parsing "file.csv" failed')
Example #4
0
    def parse_csv_file_test(self):
        """
        Test the parse_csv_file() with sample data
        """
        # GIVEN: A mocked csv.reader which returns an iterator with test data
        test_data = [['1', 'Line 1', 'Data 1'], ['2', 'Line 2', 'Data 2'],
                     ['3', 'Line 3', 'Data 3']]
        TestTuple = namedtuple('TestTuple',
                               'line_no line_description line_data')

        with patch('openlp.plugins.bibles.lib.importers.csvbible.get_file_encoding',
                   return_value={'encoding': 'utf-8', 'confidence': 0.99}),\
                patch('openlp.plugins.bibles.lib.importers.csvbible.open', create=True) as mocked_open,\
                patch('openlp.plugins.bibles.lib.importers.csvbible.csv.reader',
                      return_value=iter(test_data)) as mocked_reader:

            # WHEN: Calling the CSVBible parse_csv_file method with a file name and TestTuple
            result = CSVBible.parse_csv_file('file.csv', TestTuple)

            # THEN: A list of TestTuple instances with the parsed data should be returned
            self.assertEqual(result, [
                TestTuple('1', 'Line 1', 'Data 1'),
                TestTuple('2', 'Line 2', 'Data 2'),
                TestTuple('3', 'Line 3', 'Data 3')
            ])
            mocked_open.assert_called_once_with('file.csv',
                                                'r',
                                                encoding='utf-8',
                                                newline='')
            mocked_reader.assert_called_once_with(ANY,
                                                  delimiter=',',
                                                  quotechar='"')
Example #5
0
    def test_parse_csv_file(self):
        """
        Test the parse_csv_file() with sample data
        """
        # GIVEN: A mocked csv.reader which returns an iterator with test data
        test_data = [['1', 'Line 1', 'Data 1'], ['2', 'Line 2', 'Data 2'],
                     ['3', 'Line 3', 'Data 3']]
        TestTuple = namedtuple('TestTuple',
                               'line_no line_description line_data')
        mocked_csv_file = MagicMock()
        mocked_enter_file = MagicMock()
        mocked_csv_file.open.return_value.__enter__.return_value = mocked_enter_file

        with patch('openlp.plugins.bibles.lib.importers.csvbible.get_file_encoding', return_value='utf-8'), \
                patch('openlp.plugins.bibles.lib.importers.csvbible.csv.reader',
                      return_value=iter(test_data)) as mocked_reader:

            # WHEN: Calling the CSVBible parse_csv_file method with a file name and TestTuple
            result = CSVBible.parse_csv_file(mocked_csv_file, TestTuple)

            # THEN: A list of TestTuple instances with the parsed data should be returned
            assert result == [
                TestTuple('1', 'Line 1', 'Data 1'),
                TestTuple('2', 'Line 2', 'Data 2'),
                TestTuple('3', 'Line 3', 'Data 3')
            ]
            mocked_csv_file.open.assert_called_once_with('r',
                                                         encoding='utf-8',
                                                         newline='')
            mocked_reader.assert_called_once_with(mocked_enter_file,
                                                  delimiter=',',
                                                  quotechar='"')
Example #6
0
    def test_parse_csv_file_csverror(self):
        """
        Test the parse_csv_file() handles an csv.Error correctly
        """
        # GIVEN: Mocked a csv.reader which raises an csv.Error
        mocked_csv_file = MagicMock()
        mocked_csv_file.__str__.return_value = 'file.csv'

        with patch('openlp.plugins.bibles.lib.importers.csvbible.get_file_encoding',
                   return_value={'encoding': 'utf-8', 'confidence': 0.99}),\
                patch('openlp.plugins.bibles.lib.importers.csvbible.csv.reader', side_effect=csv.Error):

            # WHEN: Calling CSVBible.parse_csv_file
            # THEN: A ValidationError should be raised
            with self.assertRaises(ValidationError) as context:
                CSVBible.parse_csv_file(mocked_csv_file, None)
            assert context.exception.msg == 'Parsing "file.csv" failed'