Commit 1898dda3 authored by Tammo Jan Dijkema's avatar Tammo Jan Dijkema
Browse files

Fix test_hpib

parent cd8ce260
......@@ -2,151 +2,61 @@ import hpib
import unittest
from mock import patch, call
from serial import SerialException
import serial.threaded
class PrologixGpibUsbTest(unittest.TestCase):
class TestGPIBProtocol(unittest.TestCase):
def setUp(self):
serial_patcher = patch('hpib.serial.Serial')
self.serial_mock = serial_patcher.start()
self.addCleanup(serial_patcher.stop)
platform_pathcer = patch('hpib.platform')
self.platform_mock = platform_pathcer.start()
self.addCleanup(platform_pathcer.stop)
self.command_thread = serial.threaded.ReaderThread(self.serial_mock, hpib.GPIBProtocol)
self.command_thread.start()
self.command_thread._connection_made.wait()
self.platform_mock.system.return_value = "Linux"
self.protocol = self.command_thread.protocol
def test_when_platform_linux_port_gets_set_to_ttyusb0(self):
hpib.PrologixGpibUsb()
def test_init_gpibprotocol(self):
self.command_thread.protocol.init_hpib()
self.serial_mock.assert_called_with('/dev/ttyUSB0', timeout=1)
call_list = self.serial_mock.write.call_args_list
def test_when_platform_darwin_port_gets_set_to_ttys000(self):
self.platform_mock.system.return_value = "Darwin"
self.assertEqual(call_list[0], call(b"++savecfg 0\n"))
self.assertEqual(call_list[1], call(b"++auto 0\n"))
self.assertEqual(call_list[2], call(b"++eoi 1\n"))
self.assertEqual(call_list[3], call(b"++eos 2\n"))
self.assertEqual(call_list[4], call(b"++eot_enable 0\n"))
self.assertEqual(call_list[5], call(b"++eot_char 0\n"))
self.assertEqual(call_list[6], call(b"++read_tmo_ms 500\n"))
hpib.PrologixGpibUsb()
def test_command_with_address(self):
self.command_thread.protocol.command("freq 1420 Hz", address=28)
self.serial_mock.assert_called_with('/dev/ttys000', timeout=1)
call_list = self.serial_mock.write.call_args_list
def test_getDevices_returns_not_scanned_when_find_devices_is_false(self):
usb = hpib.PrologixGpibUsb(find_devices=False)
self.assertEqual(call_list[0], call(b"++addr 28\n"))
self.assertEqual(call_list[1], call(b"freq 1420 Hz\n"))
self.assertEqual(usb.get_devices(), 'Not scanned.')
def test_command_query_no_response(self):
response = self.command_thread.protocol.command("freq?")
def test_raises_serial_exception_when_all_serial_ports_fail(self):
self.serial_mock.side_effect = SerialException
call_list = self.serial_mock.write.call_args_list
self.assertEqual(call_list[0], call(b"freq?\n"))
self.assertEqual(call_list[1], call(b"++read eoi\n"))
with self.assertRaises(SerialException):
hpib.PrologixGpibUsb()
self.assertEqual(response, None)
def test_when_platform_linux_last_port_tried_is_ttyusb98(self):
self.serial_mock.side_effect = SerialException
def test_command_query_with_response(self):
# Put the response in the queue before asking for it
self.command_thread.protocol.responses.put("1420000000")
response = self.command_thread.protocol.command("freq?")
with self.assertRaises(SerialException):
hpib.PrologixGpibUsb()
self.serial_mock.assert_called_with('/dev/ttyUSB98', timeout=1)
def test_when_platform_darwin_last_port_tried_is_ttyusb98(self):
self.platform_mock.system.return_value = "Darwin"
self.serial_mock.side_effect = SerialException
with self.assertRaises(SerialException):
hpib.PrologixGpibUsb()
self.serial_mock.assert_called_with('/dev/ttys998', timeout=1)
def test_setup_device_when_port_found(self):
write_mock = self.serial_mock.return_value.write
hpib.PrologixGpibUsb()
self.assertEqual(write_mock.call_args_list[0], call("++savecfg 0" + chr(10)))
self.assertEqual(write_mock.call_args_list[1], call("++auto 0" + chr(10)))
self.assertEqual(write_mock.call_args_list[2], call("++eoi 1" + chr(10)))
self.assertEqual(write_mock.call_args_list[3], call("++eos 2" + chr(10)))
self.assertEqual(write_mock.call_args_list[4], call("++eot_enable 0" + chr(10)))
self.assertEqual(write_mock.call_args_list[5], call("++eot_char 0" + chr(10)))
self.assertEqual(write_mock.call_args_list[6], call("++read_tmo_ms 500" + chr(10)))
def test_setup_calls_identify_command_when_finding_devices(self):
self.serial_mock.return_value.readline.return_value = ""
write_mock = self.serial_mock.return_value.write
hpib.PrologixGpibUsb(find_devices=True)
self.assertEqual(write_mock.call_args_list[7],
call("++addr " + str(0) + chr(10) + "*idn?" + chr(10) + "++read" + chr(10)))
def test_if_find_devices_getDevices_will_return_a_list_of_devices(self):
self.serial_mock.return_value.readline.return_value = "Device 1"
usb = hpib.PrologixGpibUsb(find_devices=True)
devices = usb.get_devices()
self.assertEqual(len(devices), 2 * 31)
self.assertEqual(devices[0], 0)
self.assertEqual(devices[1], "Device 1")
def test_if_platform_windows_and_not_port_defined_raises_not_implemented_exception(self):
self.platform_mock.system.return_value = "Windows"
with self.assertRaises(NotImplementedError):
hpib.PrologixGpibUsb()
def test_if_given_port_does_not_start_with_dev_tty_an_exception_is_raised(self):
with self.assertRaises(ValueError):
hpib.PrologixGpibUsb(port="device")
def test_command_prepend_plusplus_addr_if_new_address(self):
address = 0
command = "command"
write_mock = self.serial_mock.return_value.write
usb = hpib.PrologixGpibUsb()
usb.command(address, command)
write_mock.assert_called_with("++addr " + str(address) + chr(10) + command + chr(10))
def test_command_no_prepend_plusplus_addr_if_address_already_used(self):
address = 0
command = "command"
write_mock = self.serial_mock.return_value.write
usb = hpib.PrologixGpibUsb()
usb.command(address, command)
usb.command(address, command)
write_mock.assert_called_with(command + chr(10))
def test_query_prepend_plusplus_addr_if_new_address(self):
address = 0
query = "query"
write_mock = self.serial_mock.return_value.write
usb = hpib.PrologixGpibUsb()
usb.query(address, query)
write_mock.assert_called_with("++addr " + str(address) + chr(10) +
query + chr(10) +
"++read eoi" + chr(10))
def test_query_no_prepend_plusplus_addr_if_address_already_used(self):
address = 0
query = "query"
write_mock = self.serial_mock.return_value.write
usb = hpib.PrologixGpibUsb()
usb.query(address, query)
usb.query(address, query)
write_mock.assert_called_with(query + chr(10) +
"++read eoi" + chr(10))
call_list = self.serial_mock.write.call_args_list
self.assertEqual(call_list[0], call(b"freq?\n"))
self.assertEqual(call_list[1], call(b"++read eoi\n"))
self.assertEqual(response, "1420000000")
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment