def test_magics_missing_ns_key(RE, hw): ip = FakeIPython({}) sm = BlueskyMagics(ip) with pytest.raises(NameError): sm.mov('motor1 5') ip.user_ns['motor1'] = hw.motor1 sm.mov('motor1 5')
def test_bluesky_magics(pln, line, magic, fresh_RE): RE = fresh_RE # Build a FakeIPython instance to use the magics with. dets = [det] ip = FakeIPython({'motor1': motor1, 'motor2': motor2, 'dets': dets}) sm = BlueskyMagics(ip) sm.detectors = default_dets # Spy on all msgs processed by RE. msgs = [] def collect(msg): msgs.append(msg) RE.msg_hook = collect sm.RE.msg_hook = collect # Test magics cause the RunEngine to execute the messages we expect. RE(bp.mv(motor1, 10, motor2, 10)) # ensure known initial state RE(pln) expected = msgs.copy() msgs.clear() RE(bp.mv(motor1, 10, motor2, 10)) # ensure known initial state getattr(sm, magic)(line) actual = msgs.copy() msgs.clear() compare_msgs(actual, expected)
def test_bluesky_magics(pln, plnargs, line, magic, RE, hw): # Build a FakeIPython instance to use the magics with. dets = [hw.invariant1] ip = FakeIPython({'motor1': hw.motor1, 'motor2': hw.motor2, 'dets': dets}) sm = BlueskyMagics(ip) sm.detectors = [hw.invariant1, hw.invariant2] # Spy on all msgs processed by RE. msgs = [] def collect(msg): msgs.append(msg) RE.msg_hook = collect sm.RE.msg_hook = collect # Test magics cause the RunEngine to execute the messages we expect. RE(bps.mv(hw.motor1, 10, hw.motor2, 10)) # ensure known initial state RE(pln(*plnargs(hw))) expected = msgs.copy() msgs.clear() RE(bps.mv(hw.motor1, 10, hw.motor2, 10)) # ensure known initial state getattr(sm, magic)(line) actual = msgs.copy() msgs.clear() compare_msgs(actual, expected)
def test_wa(hw): motor = hw.motor ip = FakeIPython({'motor': motor}) sm = BlueskyMagics(ip) # Test an empty list. sm.wa('') sm.positioners.extend([motor]) sm.wa('') # Make motor support more attributes. motor.limits = (-1, 1) sm.wa('') motor.user_offset = SimpleNamespace(get=lambda: 0) sm.wa('[motor]')
def test_interrupted(motor_det): motor, det = motor_det motor._fake_sleep = 10 ip = FakeIPython({}) sm = BlueskyMagics(ip) ip.user_ns['motor'] = motor pid = os.getpid() def sim_kill(n=1): for j in range(n): print('KILL') os.kill(pid, signal.SIGINT) motor.loop = sm.RE.loop sm.RE.loop.call_later(1, sim_kill, 2) sm.mov('motor 1') assert sm.RE.state == 'idle'
def test_wa(): motor = Mover( 'motor', OrderedDict([('motor', lambda x: x), ('motor_setpoint', lambda x: x)]), {'x': 0}) ip = FakeIPython({'motor': motor}) sm = BlueskyMagics(ip) # Test an empty list. sm.wa('') sm.positioners.extend([motor]) sm.wa('') # Make motor support more attributes. motor.limits = (-1, 1) sm.wa('') motor.user_offset = SimpleNamespace(get=lambda: 0) sm.wa('[motor]')
def test_interrupted(RE, hw): motor = hw.motor motor.delay = 10 ip = FakeIPython({}) sm = BlueskyMagics(ip) ip.user_ns['motor'] = motor pid = os.getpid() def sim_kill(n=1): for j in range(n): print('KILL') os.kill(pid, signal.SIGINT) motor.loop = sm.RE.loop sm.RE.loop.call_later(1, sim_kill, 2) sm.mov('motor 1') assert sm.RE.state == 'idle'
def test_magics_missing_ns_key(fresh_RE): RE = fresh_RE ip = FakeIPython({}) sm = BlueskyMagics(ip) with pytest.raises(NameError): sm.mov('motor1 5') ip.user_ns['motor1'] = motor1 sm.mov('motor1 5')
def test_bluesky_magics(pln, plnargs, magic, line, detectors_factory, RE, hw): # Build a FakeIPython instance to use the magics with. dets = [hw.invariant1] hw.invariant1._ophyd_labels_ = set(['detectors', 'favorite_detectors']) hw.invariant2._ophyd_labels_ = set(['detectors']) ip = FakeIPython({ 'motor1': hw.motor1, 'motor2': hw.motor2, 'invariant1': hw.invariant1, 'invariant2': hw.invariant2, 'dets': dets }) sm = BlueskyMagics(ip) detectors = detectors_factory(hw) if detectors: # Test deprecated usage of %ct. with pytest.warns(UserWarning): BlueskyMagics.detectors = detectors # Spy on all msgs processed by RE. msgs = [] def collect(msg): msgs.append(msg) RE.msg_hook = collect BlueskyMagics.RE.msg_hook = collect # Test magics cause the RunEngine to execute the messages we expect. RE(bps.mv(hw.motor1, 10, hw.motor2, 10)) # ensure known initial state msgs.clear() RE(pln(*plnargs(hw))) expected = msgs.copy() RE(bps.mv(hw.motor1, 10, hw.motor2, 10)) # ensure known initial state msgs.clear() if detectors: # Test deprecated usage of %ct. Must catch warning. with pytest.warns(UserWarning): getattr(sm, magic)(line) else: # Normal usage, no warning. getattr(sm, magic)(line) actual = msgs.copy() msgs.clear() compare_msgs(actual, expected) if detectors: with pytest.warns(UserWarning): BlueskyMagics.detectors.clear()
def test_wa_legacy(hw): motor = hw.motor ip = FakeIPython({'motor': motor}) sm = BlueskyMagics(ip) BlueskyMagics.positioners.extend([motor]) with pytest.warns(UserWarning): sm.wa('') # Make motor support more attributes. motor.limits = (-1, 1) with pytest.warns(UserWarning): sm.wa('') motor.user_offset = SimpleNamespace(get=lambda: 0) with pytest.warns(UserWarning): sm.wa('[motor]') with pytest.warns(UserWarning): BlueskyMagics.positioners.clear()
def test_wa(hw): motor = hw.motor det = hw.motor ip = FakeIPython({'motor': motor, 'det': det}) sm = BlueskyMagics(ip) # Test an empty list with no labels set. sm.wa('') # Test again with labeled objects. motor._ophyd_labels_ = ['motors'] motor._ophyd_labels_ = ['detectors'] # Test an empty list. sm.wa('') # Test with a label whitelist sm.wa('motors') sm.wa('motors detectors') sm.wa('motors typo') with pytest.raises(ValueError): sm.wa('[motors, detectors]')