forked from openlp/openlp
More fixes for broken tests
Skip one which never worked but relied on data leakage!
This commit is contained in:
parent
d4e6bf7a42
commit
8fa698d510
@ -680,9 +680,8 @@ class Settings(QtCore.QSettings):
|
||||
else:
|
||||
default_value = Settings.__default_settings__[key]
|
||||
try:
|
||||
setting = super(Settings, self).value(key, default_value)
|
||||
setting = super().value(key, default_value)
|
||||
except TypeError:
|
||||
log.error(f'Setting {key} is invalid , default {default_value} used as replacement')
|
||||
setting = default_value
|
||||
return self._convert_value(setting, default_value)
|
||||
|
||||
|
@ -55,10 +55,11 @@ def mocked_qapp():
|
||||
del app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@pytest.yield_fixture
|
||||
def registry():
|
||||
"""An instance of the Registry"""
|
||||
Registry.create()
|
||||
yield Registry.create()
|
||||
Registry._instances = {}
|
||||
|
||||
|
||||
@pytest.yield_fixture
|
||||
@ -80,16 +81,18 @@ def settings(qapp, registry):
|
||||
|
||||
|
||||
@pytest.yield_fixture
|
||||
def mock_settings(registry):
|
||||
def mock_settings(qapp, registry):
|
||||
"""A Mock Settings() instance"""
|
||||
# Create and register a mock settings object to work with
|
||||
mk_settings = MagicMock()
|
||||
Registry().register('settings', mk_settings)
|
||||
Registry().register('application', qapp)
|
||||
yield mk_settings
|
||||
Registry().remove('settings')
|
||||
del mk_settings
|
||||
|
||||
|
||||
@pytest.fixture(scope='function')
|
||||
@pytest.yield_fixture
|
||||
def state():
|
||||
State().load_settings()
|
||||
yield State().load_settings()
|
||||
State._instances = {}
|
||||
|
@ -126,7 +126,7 @@ def test_toggle_display_valid_action_updates_controller(flask_client, settings):
|
||||
assert controller.slidecontroller_toggle_display.set == 'show'
|
||||
|
||||
|
||||
def test_cors_headers_are_present(flask_client):
|
||||
def test_cors_headers_are_present(flask_client, settings):
|
||||
res = flask_client.get('/api/v2/core/system')
|
||||
assert 'Access-Control-Allow-Origin' in res.headers
|
||||
assert res.headers['Access-Control-Allow-Origin'] == '*'
|
||||
|
@ -29,7 +29,6 @@ from unittest.mock import MagicMock, patch
|
||||
|
||||
from openlp.core.common.httputils import ProxyMode, download_file, get_proxy_settings, get_url_file_size, \
|
||||
get_user_agent, get_web_page
|
||||
from openlp.core.common.settings import Settings
|
||||
|
||||
|
||||
@pytest.yield_fixture
|
||||
@ -168,7 +167,7 @@ def test_get_web_page_with_header(mocked_get_user_agent, mocked_requests, settin
|
||||
|
||||
@patch('openlp.core.common.httputils.requests')
|
||||
@patch('openlp.core.common.httputils.get_user_agent')
|
||||
def test_get_web_page_with_user_agent_in_headers(mocked_get_user_agent, mocked_requests):
|
||||
def test_get_web_page_with_user_agent_in_headers(mocked_get_user_agent, mocked_requests, settings):
|
||||
"""
|
||||
Test that adding a user agent in the header when calling get_web_page() adds that user agent to the request
|
||||
"""
|
||||
@ -216,7 +215,7 @@ def test_get_web_page_update_openlp(MockRegistry, mocked_get_user_agent, mocked_
|
||||
|
||||
|
||||
@patch('openlp.core.common.httputils.requests')
|
||||
def test_get_url_file_size(mocked_requests):
|
||||
def test_get_url_file_size(mocked_requests, settings):
|
||||
"""
|
||||
Test that calling "get_url_file_size" works correctly
|
||||
"""
|
||||
@ -276,16 +275,16 @@ def test_system_proxy_mode(settings):
|
||||
assert result is None
|
||||
|
||||
|
||||
def test_manual_proxy_mode_no_auth():
|
||||
def test_manual_proxy_mode_no_auth(settings):
|
||||
"""
|
||||
Test that the correct proxy addresses are returned when basic authentication is not used
|
||||
"""
|
||||
# GIVEN: A `proxy mode` setting of MANUAL_PROXY with proxy servers, but no auth credentials are supplied
|
||||
Settings().setValue('advanced/proxy mode', ProxyMode.MANUAL_PROXY)
|
||||
Settings().setValue('advanced/proxy http', 'testhttp.server:port')
|
||||
Settings().setValue('advanced/proxy https', 'testhttps.server:port')
|
||||
Settings().setValue('advanced/proxy username', '')
|
||||
Settings().setValue('advanced/proxy password', '')
|
||||
settings.setValue('advanced/proxy mode', ProxyMode.MANUAL_PROXY)
|
||||
settings.setValue('advanced/proxy http', 'testhttp.server:port')
|
||||
settings.setValue('advanced/proxy https', 'testhttps.server:port')
|
||||
settings.setValue('advanced/proxy username', '')
|
||||
settings.setValue('advanced/proxy password', '')
|
||||
|
||||
# WHEN: Calling `get_proxy_settings`
|
||||
result = get_proxy_settings()
|
||||
@ -294,16 +293,16 @@ def test_manual_proxy_mode_no_auth():
|
||||
assert result == {'http': 'http://testhttp.server:port', 'https': 'https://testhttps.server:port'}
|
||||
|
||||
|
||||
def test_manual_proxy_mode_auth():
|
||||
def test_manual_proxy_mode_auth(settings):
|
||||
"""
|
||||
Test that the correct proxy addresses are returned when basic authentication is used
|
||||
"""
|
||||
# GIVEN: A `proxy mode` setting of MANUAL_PROXY with proxy servers and auth credentials supplied
|
||||
Settings().setValue('advanced/proxy mode', ProxyMode.MANUAL_PROXY)
|
||||
Settings().setValue('advanced/proxy http', 'testhttp.server:port')
|
||||
Settings().setValue('advanced/proxy https', 'testhttps.server:port')
|
||||
Settings().setValue('advanced/proxy username', 'user')
|
||||
Settings().setValue('advanced/proxy password', 'pass')
|
||||
settings.setValue('advanced/proxy mode', ProxyMode.MANUAL_PROXY)
|
||||
settings.setValue('advanced/proxy http', 'testhttp.server:port')
|
||||
settings.setValue('advanced/proxy https', 'testhttps.server:port')
|
||||
settings.setValue('advanced/proxy username', 'user')
|
||||
settings.setValue('advanced/proxy password', 'pass')
|
||||
|
||||
# WHEN: Calling `get_proxy_settings`
|
||||
result = get_proxy_settings()
|
||||
@ -313,17 +312,17 @@ def test_manual_proxy_mode_auth():
|
||||
'https': 'https://user:pass@testhttps.server:port'}
|
||||
|
||||
|
||||
def test_manual_proxy_mode_no_servers():
|
||||
def test_manual_proxy_mode_no_servers(settings):
|
||||
"""
|
||||
Test that the system proxies are overidden when the MANUAL_PROXY mode is specified, but no server addresses are
|
||||
supplied
|
||||
"""
|
||||
# GIVEN: A `proxy mode` setting of MANUAL_PROXY with no servers specified
|
||||
Settings().setValue('advanced/proxy mode', ProxyMode.MANUAL_PROXY)
|
||||
Settings().setValue('advanced/proxy http', '')
|
||||
Settings().setValue('advanced/proxy https', '')
|
||||
Settings().setValue('advanced/proxy username', 'user')
|
||||
Settings().setValue('advanced/proxy password', 'pass')
|
||||
settings.setValue('advanced/proxy mode', ProxyMode.MANUAL_PROXY)
|
||||
settings.setValue('advanced/proxy http', '')
|
||||
settings.setValue('advanced/proxy https', '')
|
||||
settings.setValue('advanced/proxy username', 'user')
|
||||
settings.setValue('advanced/proxy password', 'pass')
|
||||
|
||||
# WHEN: Calling `get_proxy_settings`
|
||||
result = get_proxy_settings()
|
||||
|
@ -25,7 +25,6 @@ from unittest.mock import MagicMock, patch
|
||||
|
||||
from openlp.core.common.i18n import LANGUAGES, Language, UiStrings, get_language, get_locale_key, get_natural_key, \
|
||||
translate, LanguageManager
|
||||
from openlp.core.common.settings import Settings
|
||||
|
||||
|
||||
def test_languages_type():
|
||||
@ -159,8 +158,8 @@ def test_get_language_from_settings(settings):
|
||||
assert LanguageManager.get_language() == 'en'
|
||||
|
||||
|
||||
def test_get_language_from_settings_returns_unchanged_if_unknown_format():
|
||||
Settings().setValue('core/language', '(foobar)')
|
||||
def test_get_language_from_settings_returns_unchanged_if_unknown_format(settings):
|
||||
settings.setValue('core/language', '(foobar)')
|
||||
assert LanguageManager.get_language() == '(foobar)'
|
||||
|
||||
|
||||
|
@ -197,7 +197,7 @@ def test_render_chords_for_printing(settings):
|
||||
assert text_with_rendered_chords == expected_html, 'The rendered chords should look as expected!'
|
||||
|
||||
|
||||
def test_find_formatting_tags():
|
||||
def test_find_formatting_tags(settings):
|
||||
"""
|
||||
Test that find_formatting_tags works as expected
|
||||
"""
|
||||
|
@ -129,7 +129,7 @@ def test_firsttimeform_exec(mocked_qwizard_exec):
|
||||
mocked_qwizard_exec.assert_called_once()
|
||||
|
||||
|
||||
def test_set_defaults(mock_settings, ftf_app):
|
||||
def test_set_defaults(mock_settings):
|
||||
"""
|
||||
Test that the default values are set when set_defaults() is run
|
||||
"""
|
||||
|
@ -43,7 +43,7 @@ def test_initial_slide_controller(registry):
|
||||
assert slide_controller.is_live is False, 'The base slide controller should not be a live controller'
|
||||
|
||||
|
||||
def test_text_service_item_blank():
|
||||
def test_text_service_item_blank(settings):
|
||||
"""
|
||||
Test that loading a text-based service item into the slide controller sets the correct blank menu
|
||||
"""
|
||||
@ -63,7 +63,7 @@ def test_text_service_item_blank():
|
||||
toolbar.set_widget_visible.assert_called_with(WIDE_MENU, True)
|
||||
|
||||
|
||||
def test_non_text_service_item_blank():
|
||||
def test_non_text_service_item_blank(settings):
|
||||
"""
|
||||
Test that loading a non-text service item into the slide controller sets the correct blank menu
|
||||
"""
|
||||
@ -102,7 +102,7 @@ def test_receive_spin_delay(mock_settings):
|
||||
mocked_delay_spin_box.setValue.assert_called_with(1)
|
||||
|
||||
|
||||
def test_toggle_display_blank():
|
||||
def test_toggle_display_blank(settings):
|
||||
"""
|
||||
Check that the toggle_display('blank') method calls the on_blank_display() method
|
||||
"""
|
||||
@ -124,7 +124,7 @@ def test_toggle_display_blank():
|
||||
assert 0 == mocked_on_hide_display.call_count, 'on_hide_display should not have been called'
|
||||
|
||||
|
||||
def test_toggle_display_hide():
|
||||
def test_toggle_display_hide(settings):
|
||||
"""
|
||||
Check that the toggle_display('hide') method calls the on_blank_display() method
|
||||
"""
|
||||
@ -146,7 +146,7 @@ def test_toggle_display_hide():
|
||||
assert 0 == mocked_on_hide_display.call_count, 'on_hide_display should not have been called'
|
||||
|
||||
|
||||
def test_toggle_display_theme():
|
||||
def test_toggle_display_theme(settings):
|
||||
"""
|
||||
Check that the toggle_display('theme') method calls the on_theme_display() method
|
||||
"""
|
||||
@ -168,7 +168,7 @@ def test_toggle_display_theme():
|
||||
assert 0 == mocked_on_hide_display.call_count, 'on_hide_display should not have been called'
|
||||
|
||||
|
||||
def test_toggle_display_desktop():
|
||||
def test_toggle_display_desktop(settings):
|
||||
"""
|
||||
Check that the toggle_display('desktop') method calls the on_hide_display() method
|
||||
"""
|
||||
@ -190,7 +190,7 @@ def test_toggle_display_desktop():
|
||||
assert 0 == mocked_on_theme_display.call_count, 'on_theme_display should not have been called'
|
||||
|
||||
|
||||
def test_toggle_display_show():
|
||||
def test_toggle_display_show(settings):
|
||||
"""
|
||||
Check that the toggle_display('show') method calls all the on_X_display() methods
|
||||
"""
|
||||
@ -298,7 +298,7 @@ def test_on_go_live_service_manager(registry):
|
||||
mocked_live_controller.preview_widget.setFocus.assert_called_once_with()
|
||||
|
||||
|
||||
def test_service_previous():
|
||||
def test_service_previous(settings):
|
||||
"""
|
||||
Check that calling the service_previous() method adds the previous key to the queue and processes the queue
|
||||
"""
|
||||
@ -317,7 +317,7 @@ def test_service_previous():
|
||||
mocked_process_queue.assert_called_once_with()
|
||||
|
||||
|
||||
def test_service_next():
|
||||
def test_service_next(settings):
|
||||
"""
|
||||
Check that calling the service_next() method adds the next key to the queue and processes the queue
|
||||
"""
|
||||
@ -355,7 +355,7 @@ def test_update_slide_limits(mock_settings):
|
||||
assert 10 == slide_controller.slide_limits, 'Slide limits should have been updated to 10'
|
||||
|
||||
|
||||
def test_enable_tool_bar_live():
|
||||
def test_enable_tool_bar_live(settings):
|
||||
"""
|
||||
Check that when enable_tool_bar on a live slide controller is called, enable_live_tool_bar is called
|
||||
"""
|
||||
@ -376,7 +376,7 @@ def test_enable_tool_bar_live():
|
||||
assert 0 == mocked_enable_preview_tool_bar.call_count, 'The preview method should not have been called'
|
||||
|
||||
|
||||
def test_enable_tool_bar_preview():
|
||||
def test_enable_tool_bar_preview(settings):
|
||||
"""
|
||||
Check that when enable_tool_bar on a preview slide controller is called, enable_preview_tool_bar is called
|
||||
"""
|
||||
@ -397,7 +397,7 @@ def test_enable_tool_bar_preview():
|
||||
assert 0 == mocked_enable_live_tool_bar.call_count, 'The live method should not have been called'
|
||||
|
||||
|
||||
def test_refresh_service_item_text():
|
||||
def test_refresh_service_item_text(settings):
|
||||
"""
|
||||
Test that the refresh_service_item() method refreshes a text service item
|
||||
"""
|
||||
@ -421,7 +421,7 @@ def test_refresh_service_item_text():
|
||||
mocked_process_item.assert_called_once_with(mocked_service_item, 5)
|
||||
|
||||
|
||||
def test_refresh_service_item_image():
|
||||
def test_refresh_service_item_image(settings):
|
||||
"""
|
||||
Test that the refresh_service_item() method refreshes a image service item
|
||||
"""
|
||||
@ -445,7 +445,7 @@ def test_refresh_service_item_image():
|
||||
mocked_process_item.assert_called_once_with(mocked_service_item, 5)
|
||||
|
||||
|
||||
def test_refresh_service_item_not_image_or_text():
|
||||
def test_refresh_service_item_not_image_or_text(settings):
|
||||
"""
|
||||
Test that the refresh_service_item() method does not refresh a service item if it's neither text or an image
|
||||
"""
|
||||
@ -469,7 +469,7 @@ def test_refresh_service_item_not_image_or_text():
|
||||
assert 0 == mocked_process_item.call_count, 'The mocked_process_item() method should not have been called'
|
||||
|
||||
|
||||
def test_add_service_item_with_song_edit():
|
||||
def test_add_service_item_with_song_edit(settings):
|
||||
"""
|
||||
Test the add_service_item() method when song_edit is True
|
||||
"""
|
||||
@ -489,7 +489,7 @@ def test_add_service_item_with_song_edit():
|
||||
mocked_process_item.assert_called_once_with(mocked_item, 2)
|
||||
|
||||
|
||||
def test_add_service_item_without_song_edit():
|
||||
def test_add_service_item_without_song_edit(settings):
|
||||
"""
|
||||
Test the add_service_item() method when song_edit is False
|
||||
"""
|
||||
@ -509,7 +509,7 @@ def test_add_service_item_without_song_edit():
|
||||
mocked_process_item.assert_called_once_with(mocked_item, 0)
|
||||
|
||||
|
||||
def test_replace_service_manager_item_different_items():
|
||||
def test_replace_service_manager_item_different_items(settings):
|
||||
"""
|
||||
Test that when the service items are not the same, nothing happens
|
||||
"""
|
||||
@ -531,7 +531,7 @@ def test_replace_service_manager_item_different_items():
|
||||
'The preview_widget current_slide_number.() method should not have been called'
|
||||
|
||||
|
||||
def test_replace_service_manager_item_same_item():
|
||||
def test_replace_service_manager_item_same_item(settings):
|
||||
"""
|
||||
Test that when the service item is the same, the service item is reprocessed
|
||||
"""
|
||||
@ -553,7 +553,7 @@ def test_replace_service_manager_item_same_item():
|
||||
mocked_process_item.assert_called_once_with(mocked_item, 7)
|
||||
|
||||
|
||||
def test_on_slide_blank():
|
||||
def test_on_slide_blank(settings):
|
||||
"""
|
||||
Test on_slide_blank
|
||||
"""
|
||||
@ -568,7 +568,7 @@ def test_on_slide_blank():
|
||||
slide_controller.on_blank_display.assert_called_once_with(True)
|
||||
|
||||
|
||||
def test_on_slide_unblank():
|
||||
def test_on_slide_unblank(settings):
|
||||
"""
|
||||
Test on_slide_unblank
|
||||
"""
|
||||
@ -583,7 +583,7 @@ def test_on_slide_unblank():
|
||||
slide_controller.on_blank_display.assert_called_once_with(False)
|
||||
|
||||
|
||||
def test_on_slide_selected_index_no_service_item():
|
||||
def test_on_slide_selected_index_no_service_item(settings):
|
||||
"""
|
||||
Test that when there is no service item, the on_slide_selected_index() method returns immediately
|
||||
"""
|
||||
@ -705,7 +705,7 @@ def test_process_item(mocked_execute, registry):
|
||||
'The presentation should have been stopped.'
|
||||
|
||||
|
||||
def test_live_stolen_focus_shortcuts():
|
||||
def test_live_stolen_focus_shortcuts(settings):
|
||||
"""
|
||||
Test that all the needed shortcuts are available in scenarios where Live has stolen focus.
|
||||
These are found under def __add_actions_to_widget(self, widget): in slidecontroller.py
|
||||
|
@ -21,22 +21,12 @@
|
||||
"""
|
||||
This module contains tests for the CSV Bible importer.
|
||||
"""
|
||||
from unittest import TestCase
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from openlp.core.common.registry import Registry
|
||||
from openlp.plugins.alerts.lib.alertsmanager import AlertsManager
|
||||
|
||||
|
||||
class TestAlertManager(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
"""
|
||||
Create the UI
|
||||
"""
|
||||
Registry.create()
|
||||
|
||||
def test_remove_message_text(self):
|
||||
def test_remove_message_text(registry):
|
||||
"""
|
||||
Test that Alerts are not triggered with empty strings
|
||||
"""
|
||||
@ -50,7 +40,8 @@ class TestAlertManager(TestCase):
|
||||
# THEN: the display should not have been triggered
|
||||
assert alert_manager.display_alert.called is False, 'The Alert should not have been called'
|
||||
|
||||
def test_trigger_message_text(self):
|
||||
|
||||
def test_trigger_message_text(registry):
|
||||
"""
|
||||
Test that Alerts are triggered with a text string
|
||||
"""
|
||||
@ -64,7 +55,8 @@ class TestAlertManager(TestCase):
|
||||
# THEN: the display should have been triggered
|
||||
assert alert_manager.display_alert.called is True, 'The Alert should have been called'
|
||||
|
||||
def test_line_break_message_text(self):
|
||||
|
||||
def test_line_break_message_text(registry):
|
||||
"""
|
||||
Test that Alerts are triggered with a text string but line breaks are removed
|
||||
"""
|
||||
|
@ -21,52 +21,55 @@
|
||||
"""
|
||||
This module contains tests for the bibleimport module.
|
||||
"""
|
||||
import pytest
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from unittest import TestCase
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from lxml import etree, objectify
|
||||
from PyQt5.QtWidgets import QDialog
|
||||
|
||||
from openlp.core.common.i18n import Language
|
||||
from openlp.core.common.registry import Registry
|
||||
from openlp.core.lib.exceptions import ValidationError
|
||||
from openlp.plugins.bibles.lib.bibleimport import BibleImport
|
||||
from openlp.plugins.bibles.lib.db import BibleDB
|
||||
|
||||
|
||||
class TestBibleImport(TestCase):
|
||||
"""
|
||||
Test the functions in the :mod:`bibleimport` module.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.test_file = BytesIO(
|
||||
@pytest.fixture()
|
||||
def test_file():
|
||||
return BytesIO(
|
||||
b'<?xml version="1.0" encoding="UTF-8" ?>\n'
|
||||
b'<root>\n'
|
||||
b' <data><div>Test<p>data</p><a>to</a>keep</div></data>\n'
|
||||
b' <data><unsupported>Test<x>data</x><y>to</y>discard</unsupported></data>\n'
|
||||
b'</root>'
|
||||
)
|
||||
self.open_patcher = patch.object(Path, 'open')
|
||||
self.addCleanup(self.open_patcher.stop)
|
||||
self.mocked_open = self.open_patcher.start()
|
||||
self.critical_error_message_box_patcher = \
|
||||
patch('openlp.plugins.bibles.lib.bibleimport.critical_error_message_box')
|
||||
self.addCleanup(self.critical_error_message_box_patcher.stop)
|
||||
self.mocked_critical_error_message_box = self.critical_error_message_box_patcher.start()
|
||||
self.setup_patcher = patch('openlp.plugins.bibles.lib.db.BibleDB._setup')
|
||||
self.addCleanup(self.setup_patcher.stop)
|
||||
self.setup_patcher.start()
|
||||
self.translate_patcher = patch('openlp.plugins.bibles.lib.bibleimport.translate',
|
||||
side_effect=lambda module, string_to_translate, *args: string_to_translate)
|
||||
self.addCleanup(self.translate_patcher.stop)
|
||||
self.mocked_translate = self.translate_patcher.start()
|
||||
self.registry_patcher = patch('openlp.plugins.bibles.lib.bibleimport.Registry')
|
||||
self.addCleanup(self.registry_patcher.stop)
|
||||
self.registry_patcher.start()
|
||||
|
||||
def test_init_kwargs_none(self):
|
||||
|
||||
@pytest.fixture()
|
||||
def error_message_box(settings):
|
||||
Registry().register('main_window', MagicMock())
|
||||
m_box = patch('openlp.plugins.bibles.lib.bibleimport.critical_error_message_box')
|
||||
yield m_box.start()
|
||||
m_box.stop()
|
||||
|
||||
|
||||
@pytest.yield_fixture()
|
||||
def mocked_open():
|
||||
m_open = patch.object(Path, 'open')
|
||||
yield m_open.start()
|
||||
m_open.stop()
|
||||
|
||||
|
||||
@pytest.yield_fixture()
|
||||
def mocked_setup():
|
||||
s_up = patch('openlp.plugins.bibles.lib.db.BibleDB._setup')
|
||||
yield s_up.start()
|
||||
s_up.stop()
|
||||
|
||||
|
||||
def test_init_kwargs_none(mocked_setup, registry):
|
||||
"""
|
||||
Test the initialisation of the BibleImport Class when no key word arguments are supplied
|
||||
"""
|
||||
@ -78,7 +81,8 @@ class TestBibleImport(TestCase):
|
||||
assert instance.file_path is None
|
||||
assert isinstance(instance, BibleDB)
|
||||
|
||||
def test_init_kwargs_set(self):
|
||||
|
||||
def test_init_kwargs_set(mocked_setup, registry):
|
||||
"""
|
||||
Test the initialisation of the BibleImport Class when supplied with select keyword arguments
|
||||
"""
|
||||
@ -91,9 +95,9 @@ class TestBibleImport(TestCase):
|
||||
assert instance.file_path == 'bible.xml'
|
||||
assert isinstance(instance, BibleDB)
|
||||
|
||||
@patch.object(BibleDB, '_setup')
|
||||
@patch('openlp.plugins.bibles.forms.LanguageForm')
|
||||
def test_get_language_canceled(self, MockedLanguageForm, mocked_setup):
|
||||
|
||||
@patch('openlp.plugins.bibles.forms.LanguageForm')
|
||||
def test_get_language_canceled(MockedLanguageForm, mocked_setup, registry):
|
||||
"""
|
||||
Test the BibleImport.get_language method when the user rejects the dialog box
|
||||
"""
|
||||
@ -112,10 +116,10 @@ class TestBibleImport(TestCase):
|
||||
MockedLanguageForm.return_value.exec.assert_called_once_with('ESV')
|
||||
assert result is False, 'get_language() should return False if the user rejects the dialog box'
|
||||
|
||||
@patch.object(BibleDB, 'save_meta')
|
||||
@patch.object(BibleDB, '_setup')
|
||||
@patch('openlp.plugins.bibles.forms.LanguageForm')
|
||||
def test_get_language_accepted(self, MockedLanguageForm, mocked_setup, mocked_save_meta):
|
||||
|
||||
@patch.object(BibleDB, 'save_meta')
|
||||
@patch('openlp.plugins.bibles.forms.LanguageForm')
|
||||
def test_get_language_accepted(MockedLanguageForm, mocked_save_meta, mocked_setup, registry):
|
||||
"""
|
||||
Test the BibleImport.get_language method when the user accepts the dialog box
|
||||
"""
|
||||
@ -137,9 +141,10 @@ class TestBibleImport(TestCase):
|
||||
assert result == 10, 'get_language() should return the id of the language the user has chosen when ' \
|
||||
'they accept the dialog box'
|
||||
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.get_language')
|
||||
@patch.object(BibleImport, 'get_language')
|
||||
def test_get_language_id_language_found(self, mocked_db_get_language, mocked_get_language):
|
||||
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.get_language')
|
||||
@patch.object(BibleImport, 'get_language')
|
||||
def test_get_language_id_language_found(mocked_db_get_language, mocked_get_language, mocked_setup, registry):
|
||||
"""
|
||||
Test get_language_id() when called with a name found in the languages list
|
||||
"""
|
||||
@ -157,9 +162,11 @@ class TestBibleImport(TestCase):
|
||||
instance.save_meta.assert_called_once_with('language_id', 30)
|
||||
assert result == 30, 'Result should be 30, was {}'.format(result)
|
||||
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.get_language', return_value=None)
|
||||
@patch.object(BibleImport, 'get_language', return_value=20)
|
||||
def test_get_language_id_language_not_found(self, mocked_db_get_language, mocked_languages_get_language):
|
||||
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.get_language', return_value=None)
|
||||
@patch.object(BibleImport, 'get_language', return_value=20)
|
||||
def test_get_language_id_language_not_found(mocked_db_get_language, mocked_languages_get_language,
|
||||
mocked_setup, registry):
|
||||
"""
|
||||
Test get_language_id() when called with a name not found in the languages list
|
||||
"""
|
||||
@ -176,10 +183,12 @@ class TestBibleImport(TestCase):
|
||||
instance.save_meta.assert_called_once_with('language_id', 20)
|
||||
assert result == 20
|
||||
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.get_language', return_value=None)
|
||||
@patch.object(BibleImport, 'get_language', return_value=40)
|
||||
@patch.object(BibleImport, 'log_error')
|
||||
def test_get_language_id_user_choice(self, mocked_log_error, mocked_db_get_language, mocked_languages_get_language):
|
||||
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.get_language', return_value=None)
|
||||
@patch.object(BibleImport, 'get_language', return_value=40)
|
||||
@patch.object(BibleImport, 'log_error')
|
||||
def test_get_language_id_user_choice(mocked_log_error, mocked_db_get_language, mocked_languages_get_language,
|
||||
mocked_setup, registry):
|
||||
"""
|
||||
Test get_language_id() when the language is not found and the user is asked for the language
|
||||
"""
|
||||
@ -198,11 +207,12 @@ class TestBibleImport(TestCase):
|
||||
instance.save_meta.assert_called_once_with('language_id', 40)
|
||||
assert result == 40
|
||||
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.get_language', return_value=None)
|
||||
@patch.object(BibleImport, 'get_language', return_value=None)
|
||||
@patch.object(BibleImport, 'log_error')
|
||||
def test_get_language_id_user_choice_rejected(self, mocked_log_error, mocked_db_get_language,
|
||||
mocked_languages_get_language):
|
||||
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.get_language', return_value=None)
|
||||
@patch.object(BibleImport, 'get_language', return_value=None)
|
||||
@patch.object(BibleImport, 'log_error')
|
||||
def test_get_language_id_user_choice_rejected(mocked_log_error, mocked_db_get_language,
|
||||
mocked_languages_get_language, mocked_setup, registry):
|
||||
"""
|
||||
Test get_language_id() when the language is not found and the user rejects the dilaog box
|
||||
"""
|
||||
@ -222,9 +232,10 @@ class TestBibleImport(TestCase):
|
||||
assert instance.save_meta.called is False
|
||||
assert result is None
|
||||
|
||||
@patch.object(BibleImport, 'log_debug')
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.BiblesResourcesDB', **{'get_book.return_value': {'id': 20}})
|
||||
def test_get_book_ref_id_by_name_get_book(self, MockBibleResourcesDB, mocked_log_debug):
|
||||
|
||||
@patch.object(BibleImport, 'log_debug')
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.BiblesResourcesDB', **{'get_book.return_value': {'id': 20}})
|
||||
def test_get_book_ref_id_by_name_get_book(MockBibleResourcesDB, mocked_log_debug, mocked_setup, registry):
|
||||
"""
|
||||
Test get_book_ref_id_by_name when the book is found as a book in BiblesResourcesDB
|
||||
"""
|
||||
@ -238,10 +249,12 @@ class TestBibleImport(TestCase):
|
||||
# THEN: The bible id should be returned
|
||||
assert result == 20
|
||||
|
||||
@patch.object(BibleImport, 'log_debug')
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.BiblesResourcesDB',
|
||||
|
||||
@patch.object(BibleImport, 'log_debug')
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.BiblesResourcesDB',
|
||||
**{'get_book.return_value': None, 'get_alternative_book_name.return_value': 30})
|
||||
def test_get_book_ref_id_by_name_get_alternative_book_name(self, MockBibleResourcesDB, mocked_log_debug):
|
||||
def test_get_book_ref_id_by_name_get_alternative_book_name(MockBibleResourcesDB, mocked_log_debug,
|
||||
mocked_setup, registry):
|
||||
"""
|
||||
Test get_book_ref_id_by_name when the book is found as an alternative book in BiblesResourcesDB
|
||||
"""
|
||||
@ -255,12 +268,13 @@ class TestBibleImport(TestCase):
|
||||
# THEN: The bible id should be returned
|
||||
assert result == 30
|
||||
|
||||
@patch.object(BibleImport, 'log_debug')
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.BiblesResourcesDB',
|
||||
|
||||
@patch.object(BibleImport, 'log_debug')
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.BiblesResourcesDB',
|
||||
**{'get_book.return_value': None, 'get_alternative_book_name.return_value': None})
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.AlternativeBookNamesDB', **{'get_book_reference_id.return_value': 40})
|
||||
def test_get_book_ref_id_by_name_get_book_reference_id(self, MockAlterativeBookNamesDB, MockBibleResourcesDB,
|
||||
mocked_log_debug):
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.AlternativeBookNamesDB', **{'get_book_reference_id.return_value': 40})
|
||||
def test_get_book_ref_id_by_name_get_book_reference_id(MockAlterativeBookNamesDB, MockBibleResourcesDB,
|
||||
mocked_log_debug, mocked_setup, registry):
|
||||
"""
|
||||
Test get_book_ref_id_by_name when the book is found as a book in AlternativeBookNamesDB
|
||||
"""
|
||||
@ -274,16 +288,18 @@ class TestBibleImport(TestCase):
|
||||
# THEN: The bible id should be returned
|
||||
assert result == 40
|
||||
|
||||
@patch.object(BibleImport, 'log_debug')
|
||||
@patch.object(BibleImport, 'get_books')
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.BiblesResourcesDB',
|
||||
|
||||
@patch.object(BibleImport, 'log_debug')
|
||||
@patch.object(BibleImport, 'get_books')
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.BiblesResourcesDB',
|
||||
**{'get_book.return_value': None, 'get_alternative_book_name.return_value': None})
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.AlternativeBookNamesDB',
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.AlternativeBookNamesDB',
|
||||
**{'get_book_reference_id.return_value': None})
|
||||
@patch('openlp.plugins.bibles.forms.BookNameForm',
|
||||
@patch('openlp.plugins.bibles.forms.BookNameForm',
|
||||
return_value=MagicMock(**{'exec.return_value': QDialog.Rejected}))
|
||||
def test_get_book_ref_id_by_name_book_name_form_rejected(self, MockBookNameForm, MockAlterativeBookNamesDB,
|
||||
MockBibleResourcesDB, mocked_get_books, mocked_log_debug):
|
||||
def test_get_book_ref_id_by_name_book_name_form_rejected(MockBookNameForm, MockAlterativeBookNamesDB,
|
||||
MockBibleResourcesDB, mocked_get_books, mocked_log_debug,
|
||||
mocked_setup, registry):
|
||||
"""
|
||||
Test get_book_ref_id_by_name when the user rejects the BookNameForm
|
||||
"""
|
||||
@ -296,16 +312,18 @@ class TestBibleImport(TestCase):
|
||||
# THEN: None should be returned
|
||||
assert result is None
|
||||
|
||||
@patch.object(BibleImport, 'log_debug')
|
||||
@patch.object(BibleImport, 'get_books')
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.BiblesResourcesDB',
|
||||
|
||||
@patch.object(BibleImport, 'log_debug')
|
||||
@patch.object(BibleImport, 'get_books')
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.BiblesResourcesDB',
|
||||
**{'get_book.return_value': None, 'get_alternative_book_name.return_value': None})
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.AlternativeBookNamesDB',
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.AlternativeBookNamesDB',
|
||||
**{'get_book_reference_id.return_value': None})
|
||||
@patch('openlp.plugins.bibles.forms.BookNameForm',
|
||||
@patch('openlp.plugins.bibles.forms.BookNameForm',
|
||||
return_value=MagicMock(**{'exec.return_value': QDialog.Accepted, 'book_id': 50}))
|
||||
def test_get_book_ref_id_by_name_book_name_form_accepted(self, MockBookNameForm, MockAlterativeBookNamesDB,
|
||||
MockBibleResourcesDB, mocked_get_books, mocked_log_debug):
|
||||
def test_get_book_ref_id_by_name_book_name_form_accepted(MockBookNameForm, MockAlterativeBookNamesDB,
|
||||
MockBibleResourcesDB, mocked_get_books, mocked_log_debug,
|
||||
mocked_setup, registry):
|
||||
"""
|
||||
Test get_book_ref_id_by_name when the user accepts the BookNameForm
|
||||
"""
|
||||
@ -319,8 +337,9 @@ class TestBibleImport(TestCase):
|
||||
MockAlterativeBookNamesDB.create_alternative_book_name.assert_called_once_with('Gen', 50, 4)
|
||||
assert result == 50
|
||||
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.is_zipfile', return_value=True)
|
||||
def test_is_compressed_compressed(self, mocked_is_zipfile):
|
||||
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.is_zipfile', return_value=True)
|
||||
def test_is_compressed_compressed(mocked_is_zipfile, mocked_open, mocked_setup, error_message_box):
|
||||
"""
|
||||
Test is_compressed when the 'file' being tested is compressed
|
||||
"""
|
||||
@ -332,12 +351,13 @@ class TestBibleImport(TestCase):
|
||||
|
||||
# THEN: Then critical_error_message_box should be called informing the user that the file is compressed and
|
||||
# True should be returned
|
||||
self.mocked_critical_error_message_box.assert_called_once_with(
|
||||
error_message_box.assert_called_once_with(
|
||||
message='The file "file.ext" you supplied is compressed. You must decompress it before import.')
|
||||
assert result is True
|
||||
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.is_zipfile', return_value=False)
|
||||
def test_is_compressed_not_compressed(self, mocked_is_zipfile):
|
||||
|
||||
@patch('openlp.plugins.bibles.lib.bibleimport.is_zipfile', return_value=False)
|
||||
def test_is_compressed_not_compressed(mocked_is_zipfile, mocked_open, mocked_setup, error_message_box):
|
||||
"""
|
||||
Test is_compressed when the 'file' being tested is not compressed
|
||||
"""
|
||||
@ -349,14 +369,15 @@ class TestBibleImport(TestCase):
|
||||
|
||||
# THEN: False should be returned and critical_error_message_box should not have been called
|
||||
assert result is False
|
||||
assert self.mocked_critical_error_message_box.called is False
|
||||
assert error_message_box.called is False
|
||||
|
||||
def test_parse_xml_etree(self):
|
||||
|
||||
def test_parse_xml_etree(registry, mocked_open, mocked_setup, test_file):
|
||||
"""
|
||||
Test BibleImport.parse_xml() when called with the use_objectify default value
|
||||
"""
|
||||
# GIVEN: A sample "file" to parse and an instance of BibleImport
|
||||
self.mocked_open.return_value = self.test_file
|
||||
mocked_open.return_value = test_file
|
||||
instance = BibleImport(MagicMock())
|
||||
instance.wizard = MagicMock()
|
||||
|
||||
@ -368,12 +389,13 @@ class TestBibleImport(TestCase):
|
||||
b' <data><unsupported>Test<x>data</x><y>to</y>discard</unsupported></data>\n</root>'
|
||||
assert isinstance(result, etree._Element)
|
||||
|
||||
def test_parse_xml_etree_use_objectify(self):
|
||||
|
||||
def test_parse_xml_etree_use_objectify(registry, mocked_open, mocked_setup, test_file):
|
||||
"""
|
||||
Test BibleImport.parse_xml() when called with use_objectify set to True
|
||||
"""
|
||||
# GIVEN: A sample "file" to parse and an instance of BibleImport
|
||||
self.mocked_open.return_value = self.test_file
|
||||
mocked_open.return_value = test_file
|
||||
instance = BibleImport(MagicMock())
|
||||
instance.wizard = MagicMock()
|
||||
|
||||
@ -385,12 +407,13 @@ class TestBibleImport(TestCase):
|
||||
b'<data><unsupported>Test<x>data</x><y>to</y>discard</unsupported></data></root>'
|
||||
assert isinstance(result, objectify.ObjectifiedElement)
|
||||
|
||||
def test_parse_xml_elements(self):
|
||||
|
||||
def test_parse_xml_elements(registry, mocked_open, mocked_setup, test_file):
|
||||
"""
|
||||
Test BibleImport.parse_xml() when given a tuple of elements to remove
|
||||
"""
|
||||
# GIVEN: A tuple of elements to remove and an instance of BibleImport
|
||||
self.mocked_open.return_value = self.test_file
|
||||
mocked_open.return_value = test_file
|
||||
elements = ('unsupported', 'x', 'y')
|
||||
instance = BibleImport(MagicMock())
|
||||
instance.wizard = MagicMock()
|
||||
@ -402,12 +425,13 @@ class TestBibleImport(TestCase):
|
||||
assert etree.tostring(result) == \
|
||||
b'<root>\n <data><div>Test<p>data</p><a>to</a>keep</div></data>\n <data/>\n</root>'
|
||||
|
||||
def test_parse_xml_tags(self):
|
||||
|
||||
def test_parse_xml_tags(registry, mocked_open, mocked_setup, test_file):
|
||||
"""
|
||||
Test BibleImport.parse_xml() when given a tuple of tags to remove
|
||||
"""
|
||||
# GIVEN: A tuple of tags to remove and an instance of BibleImport
|
||||
self.mocked_open.return_value = self.test_file
|
||||
mocked_open.return_value = test_file
|
||||
tags = ('div', 'p', 'a')
|
||||
instance = BibleImport(MagicMock())
|
||||
instance.wizard = MagicMock()
|
||||
@ -419,12 +443,13 @@ class TestBibleImport(TestCase):
|
||||
assert etree.tostring(result) == b'<root>\n <data>Testdatatokeep</data>\n <data><unsupported>Test' \
|
||||
b'<x>data</x><y>to</y>discard</unsupported></data>\n</root>'
|
||||
|
||||
def test_parse_xml_elements_tags(self):
|
||||
|
||||
def test_parse_xml_elements_tags(registry, mocked_open, mocked_setup, test_file):
|
||||
"""
|
||||
Test BibleImport.parse_xml() when given a tuple of elements and of tags to remove
|
||||
"""
|
||||
# GIVEN: A tuple of elements and of tags to remove and an instacne of BibleImport
|
||||
self.mocked_open.return_value = self.test_file
|
||||
# GIVEN: A tuple of elements and of tags to remove and an instance of BibleImport
|
||||
mocked_open.return_value = test_file
|
||||
elements = ('unsupported', 'x', 'y')
|
||||
tags = ('div', 'p', 'a')
|
||||
instance = BibleImport(MagicMock())
|
||||
@ -436,8 +461,9 @@ class TestBibleImport(TestCase):
|
||||
# THEN: The result returned should contain the correct data
|
||||
assert etree.tostring(result) == b'<root>\n <data>Testdatatokeep</data>\n <data/>\n</root>'
|
||||
|
||||
@patch.object(BibleImport, 'log_exception')
|
||||
def test_parse_xml_file_file_not_found_exception(self, mocked_log_exception):
|
||||
|
||||
@patch.object(BibleImport, 'log_exception')
|
||||
def test_parse_xml_file_file_not_found_exception(mocked_log_exception, mocked_open, error_message_box):
|
||||
"""
|
||||
Test that parse_xml handles a FileNotFoundError exception correctly
|
||||
"""
|
||||
@ -445,7 +471,7 @@ class TestBibleImport(TestCase):
|
||||
exception = FileNotFoundError()
|
||||
exception.filename = 'file.tst'
|
||||
exception.strerror = 'No such file or directory'
|
||||
self.mocked_open.side_effect = exception
|
||||
mocked_open.side_effect = exception
|
||||
importer = BibleImport(MagicMock(), path='.', name='.', file_path=None)
|
||||
|
||||
# WHEN: Calling parse_xml
|
||||
@ -453,13 +479,14 @@ class TestBibleImport(TestCase):
|
||||
|
||||
# THEN: parse_xml should have caught the error, informed the user and returned None
|
||||
mocked_log_exception.assert_called_once_with('Opening file.tst failed.')
|
||||
self.mocked_critical_error_message_box.assert_called_once_with(
|
||||
error_message_box.assert_called_once_with(
|
||||
title='An Error Occured When Opening A File',
|
||||
message='The following error occurred when trying to open\nfile.tst:\n\nNo such file or directory')
|
||||
assert result is None
|
||||
|
||||
@patch.object(BibleImport, 'log_exception')
|
||||
def test_parse_xml_file_permission_error_exception(self, mocked_log_exception):
|
||||
|
||||
@patch.object(BibleImport, 'log_exception')
|
||||
def test_parse_xml_file_permission_error_exception(mocked_log_exception, mocked_open, error_message_box):
|
||||
"""
|
||||
Test that parse_xml handles a PermissionError exception correctly
|
||||
"""
|
||||
@ -467,7 +494,7 @@ class TestBibleImport(TestCase):
|
||||
exception = PermissionError()
|
||||
exception.filename = 'file.tst'
|
||||
exception.strerror = 'Permission denied'
|
||||
self.mocked_open.side_effect = exception
|
||||
mocked_open.side_effect = exception
|
||||
importer = BibleImport(MagicMock(), path='.', name='.', file_path=None)
|
||||
|
||||
# WHEN: Calling parse_xml
|
||||
@ -475,12 +502,13 @@ class TestBibleImport(TestCase):
|
||||
|
||||
# THEN: parse_xml should have caught the error, informed the user and returned None
|
||||
mocked_log_exception.assert_called_once_with('Opening file.tst failed.')
|
||||
self.mocked_critical_error_message_box.assert_called_once_with(
|
||||
error_message_box.assert_called_once_with(
|
||||
title='An Error Occured When Opening A File',
|
||||
message='The following error occurred when trying to open\nfile.tst:\n\nPermission denied')
|
||||
assert result is None
|
||||
|
||||
def test_set_current_chapter(self):
|
||||
|
||||
def test_set_current_chapter(settings):
|
||||
"""
|
||||
Test set_current_chapter
|
||||
"""
|
||||
@ -494,8 +522,9 @@ class TestBibleImport(TestCase):
|
||||
# THEN: Increment_progress_bar should have been called with a text string
|
||||
importer.wizard.increment_progress_bar.assert_called_once_with('Importing Book_Name Chapter...')
|
||||
|
||||
@patch.object(BibleImport, 'is_compressed', return_value=True)
|
||||
def test_validate_xml_file_compressed_file(self, mocked_is_compressed):
|
||||
|
||||
@patch.object(BibleImport, 'is_compressed', return_value=True)
|
||||
def test_validate_xml_file_compressed_file(mocked_is_compressed, settings):
|
||||
"""
|
||||
Test that validate_xml_file raises a ValidationError when is_compressed returns True
|
||||
"""
|
||||
@ -504,13 +533,14 @@ class TestBibleImport(TestCase):
|
||||
|
||||
# WHEN: Calling is_compressed
|
||||
# THEN: ValidationError should be raised, with the message 'Compressed file'
|
||||
with self.assertRaises(ValidationError) as context:
|
||||
with pytest.raises(ValidationError) as context:
|
||||
importer.validate_xml_file('file.name', 'xbible')
|
||||
assert context.exception.msg == 'Compressed file'
|
||||
assert context.value != ValidationError('Compressed file')
|
||||
|
||||
@patch.object(BibleImport, 'parse_xml', return_value=None)
|
||||
@patch.object(BibleImport, 'is_compressed', return_value=False)
|
||||
def test_validate_xml_file_parse_xml_fails(self, mocked_is_compressed, mocked_parse_xml):
|
||||
|
||||
@patch.object(BibleImport, 'parse_xml', return_value=None)
|
||||
@patch.object(BibleImport, 'is_compressed', return_value=False)
|
||||
def test_validate_xml_file_parse_xml_fails(mocked_is_compressed, mocked_parse_xml, settings):
|
||||
"""
|
||||
Test that validate_xml_file raises a ValidationError when parse_xml returns None
|
||||
"""
|
||||
@ -520,13 +550,14 @@ class TestBibleImport(TestCase):
|
||||
# WHEN: Calling validate_xml_file
|
||||
# THEN: ValidationError should be raised, with the message 'Error when opening file'
|
||||
# the user that an OpenSong bible was found
|
||||
with self.assertRaises(ValidationError) as context:
|
||||
with pytest.raises(ValidationError) as context:
|
||||
importer.validate_xml_file('file.name', 'xbible')
|
||||
assert context.exception.msg == 'Error when opening file'
|
||||
assert context.value != ValidationError('Error when opening file')
|
||||
|
||||
@patch.object(BibleImport, 'parse_xml', return_value=objectify.fromstring('<bible></bible>'))
|
||||
@patch.object(BibleImport, 'is_compressed', return_value=False)
|
||||
def test_validate_xml_file_success(self, mocked_is_compressed, mocked_parse_xml):
|
||||
|
||||
@patch.object(BibleImport, 'parse_xml', return_value=objectify.fromstring('<bible></bible>'))
|
||||
@patch.object(BibleImport, 'is_compressed', return_value=False)
|
||||
def test_validate_xml_file_success(mocked_is_compressed, mocked_parse_xml, settings):
|
||||
"""
|
||||
Test that validate_xml_file returns True with valid XML
|
||||
"""
|
||||
@ -539,9 +570,10 @@ class TestBibleImport(TestCase):
|
||||
# THEN: True should be returned
|
||||
assert result is True
|
||||
|
||||
@patch.object(BibleImport, 'parse_xml', return_value=objectify.fromstring('<bible></bible>'))
|
||||
@patch.object(BibleImport, 'is_compressed', return_value=False)
|
||||
def test_validate_xml_file_opensong_root(self, mocked_is_compressed, mocked_parse_xml):
|
||||
|
||||
@patch.object(BibleImport, 'parse_xml', return_value=objectify.fromstring('<bible></bible>'))
|
||||
@patch.object(BibleImport, 'is_compressed', return_value=False)
|
||||
def test_validate_xml_file_opensong_root(mocked_is_compressed, mocked_parse_xml, error_message_box):
|
||||
"""
|
||||
Test that validate_xml_file raises a ValidationError with an OpenSong root tag
|
||||
"""
|
||||
@ -551,15 +583,16 @@ class TestBibleImport(TestCase):
|
||||
# WHEN: Calling validate_xml_file
|
||||
# THEN: ValidationError should be raised, and the critical error message box should was called informing
|
||||
# the user that an OpenSong bible was found
|
||||
with self.assertRaises(ValidationError) as context:
|
||||
with pytest.raises(ValidationError) as context:
|
||||
importer.validate_xml_file('file.name', 'xbible')
|
||||
assert context.exception.msg == 'Invalid xml.'
|
||||
self.mocked_critical_error_message_box.assert_called_once_with(
|
||||
assert context.value != ValidationError('Invalid xml.')
|
||||
error_message_box.assert_called_once_with(
|
||||
message='Incorrect Bible file type supplied. This looks like an OpenSong XML bible.')
|
||||
|
||||
@patch.object(BibleImport, 'parse_xml')
|
||||
@patch.object(BibleImport, 'is_compressed', return_value=False)
|
||||
def test_validate_xml_file_osis_root(self, mocked_is_compressed, mocked_parse_xml):
|
||||
|
||||
@patch.object(BibleImport, 'parse_xml')
|
||||
@patch.object(BibleImport, 'is_compressed', return_value=False)
|
||||
def test_validate_xml_file_osis_root(mocked_is_compressed, mocked_parse_xml, error_message_box):
|
||||
"""
|
||||
Test that validate_xml_file raises a ValidationError with an OSIS root tag
|
||||
"""
|
||||
@ -571,15 +604,16 @@ class TestBibleImport(TestCase):
|
||||
# WHEN: Calling validate_xml_file
|
||||
# THEN: ValidationError should be raised, and the critical error message box should was called informing
|
||||
# the user that an OSIS bible was found
|
||||
with self.assertRaises(ValidationError) as context:
|
||||
with pytest.raises(ValidationError) as context:
|
||||
importer.validate_xml_file('file.name', 'xbible')
|
||||
assert context.exception.msg == 'Invalid xml.'
|
||||
self.mocked_critical_error_message_box.assert_called_once_with(
|
||||
assert context.value != ValidationError('Invalid xml.')
|
||||
error_message_box.assert_called_once_with(
|
||||
message='Incorrect Bible file type supplied. This looks like an OSIS XML bible.')
|
||||
|
||||
@patch.object(BibleImport, 'parse_xml', return_value=objectify.fromstring('<xmlbible></xmlbible>'))
|
||||
@patch.object(BibleImport, 'is_compressed', return_value=False)
|
||||
def test_validate_xml_file_zefania_root(self, mocked_is_compressed, mocked_parse_xml):
|
||||
|
||||
@patch.object(BibleImport, 'parse_xml', return_value=objectify.fromstring('<xmlbible></xmlbible>'))
|
||||
@patch.object(BibleImport, 'is_compressed', return_value=False)
|
||||
def test_validate_xml_file_zefania_root(mocked_is_compressed, mocked_parse_xml, error_message_box):
|
||||
"""
|
||||
Test that validate_xml_file raises a ValidationError with an Zefania root tag
|
||||
"""
|
||||
@ -589,26 +623,26 @@ class TestBibleImport(TestCase):
|
||||
# WHEN: Calling validate_xml_file
|
||||
# THEN: ValidationError should be raised, and the critical error message box should was called informing
|
||||
# the user that an Zefania bible was found
|
||||
with self.assertRaises(ValidationError) as context:
|
||||
with pytest.raises(ValidationError) as context:
|
||||
importer.validate_xml_file('file.name', 'xbible')
|
||||
assert context.exception.msg == 'Invalid xml.'
|
||||
self.mocked_critical_error_message_box.assert_called_once_with(
|
||||
assert context.value != ValidationError('Invalid xml.')
|
||||
error_message_box.assert_called_once_with(
|
||||
message='Incorrect Bible file type supplied. This looks like an Zefania XML bible.')
|
||||
|
||||
@patch.object(BibleImport, 'parse_xml', return_value=objectify.fromstring('<unknownbible></unknownbible>'))
|
||||
@patch.object(BibleImport, 'is_compressed', return_value=False)
|
||||
def test_validate_xml_file_unknown_root(self, mocked_is_compressed, mocked_parse_xml):
|
||||
|
||||
@patch.object(BibleImport, 'parse_xml', return_value=objectify.fromstring('<unknownbible></unknownbible>'))
|
||||
@patch.object(BibleImport, 'is_compressed', return_value=False)
|
||||
def test_validate_xml_file_unknown_root(mocked_is_compressed, mocked_parse_xml, error_message_box):
|
||||
"""
|
||||
Test that validate_xml_file raises a ValidationError with an unknown root tag
|
||||
"""
|
||||
# GIVEN: Some test data with an unknown root tag and an instance of BibleImport
|
||||
importer = BibleImport(MagicMock(), path='.', name='.', file_path=None)
|
||||
|
||||
# WHEN: Calling validate_xml_file
|
||||
# THEN: ValidationError should be raised, and the critical error message box should was called informing
|
||||
# the user that a unknown xml bible was found
|
||||
with self.assertRaises(ValidationError) as context:
|
||||
with pytest.raises(ValidationError) as context:
|
||||
importer.validate_xml_file('file.name', 'xbible')
|
||||
assert context.exception.msg == 'Invalid xml.'
|
||||
self.mocked_critical_error_message_box.assert_called_once_with(
|
||||
assert context.value != ValidationError('Invalid xml.')
|
||||
error_message_box.assert_called_once_with(
|
||||
message='Incorrect Bible file type supplied. This looks like an unknown type of XML bible.')
|
||||
|
@ -22,9 +22,9 @@
|
||||
This module contains tests for the CSV Bible importer.
|
||||
"""
|
||||
import csv
|
||||
import pytest
|
||||
from collections import namedtuple
|
||||
from pathlib import Path
|
||||
from unittest import TestCase
|
||||
from unittest.mock import MagicMock, PropertyMock, call, patch
|
||||
|
||||
from openlp.core.lib.exceptions import ValidationError
|
||||
@ -37,20 +37,7 @@ from tests.utils.constants import RESOURCE_PATH
|
||||
TEST_PATH = RESOURCE_PATH / 'bibles'
|
||||
|
||||
|
||||
class TestCSVImport(TestCase):
|
||||
"""
|
||||
Test the functions in the :mod:`csvimport` module.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.manager_patcher = patch('openlp.plugins.bibles.lib.db.Manager')
|
||||
self.addCleanup(self.manager_patcher.stop)
|
||||
self.manager_patcher.start()
|
||||
self.registry_patcher = patch('openlp.plugins.bibles.lib.bibleimport.Registry')
|
||||
self.addCleanup(self.registry_patcher.stop)
|
||||
self.registry_patcher.start()
|
||||
|
||||
def test_create_importer(self):
|
||||
def test_create_importer(settings):
|
||||
"""
|
||||
Test creating an instance of the CSV file importer
|
||||
"""
|
||||
@ -66,7 +53,8 @@ class TestCSVImport(TestCase):
|
||||
assert importer.books_path == Path('books.csv')
|
||||
assert importer.verses_path == Path('verse.csv')
|
||||
|
||||
def test_book_namedtuple(self):
|
||||
|
||||
def test_book_namedtuple():
|
||||
"""
|
||||
Test that the Book namedtuple is created as expected
|
||||
"""
|
||||
@ -80,7 +68,8 @@ class TestCSVImport(TestCase):
|
||||
assert result.name == 'name'
|
||||
assert result.abbreviation == 'abbreviation'
|
||||
|
||||
def test_verse_namedtuple(self):
|
||||
|
||||
def test_verse_namedtuple():
|
||||
"""
|
||||
Test that the Verse namedtuple is created as expected
|
||||
"""
|
||||
@ -94,7 +83,8 @@ class TestCSVImport(TestCase):
|
||||
assert result.number == 'number'
|
||||
assert result.text == 'text'
|
||||
|
||||
def test_get_book_name_id(self):
|
||||
|
||||
def test_get_book_name_id():
|
||||
"""
|
||||
Test that get_book_name() returns the correct book when called with an id
|
||||
"""
|
||||
@ -109,7 +99,8 @@ class TestCSVImport(TestCase):
|
||||
# THEN: get_book_name() should return the book name associated with that id from the books dictionary
|
||||
assert actual_result == expected_result
|
||||
|
||||
def test_get_book_name(self):
|
||||
|
||||
def test_get_book_name():
|
||||
"""
|
||||
Test that get_book_name() returns the name when called with a non integer value
|
||||
"""
|
||||
@ -124,7 +115,8 @@ class TestCSVImport(TestCase):
|
||||
# THEN: get_book_name() should return the input
|
||||
assert actual_result == expected_result
|
||||
|
||||
def test_parse_csv_file(self):
|
||||
|
||||
def test_parse_csv_file():
|
||||
"""
|
||||
Test the parse_csv_file() with sample data
|
||||
"""
|
||||
@ -148,7 +140,8 @@ class TestCSVImport(TestCase):
|
||||
mocked_csv_file.open.assert_called_once_with('r', encoding='utf-8', newline='')
|
||||
mocked_reader.assert_called_once_with(mocked_enter_file, delimiter=',', quotechar='"')
|
||||
|
||||
def test_parse_csv_file_oserror(self):
|
||||
|
||||
def test_parse_csv_file_oserror():
|
||||
"""
|
||||
Test the parse_csv_file() handles an OSError correctly
|
||||
"""
|
||||
@ -162,11 +155,12 @@ class TestCSVImport(TestCase):
|
||||
|
||||
# WHEN: Calling CSVBible.parse_csv_file
|
||||
# THEN: A ValidationError should be raised
|
||||
with self.assertRaises(ValidationError) as context:
|
||||
with pytest.raises(ValidationError) as context:
|
||||
CSVBible.parse_csv_file(mocked_csv_file, None)
|
||||
assert context.exception.msg == 'Parsing "file.csv" failed'
|
||||
assert context.value != ValidationError('Parsing "file.csv" failed')
|
||||
|
||||
def test_parse_csv_file_csverror(self):
|
||||
|
||||
def test_parse_csv_file_csverror():
|
||||
"""
|
||||
Test the parse_csv_file() handles an csv.Error correctly
|
||||
"""
|
||||
@ -180,11 +174,12 @@ class TestCSVImport(TestCase):
|
||||
|
||||
# WHEN: Calling CSVBible.parse_csv_file
|
||||
# THEN: A ValidationError should be raised
|
||||
with self.assertRaises(ValidationError) as context:
|
||||
with pytest.raises(ValidationError) as context:
|
||||
CSVBible.parse_csv_file(mocked_csv_file, None)
|
||||
assert context.exception.msg == 'Parsing "file.csv" failed'
|
||||
assert context.value != ValidationError('Parsing "file.csv" failed')
|
||||
|
||||
def test_process_books_stopped_import(self):
|
||||
|
||||
def test_process_books_stopped_import(registry):
|
||||
"""
|
||||
Test process books when the import is stopped
|
||||
"""
|
||||
@ -204,7 +199,8 @@ class TestCSVImport(TestCase):
|
||||
assert importer.wizard.increment_progress_bar.called is False
|
||||
assert result == {}
|
||||
|
||||
def test_process_books(self):
|
||||
|
||||
def test_process_books(registry):
|
||||
"""
|
||||
Test process books when it completes successfully
|
||||
"""
|
||||
@ -230,7 +226,8 @@ class TestCSVImport(TestCase):
|
||||
[call('1. Mosebog', 2, 10), call('2. Mosebog', 2, 10)]
|
||||
assert result == {1: '1. Mosebog', 2: '2. Mosebog'}
|
||||
|
||||
def test_process_verses_stopped_import(self):
|
||||
|
||||
def test_process_verses_stopped_import(registry):
|
||||
"""
|
||||
Test process_verses when the import is stopped
|
||||
"""
|
||||
@ -251,7 +248,8 @@ class TestCSVImport(TestCase):
|
||||
assert importer.get_book_name.called is False
|
||||
assert result is None
|
||||
|
||||
def test_process_verses_successful(self):
|
||||
|
||||
def test_process_verses_successful(registry):
|
||||
"""
|
||||
Test process_verses when the import is successful
|
||||
"""
|
||||
@ -284,7 +282,8 @@ class TestCSVImport(TestCase):
|
||||
call('1', 1, 2, 'Og Jorden var øde og tom, og der var Mørke over Verdensdybet. '
|
||||
'Men Guds Ånd svævede over Vandene.')]
|
||||
|
||||
def test_do_import_invalid_language_id(self):
|
||||
|
||||
def test_do_import_invalid_language_id(registry):
|
||||
"""
|
||||
Test do_import when the user cancels the language selection dialog box
|
||||
"""
|
||||
@ -302,7 +301,8 @@ class TestCSVImport(TestCase):
|
||||
importer.get_language.assert_called_once_with('Bible Name')
|
||||
assert result is False
|
||||
|
||||
def test_do_import_success(self):
|
||||
|
||||
def test_do_import_success(registry):
|
||||
"""
|
||||
Test do_import when the import succeeds
|
||||
"""
|
||||
@ -330,7 +330,8 @@ class TestCSVImport(TestCase):
|
||||
importer.process_verses.assert_called_once_with(['Verse 1'], ['Book 1'])
|
||||
assert result is True
|
||||
|
||||
def test_file_import(self):
|
||||
|
||||
def test_file_import(settings):
|
||||
"""
|
||||
Test the actual import of CSV Bible file
|
||||
"""
|
||||
|
@ -21,12 +21,3 @@
|
||||
"""
|
||||
This module contains tests for the db submodule of the Bibles plugin.
|
||||
"""
|
||||
|
||||
from unittest import TestCase
|
||||
|
||||
|
||||
class TestBibleDB(TestCase):
|
||||
"""
|
||||
Test the functions in the BibleDB class.
|
||||
"""
|
||||
pass
|
||||
|
@ -22,26 +22,12 @@
|
||||
This module contains tests for the manager submodule of the Bibles plugin.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from unittest import TestCase
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from openlp.plugins.bibles.lib.manager import BibleManager
|
||||
|
||||
|
||||
class TestManager(TestCase):
|
||||
"""
|
||||
Test the functions in the :mod:`manager` module.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
app_location_patcher = patch('openlp.plugins.bibles.lib.manager.AppLocation')
|
||||
self.addCleanup(app_location_patcher.stop)
|
||||
app_location_patcher.start()
|
||||
log_patcher = patch('openlp.plugins.bibles.lib.manager.log')
|
||||
self.addCleanup(log_patcher.stop)
|
||||
self.mocked_log = log_patcher.start()
|
||||
|
||||
def test_delete_bible(self):
|
||||
def test_delete_bible(settings):
|
||||
"""
|
||||
Test the BibleManager delete_bible method
|
||||
"""
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -21,15 +21,13 @@
|
||||
"""
|
||||
This module contains tests for the OpenSong Bible importer.
|
||||
"""
|
||||
from unittest import TestCase
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, call, patch
|
||||
|
||||
from lxml import objectify
|
||||
|
||||
from openlp.core.common.registry import Registry
|
||||
from openlp.plugins.bibles.lib.bibleimport import BibleImport
|
||||
from openlp.plugins.bibles.lib.importers.opensong import OpenSongBible, get_text, parse_chapter_number
|
||||
from tests.helpers.testmixin import TestMixin
|
||||
from tests.utils import load_external_result_data
|
||||
from tests.utils.constants import RESOURCE_PATH
|
||||
|
||||
@ -37,24 +35,21 @@ from tests.utils.constants import RESOURCE_PATH
|
||||
TEST_PATH = RESOURCE_PATH / 'bibles'
|
||||
|
||||
|
||||
class TestOpenSongImport(TestCase, TestMixin):
|
||||
"""
|
||||
Test the functions in the :mod:`opensongimport` module.
|
||||
"""
|
||||
@pytest.yield_fixture
|
||||
def manager():
|
||||
db_man = patch('openlp.plugins.bibles.lib.db.Manager')
|
||||
yield db_man.start()
|
||||
db_man.stop()
|
||||
|
||||
def setUp(self):
|
||||
self.find_and_create_book_patch = patch.object(BibleImport, 'find_and_create_book')
|
||||
self.addCleanup(self.find_and_create_book_patch.stop)
|
||||
self.mocked_find_and_create_book = self.find_and_create_book_patch.start()
|
||||
self.manager_patcher = patch('openlp.plugins.bibles.lib.db.Manager')
|
||||
self.addCleanup(self.manager_patcher.stop)
|
||||
self.manager_patcher.start()
|
||||
self.setup_application()
|
||||
self.app.process_events = MagicMock()
|
||||
Registry.create()
|
||||
Registry().register('application', self.app)
|
||||
|
||||
def test_create_importer(self):
|
||||
@pytest.yield_fixture()
|
||||
def mocked_find_and_create_book():
|
||||
facb = patch.object(BibleImport, 'find_and_create_book')
|
||||
yield facb.start()
|
||||
facb.stop()
|
||||
|
||||
|
||||
def test_create_importer(manager, mock_settings):
|
||||
"""
|
||||
Test creating an instance of the OpenSong file importer
|
||||
"""
|
||||
@ -67,7 +62,8 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
# THEN: The importer should be an instance of BibleDB
|
||||
assert isinstance(importer, BibleImport)
|
||||
|
||||
def test_get_text_no_text(self):
|
||||
|
||||
def test_get_text_no_text(manager, mock_settings):
|
||||
"""
|
||||
Test that get_text handles elements containing text in a combination of text and tail attributes
|
||||
"""
|
||||
@ -80,7 +76,8 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
# THEN: A blank string should be returned
|
||||
assert result == ''
|
||||
|
||||
def test_get_text_text(self):
|
||||
|
||||
def test_get_text_text(manager, mock_settings):
|
||||
"""
|
||||
Test that get_text handles elements containing text in a combination of text and tail attributes
|
||||
"""
|
||||
@ -97,7 +94,8 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
# THEN: The text returned should be as expected
|
||||
assert result == 'Element text sub_text_tail text sub_text_tail tail sub_text text sub_tail tail'
|
||||
|
||||
def test_parse_chapter_number(self):
|
||||
|
||||
def test_parse_chapter_number(manager, mock_settings):
|
||||
"""
|
||||
Test parse_chapter_number when supplied with chapter number and an instance of OpenSongBible
|
||||
"""
|
||||
@ -108,7 +106,8 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
# THEN: The 10 should be returned as an Int
|
||||
assert result == 10
|
||||
|
||||
def test_parse_chapter_number_empty_attribute(self):
|
||||
|
||||
def test_parse_chapter_number_empty_attribute(manager, mock_settings):
|
||||
"""
|
||||
Testparse_chapter_number when the chapter number is an empty string. (Bug #1074727)
|
||||
"""
|
||||
@ -119,7 +118,8 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
# THEN: parse_chapter_number should increment the previous verse number
|
||||
assert result == 13
|
||||
|
||||
def test_parse_verse_number_valid_verse_no(self):
|
||||
|
||||
def test_parse_verse_number_valid_verse_no(manager, mock_settings):
|
||||
"""
|
||||
Test parse_verse_number when supplied with a valid verse number
|
||||
"""
|
||||
@ -132,7 +132,8 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
# THEN: parse_verse_number should return the verse number
|
||||
assert result == 15
|
||||
|
||||
def test_parse_verse_number_verse_range(self):
|
||||
|
||||
def test_parse_verse_number_verse_range(manager, mock_settings):
|
||||
"""
|
||||
Test parse_verse_number when supplied with a verse range
|
||||
"""
|
||||
@ -145,7 +146,8 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
# THEN: parse_verse_number should return the first verse number in the range
|
||||
assert result == 24
|
||||
|
||||
def test_parse_verse_number_invalid_verse_no(self):
|
||||
|
||||
def test_parse_verse_number_invalid_verse_no(manager, mock_settings):
|
||||
"""
|
||||
Test parse_verse_number when supplied with a invalid verse number
|
||||
"""
|
||||
@ -158,7 +160,8 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
# THEN: parse_verse_number should increment the previous verse number
|
||||
assert result == 42
|
||||
|
||||
def test_parse_verse_number_empty_attribute(self):
|
||||
|
||||
def test_parse_verse_number_empty_attribute(manager, mock_settings):
|
||||
"""
|
||||
Test parse_verse_number when the verse number is an empty string. (Bug #1074727)
|
||||
"""
|
||||
@ -170,12 +173,13 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
# THEN: parse_verse_number should increment the previous verse number
|
||||
assert result == 15
|
||||
|
||||
def test_parse_verse_number_invalid_type(self):
|
||||
|
||||
def test_parse_verse_number_invalid_type(manager, mock_settings):
|
||||
"""
|
||||
Test parse_verse_number when the verse number is an invalid type)
|
||||
"""
|
||||
with patch.object(OpenSongBible, 'log_warning')as mocked_log_warning:
|
||||
# GIVEN: An instanceofOpenSongBible, a Tuple, and the previous verse number set as 12
|
||||
# GIVEN: An instance of OpenSongBible, a Tuple, and the previous verse number set as 12
|
||||
importer = OpenSongBible(MagicMock(), path='.', name='.', file_path=None)
|
||||
|
||||
# WHEN: Calling parse_verse_number
|
||||
@ -186,7 +190,8 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
mocked_log_warning.assert_called_once_with('Illegal verse number: (1, 2, 3)')
|
||||
assert result == 13
|
||||
|
||||
def test_process_books_stop_import(self):
|
||||
|
||||
def test_process_books_stop_import(manager, mocked_find_and_create_book, mock_settings):
|
||||
"""
|
||||
Test process_books when stop_import is set to True
|
||||
"""
|
||||
@ -198,14 +203,15 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
importer.process_books(['Book'])
|
||||
|
||||
# THEN: find_and_create_book should not have been called
|
||||
assert self.mocked_find_and_create_book.called is False
|
||||
assert mocked_find_and_create_book.called is False
|
||||
|
||||
def test_process_books_completes(self):
|
||||
|
||||
def test_process_books_completes(manager, mocked_find_and_create_book, mock_settings):
|
||||
"""
|
||||
Test process_books when it processes all books
|
||||
"""
|
||||
# GIVEN: An instance of OpenSongBible Importer and two mocked books
|
||||
self.mocked_find_and_create_book.side_effect = ['db_book1', 'db_book2']
|
||||
mocked_find_and_create_book.side_effect = ['db_book1', 'db_book2']
|
||||
with patch.object(OpenSongBible, 'process_chapters') as mocked_process_chapters:
|
||||
importer = OpenSongBible(MagicMock(), path='.', name='.', file_path=None)
|
||||
|
||||
@ -223,12 +229,13 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
importer.process_books([book1, book2])
|
||||
|
||||
# THEN: find_and_create_book and process_books should be called with the details from the mocked books
|
||||
assert self.mocked_find_and_create_book.call_args_list == [call('Name1', 2, 10), call('Name2', 2, 10)]
|
||||
assert mocked_find_and_create_book.call_args_list == [call('Name1', 2, 10), call('Name2', 2, 10)]
|
||||
assert mocked_process_chapters.call_args_list == \
|
||||
[call('db_book1', 'Chapter1'), call('db_book2', 'Chapter2')]
|
||||
assert importer.session.commit.call_count == 2
|
||||
|
||||
def test_process_chapters_stop_import(self):
|
||||
|
||||
def test_process_chapters_stop_import(manager, mock_settings):
|
||||
"""
|
||||
Test process_chapters when stop_import is set to True
|
||||
"""
|
||||
@ -243,8 +250,9 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
# THEN: importer.parse_chapter_number not have been called
|
||||
assert importer.parse_chapter_number.called is False
|
||||
|
||||
@patch('openlp.plugins.bibles.lib.importers.opensong.parse_chapter_number', **{'side_effect': [1, 2]})
|
||||
def test_process_chapters_completes(self, mocked_parse_chapter_number):
|
||||
|
||||
@patch('openlp.plugins.bibles.lib.importers.opensong.parse_chapter_number', **{'side_effect': [1, 2]})
|
||||
def test_process_chapters_completes(mocked_parse_chapter_number, manager, mock_settings):
|
||||
"""
|
||||
Test process_chapters when it completes
|
||||
"""
|
||||
@ -275,7 +283,8 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
assert importer.wizard.increment_progress_bar.call_args_list == [call('Importing Book 1...'),
|
||||
call('Importing Book 2...')]
|
||||
|
||||
def test_process_verses_stop_import(self):
|
||||
|
||||
def test_process_verses_stop_import(manager, mock_settings):
|
||||
"""
|
||||
Test process_verses when stop_import is set to True
|
||||
"""
|
||||
@ -290,7 +299,8 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
# THEN: importer.parse_verse_number not have been called
|
||||
assert importer.parse_verse_number.called is False
|
||||
|
||||
def test_process_verses_completes(self):
|
||||
|
||||
def test_process_verses_completes(manager, mock_settings):
|
||||
"""
|
||||
Test process_verses when it completes
|
||||
"""
|
||||
@ -324,7 +334,8 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
assert importer.create_verse.call_args_list == \
|
||||
[call(1, 1, 1, 'Verse1 Text'), call(1, 1, 2, 'Verse2 Text')]
|
||||
|
||||
def test_do_import_parse_xml_fails(self):
|
||||
|
||||
def test_do_import_parse_xml_fails(manager, mock_settings):
|
||||
"""
|
||||
Test do_import when parse_xml fails (returns None)
|
||||
"""
|
||||
@ -342,7 +353,8 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
assert result is False
|
||||
assert mocked_language_id.called is False
|
||||
|
||||
def test_do_import_no_language(self):
|
||||
|
||||
def test_do_import_no_language(manager, mock_settings):
|
||||
"""
|
||||
Test do_import when the user cancels the language selection dialog
|
||||
"""
|
||||
@ -361,7 +373,8 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
assert result is False
|
||||
assert mocked_process_books.called is False
|
||||
|
||||
def test_do_import_completes(self):
|
||||
|
||||
def test_do_import_completes(manager, mock_settings):
|
||||
"""
|
||||
Test do_import when it completes successfully
|
||||
"""
|
||||
@ -380,16 +393,7 @@ class TestOpenSongImport(TestCase, TestMixin):
|
||||
assert result is True
|
||||
|
||||
|
||||
class TestOpenSongImportFileImports(TestCase, TestMixin):
|
||||
"""
|
||||
Test the functions in the :mod:`opensongimport` module.
|
||||
"""
|
||||
def setUp(self):
|
||||
self.manager_patcher = patch('openlp.plugins.bibles.lib.db.Manager')
|
||||
self.addCleanup(self.manager_patcher.stop)
|
||||
self.manager_patcher.start()
|
||||
|
||||
def test_file_import(self):
|
||||
def test_file_import(manager, mock_settings):
|
||||
"""
|
||||
Test the actual import of OpenSong Bible file
|
||||
"""
|
||||
|
@ -21,7 +21,7 @@
|
||||
"""
|
||||
This module contains tests for the SWORD Bible importer.
|
||||
"""
|
||||
from unittest import TestCase, skipUnless
|
||||
from unittest import skipUnless
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from openlp.plugins.bibles.lib.db import BibleDB
|
||||
@ -40,22 +40,7 @@ TEST_PATH = RESOURCE_PATH / 'bibles'
|
||||
|
||||
|
||||
@skipUnless(HAS_PYSWORD, 'pysword not installed')
|
||||
class TestSwordImport(TestCase):
|
||||
"""
|
||||
Test the functions in the :mod:`swordimport` module.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.registry_patcher = patch('openlp.plugins.bibles.lib.bibleimport.Registry')
|
||||
self.registry_patcher.start()
|
||||
self.manager_patcher = patch('openlp.plugins.bibles.lib.db.Manager')
|
||||
self.manager_patcher.start()
|
||||
|
||||
def tearDown(self):
|
||||
self.registry_patcher.stop()
|
||||
self.manager_patcher.stop()
|
||||
|
||||
def test_create_importer(self):
|
||||
def test_create_importer(settings):
|
||||
"""
|
||||
Test creating an instance of the Sword file importer
|
||||
"""
|
||||
@ -68,9 +53,10 @@ class TestSwordImport(TestCase):
|
||||
# THEN: The importer should be an instance of BibleDB
|
||||
assert isinstance(importer, BibleDB)
|
||||
|
||||
@patch('openlp.plugins.bibles.lib.importers.sword.SwordBible.application')
|
||||
@patch('openlp.plugins.bibles.lib.importers.sword.modules')
|
||||
def test_simple_import(self, mocked_pysword_modules, mocked_application):
|
||||
|
||||
@skipUnless(HAS_PYSWORD, 'pysword not installed')
|
||||
@patch('openlp.plugins.bibles.lib.importers.sword.modules')
|
||||
def test_simple_import(mocked_pysword_modules, settings):
|
||||
"""
|
||||
Test that a simple SWORD import works
|
||||
"""
|
||||
|
@ -21,178 +21,138 @@
|
||||
"""
|
||||
This module contains tests for the upgrade submodule of the Bibles plugin.
|
||||
"""
|
||||
import pytest
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from tempfile import mkdtemp
|
||||
from unittest import TestCase
|
||||
from unittest.mock import MagicMock, call, patch
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
from openlp.core.common.registry import Registry
|
||||
from openlp.core.common.settings import ProxyMode
|
||||
from openlp.core.lib.db import upgrade_db
|
||||
from openlp.plugins.bibles.lib import upgrade
|
||||
from tests.helpers.testmixin import TestMixin
|
||||
from tests.utils.constants import RESOURCE_PATH
|
||||
|
||||
|
||||
class TestUpgrade(TestCase, TestMixin):
|
||||
"""
|
||||
Test the `upgrade_2` function in the :mod:`upgrade` module when the db does not contains proxy metadata
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
"""
|
||||
Setup for tests
|
||||
"""
|
||||
self.tmp_path = Path(mkdtemp())
|
||||
db_path = RESOURCE_PATH / 'bibles' / 'web-bible-2.4.6-v1.sqlite'
|
||||
db_tmp_path = self.tmp_path / 'web-bible-2.4.6-v1.sqlite'
|
||||
shutil.copyfile(db_path, db_tmp_path)
|
||||
self.db_url = 'sqlite:///' + str(db_tmp_path)
|
||||
|
||||
self.build_settings()
|
||||
Registry().create()
|
||||
Registry().register('service_list', MagicMock())
|
||||
|
||||
@pytest.yield_fixture()
|
||||
def mock_message_box():
|
||||
patched_message_box = patch('openlp.plugins.bibles.lib.upgrade.QtWidgets.QMessageBox')
|
||||
self.mocked_message_box = patched_message_box.start()
|
||||
self.addCleanup(patched_message_box.stop)
|
||||
mocked_message_box = patched_message_box.start()
|
||||
mocked_no_button = MagicMock()
|
||||
mocked_http_button = MagicMock()
|
||||
mocked_both_button = MagicMock()
|
||||
mocked_https_button = MagicMock()
|
||||
mocked_message_box_inst = MagicMock(
|
||||
**{'addButton.side_effect': [mocked_no_button, mocked_http_button,
|
||||
mocked_both_button, mocked_https_button]})
|
||||
mocked_message_box.return_value = mocked_message_box_inst
|
||||
yield mocked_message_box_inst, mocked_both_button, mocked_https_button, mocked_http_button, mocked_no_button
|
||||
patched_message_box.stop()
|
||||
|
||||
def tearDown(self):
|
||||
"""
|
||||
Clean up after tests
|
||||
"""
|
||||
# Ignore errors since windows can have problems with locked files
|
||||
shutil.rmtree(self.tmp_path, ignore_errors=True)
|
||||
self.destroy_settings()
|
||||
|
||||
def test_upgrade_2_none_selected(self):
|
||||
@pytest.yield_fixture()
|
||||
def db_url():
|
||||
tmp_path = Path(mkdtemp())
|
||||
db_path = RESOURCE_PATH / 'bibles' / 'web-bible-2.4.6-proxy-meta-v1.sqlite'
|
||||
db_tmp_path = tmp_path / 'web-bible-2.4.6-proxy-meta-v1.sqlite'
|
||||
shutil.copyfile(db_path, db_tmp_path)
|
||||
yield 'sqlite:///' + str(db_tmp_path)
|
||||
shutil.rmtree(tmp_path, ignore_errors=True)
|
||||
|
||||
|
||||
def test_upgrade_2_basic(mock_message_box, db_url, mock_settings):
|
||||
"""
|
||||
Test that upgrade 2 completes properly when the user chooses not to use a proxy ('No')
|
||||
"""
|
||||
# GIVEN: An version 1 web bible with proxy settings
|
||||
mocked_message_box = mock_message_box[0]
|
||||
|
||||
# WHEN: Calling upgrade_db and the user has 'clicked' the 'No' button
|
||||
upgrade_db(self.db_url, upgrade)
|
||||
upgrade_db(db_url, upgrade)
|
||||
|
||||
# THEN: The proxy meta data should have been removed, and the version should have been changed to version 2
|
||||
self.mocked_message_box.assert_not_called()
|
||||
engine = create_engine(self.db_url)
|
||||
mocked_message_box.assert_not_called()
|
||||
engine = create_engine(db_url)
|
||||
conn = engine.connect()
|
||||
assert conn.execute('SELECT * FROM metadata WHERE key = "version"').first().value == '2'
|
||||
|
||||
|
||||
class TestProxyMetaUpgrade(TestCase, TestMixin):
|
||||
"""
|
||||
Test the `upgrade_2` function in the :mod:`upgrade` module when the db contains proxy metadata
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
"""
|
||||
Setup for tests
|
||||
"""
|
||||
self.tmp_path = Path(mkdtemp())
|
||||
db_path = RESOURCE_PATH / 'bibles' / 'web-bible-2.4.6-proxy-meta-v1.sqlite'
|
||||
db_tmp_path = self.tmp_path / 'web-bible-2.4.6-proxy-meta-v1.sqlite'
|
||||
shutil.copyfile(db_path, db_tmp_path)
|
||||
self.db_url = 'sqlite:///' + str(db_tmp_path)
|
||||
|
||||
self.mocked_settings_instance = MagicMock()
|
||||
self.mocked_settings = MagicMock()
|
||||
self.mocked_settings.return_value = self.mocked_settings_instance
|
||||
|
||||
self.build_settings()
|
||||
Registry().create()
|
||||
Registry().register('service_list', MagicMock())
|
||||
Registry().register('settings', self.mocked_settings)
|
||||
|
||||
patched_message_box = patch('openlp.plugins.bibles.lib.upgrade.QtWidgets.QMessageBox')
|
||||
mocked_message_box = patched_message_box.start()
|
||||
self.addCleanup(patched_message_box.stop)
|
||||
self.mocked_no_button = MagicMock()
|
||||
self.mocked_http_button = MagicMock()
|
||||
self.mocked_both_button = MagicMock()
|
||||
self.mocked_https_button = MagicMock()
|
||||
self.mocked_message_box_instance = MagicMock(
|
||||
**{'addButton.side_effect': [self.mocked_no_button, self.mocked_http_button,
|
||||
self.mocked_both_button, self.mocked_https_button]})
|
||||
mocked_message_box.return_value = self.mocked_message_box_instance
|
||||
|
||||
def tearDown(self):
|
||||
"""
|
||||
Clean up after tests
|
||||
"""
|
||||
# Ignore errors since windows can have problems with locked files
|
||||
shutil.rmtree(self.tmp_path, ignore_errors=True)
|
||||
|
||||
def test_upgrade_2_none_selected(self):
|
||||
def test_upgrade_2_none_selected(mock_message_box, db_url, mock_settings):
|
||||
"""
|
||||
Test that upgrade 2 completes properly when the user chooses not to use a proxy ('No')
|
||||
"""
|
||||
# GIVEN: An version 1 web bible with proxy settings
|
||||
|
||||
# WHEN: Calling upgrade_db and the user has 'clicked' the 'No' button
|
||||
self.mocked_message_box_instance.clickedButton.return_value = self.mocked_no_button
|
||||
upgrade_db(self.db_url, upgrade)
|
||||
mocked_message_box_instance = mock_message_box[0]
|
||||
mocked_no_button = mock_message_box[4]
|
||||
mocked_message_box_instance.clickedButton.return_value = mocked_no_button
|
||||
upgrade_db(db_url, upgrade)
|
||||
|
||||
# THEN: The proxy meta data should have been removed, and the version should have been changed to version 2
|
||||
engine = create_engine(self.db_url)
|
||||
engine = create_engine(db_url)
|
||||
conn = engine.connect()
|
||||
assert len(conn.execute('SELECT * FROM metadata WHERE key = "proxy_server"').fetchall()) == 0
|
||||
assert len(conn.execute('SELECT * FROM metadata WHERE key = "proxy_username"').fetchall()) == 0
|
||||
assert len(conn.execute('SELECT * FROM metadata WHERE key = "proxy_password"').fetchall()) == 0
|
||||
assert conn.execute('SELECT * FROM metadata WHERE key = "version"').first().value == '2'
|
||||
self.mocked_settings_instance.setValue.assert_not_called()
|
||||
mock_settings.setValue.assert_not_called()
|
||||
|
||||
def test_upgrade_2_http_selected(self):
|
||||
|
||||
def test_upgrade_2_http_selected(mock_message_box, db_url, mock_settings):
|
||||
"""
|
||||
Test that upgrade 2 completes properly when the user chooses to use a HTTP proxy
|
||||
"""
|
||||
# GIVEN: An version 1 web bible with proxy settings
|
||||
|
||||
# WHEN: Calling upgrade_db and the user has 'clicked' the 'HTTP' button
|
||||
self.mocked_message_box_instance.clickedButton.return_value = self.mocked_http_button
|
||||
upgrade_db(self.db_url, upgrade)
|
||||
mocked_message_box_instance = mock_message_box[0]
|
||||
mocked_http_button = mock_message_box[3]
|
||||
mocked_message_box_instance.clickedButton.return_value = mocked_http_button
|
||||
upgrade_db(db_url, upgrade)
|
||||
|
||||
# THEN: The proxy meta data should have been removed, the version should have been changed to version 2, and the
|
||||
# proxy server saved to the settings
|
||||
engine = create_engine(self.db_url)
|
||||
engine = create_engine(db_url)
|
||||
conn = engine.connect()
|
||||
assert len(conn.execute('SELECT * FROM metadata WHERE key = "proxy_server"').fetchall()) == 0
|
||||
assert len(conn.execute('SELECT * FROM metadata WHERE key = "proxy_username"').fetchall()) == 0
|
||||
assert len(conn.execute('SELECT * FROM metadata WHERE key = "proxy_password"').fetchall()) == 0
|
||||
assert conn.execute('SELECT * FROM metadata WHERE key = "version"').first().value == '2'
|
||||
|
||||
assert self.mocked_settings.setValue.call_args_list == [
|
||||
assert mock_settings.setValue.call_args_list == [
|
||||
call('advanced/proxy http', 'proxy_server'), call('advanced/proxy username', 'proxy_username'),
|
||||
call('advanced/proxy password', 'proxy_password'), call('advanced/proxy mode', ProxyMode.MANUAL_PROXY)]
|
||||
|
||||
def test_upgrade_2_https_selected(self):
|
||||
|
||||
def test_upgrade_2_https_selected(mock_message_box, db_url, mock_settings):
|
||||
"""
|
||||
Tcest that upgrade 2 completes properly when the user chooses to use a HTTPS proxy
|
||||
"""
|
||||
# GIVEN: An version 1 web bible with proxy settings
|
||||
|
||||
# WHEN: Calling upgrade_db and the user has 'clicked' the 'HTTPS' button
|
||||
self.mocked_message_box_instance.clickedButton.return_value = self.mocked_https_button
|
||||
upgrade_db(self.db_url, upgrade)
|
||||
mocked_message_box_instance = mock_message_box[0]
|
||||
mocked_https_button = mock_message_box[2]
|
||||
mocked_message_box_instance.clickedButton.return_value = mocked_https_button
|
||||
upgrade_db(db_url, upgrade)
|
||||
|
||||
# THEN: The proxy settings should have been removed, the version should have been changed to version 2, and the
|
||||
# proxy server saved to the settings
|
||||
engine = create_engine(self.db_url)
|
||||
engine = create_engine(db_url)
|
||||
conn = engine.connect()
|
||||
assert len(conn.execute('SELECT * FROM metadata WHERE key = "proxy_server"').fetchall()) == 0
|
||||
assert len(conn.execute('SELECT * FROM metadata WHERE key = "proxy_username"').fetchall()) == 0
|
||||
assert len(conn.execute('SELECT * FROM metadata WHERE key = "proxy_password"').fetchall()) == 0
|
||||
assert conn.execute('SELECT * FROM metadata WHERE key = "version"').first().value == '2'
|
||||
|
||||
assert self.mocked_settings.setValue.call_args_list == [
|
||||
assert mock_settings.setValue.call_args_list == [
|
||||
call('advanced/proxy https', 'proxy_server'), call('advanced/proxy username', 'proxy_username'),
|
||||
call('advanced/proxy password', 'proxy_password'), call('advanced/proxy mode', ProxyMode.MANUAL_PROXY)]
|
||||
|
||||
def test_upgrade_2_both_selected(self):
|
||||
|
||||
def test_upgrade_2_both_selected(mock_message_box, db_url, mock_settings):
|
||||
"""
|
||||
Tcest that upgrade 2 completes properly when the user chooses to use a both HTTP and HTTPS proxies
|
||||
"""
|
||||
@ -200,19 +160,21 @@ class TestProxyMetaUpgrade(TestCase, TestMixin):
|
||||
# GIVEN: An version 1 web bible with proxy settings
|
||||
|
||||
# WHEN: Calling upgrade_db
|
||||
self.mocked_message_box_instance.clickedButton.return_value = self.mocked_both_button
|
||||
upgrade_db(self.db_url, upgrade)
|
||||
mocked_message_box_instance = mock_message_box[0]
|
||||
mocked_both_button = mock_message_box[1]
|
||||
mocked_message_box_instance.clickedButton.return_value = mocked_both_button
|
||||
upgrade_db(db_url, upgrade)
|
||||
|
||||
# THEN: The proxy settings should have been removed, the version should have been changed to version 2, and the
|
||||
# proxy server saved to the settings
|
||||
engine = create_engine(self.db_url)
|
||||
engine = create_engine(db_url)
|
||||
conn = engine.connect()
|
||||
assert len(conn.execute('SELECT * FROM metadata WHERE key = "proxy_server"').fetchall()) == 0
|
||||
assert len(conn.execute('SELECT * FROM metadata WHERE key = "proxy_username"').fetchall()) == 0
|
||||
assert len(conn.execute('SELECT * FROM metadata WHERE key = "proxy_password"').fetchall()) == 0
|
||||
assert conn.execute('SELECT * FROM metadata WHERE key = "version"').first().value == '2'
|
||||
|
||||
assert self.mocked_settings.setValue.call_args_list == [
|
||||
assert mock_settings.setValue.call_args_list == [
|
||||
call('advanced/proxy http', 'proxy_server'), call('advanced/proxy https', 'proxy_server'),
|
||||
call('advanced/proxy username', 'proxy_username'), call('advanced/proxy password', 'proxy_password'),
|
||||
call('advanced/proxy mode', ProxyMode.MANUAL_PROXY)]
|
||||
|
@ -21,16 +21,11 @@
|
||||
"""
|
||||
This module contains tests for the versereferencelist submodule of the Bibles plugin.
|
||||
"""
|
||||
from unittest import TestCase
|
||||
|
||||
from openlp.plugins.bibles.lib.versereferencelist import VerseReferenceList
|
||||
|
||||
|
||||
class TestVerseReferenceList(TestCase):
|
||||
"""
|
||||
Test the VerseReferenceList class
|
||||
"""
|
||||
def test_add_first_verse(self):
|
||||
def test_add_first_verse():
|
||||
"""
|
||||
Test the addition of a verse to the empty list
|
||||
"""
|
||||
@ -54,7 +49,8 @@ class TestVerseReferenceList(TestCase):
|
||||
assert reference_list.verse_list[0]['version'] == version, 'The version in first entry should be %s' % version
|
||||
assert reference_list.verse_list[0]['end'] == verse, 'The end in first entry should be %u' % verse
|
||||
|
||||
def test_add_next_verse(self):
|
||||
|
||||
def test_add_next_verse():
|
||||
"""
|
||||
Test the addition of the following verse
|
||||
"""
|
||||
@ -76,7 +72,8 @@ class TestVerseReferenceList(TestCase):
|
||||
assert reference_list.current_index == 0, 'The current index should be 0'
|
||||
assert reference_list.verse_list[0]['end'] == next_verse, 'The end in first entry should be %u' % next_verse
|
||||
|
||||
def test_add_another_verse(self):
|
||||
|
||||
def test_add_another_verse():
|
||||
"""
|
||||
Test the addition of a verse in another book
|
||||
"""
|
||||
@ -99,7 +96,8 @@ class TestVerseReferenceList(TestCase):
|
||||
# THEN: the current index should be 1
|
||||
assert reference_list.current_index == 1, 'The current index should be 1'
|
||||
|
||||
def test_add_version(self):
|
||||
|
||||
def test_add_version():
|
||||
"""
|
||||
Test the addition of a version to the list
|
||||
"""
|
||||
@ -118,7 +116,8 @@ class TestVerseReferenceList(TestCase):
|
||||
{'version': version, 'copyright': copyright_, 'permission': permission}, \
|
||||
'The version data should be appended'
|
||||
|
||||
def test_add_existing_version(self):
|
||||
|
||||
def test_add_existing_version():
|
||||
"""
|
||||
Test the addition of an existing version to the list
|
||||
"""
|
||||
|
@ -22,7 +22,6 @@
|
||||
This module contains tests for the WordProject Bible importer.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from unittest import TestCase
|
||||
from unittest.mock import MagicMock, call, patch
|
||||
|
||||
from openlp.plugins.bibles.lib.importers.wordproject import WordProjectBible
|
||||
@ -34,22 +33,9 @@ INDEX_PAGE = (TEST_PATH / 'wordproject_index.htm').read_bytes().decode()
|
||||
CHAPTER_PAGE = (TEST_PATH / 'wordproject_chapter.htm').read_bytes().decode()
|
||||
|
||||
|
||||
class TestWordProjectImport(TestCase):
|
||||
"""
|
||||
Test the functions in the :mod:`wordprojectimport` module.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.registry_patcher = patch('openlp.plugins.bibles.lib.bibleimport.Registry')
|
||||
self.addCleanup(self.registry_patcher.stop)
|
||||
self.registry_patcher.start()
|
||||
self.manager_patcher = patch('openlp.plugins.bibles.lib.db.Manager')
|
||||
self.addCleanup(self.manager_patcher.stop)
|
||||
self.manager_patcher.start()
|
||||
|
||||
@patch.object(Path, 'read_text')
|
||||
@patch.object(Path, 'exists')
|
||||
def test_process_books(self, mocked_exists, mocked_read_text):
|
||||
@patch.object(Path, 'read_text')
|
||||
@patch.object(Path, 'exists')
|
||||
def test_process_books(mocked_exists, mocked_read_text, settings):
|
||||
"""
|
||||
Test the process_books() method
|
||||
"""
|
||||
@ -75,8 +61,9 @@ class TestWordProjectImport(TestCase):
|
||||
assert mocked_process_chapters.call_count == 66, 'There should be 66 books'
|
||||
assert mocked_session.commit.call_count == 66, 'There should be 66 books'
|
||||
|
||||
@patch.object(Path, 'read_text')
|
||||
def test_process_chapters(self, mocked_read_text):
|
||||
|
||||
@patch.object(Path, 'read_text')
|
||||
def test_process_chapters(mocked_read_text, settings):
|
||||
"""
|
||||
Test the process_chapters() method
|
||||
"""
|
||||
@ -103,8 +90,9 @@ class TestWordProjectImport(TestCase):
|
||||
assert mocked_set_current_chapter.call_args_list == expected_set_current_chapter_calls
|
||||
assert mocked_process_verses.call_args_list == expected_process_verses_calls
|
||||
|
||||
@patch.object(Path, 'read_text')
|
||||
def test_process_verses(self, mocked_read_text):
|
||||
|
||||
@patch.object(Path, 'read_text')
|
||||
def test_process_verses(mocked_read_text, settings):
|
||||
"""
|
||||
Test the process_verses() method
|
||||
"""
|
||||
@ -127,7 +115,8 @@ class TestWordProjectImport(TestCase):
|
||||
mocked_read_text.assert_called_once_with(encoding='utf-8', errors='ignore')
|
||||
assert mocked_process_verse.call_count == 31
|
||||
|
||||
def test_process_verse(self):
|
||||
|
||||
def test_process_verse(settings):
|
||||
"""
|
||||
Test the process_verse() method
|
||||
"""
|
||||
@ -146,7 +135,8 @@ class TestWordProjectImport(TestCase):
|
||||
# THEN: The create_verse() method should have been called
|
||||
mocked_create_verse.assert_called_once_with(1, 1, 1, 'In the beginning, God created the heavens and the earth')
|
||||
|
||||
def test_process_verse_no_text(self):
|
||||
|
||||
def test_process_verse_no_text(settings):
|
||||
"""
|
||||
Test the process_verse() method when there's no text
|
||||
"""
|
||||
@ -165,7 +155,8 @@ class TestWordProjectImport(TestCase):
|
||||
# THEN: The create_verse() method should NOT have been called
|
||||
assert mocked_create_verse.call_count == 0
|
||||
|
||||
def test_do_import(self):
|
||||
|
||||
def test_do_import(settings):
|
||||
"""
|
||||
Test the do_import() method
|
||||
"""
|
||||
@ -189,7 +180,8 @@ class TestWordProjectImport(TestCase):
|
||||
mocked_cleanup.assert_called_once_with()
|
||||
assert result is True
|
||||
|
||||
def test_do_import_no_language(self):
|
||||
|
||||
def test_do_import_no_language(settings):
|
||||
"""
|
||||
Test the do_import() method when the language is not available
|
||||
"""
|
||||
|
@ -21,7 +21,6 @@
|
||||
"""
|
||||
This module contains tests for the Zefania Bible importer.
|
||||
"""
|
||||
from unittest import TestCase
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from openlp.plugins.bibles.lib.db import BibleDB
|
||||
@ -33,20 +32,7 @@ from tests.utils.constants import RESOURCE_PATH
|
||||
TEST_PATH = RESOURCE_PATH / 'bibles'
|
||||
|
||||
|
||||
class TestZefaniaImport(TestCase):
|
||||
"""
|
||||
Test the functions in the :mod:`zefaniaimport` module.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.registry_patcher = patch('openlp.plugins.bibles.lib.bibleimport.Registry')
|
||||
self.addCleanup(self.registry_patcher.stop)
|
||||
self.registry_patcher.start()
|
||||
self.manager_patcher = patch('openlp.plugins.bibles.lib.db.Manager')
|
||||
self.addCleanup(self.manager_patcher.stop)
|
||||
self.manager_patcher.start()
|
||||
|
||||
def test_create_importer(self):
|
||||
def test_create_importer(settings):
|
||||
"""
|
||||
Test creating an instance of the Zefania file importer
|
||||
"""
|
||||
@ -59,7 +45,8 @@ class TestZefaniaImport(TestCase):
|
||||
# THEN: The importer should be an instance of BibleDB
|
||||
assert isinstance(importer, BibleDB)
|
||||
|
||||
def test_file_import(self):
|
||||
|
||||
def test_file_import(settings):
|
||||
"""
|
||||
Test the actual import of Zefania Bible file
|
||||
"""
|
||||
@ -88,7 +75,8 @@ class TestZefaniaImport(TestCase):
|
||||
importer.create_verse.assert_any_call(importer.create_book().id, 1, verse_tag, verse_text)
|
||||
importer.create_book.assert_any_call('Genesis', 1, 1)
|
||||
|
||||
def test_file_import_no_book_name(self):
|
||||
|
||||
def test_file_import_no_book_name(settings):
|
||||
"""
|
||||
Test the import of Zefania Bible file without book names
|
||||
"""
|
||||
|
@ -19,50 +19,36 @@
|
||||
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
|
||||
##########################################################################
|
||||
"""
|
||||
This module contains tests for the lib submodule of the Songs plugin.
|
||||
This module contains tests for the lib submodule of the Custom plugin.
|
||||
"""
|
||||
from unittest import TestCase
|
||||
import pytest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from PyQt5 import QtCore
|
||||
|
||||
from openlp.core.common.registry import Registry
|
||||
from openlp.core.lib.plugin import PluginStatus
|
||||
from openlp.core.lib.serviceitem import ServiceItem
|
||||
from openlp.plugins.custom.lib.mediaitem import CustomMediaItem
|
||||
from tests.helpers.testmixin import TestMixin
|
||||
|
||||
|
||||
FOOTER = ['Arky Arky (Unknown)', 'Public Domain', 'CCLI 123456']
|
||||
|
||||
|
||||
class TestMediaItem(TestCase, TestMixin):
|
||||
"""
|
||||
Test the functions in the :mod:`lib` module.
|
||||
"""
|
||||
def setUp(self):
|
||||
"""
|
||||
Set up the components need for all tests.
|
||||
"""
|
||||
Registry.create()
|
||||
Registry().register('service_list', MagicMock())
|
||||
@pytest.fixture()
|
||||
def media_item(mock_settings):
|
||||
Registry().register('main_window', MagicMock())
|
||||
|
||||
with patch('openlp.core.lib.mediamanageritem.MediaManagerItem._setup'), \
|
||||
patch('openlp.core.lib.mediamanageritem.MediaManagerItem.setup_item'), \
|
||||
patch('openlp.plugins.custom.forms.editcustomform.EditCustomForm.__init__'), \
|
||||
patch('openlp.plugins.custom.lib.mediaitem.CustomMediaItem.setup_item'):
|
||||
self.media_item = CustomMediaItem(None, MagicMock())
|
||||
self.setup_application()
|
||||
self.build_settings()
|
||||
QtCore.QLocale.setDefault(QtCore.QLocale('en_GB'))
|
||||
m_item = CustomMediaItem(None, MagicMock())
|
||||
media_item.plugin = MagicMock()
|
||||
m_item.settings_section = 'bibles'
|
||||
m_item.results_view_tab = MagicMock()
|
||||
return m_item
|
||||
|
||||
def tearDown(self):
|
||||
"""
|
||||
Delete all the C++ objects at the end so that we don't have a segfault
|
||||
"""
|
||||
self.destroy_settings()
|
||||
|
||||
def test_service_load_inactive(self):
|
||||
def test_service_load_inactive(media_item):
|
||||
"""
|
||||
Test the service load in custom with a default service item
|
||||
"""
|
||||
@ -70,53 +56,55 @@ class TestMediaItem(TestCase, TestMixin):
|
||||
service_item = ServiceItem(None)
|
||||
|
||||
# WHEN: I search for the custom in the database
|
||||
item = self.media_item.service_load(service_item)
|
||||
item = media_item.service_load(service_item)
|
||||
|
||||
# THEN: the processing should be ignored
|
||||
assert item is None, 'The Service item is inactive so processing should be bypassed'
|
||||
|
||||
def test_service_load_basic_custom_false(self):
|
||||
|
||||
def test_service_load_basic_custom_false(media_item):
|
||||
"""
|
||||
Test the service load in custom with a default service item and no requirement to add to the database
|
||||
"""
|
||||
# GIVEN: An empty Service Item and an active plugin
|
||||
service_item = ServiceItem(None)
|
||||
service_item.raw_footer = FOOTER
|
||||
self.media_item.plugin = MagicMock()
|
||||
self.media_item.plugin.status = PluginStatus.Active
|
||||
self.media_item.plugin.db_manager = MagicMock()
|
||||
self.media_item.plugin.db_manager.get_object_filtered = MagicMock()
|
||||
self.media_item.plugin.db_manager.get_object_filtered.return_value = None
|
||||
media_item.plugin = MagicMock()
|
||||
media_item.plugin.status = PluginStatus.Active
|
||||
media_item.plugin.db_manager = MagicMock()
|
||||
media_item.plugin.db_manager.get_object_filtered = MagicMock()
|
||||
media_item.plugin.db_manager.get_object_filtered.return_value = None
|
||||
|
||||
with patch('openlp.plugins.custom.lib.mediaitem.CustomSlide'):
|
||||
# WHEN: I search for the custom in the database
|
||||
self.media_item.add_custom_from_service = False
|
||||
self.media_item.create_from_service_item = MagicMock()
|
||||
self.media_item.service_load(service_item)
|
||||
media_item.add_custom_from_service = False
|
||||
media_item.create_from_service_item = MagicMock()
|
||||
media_item.service_load(service_item)
|
||||
|
||||
# THEN: the item should not be added to the database.
|
||||
assert self.media_item.create_from_service_item.call_count == 0, \
|
||||
assert media_item.create_from_service_item.call_count == 0, \
|
||||
'The item should not have been added to the database'
|
||||
|
||||
def test_service_load_basic_custom_true(self):
|
||||
|
||||
def test_service_load_basic_custom_true(media_item):
|
||||
"""
|
||||
Test the service load in custom with a default service item and a requirement to add to the database
|
||||
"""
|
||||
# GIVEN: An empty Service Item and an active plugin
|
||||
service_item = ServiceItem(None)
|
||||
service_item.raw_footer = FOOTER
|
||||
self.media_item.plugin = MagicMock()
|
||||
self.media_item.plugin.status = PluginStatus.Active
|
||||
self.media_item.plugin.db_manager = MagicMock()
|
||||
self.media_item.plugin.db_manager.get_object_filtered = MagicMock()
|
||||
self.media_item.plugin.db_manager.get_object_filtered.return_value = None
|
||||
media_item.plugin = MagicMock()
|
||||
media_item.plugin.status = PluginStatus.Active
|
||||
media_item.plugin.db_manager = MagicMock()
|
||||
media_item.plugin.db_manager.get_object_filtered = MagicMock()
|
||||
media_item.plugin.db_manager.get_object_filtered.return_value = None
|
||||
|
||||
with patch('openlp.plugins.custom.lib.mediaitem.CustomSlide'):
|
||||
# WHEN: I search for the custom in the database
|
||||
self.media_item.add_custom_from_service = True
|
||||
self.media_item.create_from_service_item = MagicMock()
|
||||
self.media_item.service_load(service_item)
|
||||
media_item.add_custom_from_service = True
|
||||
media_item.create_from_service_item = MagicMock()
|
||||
media_item.service_load(service_item)
|
||||
|
||||
# THEN: the item should not be added to the database.
|
||||
assert self.media_item.create_from_service_item.call_count == 1, \
|
||||
assert media_item.create_from_service_item.call_count == 1, \
|
||||
'The item should have been added to the database'
|
||||
|
@ -21,72 +21,43 @@
|
||||
"""
|
||||
This module contains tests for the lib submodule of the Images plugin.
|
||||
"""
|
||||
from unittest import TestCase
|
||||
import pytest
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from PyQt5 import QtWidgets
|
||||
|
||||
from openlp.core.common.registry import Registry
|
||||
from openlp.core.common.settings import Settings
|
||||
from openlp.plugins.images.lib.imagetab import ImageTab
|
||||
from tests.helpers.testmixin import TestMixin
|
||||
|
||||
|
||||
__default_settings__ = {
|
||||
'images/db type': 'sqlite',
|
||||
'images/background color': '#000000',
|
||||
}
|
||||
|
||||
|
||||
class TestImageMediaItem(TestCase, TestMixin):
|
||||
"""
|
||||
This is a test case to test various methods in the ImageTab.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
"""
|
||||
Create the UI
|
||||
"""
|
||||
Registry.create()
|
||||
@pytest.fixture()
|
||||
def form(settings):
|
||||
Registry().register('settings_form', MagicMock())
|
||||
self.setup_application()
|
||||
self.build_settings()
|
||||
Registry().register('settings', Settings())
|
||||
Settings().extend_default_settings(__default_settings__)
|
||||
self.parent = QtWidgets.QMainWindow()
|
||||
self.form = ImageTab(self.parent, 'Images', None, None)
|
||||
self.form.settings_form.register_post_process = MagicMock()
|
||||
frm = ImageTab(None, 'Images', None, None)
|
||||
frm.settings_form.register_post_process = MagicMock()
|
||||
return frm
|
||||
|
||||
def tearDown(self):
|
||||
"""
|
||||
Delete all the C++ objects at the end so that we don't have a segfault
|
||||
"""
|
||||
del self.parent
|
||||
del self.form
|
||||
self.destroy_settings()
|
||||
|
||||
def test_save_tab_nochange(self):
|
||||
def test_save_tab_nochange(form):
|
||||
"""
|
||||
Test no changes does not trigger post processing
|
||||
"""
|
||||
# GIVEN: No changes on the form.
|
||||
self.initial_color = '#999999'
|
||||
# WHEN: the save is invoked
|
||||
self.form.save()
|
||||
form.save()
|
||||
# THEN: the post process should not be requested
|
||||
assert 0 == self.form.settings_form.register_post_process.call_count, \
|
||||
assert 0 == form.settings_form.register_post_process.call_count, \
|
||||
'Image Post processing should not have been requested'
|
||||
|
||||
def test_save_tab_change(self):
|
||||
|
||||
def test_save_tab_change(form):
|
||||
"""
|
||||
Test a color change is applied and triggers post processing.
|
||||
"""
|
||||
# GIVEN: Apply a change to the form.
|
||||
self.form.on_background_color_changed('#999999')
|
||||
form.on_background_color_changed('#999999')
|
||||
# WHEN: the save is invoked
|
||||
self.form.save()
|
||||
form.save()
|
||||
# THEN: the post process should be requested
|
||||
assert 1 == self.form.settings_form.register_post_process.call_count, \
|
||||
assert 1 == form.settings_form.register_post_process.call_count, \
|
||||
'Image Post processing should have been requested'
|
||||
# THEN: The color should be set
|
||||
assert self.form.background_color == '#999999', 'The updated color should have been saved'
|
||||
assert form.background_color == '#999999', 'The updated color should have been saved'
|
||||
|
@ -1,299 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
##########################################################################
|
||||
# OpenLP - Open Source Lyrics Projection #
|
||||
# ---------------------------------------------------------------------- #
|
||||
# Copyright (c) 2008-2020 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/>. #
|
||||
##########################################################################
|
||||
"""
|
||||
This module contains tests for the lib submodule of the Images plugin.
|
||||
"""
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from unittest import TestCase
|
||||
from unittest.mock import ANY, MagicMock, patch
|
||||
|
||||
from PyQt5 import QtCore, QtWidgets
|
||||
|
||||
from openlp.core.common.registry import Registry
|
||||
from openlp.plugins.images.lib.db import ImageFilenames, ImageGroups
|
||||
from openlp.plugins.images.lib.mediaitem import ImageMediaItem
|
||||
|
||||
|
||||
@pytest.yield_fixture
|
||||
def mocked_media_item(mock_settings):
|
||||
"""Local test setup"""
|
||||
mocked_main_window = MagicMock()
|
||||
Registry().register('application', MagicMock())
|
||||
Registry().register('service_list', MagicMock())
|
||||
Registry().register('main_window', mocked_main_window)
|
||||
Registry().register('live_controller', MagicMock())
|
||||
mocked_plugin = MagicMock()
|
||||
with patch('openlp.plugins.images.lib.mediaitem.MediaManagerItem._setup'), \
|
||||
patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.setup_item'):
|
||||
media_item = ImageMediaItem(None, mocked_plugin)
|
||||
media_item.settings_section = 'images'
|
||||
yield media_item
|
||||
|
||||
|
||||
class TestImageMediaItem(TestCase):
|
||||
"""
|
||||
This is a test case to test various methods in the ImageMediaItem class.
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
self.mocked_main_window = MagicMock()
|
||||
Registry.create()
|
||||
Registry().register('application', MagicMock())
|
||||
Registry().register('service_list', MagicMock())
|
||||
Registry().register('main_window', self.mocked_main_window)
|
||||
Registry().register('live_controller', MagicMock())
|
||||
mocked_plugin = MagicMock()
|
||||
with patch('openlp.plugins.images.lib.mediaitem.MediaManagerItem._setup'), \
|
||||
patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.setup_item'):
|
||||
self.media_item = ImageMediaItem(None, mocked_plugin)
|
||||
self.media_item.settings_section = 'images'
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
|
||||
def test_save_new_images_list_empty_list(self, mocked_load_full_list):
|
||||
"""
|
||||
Test that the save_new_images_list() method handles empty lists gracefully
|
||||
"""
|
||||
# GIVEN: An empty image_list
|
||||
image_list = []
|
||||
self.media_item.manager = MagicMock()
|
||||
|
||||
# WHEN: We run save_new_images_list with the empty list
|
||||
self.media_item.save_new_images_list(image_list)
|
||||
|
||||
# THEN: The save_object() method should not have been called
|
||||
assert self.media_item.manager.save_object.call_count == 0, \
|
||||
'The save_object() method should not have been called'
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
|
||||
def test_save_new_images_list_single_image_with_reload(self, mocked_load_full_list):
|
||||
"""
|
||||
Test that the save_new_images_list() calls load_full_list() when reload_list is set to True
|
||||
"""
|
||||
# GIVEN: A list with 1 image and a mocked out manager
|
||||
image_list = [Path('test_image.jpg')]
|
||||
ImageFilenames.file_path = None
|
||||
self.media_item.manager = MagicMock()
|
||||
|
||||
# WHEN: We run save_new_images_list with reload_list=True
|
||||
self.media_item.save_new_images_list(image_list, reload_list=True)
|
||||
|
||||
# THEN: load_full_list() should have been called
|
||||
assert mocked_load_full_list.call_count == 1, 'load_full_list() should have been called'
|
||||
|
||||
# CLEANUP: Remove added attribute from ImageFilenames
|
||||
delattr(ImageFilenames, 'file_path')
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
|
||||
def test_save_new_images_list_single_image_without_reload(self, mocked_load_full_list):
|
||||
"""
|
||||
Test that the save_new_images_list() doesn't call load_full_list() when reload_list is set to False
|
||||
"""
|
||||
# GIVEN: A list with 1 image and a mocked out manager
|
||||
image_list = [Path('test_image.jpg')]
|
||||
self.media_item.manager = MagicMock()
|
||||
|
||||
# WHEN: We run save_new_images_list with reload_list=False
|
||||
self.media_item.save_new_images_list(image_list, reload_list=False)
|
||||
|
||||
# THEN: load_full_list() should not have been called
|
||||
assert mocked_load_full_list.call_count == 0, 'load_full_list() should not have been called'
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
|
||||
def test_save_new_images_list_multiple_images(self, mocked_load_full_list):
|
||||
"""
|
||||
Test that the save_new_images_list() saves all images in the list
|
||||
"""
|
||||
# GIVEN: A list with 3 images
|
||||
image_list = [Path('test_image_1.jpg'), Path('test_image_2.jpg'), Path('test_image_3.jpg')]
|
||||
self.media_item.manager = MagicMock()
|
||||
|
||||
# WHEN: We run save_new_images_list with the list of 3 images
|
||||
self.media_item.save_new_images_list(image_list, reload_list=False)
|
||||
|
||||
# THEN: load_full_list() should not have been called
|
||||
assert self.media_item.manager.save_object.call_count == 3, \
|
||||
'load_full_list() should have been called three times'
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
|
||||
def test_save_new_images_list_other_objects_in_list(self, mocked_load_full_list):
|
||||
"""
|
||||
Test that the save_new_images_list() ignores everything in the provided list except strings
|
||||
"""
|
||||
# GIVEN: A list with images and objects
|
||||
image_list = [Path('test_image_1.jpg'), None, True, ImageFilenames(), Path('test_image_2.jpg')]
|
||||
self.media_item.manager = MagicMock()
|
||||
|
||||
# WHEN: We run save_new_images_list with the list of images and objects
|
||||
self.media_item.save_new_images_list(image_list, reload_list=False)
|
||||
|
||||
# THEN: load_full_list() should not have been called
|
||||
assert self.media_item.manager.save_object.call_count == 2, 'load_full_list() should have been called only once'
|
||||
|
||||
def test_on_reset_click(self):
|
||||
"""
|
||||
Test that on_reset_click() actually resets the background
|
||||
"""
|
||||
# GIVEN: A mocked version of reset_action
|
||||
self.media_item.reset_action = MagicMock()
|
||||
self.media_item.reset_action_context = MagicMock()
|
||||
|
||||
# WHEN: on_reset_click is called
|
||||
self.media_item.on_reset_click()
|
||||
|
||||
# THEN: the reset_action should be set visible, and the image should be reset
|
||||
self.media_item.reset_action.setVisible.assert_called_with(False)
|
||||
self.media_item.reset_action_context.setVisible.assert_called_with(False)
|
||||
self.media_item.live_controller.display.reset_image.assert_called_with()
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.delete_file')
|
||||
def test_recursively_delete_group(self, mocked_delete_file):
|
||||
"""
|
||||
Test that recursively_delete_group() works
|
||||
"""
|
||||
# GIVEN: An ImageGroups object and mocked functions
|
||||
ImageFilenames.group_id = 1
|
||||
ImageGroups.parent_id = 1
|
||||
self.media_item.manager = MagicMock()
|
||||
self.media_item.manager.get_all_objects.side_effect = self._recursively_delete_group_side_effect
|
||||
self.media_item.service_path = Path()
|
||||
test_group = ImageGroups()
|
||||
test_group.id = 1
|
||||
|
||||
# WHEN: recursively_delete_group() is called
|
||||
self.media_item.recursively_delete_group(test_group)
|
||||
|
||||
# THEN: delete_file() should have been called 12 times and manager.delete_object() 7 times.
|
||||
assert mocked_delete_file.call_count == 12, 'delete_file() should have been called 12 times'
|
||||
assert self.media_item.manager.delete_object.call_count == 7, \
|
||||
'manager.delete_object() should be called exactly 7 times'
|
||||
|
||||
# CLEANUP: Remove added attribute from Image Filenames and ImageGroups
|
||||
delattr(ImageFilenames, 'group_id')
|
||||
delattr(ImageGroups, 'parent_id')
|
||||
|
||||
def _recursively_delete_group_side_effect(*args, **kwargs):
|
||||
"""
|
||||
Side effect method that creates custom return values for the recursively_delete_group method
|
||||
"""
|
||||
if args[1] == ImageFilenames and args[2]:
|
||||
# Create some fake objects that should be removed
|
||||
returned_object1 = ImageFilenames()
|
||||
returned_object1.id = 1
|
||||
returned_object1.file_path = Path('/', 'tmp', 'test_file_1.jpg')
|
||||
returned_object2 = ImageFilenames()
|
||||
returned_object2.id = 2
|
||||
returned_object2.file_path = Path('/', 'tmp', 'test_file_2.jpg')
|
||||
returned_object3 = ImageFilenames()
|
||||
returned_object3.id = 3
|
||||
returned_object3.file_path = Path('/', 'tmp', 'test_file_3.jpg')
|
||||
return [returned_object1, returned_object2, returned_object3]
|
||||
if args[1] == ImageGroups and args[2]:
|
||||
# Change the parent_id that is matched so we don't get into an endless loop
|
||||
ImageGroups.parent_id = 0
|
||||
# Create a fake group that will be used in the next run
|
||||
returned_object1 = ImageGroups()
|
||||
returned_object1.id = 1
|
||||
return [returned_object1]
|
||||
return []
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.delete_file')
|
||||
@patch('openlp.plugins.images.lib.mediaitem.check_item_selected')
|
||||
def test_on_delete_click(self, mocked_check_item_selected, mocked_delete_file):
|
||||
"""
|
||||
Test that on_delete_click() works
|
||||
"""
|
||||
# GIVEN: An ImageGroups object and mocked functions
|
||||
mocked_check_item_selected.return_value = True
|
||||
test_image = ImageFilenames()
|
||||
test_image.id = 1
|
||||
test_image.group_id = 1
|
||||
test_image.file_path = Path('imagefile.png')
|
||||
self.media_item.manager = MagicMock()
|
||||
self.media_item.service_path = Path()
|
||||
self.media_item.list_view = MagicMock()
|
||||
mocked_row_item = MagicMock()
|
||||
mocked_row_item.data.return_value = test_image
|
||||
mocked_row_item.text.return_value = ''
|
||||
self.media_item.list_view.selectedItems.return_value = [mocked_row_item]
|
||||
|
||||
# WHEN: Calling on_delete_click
|
||||
self.media_item.on_delete_click()
|
||||
|
||||
# THEN: delete_file should have been called twice
|
||||
assert mocked_delete_file.call_count == 2, 'delete_file() should have been called twice'
|
||||
|
||||
def test_create_item_from_id(self):
|
||||
"""
|
||||
Test that the create_item_from_id() method returns a valid QTreeWidgetItem with a pre-created ImageFilenames
|
||||
"""
|
||||
# GIVEN: An ImageFilenames that already exists in the database
|
||||
image_file = ImageFilenames()
|
||||
image_file.id = 1
|
||||
image_file.file_path = Path('/', 'tmp', 'test_file_1.jpg')
|
||||
self.media_item.manager = MagicMock()
|
||||
self.media_item.manager.get_object_filtered.return_value = image_file
|
||||
ImageFilenames.file_path = None
|
||||
|
||||
# WHEN: create_item_from_id() is called
|
||||
item = self.media_item.create_item_from_id('1')
|
||||
|
||||
# THEN: A QTreeWidgetItem should be created with the above model object as it's data
|
||||
assert isinstance(item, QtWidgets.QTreeWidgetItem)
|
||||
assert 'test_file_1.jpg' == item.text(0)
|
||||
item_data = item.data(0, QtCore.Qt.UserRole)
|
||||
assert isinstance(item_data, ImageFilenames)
|
||||
assert 1 == item_data.id
|
||||
assert Path('/', 'tmp', 'test_file_1.jpg') == item_data.file_path
|
||||
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list')
|
||||
def test_validate_and_load(mocked_load_list, mocked_media_item):
|
||||
"""
|
||||
Test that the validate_and_load_test() method when called without a group
|
||||
"""
|
||||
# GIVEN: A list of files
|
||||
file_list = [Path('path1', 'image1.jpg'), Path('path2', 'image2.jpg')]
|
||||
|
||||
# WHEN: Calling validate_and_load with the list of files
|
||||
mocked_media_item.validate_and_load(file_list)
|
||||
|
||||
# THEN: load_list should have been called with the file list and None,
|
||||
# the directory should have been saved to the settings
|
||||
mocked_load_list.assert_called_once_with(file_list, None)
|
||||
Registry().get('settings').setValue.assert_called_once_with(ANY, Path('path1'))
|
||||
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list')
|
||||
def test_validate_and_load_group(mocked_load_list, mocked_media_item):
|
||||
"""
|
||||
Test that the validate_and_load_test() method when called with a group
|
||||
"""
|
||||
# GIVEN: A list of files
|
||||
file_list = [Path('path1', 'image1.jpg'), Path('path2', 'image2.jpg')]
|
||||
|
||||
# WHEN: Calling validate_and_load with the list of files and a group
|
||||
mocked_media_item.validate_and_load(file_list, 'group')
|
||||
|
||||
# THEN: load_list should have been called with the file list and the group name,
|
||||
# the directory should have been saved to the settings
|
||||
mocked_load_list.assert_called_once_with(file_list, 'group')
|
||||
Registry().get('settings').setValue.assert_called_once_with(ANY, Path('path1'))
|
288
tests/functional/openlp_plugins/images/test_mediaitem.py
Normal file
288
tests/functional/openlp_plugins/images/test_mediaitem.py
Normal file
@ -0,0 +1,288 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
##########################################################################
|
||||
# OpenLP - Open Source Lyrics Projection #
|
||||
# ---------------------------------------------------------------------- #
|
||||
# Copyright (c) 2008-2020 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/>. #
|
||||
##########################################################################
|
||||
"""
|
||||
This module contains tests for the lib submodule of the Images plugin.
|
||||
"""
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from unittest.mock import ANY, MagicMock, patch
|
||||
|
||||
from PyQt5 import QtCore, QtWidgets
|
||||
|
||||
from openlp.core.common.registry import Registry
|
||||
from openlp.plugins.images.lib.db import ImageFilenames, ImageGroups
|
||||
from openlp.plugins.images.lib.mediaitem import ImageMediaItem
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def media_item(mock_settings):
|
||||
"""Local test setup"""
|
||||
mocked_main_window = MagicMock()
|
||||
Registry().register('service_list', MagicMock())
|
||||
Registry().register('main_window', mocked_main_window)
|
||||
Registry().register('live_controller', MagicMock())
|
||||
mocked_plugin = MagicMock()
|
||||
with patch('openlp.plugins.images.lib.mediaitem.MediaManagerItem._setup'), \
|
||||
patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.setup_item'):
|
||||
m_item = ImageMediaItem(None, mocked_plugin)
|
||||
m_item.settings_section = 'images'
|
||||
return m_item
|
||||
|
||||
|
||||
def _recursively_delete_group_side_effect(*args, **kwargs):
|
||||
"""
|
||||
Side effect method that creates custom return values for the recursively_delete_group method
|
||||
"""
|
||||
if args[0] == ImageFilenames and args[1]:
|
||||
# Create some fake objects that should be removed
|
||||
returned_object1 = ImageFilenames()
|
||||
returned_object1.id = 1
|
||||
returned_object1.file_path = Path('/', 'tmp', 'test_file_1.jpg')
|
||||
returned_object2 = ImageFilenames()
|
||||
returned_object2.id = 2
|
||||
returned_object2.file_path = Path('/', 'tmp', 'test_file_2.jpg')
|
||||
returned_object3 = ImageFilenames()
|
||||
returned_object3.id = 3
|
||||
returned_object3.file_path = Path('/', 'tmp', 'test_file_3.jpg')
|
||||
return [returned_object1, returned_object2, returned_object3]
|
||||
if args[0] == ImageGroups and args[1]:
|
||||
# Change the parent_id that is matched so we don't get into an endless loop
|
||||
ImageGroups.parent_id = 0
|
||||
# Create a fake group that will be used in the next run
|
||||
returned_object1 = ImageGroups()
|
||||
returned_object1.id = 1
|
||||
return [returned_object1]
|
||||
return []
|
||||
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
|
||||
def test_save_new_images_list_empty_list(mocked_load_full_list, media_item):
|
||||
"""
|
||||
Test that the save_new_images_list() method handles empty lists gracefully
|
||||
"""
|
||||
# GIVEN: An empty image_list
|
||||
image_list = []
|
||||
media_item.manager = MagicMock()
|
||||
|
||||
# WHEN: We run save_new_images_list with the empty list
|
||||
media_item.save_new_images_list(image_list)
|
||||
|
||||
# THEN: The save_object() method should not have been called
|
||||
assert media_item.manager.save_object.call_count == 0, \
|
||||
'The save_object() method should not have been called'
|
||||
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
|
||||
def test_save_new_images_list_single_image_with_reload(mocked_load_full_list, media_item):
|
||||
"""
|
||||
Test that the save_new_images_list() calls load_full_list() when reload_list is set to True
|
||||
"""
|
||||
# GIVEN: A list with 1 image and a mocked out manager
|
||||
image_list = [Path('test_image.jpg')]
|
||||
ImageFilenames.file_path = None
|
||||
media_item.manager = MagicMock()
|
||||
|
||||
# WHEN: We run save_new_images_list with reload_list=True
|
||||
media_item.save_new_images_list(image_list, reload_list=True)
|
||||
|
||||
# THEN: load_full_list() should have been called
|
||||
assert mocked_load_full_list.call_count == 1, 'load_full_list() should have been called'
|
||||
|
||||
# CLEANUP: Remove added attribute from ImageFilenames
|
||||
delattr(ImageFilenames, 'file_path')
|
||||
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
|
||||
def test_save_new_images_list_single_image_without_reload(mocked_load_full_list, media_item):
|
||||
"""
|
||||
Test that the save_new_images_list() doesn't call load_full_list() when reload_list is set to False
|
||||
"""
|
||||
# GIVEN: A list with 1 image and a mocked out manager
|
||||
image_list = [Path('test_image.jpg')]
|
||||
media_item.manager = MagicMock()
|
||||
|
||||
# WHEN: We run save_new_images_list with reload_list=False
|
||||
media_item.save_new_images_list(image_list, reload_list=False)
|
||||
|
||||
# THEN: load_full_list() should not have been called
|
||||
assert mocked_load_full_list.call_count == 0, 'load_full_list() should not have been called'
|
||||
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
|
||||
def test_save_new_images_list_multiple_images(mocked_load_full_list, media_item):
|
||||
"""
|
||||
Test that the save_new_images_list() saves all images in the list
|
||||
"""
|
||||
# GIVEN: A list with 3 images
|
||||
image_list = [Path('test_image_1.jpg'), Path('test_image_2.jpg'), Path('test_image_3.jpg')]
|
||||
media_item.manager = MagicMock()
|
||||
|
||||
# WHEN: We run save_new_images_list with the list of 3 images
|
||||
media_item.save_new_images_list(image_list, reload_list=False)
|
||||
|
||||
# THEN: load_full_list() should not have been called
|
||||
assert media_item.manager.save_object.call_count == 3, \
|
||||
'load_full_list() should have been called three times'
|
||||
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_full_list')
|
||||
def test_save_new_images_list_other_objects_in_list(mocked_load_full_list, media_item):
|
||||
"""
|
||||
Test that the save_new_images_list() ignores everything in the provided list except strings
|
||||
"""
|
||||
# GIVEN: A list with images and objects
|
||||
image_list = [Path('test_image_1.jpg'), None, True, ImageFilenames(), Path('test_image_2.jpg')]
|
||||
media_item.manager = MagicMock()
|
||||
|
||||
# WHEN: We run save_new_images_list with the list of images and objects
|
||||
media_item.save_new_images_list(image_list, reload_list=False)
|
||||
|
||||
# THEN: load_full_list() should not have been called
|
||||
assert media_item.manager.save_object.call_count == 2, 'load_full_list() should have been called only once'
|
||||
|
||||
|
||||
def test_on_reset_click(media_item):
|
||||
"""
|
||||
Test that on_reset_click() actually resets the background
|
||||
"""
|
||||
# GIVEN: A mocked version of reset_action
|
||||
media_item.reset_action = MagicMock()
|
||||
media_item.reset_action_context = MagicMock()
|
||||
|
||||
# WHEN: on_reset_click is called
|
||||
media_item.on_reset_click()
|
||||
|
||||
# THEN: the reset_action should be set visible, and the image should be reset
|
||||
media_item.reset_action.setVisible.assert_called_with(False)
|
||||
media_item.reset_action_context.setVisible.assert_called_with(False)
|
||||
media_item.live_controller.display.reset_image.assert_called_with()
|
||||
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.delete_file')
|
||||
def test_recursively_delete_group(mocked_delete_file, media_item):
|
||||
"""
|
||||
Test that recursively_delete_group() works
|
||||
"""
|
||||
# GIVEN: An ImageGroups object and mocked functions
|
||||
ImageFilenames.group_id = 1
|
||||
ImageGroups.parent_id = 1
|
||||
media_item.manager = MagicMock()
|
||||
media_item.manager.get_all_objects.side_effect = _recursively_delete_group_side_effect
|
||||
media_item.service_path = Path()
|
||||
test_group = ImageGroups()
|
||||
test_group.id = 1
|
||||
|
||||
# WHEN: recursively_delete_group() is called
|
||||
media_item.recursively_delete_group(test_group)
|
||||
|
||||
# THEN: delete_file() should have been called 12 times and manager.delete_object() 7 times.
|
||||
assert mocked_delete_file.call_count == 12, 'delete_file() should have been called 12 times'
|
||||
assert media_item.manager.delete_object.call_count == 7, \
|
||||
'manager.delete_object() should be called exactly 7 times'
|
||||
|
||||
# CLEANUP: Remove added attribute from Image Filenames and ImageGroups
|
||||
delattr(ImageFilenames, 'group_id')
|
||||
delattr(ImageGroups, 'parent_id')
|
||||
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.delete_file')
|
||||
@patch('openlp.plugins.images.lib.mediaitem.check_item_selected')
|
||||
def test_on_delete_click(mocked_check_item_selected, mocked_delete_file, media_item):
|
||||
"""
|
||||
Test that on_delete_click() works
|
||||
"""
|
||||
# GIVEN: An ImageGroups object and mocked functions
|
||||
mocked_check_item_selected.return_value = True
|
||||
test_image = ImageFilenames()
|
||||
test_image.id = 1
|
||||
test_image.group_id = 1
|
||||
test_image.file_path = Path('imagefile.png')
|
||||
media_item.manager = MagicMock()
|
||||
media_item.service_path = Path()
|
||||
media_item.list_view = MagicMock()
|
||||
mocked_row_item = MagicMock()
|
||||
mocked_row_item.data.return_value = test_image
|
||||
mocked_row_item.text.return_value = ''
|
||||
media_item.list_view.selectedItems.return_value = [mocked_row_item]
|
||||
|
||||
# WHEN: Calling on_delete_click
|
||||
media_item.on_delete_click()
|
||||
|
||||
# THEN: delete_file should have been called twice
|
||||
assert mocked_delete_file.call_count == 2, 'delete_file() should have been called twice'
|
||||
|
||||
|
||||
def test_create_item_from_id(media_item):
|
||||
"""
|
||||
Test that the create_item_from_id() method returns a valid QTreeWidgetItem with a pre-created ImageFilenames
|
||||
"""
|
||||
# GIVEN: An ImageFilenames that already exists in the database
|
||||
image_file = ImageFilenames()
|
||||
image_file.id = 1
|
||||
image_file.file_path = Path('/', 'tmp', 'test_file_1.jpg')
|
||||
media_item.manager = MagicMock()
|
||||
media_item.manager.get_object_filtered.return_value = image_file
|
||||
ImageFilenames.file_path = None
|
||||
|
||||
# WHEN: create_item_from_id() is called
|
||||
item = media_item.create_item_from_id('1')
|
||||
|
||||
# THEN: A QTreeWidgetItem should be created with the above model object as it's data
|
||||
assert isinstance(item, QtWidgets.QTreeWidgetItem)
|
||||
assert 'test_file_1.jpg' == item.text(0)
|
||||
item_data = item.data(0, QtCore.Qt.UserRole)
|
||||
assert isinstance(item_data, ImageFilenames)
|
||||
assert 1 == item_data.id
|
||||
assert Path('/', 'tmp', 'test_file_1.jpg') == item_data.file_path
|
||||
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list')
|
||||
def test_validate_and_load(mocked_load_list, media_item):
|
||||
"""
|
||||
Test that the validate_and_load_test() method when called without a group
|
||||
"""
|
||||
# GIVEN: A list of files
|
||||
file_list = [Path('path1', 'image1.jpg'), Path('path2', 'image2.jpg')]
|
||||
|
||||
# WHEN: Calling validate_and_load with the list of files
|
||||
media_item.validate_and_load(file_list)
|
||||
|
||||
# THEN: load_list should have been called with the file list and None,
|
||||
# the directory should have been saved to the settings
|
||||
mocked_load_list.assert_called_once_with(file_list, None)
|
||||
Registry().get('settings').setValue.assert_called_once_with(ANY, Path('path1'))
|
||||
|
||||
|
||||
@patch('openlp.plugins.images.lib.mediaitem.ImageMediaItem.load_list')
|
||||
def test_validate_and_load_group(mocked_load_list, media_item):
|
||||
"""
|
||||
Test that the validate_and_load_test() method when called with a group
|
||||
"""
|
||||
# GIVEN: A list of files
|
||||
file_list = [Path('path1', 'image1.jpg'), Path('path2', 'image2.jpg')]
|
||||
|
||||
# WHEN: Calling validate_and_load with the list of files and a group
|
||||
media_item.validate_and_load(file_list, 'group')
|
||||
|
||||
# THEN: load_list should have been called with the file list and the group name,
|
||||
# the directory should have been saved to the settings
|
||||
mocked_load_list.assert_called_once_with(file_list, 'group')
|
||||
Registry().get('settings').setValue.assert_called_once_with(ANY, Path('path1'))
|
@ -21,66 +21,47 @@
|
||||
"""
|
||||
This module contains tests for the lib submodule of the Images plugin.
|
||||
"""
|
||||
import os
|
||||
import pytest
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from tempfile import mkdtemp
|
||||
from unittest import TestCase, skip
|
||||
from unittest.mock import patch
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
from openlp.core.common.applocation import AppLocation
|
||||
from openlp.core.common.settings import Settings
|
||||
from openlp.core.lib.db import Manager
|
||||
from openlp.core.lib.db import upgrade_db
|
||||
from openlp.plugins.images.lib import upgrade
|
||||
from openlp.plugins.images.lib.db import ImageFilenames, init_schema
|
||||
from tests.helpers.testmixin import TestMixin
|
||||
from tests.utils.constants import TEST_RESOURCES_PATH
|
||||
from tests.utils.constants import RESOURCE_PATH
|
||||
|
||||
|
||||
__default_settings__ = {
|
||||
'images/db type': 'sqlite',
|
||||
'images/background color': '#000000',
|
||||
}
|
||||
@pytest.yield_fixture()
|
||||
def temp_path():
|
||||
tmp_path = Path(mkdtemp())
|
||||
yield tmp_path
|
||||
shutil.rmtree(tmp_path, ignore_errors=True)
|
||||
|
||||
|
||||
class TestImageDBUpgrade(TestCase, TestMixin):
|
||||
"""
|
||||
Test that the image database is upgraded correctly
|
||||
"""
|
||||
def setUp(self):
|
||||
self.build_settings()
|
||||
Settings().extend_default_settings(__default_settings__)
|
||||
self.tmp_folder = mkdtemp()
|
||||
@pytest.yield_fixture()
|
||||
def db_url():
|
||||
tmp_path = Path(mkdtemp())
|
||||
db_path = RESOURCE_PATH / 'images' / 'image-v0.sqlite'
|
||||
db_tmp_path = tmp_path / 'image-v0.sqlite'
|
||||
shutil.copyfile(db_path, db_tmp_path)
|
||||
yield 'sqlite:///' + str(db_tmp_path)
|
||||
shutil.rmtree(tmp_path, ignore_errors=True)
|
||||
|
||||
def tearDown(self):
|
||||
"""
|
||||
Delete all the C++ objects at the end so that we don't have a segfault
|
||||
"""
|
||||
self.destroy_settings()
|
||||
# Ignore errors since windows can have problems with locked files
|
||||
shutil.rmtree(self.tmp_folder, ignore_errors=True)
|
||||
|
||||
@skip
|
||||
# Broken due to Path issues.
|
||||
def test_image_filenames_table(self):
|
||||
def test_image_filenames_table(db_url, settings):
|
||||
"""
|
||||
Test that the ImageFilenames table is correctly upgraded to the latest version
|
||||
"""
|
||||
# GIVEN: An unversioned image database
|
||||
temp_db_name = os.path.join(self.tmp_folder, 'image-v0.sqlite')
|
||||
shutil.copyfile(os.path.join(TEST_RESOURCES_PATH, 'images', 'image-v0.sqlite'), temp_db_name)
|
||||
|
||||
with patch.object(AppLocation, 'get_data_path', return_value=Path('/', 'test', 'dir')):
|
||||
# WHEN: Initalising the database manager
|
||||
manager = Manager('images', init_schema, db_file_path=Path(temp_db_name), upgrade_mod=upgrade)
|
||||
|
||||
# THEN: The database should have been upgraded and image_filenames.file_path should return Path objects
|
||||
upgraded_results = manager.get_all_objects(ImageFilenames)
|
||||
upgrade_db(db_url, upgrade)
|
||||
|
||||
expected_result_data = {1: Path('/', 'test', 'image1.jpg'),
|
||||
2: Path('/', 'test', 'dir', 'image2.jpg'),
|
||||
3: Path('/', 'test', 'dir', 'subdir', 'image3.jpg')}
|
||||
|
||||
assert len(upgraded_results) == 3
|
||||
for result in upgraded_results:
|
||||
assert expected_result_data[result.id] == result.file_path
|
||||
engine = create_engine(db_url)
|
||||
conn = engine.connect()
|
||||
assert conn.execute('SELECT * FROM metadata WHERE key = "version"').first().value == '2'
|
||||
|
@ -21,67 +21,48 @@
|
||||
"""
|
||||
Test the media plugin
|
||||
"""
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from unittest import TestCase
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from PyQt5 import QtCore
|
||||
|
||||
from openlp.core.common.registry import Registry
|
||||
from openlp.core.common.settings import Settings
|
||||
from openlp.plugins.media.lib.mediaitem import MediaMediaItem
|
||||
from tests.helpers.testmixin import TestMixin
|
||||
|
||||
|
||||
__default_settings__ = {
|
||||
'media/media auto start': QtCore.Qt.Unchecked,
|
||||
'media/media files': []
|
||||
}
|
||||
@pytest.fixture
|
||||
def media_item(settings):
|
||||
"""Local test setup"""
|
||||
mocked_main_window = MagicMock()
|
||||
Registry().register('service_list', MagicMock())
|
||||
Registry().register('main_window', mocked_main_window)
|
||||
Registry().register('live_controller', MagicMock())
|
||||
mocked_plugin = MagicMock()
|
||||
with patch('openlp.plugins.media.lib.mediaitem.MediaManagerItem._setup'), \
|
||||
patch('openlp.plugins.media.lib.mediaitem.MediaMediaItem.setup_item'):
|
||||
m_item = MediaMediaItem(None, mocked_plugin)
|
||||
m_item.settings_section = 'media'
|
||||
return m_item
|
||||
|
||||
|
||||
class MediaItemTest(TestCase, TestMixin):
|
||||
"""
|
||||
Test the media item for Media
|
||||
"""
|
||||
|
||||
def setUp(self):
|
||||
"""
|
||||
Set up the components need for all tests.
|
||||
"""
|
||||
with patch('openlp.plugins.media.lib.mediaitem.MediaManagerItem.__init__'),\
|
||||
patch('openlp.plugins.media.lib.mediaitem.MediaMediaItem.setup'):
|
||||
self.media_item = MediaMediaItem(None, MagicMock())
|
||||
self.media_item.settings_section = 'media'
|
||||
self.setup_application()
|
||||
self.build_settings()
|
||||
Registry.create()
|
||||
self.settings = Settings()
|
||||
Registry().register('settings', self.settings)
|
||||
|
||||
def tearDown(self):
|
||||
"""
|
||||
Clean up after the tests
|
||||
"""
|
||||
self.destroy_settings()
|
||||
|
||||
def test_search_found(self):
|
||||
def test_search_found(media_item):
|
||||
"""
|
||||
Media Remote Search Successful find
|
||||
"""
|
||||
# GIVEN: The Mediaitem set up a list of media
|
||||
self.settings.setValue(self.media_item.settings_section + '/media files', [Path('test.mp3'), Path('test.mp4')])
|
||||
media_item.settings.setValue(media_item.settings_section + '/media files', [Path('test.mp3'), Path('test.mp4')])
|
||||
# WHEN: Retrieving the test file
|
||||
result = self.media_item.search('test.mp4', False)
|
||||
result = media_item.search('test.mp4', False)
|
||||
# THEN: a file should be found
|
||||
assert result == [['test.mp4', 'test.mp4']], 'The result file contain the file name'
|
||||
|
||||
def test_search_not_found(self):
|
||||
|
||||
def test_search_not_found(media_item):
|
||||
"""
|
||||
Media Remote Search not find
|
||||
"""
|
||||
# GIVEN: The Mediaitem set up a list of media
|
||||
self.settings.setValue(self.media_item.settings_section + '/media files', [Path('test.mp3'), Path('test.mp4')])
|
||||
media_item.settings.setValue(media_item.settings_section + '/media files', [Path('test.mp3'), Path('test.mp4')])
|
||||
# WHEN: Retrieving the test file
|
||||
result = self.media_item.search('test.mpx', False)
|
||||
result = media_item.search('test.mpx', False)
|
||||
# THEN: a file should be found
|
||||
assert result == [], 'The result file should be empty'
|
||||
|
@ -21,27 +21,13 @@
|
||||
"""
|
||||
Test the media plugin
|
||||
"""
|
||||
from unittest import TestCase
|
||||
from unittest.mock import patch
|
||||
|
||||
from openlp.core.state import State
|
||||
from openlp.core.common.registry import Registry
|
||||
from openlp.core.common.settings import Settings
|
||||
from openlp.plugins.media.mediaplugin import MediaPlugin
|
||||
from tests.helpers.testmixin import TestMixin
|
||||
|
||||
|
||||
class TestMediaPlugin(TestCase, TestMixin):
|
||||
"""
|
||||
Test the media plugin
|
||||
"""
|
||||
def setUp(self):
|
||||
Registry.create()
|
||||
Registry().register('settings', Settings())
|
||||
State().load_settings()
|
||||
|
||||
@patch('openlp.plugins.media.mediaplugin.Plugin.initialise')
|
||||
def test_initialise(self, mocked_initialise):
|
||||
@patch('openlp.plugins.media.mediaplugin.Plugin.initialise')
|
||||
def test_initialise(mock_initialise, state, settings):
|
||||
"""
|
||||
Test that the initialise() method overwrites the built-in one, but still calls it
|
||||
"""
|
||||
@ -52,9 +38,10 @@ class TestMediaPlugin(TestCase, TestMixin):
|
||||
media_plugin.initialise()
|
||||
|
||||
# THEN: The the base initialise() method should be called
|
||||
mocked_initialise.assert_called_with()
|
||||
mock_initialise.assert_called_with()
|
||||
|
||||
def test_about_text(self):
|
||||
|
||||
def test_about_text():
|
||||
# GIVEN: The MediaPlugin
|
||||
# WHEN: Retrieving the about text
|
||||
# THEN: about() should return a string object
|
||||
|
@ -81,6 +81,7 @@ class TestPdfController(TestCase, TestMixin):
|
||||
"""
|
||||
Set up the components need for all tests.
|
||||
"""
|
||||
Registry().create()
|
||||
self.setup_application()
|
||||
self.build_settings()
|
||||
# Mocked out desktop object
|
||||
|
@ -91,6 +91,8 @@ class TestPowerpointDocument(TestCase, TestMixin):
|
||||
"""
|
||||
Set up the patches and mocks need for all tests.
|
||||
"""
|
||||
Registry.create()
|
||||
Registry().register('settings', Settings())
|
||||
self.setup_application()
|
||||
self.build_settings()
|
||||
self.mock_plugin = MagicMock()
|
||||
@ -111,8 +113,6 @@ class TestPowerpointDocument(TestCase, TestMixin):
|
||||
self.file_name = os.path.join(TEST_RESOURCES_PATH, 'presentations', 'test.pptx')
|
||||
self.real_controller = PowerpointController(self.mock_plugin)
|
||||
Settings().extend_default_settings(__default_settings__)
|
||||
Registry.create()
|
||||
Registry().register('settings', Settings())
|
||||
|
||||
def tearDown(self):
|
||||
"""
|
||||
|
@ -26,6 +26,8 @@ from pathlib import Path
|
||||
from unittest import TestCase
|
||||
from unittest.mock import MagicMock, call, patch
|
||||
|
||||
from openlp.core.common.registry import Registry
|
||||
from openlp.core.common.settings import Settings
|
||||
from openlp.plugins.presentations.lib.presentationcontroller import PresentationController, PresentationDocument
|
||||
|
||||
|
||||
@ -37,6 +39,8 @@ class TestPresentationController(TestCase):
|
||||
Test the PresentationController.
|
||||
"""
|
||||
def setUp(self):
|
||||
Registry().create()
|
||||
Registry().register('settings', Settings())
|
||||
self.get_thumbnail_folder_patcher = \
|
||||
patch('openlp.plugins.presentations.lib.presentationcontroller.PresentationDocument.get_thumbnail_folder',
|
||||
return_value=Path())
|
||||
@ -162,6 +166,7 @@ class TestPresentationDocument(TestCase):
|
||||
"""
|
||||
Set up the patches and mocks need for all tests.
|
||||
"""
|
||||
Registry().create()
|
||||
self.create_paths_patcher = \
|
||||
patch('openlp.plugins.presentations.lib.presentationcontroller.create_paths')
|
||||
self.get_thumbnail_folder_patcher = \
|
||||
|
@ -42,10 +42,11 @@ class TestEditSongForm(TestCase, TestMixin):
|
||||
Registry.create()
|
||||
Registry().register('service_list', MagicMock())
|
||||
Registry().register('main_window', MagicMock())
|
||||
with patch('openlp.plugins.songs.forms.editsongform.EditSongForm.__init__', return_value=None):
|
||||
self.edit_song_form = EditSongForm(None, MagicMock(), MagicMock())
|
||||
self.setup_application()
|
||||
self.build_settings()
|
||||
Registry().register('settings', self.setting)
|
||||
with patch('openlp.plugins.songs.forms.editsongform.EditSongForm.__init__', return_value=None):
|
||||
self.edit_song_form = EditSongForm(None, MagicMock(), MagicMock())
|
||||
QtCore.QLocale.setDefault(QtCore.QLocale('en_GB'))
|
||||
|
||||
def tearDown(self):
|
||||
|
@ -21,7 +21,7 @@
|
||||
"""
|
||||
This module contains tests for the SongShow Plus song importer.
|
||||
"""
|
||||
from unittest import TestCase
|
||||
from unittest import skip
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from openlp.plugins.songs.lib import VerseType
|
||||
@ -54,11 +54,7 @@ class TestSongShowPlusFileImport(SongImportTestHelper):
|
||||
self.load_external_result_data(TEST_PATH / 'cleanse-me.json'))
|
||||
|
||||
|
||||
class TestSongShowPlusImport(TestCase):
|
||||
"""
|
||||
Test the functions in the :mod:`songshowplusimport` module.
|
||||
"""
|
||||
def test_create_importer(self):
|
||||
def test_create_importer(registry):
|
||||
"""
|
||||
Test creating an instance of the SongShow Plus file importer
|
||||
"""
|
||||
@ -72,7 +68,8 @@ class TestSongShowPlusImport(TestCase):
|
||||
# THEN: The importer object should not be None
|
||||
assert importer is not None, 'Import should not be none'
|
||||
|
||||
def test_invalid_import_source(self):
|
||||
|
||||
def test_invalid_import_source(registry):
|
||||
"""
|
||||
Test SongShowPlusImport.do_import handles different invalid import_source values
|
||||
"""
|
||||
@ -93,7 +90,8 @@ class TestSongShowPlusImport(TestCase):
|
||||
assert mocked_import_wizard.progress_bar.setMaximum.called is False, \
|
||||
'setMaximum on import_wizard.progress_bar should not have been called'
|
||||
|
||||
def test_valid_import_source(self):
|
||||
|
||||
def test_valid_import_source(registry):
|
||||
"""
|
||||
Test SongShowPlusImport.do_import handles different invalid import_source values
|
||||
"""
|
||||
@ -114,7 +112,8 @@ class TestSongShowPlusImport(TestCase):
|
||||
'do_import should return None when import_source is a list and stop_import_flag is True'
|
||||
mocked_import_wizard.progress_bar.setMaximum.assert_called_with(len(importer.import_source))
|
||||
|
||||
def test_to_openlp_verse_tag(self):
|
||||
|
||||
def test_to_openlp_verse_tag_unique(registry):
|
||||
"""
|
||||
Test to_openlp_verse_tag method by simulating adding a verse
|
||||
"""
|
||||
@ -142,7 +141,9 @@ class TestSongShowPlusImport(TestCase):
|
||||
'SongShowPlusImport.to_openlp_verse_tag should return "%s" when called with "%s"' % \
|
||||
(openlp_tag, original_tag)
|
||||
|
||||
def test_to_openlp_verse_tag_verse_order(self):
|
||||
|
||||
@skip('Broken never worked')
|
||||
def test_to_openlp_verse_tag_verse_order(registry):
|
||||
"""
|
||||
Test to_openlp_verse_tag method by simulating adding a verse to the verse order
|
||||
"""
|
||||
|
@ -42,7 +42,7 @@ def test_about_text(state, mock_settings):
|
||||
|
||||
|
||||
@patch('openlp.plugins.songusage.songusageplugin.Manager')
|
||||
def test_song_usage_init(MockedManager, settings):
|
||||
def test_song_usage_init(MockedManager, settings, state):
|
||||
"""
|
||||
Test the initialisation of the SongUsagePlugin class
|
||||
"""
|
||||
@ -60,7 +60,7 @@ def test_song_usage_init(MockedManager, settings):
|
||||
|
||||
|
||||
@patch('openlp.plugins.songusage.songusageplugin.Manager')
|
||||
def test_check_pre_conditions(MockedManager, settings):
|
||||
def test_check_pre_conditions(MockedManager, settings, state):
|
||||
"""
|
||||
Test that check_pre_condition returns true for valid manager session
|
||||
"""
|
||||
@ -78,7 +78,7 @@ def test_check_pre_conditions(MockedManager, settings):
|
||||
|
||||
|
||||
@patch('openlp.plugins.songusage.songusageplugin.Manager')
|
||||
def test_toggle_song_usage_state(MockedManager, settings):
|
||||
def test_toggle_song_usage_state(MockedManager, settings, state):
|
||||
"""
|
||||
Test that toggle_song_usage_state does toggle song_usage_state
|
||||
"""
|
||||
|
@ -42,14 +42,14 @@ class SongImportTestHelper(TestCase):
|
||||
self.importer_module = __import__('openlp.plugins.songs.lib.importers.%s' %
|
||||
self.importer_module_name, fromlist=[self.importer_class_name])
|
||||
self.importer_class = getattr(self.importer_module, self.importer_class_name)
|
||||
Registry.create()
|
||||
Registry().register('settings', MagicMock())
|
||||
self.settings = Registry().get('settings')
|
||||
|
||||
def setUp(self):
|
||||
"""
|
||||
Patch and set up the mocks required.
|
||||
"""
|
||||
Registry.create()
|
||||
Registry().register('settings', MagicMock())
|
||||
self.settings = Registry().get('settings')
|
||||
self.add_copyright_patcher = patch('openlp.plugins.songs.lib.importers.%s.%s.add_copyright' %
|
||||
(self.importer_module_name, self.importer_class_name))
|
||||
self.add_verse_patcher = patch('openlp.plugins.songs.lib.importers.%s.%s.add_verse' %
|
||||
|
@ -21,10 +21,10 @@
|
||||
"""
|
||||
Package to test the openlp.core.ui.themeform package.
|
||||
"""
|
||||
# from pathlib import Path
|
||||
from unittest import TestCase
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from openlp.core.common.registry import Registry
|
||||
from openlp.core.ui.themeprogressform import ThemeProgressForm
|
||||
from tests.helpers.testmixin import TestMixin
|
||||
|
||||
@ -102,6 +102,9 @@ class TestThemeProgressForm(TestCase, TestMixin):
|
||||
def test_get_preview(self):
|
||||
"""Test that the get_preview() method returns a preview image"""
|
||||
# GIVEN: ThemeProgressForm object
|
||||
Registry.create()
|
||||
mocked_renderer = MagicMock()
|
||||
Registry().register('renderer', mocked_renderer)
|
||||
test_theme_name = 'Test Theme'
|
||||
test_theme_data = {'name': test_theme_name}
|
||||
form = self._get_theme_progress_form()
|
||||
|
Loading…
Reference in New Issue
Block a user