Exemple #1
0
    def test_file_import(self):
        """
        Test the actual import of CSV Bible file
        """
        # GIVEN: Test files with a mocked out "manager", "import_wizard", and mocked functions
        #        get_book_ref_id_by_name, create_verse, create_book, session and get_language.
        test_data = load_external_result_data(TEST_PATH / 'dk1933.json')
        books_file = TEST_PATH / 'dk1933-books.csv'
        verses_file = TEST_PATH / 'dk1933-verses.csv'
        with patch('openlp.plugins.bibles.lib.importers.csvbible.CSVBible.application'):
            mocked_manager = MagicMock()
            mocked_import_wizard = MagicMock()
            importer = CSVBible(mocked_manager, path='.', name='.', books_path=books_file, verse_path=verses_file)
            importer.wizard = mocked_import_wizard
            importer.get_book_ref_id_by_name = MagicMock()
            importer.create_verse = MagicMock()
            importer.create_book = MagicMock()
            importer.session = MagicMock()
            importer.get_language = MagicMock()
            importer.get_language.return_value = 'Danish'
            importer.get_book = MagicMock()

            # WHEN: Importing bible file
            importer.do_import()

            # THEN: The create_verse() method should have been called with each verse in the file.
            assert importer.create_verse.called is True
            for verse_tag, verse_text in test_data['verses']:
                importer.create_verse.assert_any_call(importer.get_book().id, 1, verse_tag, verse_text)
            importer.create_book.assert_any_call('1. Mosebog', importer.get_book_ref_id_by_name(), 1)
            importer.create_book.assert_any_call('1. Krønikebog', importer.get_book_ref_id_by_name(), 1)
    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)
Exemple #3
0
    def test_do_import_invalid_language_id(self):
        """
        Test do_import when the user cancels the language selection dialog box
        """
        # GIVEN: An instance of CSVBible and a mocked get_language which simulates the user cancelling the language box
        mocked_manager = MagicMock()
        with patch('openlp.plugins.bibles.lib.db.BibleDB._setup'):
            importer = CSVBible(mocked_manager, path='.', name='.', books_path=Path('books.csv'),
                                verse_path=Path('verse.csv'))
            importer.get_language = MagicMock(return_value=None)

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

            # THEN: The False should be returned.
            importer.get_language.assert_called_once_with('Bible Name')
            assert result is False