2013-09-19 21:02:28 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2016-12-31 11:05:48 +00:00
|
|
|
# Copyright (c) 2008-2017 OpenLP Developers #
|
2013-09-19 21:02:28 +00:00
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# 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 #
|
|
|
|
###############################################################################
|
2012-12-05 18:52:31 +00:00
|
|
|
"""
|
|
|
|
Functional tests to test the AppLocation class and related methods.
|
|
|
|
"""
|
2013-02-27 12:03:17 +00:00
|
|
|
import copy
|
2014-03-13 20:59:10 +00:00
|
|
|
import os
|
2012-12-05 18:52:31 +00:00
|
|
|
from unittest import TestCase
|
|
|
|
|
2013-10-13 17:02:12 +00:00
|
|
|
from openlp.core.common import AppLocation, get_frozen_path
|
2013-09-19 21:02:28 +00:00
|
|
|
from tests.functional import patch
|
2013-02-27 12:03:17 +00:00
|
|
|
|
2013-08-31 18:17:38 +00:00
|
|
|
FILE_LIST = ['file1', 'file2', 'file3.txt', 'file4.txt', 'file5.mp3', 'file6.mp3']
|
2013-02-27 12:03:17 +00:00
|
|
|
|
|
|
|
|
2012-12-05 18:52:31 +00:00
|
|
|
class TestAppLocation(TestCase):
|
|
|
|
"""
|
|
|
|
A test suite to test out various methods around the AppLocation class.
|
|
|
|
"""
|
2012-12-05 20:44:42 +00:00
|
|
|
def get_data_path_test(self):
|
|
|
|
"""
|
|
|
|
Test the AppLocation.get_data_path() method
|
|
|
|
"""
|
2013-10-14 05:01:01 +00:00
|
|
|
with patch('openlp.core.common.applocation.Settings') as mocked_class, \
|
2013-10-13 17:02:12 +00:00
|
|
|
patch('openlp.core.common.AppLocation.get_directory') as mocked_get_directory, \
|
|
|
|
patch('openlp.core.common.applocation.check_directory_exists') as mocked_check_directory_exists, \
|
|
|
|
patch('openlp.core.common.applocation.os') as mocked_os:
|
2012-12-05 20:44:42 +00:00
|
|
|
# GIVEN: A mocked out Settings class and a mocked out AppLocation.get_directory()
|
|
|
|
mocked_settings = mocked_class.return_value
|
|
|
|
mocked_settings.contains.return_value = False
|
2014-04-02 18:51:21 +00:00
|
|
|
mocked_get_directory.return_value = os.path.join('test', 'dir')
|
2012-12-05 20:44:42 +00:00
|
|
|
mocked_check_directory_exists.return_value = True
|
2014-04-02 18:51:21 +00:00
|
|
|
mocked_os.path.normpath.return_value = os.path.join('test', 'dir')
|
2013-04-05 19:37:15 +00:00
|
|
|
|
2012-12-05 20:44:42 +00:00
|
|
|
# WHEN: we call AppLocation.get_data_path()
|
|
|
|
data_path = AppLocation.get_data_path()
|
2013-04-05 19:37:15 +00:00
|
|
|
|
2012-12-05 20:44:42 +00:00
|
|
|
# THEN: check that all the correct methods were called, and the result is correct
|
2013-08-31 18:17:38 +00:00
|
|
|
mocked_settings.contains.assert_called_with('advanced/data path')
|
2012-12-05 20:44:42 +00:00
|
|
|
mocked_get_directory.assert_called_with(AppLocation.DataDir)
|
2014-04-02 18:51:21 +00:00
|
|
|
mocked_check_directory_exists.assert_called_with(os.path.join('test', 'dir'))
|
|
|
|
self.assertEqual(os.path.join('test', 'dir'), data_path, 'Result should be "test/dir"')
|
2012-12-05 20:44:42 +00:00
|
|
|
|
2012-12-05 18:52:31 +00:00
|
|
|
def get_data_path_with_custom_location_test(self):
|
|
|
|
"""
|
|
|
|
Test the AppLocation.get_data_path() method when a custom location is set in the settings
|
|
|
|
"""
|
2013-10-14 05:01:01 +00:00
|
|
|
with patch('openlp.core.common.applocation.Settings') as mocked_class,\
|
2013-10-13 17:02:12 +00:00
|
|
|
patch('openlp.core.common.applocation.os') as mocked_os:
|
2012-12-05 20:44:42 +00:00
|
|
|
# GIVEN: A mocked out Settings class which returns a custom data location
|
2012-12-05 18:52:31 +00:00
|
|
|
mocked_settings = mocked_class.return_value
|
|
|
|
mocked_settings.contains.return_value = True
|
2013-08-31 18:17:38 +00:00
|
|
|
mocked_settings.value.return_value.toString.return_value = 'custom/dir'
|
|
|
|
mocked_os.path.normpath.return_value = 'custom/dir'
|
2013-04-05 19:37:15 +00:00
|
|
|
|
2012-12-05 20:44:42 +00:00
|
|
|
# WHEN: we call AppLocation.get_data_path()
|
2012-12-05 18:52:31 +00:00
|
|
|
data_path = AppLocation.get_data_path()
|
2013-04-05 19:37:15 +00:00
|
|
|
|
2012-12-05 20:44:42 +00:00
|
|
|
# THEN: the mocked Settings methods were called and the value returned was our set up value
|
2013-08-31 18:17:38 +00:00
|
|
|
mocked_settings.contains.assert_called_with('advanced/data path')
|
|
|
|
mocked_settings.value.assert_called_with('advanced/data path')
|
2013-09-19 21:02:28 +00:00
|
|
|
self.assertEqual('custom/dir', data_path, 'Result should be "custom/dir"')
|
2012-12-06 10:55:12 +00:00
|
|
|
|
2013-02-27 12:03:17 +00:00
|
|
|
def get_files_no_section_no_extension_test(self):
|
|
|
|
"""
|
|
|
|
Test the AppLocation.get_files() method with no parameters passed.
|
|
|
|
"""
|
2013-10-13 17:02:12 +00:00
|
|
|
with patch('openlp.core.common.AppLocation.get_data_path') as mocked_get_data_path, \
|
|
|
|
patch('openlp.core.common.applocation.os.listdir') as mocked_listdir:
|
2013-02-27 12:03:17 +00:00
|
|
|
# GIVEN: Our mocked modules/methods.
|
2013-08-31 18:17:38 +00:00
|
|
|
mocked_get_data_path.return_value = 'test/dir'
|
2013-02-27 12:03:17 +00:00
|
|
|
mocked_listdir.return_value = copy.deepcopy(FILE_LIST)
|
|
|
|
|
|
|
|
# When: Get the list of files.
|
|
|
|
result = AppLocation.get_files()
|
|
|
|
|
2013-02-27 21:40:55 +00:00
|
|
|
# Then: check if the file lists are identical.
|
2013-09-19 21:02:28 +00:00
|
|
|
self.assertListEqual(FILE_LIST, result, 'The file lists should be identical.')
|
2013-02-27 12:03:17 +00:00
|
|
|
|
|
|
|
def get_files_test(self):
|
|
|
|
"""
|
|
|
|
Test the AppLocation.get_files() method with all parameters passed.
|
|
|
|
"""
|
2013-10-13 17:02:12 +00:00
|
|
|
with patch('openlp.core.common.AppLocation.get_data_path') as mocked_get_data_path, \
|
|
|
|
patch('openlp.core.common.applocation.os.listdir') as mocked_listdir:
|
2013-02-27 12:03:17 +00:00
|
|
|
# GIVEN: Our mocked modules/methods.
|
2014-04-02 18:51:21 +00:00
|
|
|
mocked_get_data_path.return_value = os.path.join('test', 'dir')
|
2013-02-27 12:03:17 +00:00
|
|
|
mocked_listdir.return_value = copy.deepcopy(FILE_LIST)
|
|
|
|
|
|
|
|
# When: Get the list of files.
|
2013-08-31 18:17:38 +00:00
|
|
|
result = AppLocation.get_files('section', '.mp3')
|
2013-03-01 16:18:50 +00:00
|
|
|
|
|
|
|
# Then: Check if the section parameter was used correctly.
|
2014-04-02 18:51:21 +00:00
|
|
|
mocked_listdir.assert_called_with(os.path.join('test', 'dir', 'section'))
|
2013-02-27 12:03:17 +00:00
|
|
|
|
2013-02-27 21:40:55 +00:00
|
|
|
# Then: check if the file lists are identical.
|
2013-09-19 21:02:28 +00:00
|
|
|
self.assertListEqual(['file5.mp3', 'file6.mp3'], result, 'The file lists should be identical.')
|
2013-02-27 12:03:17 +00:00
|
|
|
|
2012-12-06 10:55:12 +00:00
|
|
|
def get_section_data_path_test(self):
|
|
|
|
"""
|
|
|
|
Test the AppLocation.get_section_data_path() method
|
|
|
|
"""
|
2013-10-13 17:02:12 +00:00
|
|
|
with patch('openlp.core.common.AppLocation.get_data_path') as mocked_get_data_path, \
|
|
|
|
patch('openlp.core.common.applocation.check_directory_exists') as mocked_check_directory_exists:
|
2012-12-06 10:55:12 +00:00
|
|
|
# GIVEN: A mocked out AppLocation.get_data_path()
|
2014-04-02 18:51:21 +00:00
|
|
|
mocked_get_data_path.return_value = os.path.join('test', 'dir')
|
2012-12-06 10:55:12 +00:00
|
|
|
mocked_check_directory_exists.return_value = True
|
2013-04-05 19:37:15 +00:00
|
|
|
|
2012-12-06 10:55:12 +00:00
|
|
|
# WHEN: we call AppLocation.get_data_path()
|
2013-08-31 18:17:38 +00:00
|
|
|
data_path = AppLocation.get_section_data_path('section')
|
2013-04-05 19:37:15 +00:00
|
|
|
|
2012-12-06 10:55:12 +00:00
|
|
|
# THEN: check that all the correct methods were called, and the result is correct
|
2014-04-02 18:51:21 +00:00
|
|
|
mocked_check_directory_exists.assert_called_with(os.path.join('test', 'dir', 'section'))
|
|
|
|
self.assertEqual(os.path.join('test', 'dir', 'section'), data_path, 'Result should be "test/dir/section"')
|
2012-12-06 10:55:12 +00:00
|
|
|
|
|
|
|
def get_directory_for_app_dir_test(self):
|
|
|
|
"""
|
|
|
|
Test the AppLocation.get_directory() method for AppLocation.AppDir
|
|
|
|
"""
|
2013-09-19 21:02:28 +00:00
|
|
|
# GIVEN: A mocked out _get_frozen_path function
|
2013-10-13 17:02:12 +00:00
|
|
|
with patch('openlp.core.common.applocation.get_frozen_path') as mocked_get_frozen_path:
|
2014-04-02 18:51:21 +00:00
|
|
|
mocked_get_frozen_path.return_value = os.path.join('app', 'dir')
|
2013-04-05 19:37:15 +00:00
|
|
|
|
2012-12-06 10:55:12 +00:00
|
|
|
# WHEN: We call AppLocation.get_directory
|
|
|
|
directory = AppLocation.get_directory(AppLocation.AppDir)
|
2013-04-05 19:37:15 +00:00
|
|
|
|
2013-09-19 21:02:28 +00:00
|
|
|
# THEN: check that the correct directory is returned
|
2014-04-02 18:51:21 +00:00
|
|
|
self.assertEqual(os.path.join('app', 'dir'), directory, 'Directory should be "app/dir"')
|
2013-01-30 18:42:22 +00:00
|
|
|
|
2012-12-06 10:55:12 +00:00
|
|
|
def get_directory_for_plugins_dir_test(self):
|
|
|
|
"""
|
|
|
|
Test the AppLocation.get_directory() method for AppLocation.PluginsDir
|
|
|
|
"""
|
2013-09-19 21:02:28 +00:00
|
|
|
# GIVEN: _get_frozen_path, abspath, split and sys are mocked out
|
2013-10-13 17:02:12 +00:00
|
|
|
with patch('openlp.core.common.applocation.get_frozen_path') as mocked_get_frozen_path, \
|
|
|
|
patch('openlp.core.common.applocation.os.path.abspath') as mocked_abspath, \
|
|
|
|
patch('openlp.core.common.applocation.os.path.split') as mocked_split, \
|
|
|
|
patch('openlp.core.common.applocation.sys') as mocked_sys:
|
2014-07-11 13:35:44 +00:00
|
|
|
mocked_abspath.return_value = os.path.join('plugins', 'dir')
|
2013-08-31 18:17:38 +00:00
|
|
|
mocked_split.return_value = ['openlp']
|
2014-07-11 13:35:44 +00:00
|
|
|
mocked_get_frozen_path.return_value = os.path.join('plugins', 'dir')
|
2012-12-06 10:55:12 +00:00
|
|
|
mocked_sys.frozen = 1
|
2012-12-06 12:58:19 +00:00
|
|
|
mocked_sys.argv = ['openlp']
|
2013-04-05 19:37:15 +00:00
|
|
|
|
2012-12-06 10:55:12 +00:00
|
|
|
# WHEN: We call AppLocation.get_directory
|
|
|
|
directory = AppLocation.get_directory(AppLocation.PluginsDir)
|
2013-04-05 19:37:15 +00:00
|
|
|
|
2013-09-19 21:02:28 +00:00
|
|
|
# THEN: The correct directory should be returned
|
2014-07-11 13:35:44 +00:00
|
|
|
self.assertEqual(os.path.join('plugins', 'dir'), directory, 'Directory should be "plugins/dir"')
|
2013-10-13 17:02:12 +00:00
|
|
|
|
|
|
|
def get_frozen_path_in_unfrozen_app_test(self):
|
|
|
|
"""
|
|
|
|
Test the _get_frozen_path() function when the application is not frozen (compiled by PyInstaller)
|
|
|
|
"""
|
|
|
|
with patch('openlp.core.utils.sys') as mocked_sys:
|
|
|
|
# GIVEN: The sys module "without" a "frozen" attribute
|
|
|
|
mocked_sys.frozen = None
|
|
|
|
|
|
|
|
# WHEN: We call _get_frozen_path() with two parameters
|
|
|
|
frozen_path = get_frozen_path('frozen', 'not frozen')
|
|
|
|
|
|
|
|
# THEN: The non-frozen parameter is returned
|
|
|
|
self.assertEqual('not frozen', frozen_path, '_get_frozen_path should return "not frozen"')
|
|
|
|
|
|
|
|
def get_frozen_path_in_frozen_app_test(self):
|
|
|
|
"""
|
|
|
|
Test the get_frozen_path() function when the application is frozen (compiled by PyInstaller)
|
|
|
|
"""
|
|
|
|
with patch('openlp.core.common.sys') as mocked_sys:
|
|
|
|
# GIVEN: The sys module *with* a "frozen" attribute
|
|
|
|
mocked_sys.frozen = 1
|
|
|
|
|
|
|
|
# WHEN: We call _get_frozen_path() with two parameters
|
|
|
|
frozen_path = get_frozen_path('frozen', 'not frozen')
|
|
|
|
|
|
|
|
# THEN: The frozen parameter is returned
|
2013-10-14 05:01:01 +00:00
|
|
|
self.assertEqual('frozen', frozen_path, 'Should return "frozen"')
|