diff --git a/openlp/plugins/songusage/forms/songusagedetailform.py b/openlp/plugins/songusage/forms/songusagedetailform.py index 07e8469ba..94f86d529 100644 --- a/openlp/plugins/songusage/forms/songusagedetailform.py +++ b/openlp/plugins/songusage/forms/songusagedetailform.py @@ -22,6 +22,7 @@ import logging import os +from datetime import datetime from PyQt5 import QtCore, QtWidgets from sqlalchemy.sql import and_ @@ -52,8 +53,16 @@ class SongUsageDetailForm(QtWidgets.QDialog, Ui_SongUsageDetailDialog, RegistryP """ We need to set up the screen """ - self.from_date_calendar.setSelectedDate(Settings().value(self.plugin.settings_section + '/from date')) - self.to_date_calendar.setSelectedDate(Settings().value(self.plugin.settings_section + '/to date')) + from_date = Settings().value(self.plugin.settings_section + '/from date') + if from_date: + self.from_date_calendar.setSelectedDate(from_date) + else: + self.from_date_calendar.setSelectedDate(datetime.today().replace(day=1)) + to_date = Settings().value(self.plugin.settings_section + '/to date') + if to_date: + self.to_date_calendar.setSelectedDate(to_date) + else: + self.to_date_calendar.setSelectedDate(datetime.today()) self.file_line_edit.setText(Settings().value(self.plugin.settings_section + '/last directory export')) def define_output_location(self): diff --git a/openlp/plugins/songusage/songusageplugin.py b/openlp/plugins/songusage/songusageplugin.py index 5915e063b..62b9cf97b 100644 --- a/openlp/plugins/songusage/songusageplugin.py +++ b/openlp/plugins/songusage/songusageplugin.py @@ -44,9 +44,9 @@ if QtCore.QDate().currentDate().month() < 9: __default_settings__ = { 'songusage/db type': 'sqlite', 'songusage/db username': '', - 'songuasge/db password': '', - 'songuasge/db hostname': '', - 'songuasge/db database': '', + 'songusage/db password': '', + 'songusage/db hostname': '', + 'songusage/db database': '', 'songusage/active': False, 'songusage/to date': QtCore.QDate(YEAR, 8, 31), 'songusage/from date': QtCore.QDate(YEAR - 1, 9, 1), diff --git a/tests/functional/openlp_plugins/songusage/test_songusage.py b/tests/functional/openlp_plugins/songusage/test_songusage.py index d2fe66872..889f82284 100644 --- a/tests/functional/openlp_plugins/songusage/test_songusage.py +++ b/tests/functional/openlp_plugins/songusage/test_songusage.py @@ -28,7 +28,7 @@ from unittest.mock import MagicMock, patch from openlp.core import Registry from openlp.plugins.songusage.lib import upgrade from openlp.plugins.songusage.lib.db import init_schema -from openlp.plugins.songusage.songusageplugin import SongUsagePlugin +from openlp.plugins.songusage.songusageplugin import SongUsagePlugin, __default_settings__ class TestSongUsage(TestCase): @@ -81,3 +81,27 @@ class TestSongUsage(TestCase): # THEN: It should return True self.assertTrue(ret) + + def test_default_settings(self): + """ + Test that all the default settings are correct + """ + # GIVEN: A list of default settings + expected_defaults = sorted([ + 'songusage/db type', + 'songusage/db username', + 'songusage/db password', + 'songusage/db hostname', + 'songusage/db database', + 'songusage/active', + 'songusage/to date', + 'songusage/from date', + 'songusage/last directory', + 'songusage/last directory export', + 'songusage/status' + ]) + + # WHEN: The plugin is initialised + # THEN: The defaults should be correct + for e_key, a_key in zip(expected_defaults, sorted(__default_settings__.keys())): + assert e_key == a_key, '{} != {}'.format(e_key, a_key) diff --git a/tests/interfaces/openlp_plugins/songusage/test_songusagedetailform.py b/tests/interfaces/openlp_plugins/songusage/test_songusagedetailform.py new file mode 100644 index 000000000..f9495bbd8 --- /dev/null +++ b/tests/interfaces/openlp_plugins/songusage/test_songusagedetailform.py @@ -0,0 +1,99 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2017 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; version 2 of the License. # +# # +# 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, write to the Free Software Foundation, Inc., 59 # +# Temple Place, Suite 330, Boston, MA 02111-1307 USA # +############################################################################### +""" +Package to test the openlp.plugins.songusage.forms.songusagedetailform package. +""" +from unittest import TestCase +from unittest.mock import MagicMock, patch + +from PyQt5 import QtCore, QtWidgets + +from openlp.core.common import Registry +from openlp.plugins.songusage.forms.songusagedetailform import SongUsageDetailForm + +from tests.helpers.testmixin import TestMixin + + +class TestSongUsageDetailForm(TestCase, TestMixin): + """ + Test the SongUsageDetailForm class + """ + + def setUp(self): + """ + Create the UI + """ + Registry.create() + self.setup_application() + self.main_window = QtWidgets.QMainWindow() + self.mocked_plugin = MagicMock() + Registry().register('main_window', self.main_window) + self.form = SongUsageDetailForm(self.mocked_plugin, self.main_window) + + def tearDown(self): + """ + Delete all the C++ objects at the end so that we don't have a segfault + """ + del self.form + del self.main_window + + @patch('openlp.plugins.songusage.forms.songusagedetailform.Settings') + def test_initalise_without_settings(self, MockedSettings): + """ + Test the initialise() method when there are no settings + """ + # GIVEN: A song usage detail form and a mocked settings object + mocked_settings = MagicMock() + mocked_settings.value.side_effect = ['', None, ''] + MockedSettings.return_value = mocked_settings + + # WHEN: initialise() is called + self.form.initialise() + + # THEN: The dates on the calendar should be this month + today = QtCore.QDate.currentDate() + month_start = QtCore.QDate.currentDate().addDays(1 - today.day()) + assert self.form.from_date_calendar.selectedDate() == month_start, \ + self.form.from_date_calendar.selectedDate() + assert self.form.to_date_calendar.selectedDate() == today, \ + self.form.to_date_calendar.selectedDate() + + @patch('openlp.plugins.songusage.forms.songusagedetailform.Settings') + def test_initalise_with_settings(self, MockedSettings): + """ + Test the initialise() method when there are existing settings + """ + # GIVEN: A song usage detail form and a mocked settings object + to_date = QtCore.QDate.currentDate().addDays(-1) + from_date = QtCore.QDate.currentDate().addDays(2 - to_date.day()) + mocked_settings = MagicMock() + mocked_settings.value.side_effect = [from_date, to_date, ''] + MockedSettings.return_value = mocked_settings + + # WHEN: initialise() is called + self.form.initialise() + + # THEN: The dates on the calendar should be this month + assert self.form.from_date_calendar.selectedDate() == from_date, \ + self.form.from_date_calendar.selectedDate() + assert self.form.to_date_calendar.selectedDate() == to_date, \ + self.form.to_date_calendar.selectedDate()