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

View File

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