Tidy up some tests

This commit is contained in:
Raoul Snyman 2013-12-18 23:54:56 +02:00
parent 06d08f61a6
commit 553c93e790
2 changed files with 18 additions and 17 deletions

View File

@ -11,9 +11,9 @@ import sys
from PyQt4 import QtGui
if sys.version_info[1] >= 3:
from unittest.mock import patch, MagicMock
from unittest.mock import MagicMock, patch, mock_open
else:
from mock import patch, MagicMock
from mock import MagicMock, patch, mock_open
# Only one QApplication can be created. Use QtGui.QApplication.instance() when you need to "create" a QApplication.
application = QtGui.QApplication([])

View File

@ -37,8 +37,7 @@ from PyQt4 import QtGui
from openlp.core.common import Settings
from openlp.plugins.remotes.lib.httpserver import HttpRouter
from tests.functional import MagicMock, patch
from mock import mock_open
from tests.functional import MagicMock, patch, mock_open
__default_settings__ = {
'remotes/twelve hour': True,
@ -53,6 +52,7 @@ __default_settings__ = {
TEST_PATH = os.path.abspath(os.path.dirname(__file__))
class TestRouter(TestCase):
"""
Test the functions in the :mod:`lib` module.
@ -109,10 +109,8 @@ class TestRouter(TestCase):
function, args = router.process_http_request('/stage/api/poll', None)
# THEN: the function should have been called only once
assert function['function'] == mocked_function, \
'The mocked function should match defined value.'
assert function['secure'] == False, \
'The mocked function should not require any security.'
self.assertEqual(mocked_function, function['function'], 'The mocked function should match defined value.')
self.assertFalse(function['secure'], 'The mocked function should not require any security.')
def get_content_type_test(self):
"""
@ -125,9 +123,11 @@ class TestRouter(TestCase):
['test.png', 'image/png'], ['test.whatever', 'text/plain'],
['test', 'text/plain'], ['', 'text/plain'],
[os.path.join(TEST_PATH, 'test.html'), 'text/html']]
# WHEN: calling each file type
for header in headers:
ext, content_type = self.router.get_content_type(header[0])
# THEN: all types should match
self.assertEqual(content_type, header[1], 'Mismatch of content type')
@ -142,13 +142,14 @@ class TestRouter(TestCase):
self.router.wfile = MagicMock()
self.router.html_dir = os.path.normpath('test/dir')
self.router.template_vars = MagicMock()
# WHEN: call serve_file with no file_name
self.router.serve_file()
# THEN: it should return a 404
self.router.send_response.assert_called_once_with(404)
self.router.send_header.assert_called_once_with('Content-type','text/html')
self.assertEqual(self.router.end_headers.call_count, 1,
'end_headers called once')
self.assertEqual(self.router.end_headers.call_count, 1, 'end_headers called once')
def serve_file_with_valid_params_test(self):
"""
@ -164,11 +165,11 @@ class TestRouter(TestCase):
with patch('openlp.core.lib.os.path.exists') as mocked_exists, \
patch('builtins.open', mock_open(read_data='123')):
mocked_exists.return_value = True
# WHEN: call serve_file with an existing html file
self.router.serve_file(os.path.normpath('test/dir/test.html'))
# THEN: it should return a 200 and the file
self.router.send_response.assert_called_once_with(200)
self.router.send_header.assert_called_once_with(
'Content-type','text/html')
self.assertEqual(self.router.end_headers.call_count, 1,
'end_headers called once')
self.router.send_header.assert_called_once_with('Content-type', 'text/html')
self.assertEqual(self.router.end_headers.call_count, 1, 'end_headers called once')