Add tests

This commit is contained in:
Tim Bentley 2016-12-22 17:02:39 +00:00
parent 126c5462a0
commit fe3014fc66
7 changed files with 197 additions and 26 deletions

View File

@ -23,13 +23,17 @@
import os
import zipfile
file_name = "/home/tim/Projects/OpenLP/openlp/remoteweb/deploy.zip"
def deploy_zipfile(zip_file, app_root):
"""
Process the downloaded zip file and add to the correct directory
def deploy_zipfile(file_name):
:param zip_file: the zip file to be processed
:param app_root: the directory where the zip get expanded to
app_root = "/home/tim/.openlp/data/remotes/"
web_zip = zipfile.ZipFile(file_name)
:return: None
"""
web_zip = zipfile.ZipFile(zip_file)
for web_file in web_zip.namelist():
(dir_name, filename) = os.path.split(web_file)
real_path = os.path.join(app_root, web_file[4:])
@ -37,14 +41,20 @@ def deploy_zipfile(file_name):
if not os.path.exists(real_path):
os.makedirs(real_path)
else:
file_web = str(web_zip.read(web_file))
file_web = web_zip.read(web_file)
out_file = open(real_path, 'w')
out_file.write(file_web)
# extract the file from the zip. If an image then the exception will be used.
try:
out_file.write(file_web.decode("utf-8"))
except UnicodeDecodeError as ude:
out_file.close()
out_file = open(real_path, 'wb')
out_file.write(file_web)
out_file.close()
web_zip.close()
def check_for_previous_deployment(create=False):
app_root = "/home/tim/.openlp/data/remotes/"
def check_for_previous_deployment(app_root, create=False):
marker_file = os.path.join(app_root, "marker.txt")
if os.path.isfile(marker_file):
return True
@ -52,24 +62,12 @@ def check_for_previous_deployment(create=False):
if create:
os.mknod(marker_file)
return False
a=1
deploy_zipfile(file_name)
#print(check_for_previous_deployment())
#print(check_for_previous_deployment(True))
#print(check_for_previous_deployment())
#file_name = "/home/tim/Projects/OpenLP/openlp/remoteweb/deploy.zip"
#app_root = "/home/tim/.openlp/data/remotes/"
# xml_file = [name for name in theme_zip.namelist() if os.path.splitext(name)[1].lower() == '.xml']
# if len(xml_file) != 1:
# self.log_error('Theme contains "{val:d}" XML files'.format(val=len(xml_file)))
# raise ValidationError
# xml_tree = ElementTree(element=XML(theme_zip.read(xml_file[0]))).getroot()
# theme_version = xml_tree.get('version', default=None)
# if not theme_version or float(theme_version) < 2.0:
# self.log_error('Theme version is less than 2.0')
# raise ValidationError
# theme_name = xml_tree.find('name').text.strip()
# theme_folder = os.path.join(directory, theme_name)
# theme_exists = os.path.exists(theme_folder)
#deploy_zipfile(file_name)
#print(check_for_previous_deployment(app_root))
#print(check_for_previous_deployment(app_root, True))
#print(check_for_previous_deployment(app_root))

View File

@ -0,0 +1,49 @@
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2016 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 #
###############################################################################
"""
Functional tests to test the Http Server Class.
"""
from unittest import TestCase
from openlp.core.api.http.server import HttpServer
from tests.functional import patch
class TestHttpServer(TestCase):
"""
A test suite to test starting the http server
"""
@patch('openlp.core.api.http.server.HttpWorker')
@patch('openlp.core.api.http.server.QtCore.QThread')
def test_serverstart(self, mock_qthread, mock_thread):
"""
Test the starting of the Waitress Server
"""
# GIVEN: A new httpserver
# WHEN: I start the server
server = HttpServer()
# THEN: the api environment should have been created
self.assertEquals(1, mock_qthread.call_count, 'The qthread should have been called once')
self.assertEquals(1, mock_thread.call_count, 'The http thread should have been called once')

View File

@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2016 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 #
###############################################################################

View File

@ -0,0 +1,103 @@
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2016 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 #
###############################################################################
import os
import shutil
from tempfile import mkdtemp
from unittest import TestCase
from openlp.core.api.http.server import HttpServer
from openlp.plugins.remotes.deploy import check_for_previous_deployment
from tests.functional import patch
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'resources'))
class TestDeploy(TestCase):
"""
Test the Remote deploy functions
"""
def setUp(self):
"""
Setup for tests
"""
self.app_root = mkdtemp()
def tearDown(self):
"""
Clean up after tests
"""
shutil.rmtree(self.app_root)
@patch('openlp.plugins.remotes.deploy.os.path.isfile')
@patch('openlp.plugins.remotes.deploy.os.mknod')
def test_check_for_previous_deployment_false(self, mocked_mknod, mocked_isfile):
"""
Remote Deploy tests - Test when the marker file is missing
"""
# GIVEN: A new setup with not market file
# WHEN: I check for a deployment which does not create the marker file
mocked_isfile.return_value = False
processed = check_for_previous_deployment(self.app_root)
# THEN test the see if marker is present and has not been created
self.assertFalse(processed, "should return False as marker does not exist")
mocked_isfile.assert_called_once_with(os.path.join(self.app_root, "marker.txt"))
mocked_mknod.assert_not_called()
@patch('openlp.plugins.remotes.deploy.os.path.isfile')
@patch('openlp.plugins.remotes.deploy.os.mknod')
def test_check_for_previous_deployment_true(self, mocked_mknod, mocked_isfile):
"""
Remote Deploy tests - Test when the marker file is missing
"""
# GIVEN: A new setup with not market file
# WHEN: I check for a deployment which does not create the marker file
mocked_isfile.return_value = False
processed = check_for_previous_deployment(self.app_root, True)
# THEN test the see if marker is present and has not been created
marker_file = os.path.join(self.app_root, "marker.txt")
self.assertFalse(processed, "should return False as marker does not exist")
mocked_isfile.assert_called_once_with(marker_file)
mocked_mknod.assert_called_once_with(marker_file)
@patch('openlp.plugins.remotes.deploy.os.path.isfile')
@patch('openlp.plugins.remotes.deploy.os.mknod')
def test_check_for_previous_deployment_true(self, mocked_mknod, mocked_isfile):
"""
Remote Deploy tests - Test when the marker file is missing
"""
# GIVEN: A new setup with not market file
# WHEN: I check for a deployment which does not create the marker file
mocked_isfile.return_value = True
processed = check_for_previous_deployment(self.app_root, True)
# THEN test the see if marker is present and has not been created
marker_file = os.path.join(self.app_root, "marker.txt")
self.assertTrue(processed, "should return True as marker does exist")
mocked_isfile.assert_called_once_with(marker_file)
mocked_mknod.assert_not_called()

Binary file not shown.