test_hpib.py 3.86 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import hpib
import unittest
from mock import patch, call
from serial import SerialException


class PrologixGpibUsbTest(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.platform_mock.system.return_value = "Linux"

    def test_when_platform_linux_port_gets_set_to_ttyusb0(self):
        hpib.PROLOGIX_GPIB_USB()

        self.serial_mock.assert_called_with('/dev/ttyUSB0', timeout=1)

    def test_when_platform_darwin_port_gets_set_to_ttys000(self):
        self.platform_mock.system.return_value = "Darwin"

        hpib.PROLOGIX_GPIB_USB()

        self.serial_mock.assert_called_with('/dev/ttys000', timeout=1)

    def test_getDevices_returns_not_scanned_when_find_devices_is_false(self):
        usb = hpib.PROLOGIX_GPIB_USB(findDevices=False)

        self.assertEqual(usb.getDevices(), 'Not scanned.')

    def test_raises_serial_exception_when_all_serial_ports_fail(self):
        self.serial_mock.side_effect = SerialException

        with self.assertRaises(SerialException):
            hpib.PROLOGIX_GPIB_USB()

    def test_when_platform_linux_last_port_tried_is_ttyusb98(self):
        self.serial_mock.side_effect = SerialException

        with self.assertRaises(SerialException):
            hpib.PROLOGIX_GPIB_USB()

        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.PROLOGIX_GPIB_USB()

        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.PROLOGIX_GPIB_USB()

        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.PROLOGIX_GPIB_USB(findDevices=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.PROLOGIX_GPIB_USB(findDevices=True)

        devices = usb.getDevices()

        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.PROLOGIX_GPIB_USB()

    def test_if_given_port_does_not_start_with_dev_tty_an_exception_is_raised(self):
        with self.assertRaises(ValueError):
            hpib.PROLOGIX_GPIB_USB(port="device")

101

102
103
if __name__ == '__main__':
    unittest.main()