test_hpib.py 5.34 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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):
auke.klazema's avatar
auke.klazema committed
20
        hpib.PrologixGpibUsb()
21
22
23
24
25
26

        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"

auke.klazema's avatar
auke.klazema committed
27
        hpib.PrologixGpibUsb()
28
29
30
31

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

    def test_getDevices_returns_not_scanned_when_find_devices_is_false(self):
auke.klazema's avatar
auke.klazema committed
32
        usb = hpib.PrologixGpibUsb(findDevices=False)
33
34
35
36
37
38
39

        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):
auke.klazema's avatar
auke.klazema committed
40
            hpib.PrologixGpibUsb()
41
42
43
44
45

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

        with self.assertRaises(SerialException):
auke.klazema's avatar
auke.klazema committed
46
            hpib.PrologixGpibUsb()
47
48
49
50
51
52
53
54

        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):
auke.klazema's avatar
auke.klazema committed
55
            hpib.PrologixGpibUsb()
56
57
58
59
60
61

        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

auke.klazema's avatar
auke.klazema committed
62
        hpib.PrologixGpibUsb()
63
64
65
66
67
68
69
70
71
72
73
74
75

        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

auke.klazema's avatar
auke.klazema committed
76
        hpib.PrologixGpibUsb(findDevices=True)
77
78
79
80
81
82

        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"
auke.klazema's avatar
auke.klazema committed
83
        usb = hpib.PrologixGpibUsb(findDevices=True)
84
85
86
87
88
89
90
91
92
93
94

        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):
auke.klazema's avatar
auke.klazema committed
95
            hpib.PrologixGpibUsb()
96
97
98

    def test_if_given_port_does_not_start_with_dev_tty_an_exception_is_raised(self):
        with self.assertRaises(ValueError):
auke.klazema's avatar
auke.klazema committed
99
            hpib.PrologixGpibUsb(port="device")
100

101
102
103
104
105
106
107
108
109
110
111
    def test_command_prepend_plusplus_addr_if_new_address(self):
        address = 2
        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))

112
113
114
115
116
117
118
119
120
121
122
123
    def test_command_no_prepend_plusplus_addr_if_address_already_used(self):
        address = 2
        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))

124
125
126
127
128
129
130
131
132
133
134
135
136
    def test_query_prepend_plusplus_addr_if_new_address(self):
        address = 2
        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))

137
138
139
140
141
142
143
144
145
146
147
148
149
    def test_query_no_prepend_plusplus_addr_if_address_already_used(self):
        address = 2
        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))

150

151
152
if __name__ == '__main__':
    unittest.main()