forked from lightwave-lab/lightlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_driverMetaclassing.py
108 lines (83 loc) · 3.52 KB
/
test_driverMetaclassing.py
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
101
102
103
104
105
106
107
108
''' Driver metaclassing allows two powerful features to connect
a ``VISAInstrumentDriver`` to an ``Instrument`` with the attribute: ``instrument_category``.
1. ``Instrument`` is initialized and returned by calling a ``VISAInstrumentDriver`` class,
as if you were initializing that ``VISAInstrumentDriver`
* Enables better Instrument/Driver init argument handling
* Better separation of Instrument-level/Driver-level attribute access
* Unambiguous pickling of these two levels
2. Enforces that ``VISAInstrumentDriver`` satisfies
the interface of its corresponding ``Instrument``.
'''
import pytest
from mock import patch
from lightlab.equipment.visa_bases import VISAInstrumentDriver, IncompleteClass
from lightlab.equipment.lab_instruments import HP_8152A_PM
from lightlab.laboratory.instruments import PowerMeter
def test_bad_driver():
# Catches incomplete API at class time
with pytest.raises(IncompleteClass):
class Delinquent_PM(VISAInstrumentDriver):
instrument_category = PowerMeter
def powerLin(self): pass
# def powerDbm(self): pass
def test_driver_init():
''' Initialization can occur via Instrument or VISAInstrumentDriver.
Both will return an Instrument
'''
# Old style still works
pm_old = PowerMeter(_driver_class=HP_8152A_PM, name='a PM',
address='NULL', extra='foolio', tempSess=False)
# New style (recommended)
pm_new = HP_8152A_PM(name='a PM', address='NULL', extra='foolio', tempSess=False)
for pm in [pm_old, pm_new]:
assert pm.__class__ == PowerMeter # not SourceMeter or HP_8152A_PM
assert pm._driver_class == HP_8152A_PM
assert pm.extra == 'foolio'
# Old style fails to initialize the driver with correct options
with pytest.raises(AssertionError):
assert pm_old.driver.tempSess is False
assert pm.driver.tempSess is False
# `tempSess` is a driver-level attribute, not seen at Instrument
with pytest.raises(AttributeError):
pm.tempSess
# `extra` is not in the driver's namespace, so it was kept in Instrument
with pytest.raises(AttributeError):
pm.driver.extra
def test_change_of_address():
''' Change of address should kill the connection and change driver's address
'''
pm = HP_8152A_PM(name='a PM', address='NULL', tempSess=False)
def open_fake(self):
self.mbSession = 'opened'
def close_fake(self):
self._close_called = True
with patch.object(HP_8152A_PM, 'open', open_fake), \
patch.object(HP_8152A_PM, 'close', close_fake):
driver = pm.driver
driver.open()
assert driver.mbSession == 'opened'
pm.address = 'new_address'
assert pm.driver is driver
assert driver._close_called is True
assert pm.driver.address == pm.address
from lightlab.laboratory.instruments import Oscilloscope
def test_optionals():
class Driver1(VISAInstrumentDriver):
instrument_category = Oscilloscope
def acquire(self): pass
def run(self): pass
def wfmDb(self): pass
def notInInterface(self): pass
class Driver2(Driver1):
def histogramStats(self): pass
d1 = Driver1()
d2 = Driver2()
d1.acquire()
with pytest.raises(AttributeError):
d1.histogramStats()
d2.histogramStats()
with pytest.raises(AttributeError):
d1.notInInterface()
d1.driver.notInInterface()
assert 'histogramStats' not in dir(d1)
assert 'histogramStats' in dir(d2)