Refactor mocks

This commit is contained in:
Ken Roberts 2017-11-24 11:08:23 -08:00
parent 2b9d2a994b
commit b650ef5730
3 changed files with 43 additions and 43 deletions

View File

@ -673,15 +673,15 @@ class ProjectorManager(QtWidgets.QWidget, RegistryBase, UiProjectorManager, LogM
count = 1
for item in projector.link.lamp:
if item['On'] is None:
onoff = translate('OpenLP.ProjectorManager', 'Unavailable')
status = translate('OpenLP.ProjectorManager', 'Unavailable')
elif item['On']:
onoff = translate('OpenLP.ProjectorManager', 'ON')
status = translate('OpenLP.ProjectorManager', 'ON')
else:
onoff = translate('OpenLP.ProjectorManager', 'OFF')
status = translate('OpenLP.ProjectorManager', 'OFF')
message += '<b>{title} {count}</b> {status} '.format(title=translate('OpenLP.ProjectorManager',
'Lamp'),
count=count,
status=onoff)
status=status)
message += '<b>{title}</b>: {hours}<br />'.format(title=translate('OpenLP.ProjectorManager', 'Hours'),
hours=item['Hours'])

View File

@ -402,20 +402,20 @@ class PJLinkCommands(object):
:param data: Lamp(s) status.
"""
lamps = []
data_dict = data.split()
if len(data_dict) < 2:
lamps.append({'Hours': int(data_dict[0]), 'On': None})
lamp_list = data.split()
if len(lamp_list) < 2:
lamps.append({'Hours': int(lamp_list[0]), 'On': None})
else:
while data_dict:
while lamp_list:
try:
fill = {'Hours': int(data_dict[0]), 'On': False if data_dict[1] == '0' else True}
fill = {'Hours': int(lamp_list[0]), 'On': False if lamp_list[1] == '0' else True}
except ValueError:
# In case of invalid entry
log.warning('({ip}) process_lamp(): Invalid data "{data}"'.format(ip=self.ip, data=data))
return
lamps.append(fill)
data_dict.pop(0) # Remove lamp hours
data_dict.pop(0) # Remove lamp on/off
lamp_list.pop(0) # Remove lamp hours
lamp_list.pop(0) # Remove lamp on/off
self.lamp = lamps
return

View File

@ -30,19 +30,29 @@ from openlp.core.projectors.pjlink import PJLink
from tests.resources.projector.data import TEST_PIN, TEST_CONNECT_AUTHENTICATE, TEST_HASH, TEST1_DATA
pjlink_test = PJLink(Projector(**TEST1_DATA), no_poll=True)
class TestPJLinkBugs(TestCase):
"""
Tests for the PJLink module bugfixes
"""
def setUp(self):
'''
Initialization
'''
self.pjlink_test = PJLink(Projector(**TEST1_DATA), no_poll=True)
def tearDown(self):
'''
Cleanups
'''
self.pjlink_test = None
def test_bug_1550891_process_clss_nonstandard_reply_1(self):
"""
Bugfix 1550891: CLSS request returns non-standard reply with Optoma/Viewsonic projector
"""
# GIVEN: Test object
pjlink = pjlink_test
pjlink = self.pjlink_test
# WHEN: Process non-standard reply
pjlink.process_clss('Class 1')
@ -56,7 +66,7 @@ class TestPJLinkBugs(TestCase):
Bugfix 1550891: CLSS request returns non-standard reply with BenQ projector
"""
# GIVEN: Test object
pjlink = pjlink_test
pjlink = self.pjlink_test
# WHEN: Process non-standard reply
pjlink.process_clss('Version2')
@ -66,22 +76,17 @@ class TestPJLinkBugs(TestCase):
self.assertEqual(pjlink.pjlink_class, '2',
'Non-standard class reply should have set class=2')
@patch.object(pjlink_test, 'send_command')
@patch.object(pjlink_test, 'waitForReadyRead')
@patch.object(pjlink_test, 'projectorAuthentication')
@patch.object(pjlink_test, 'timer')
@patch.object(pjlink_test, 'socket_timer')
def test_bug_1593882_no_pin_authenticated_connection(self,
mock_socket_timer,
mock_timer,
mock_authentication,
mock_ready_read,
mock_send_command):
def test_bug_1593882_no_pin_authenticated_connection(self):
"""
Test bug 1593882 no pin and authenticated request exception
"""
# GIVEN: Test object and mocks
pjlink = pjlink_test
mock_socket_timer = patch.object(self.pjlink_test, 'socket_timer').start()
mock_timer = patch.object(self.pjlink_test, 'timer').start()
mock_authentication = patch.object(self.pjlink_test, 'projectorAuthentication').start()
mock_ready_read = patch.object(self.pjlink_test, 'waitForReadyRead').start()
mock_send_command = patch.object(self.pjlink_test, 'send_command').start()
pjlink = self.pjlink_test
pjlink.pin = None
mock_ready_read.return_value = True
@ -91,22 +96,17 @@ class TestPJLinkBugs(TestCase):
# THEN: 'No Authentication' signal should have been sent
mock_authentication.emit.assert_called_with(pjlink.ip)
@patch.object(pjlink_test, 'waitForReadyRead')
@patch.object(pjlink_test, 'state')
@patch.object(pjlink_test, '_send_command')
@patch.object(pjlink_test, 'timer')
@patch.object(pjlink_test, 'socket_timer')
def test_bug_1593883_pjlink_authentication(self,
mock_socket_timer,
mock_timer,
mock_send_command,
mock_state,
mock_waitForReadyRead):
def test_bug_1593883_pjlink_authentication(self):
"""
Test bugfix 1593883 pjlink authentication
"""
# GIVEN: Test object and data
pjlink = pjlink_test
mock_socket_timer = patch.object(self.pjlink_test, 'socket_timer').start()
mock_timer = patch.object(self.pjlink_test, 'timer').start()
mock_send_command = patch.object(self.pjlink_test, 'write').start()
mock_state = patch.object(self.pjlink_test, 'state').start()
mock_waitForReadyRead = patch.object(self.pjlink_test, 'waitForReadyRead').start()
pjlink = self.pjlink_test
pjlink.pin = TEST_PIN
mock_state.return_value = pjlink.ConnectedState
mock_waitForReadyRead.return_value = True
@ -116,14 +116,14 @@ class TestPJLinkBugs(TestCase):
# THEN: send_command should have the proper authentication
self.assertEqual("{test}".format(test=mock_send_command.call_args),
"call(data='{hash}%1CLSS ?\\r')".format(hash=TEST_HASH))
"call(b'{hash}%1CLSS ?\\r')".format(hash=TEST_HASH))
def test_bug_1734275_pjlink_nonstandard_lamp(self):
def test_bug_1734275_process_lamp_nonstandard_reply(self):
"""
Test bugfix 17342785 non-standard LAMP response
"""
# GIVEN: Test object
pjlink = pjlink_test
pjlink = self.pjlink_test
# WHEN: Process lamp command called with only hours and no lamp power state
pjlink.process_lamp("45")
@ -131,4 +131,4 @@ class TestPJLinkBugs(TestCase):
# THEN: Lamp should show hours as 45 and lamp power as Unavailable
self.assertEqual(len(pjlink.lamp), 1, 'There should only be 1 lamp available')
self.assertEqual(pjlink.lamp[0]['Hours'], 45, 'Lamp hours should have equalled 45')
self.assertIsNone(pjlink.lamp[0]['On'], 'Lamp power should be "Unavailable"')
self.assertIsNone(pjlink.lamp[0]['On'], 'Lamp power should be "None"')