Fix some bugs in the songsusage plugin:

* Bug 1532193
 * Bug 1661416

Add this to your merge proposal:
--------------------------------
lp:~raoul-snyman/openlp/fix-songusage-2.4 (revision 2671)
[SUCCESS] https://ci.openlp.io/job/Branch-01-Pull/1899/
[SUCCESS] https://ci.openlp.io/job/Branch-02-Functional-Tests/1810/
[SUCCESS] https://ci.openlp.io/job/Branch-03-Interface-Tests/1749/
[SUCCESS] https://ci.openlp.io/job/Branch-04a-Windows_Functional_Tests/1485/
[SUCCESS] https://ci.openlp.io/jo...

bzr-revno: 2670
This commit is contained in:
raoul@snyman.info 2017-02-03 09:57:54 -07:00 committed by Raoul Snyman
commit ea8aa7588c
4 changed files with 138 additions and 6 deletions

View File

@ -22,6 +22,7 @@
import logging import logging
import os import os
from datetime import datetime
from PyQt5 import QtCore, QtWidgets from PyQt5 import QtCore, QtWidgets
from sqlalchemy.sql import and_ from sqlalchemy.sql import and_
@ -52,8 +53,16 @@ class SongUsageDetailForm(QtWidgets.QDialog, Ui_SongUsageDetailDialog, RegistryP
""" """
We need to set up the screen We need to set up the screen
""" """
self.from_date_calendar.setSelectedDate(Settings().value(self.plugin.settings_section + '/from date')) from_date = Settings().value(self.plugin.settings_section + '/from date')
self.to_date_calendar.setSelectedDate(Settings().value(self.plugin.settings_section + '/to 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')) self.file_line_edit.setText(Settings().value(self.plugin.settings_section + '/last directory export'))
def define_output_location(self): def define_output_location(self):

View File

@ -44,9 +44,9 @@ if QtCore.QDate().currentDate().month() < 9:
__default_settings__ = { __default_settings__ = {
'songusage/db type': 'sqlite', 'songusage/db type': 'sqlite',
'songusage/db username': '', 'songusage/db username': '',
'songuasge/db password': '', 'songusage/db password': '',
'songuasge/db hostname': '', 'songusage/db hostname': '',
'songuasge/db database': '', 'songusage/db database': '',
'songusage/active': False, 'songusage/active': False,
'songusage/to date': QtCore.QDate(YEAR, 8, 31), 'songusage/to date': QtCore.QDate(YEAR, 8, 31),
'songusage/from date': QtCore.QDate(YEAR - 1, 9, 1), 'songusage/from date': QtCore.QDate(YEAR - 1, 9, 1),

View File

@ -28,7 +28,7 @@ from unittest.mock import MagicMock, patch
from openlp.core import Registry from openlp.core import Registry
from openlp.plugins.songusage.lib import upgrade from openlp.plugins.songusage.lib import upgrade
from openlp.plugins.songusage.lib.db import init_schema 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): class TestSongUsage(TestCase):
@ -81,3 +81,27 @@ class TestSongUsage(TestCase):
# THEN: It should return True # THEN: It should return True
self.assertTrue(ret) 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)

View File

@ -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()