def test_all(RE, hw): c = CallbackCounter() RE(stepscan(hw.det, hw.motor), {'all': c}) assert c.value == 10 + 1 + 2 # events, descriptor, start and stop c = CallbackCounter() token = RE.subscribe(c) RE(stepscan(hw.det, hw.motor)) RE.unsubscribe(token) assert c.value == 10 + 1 + 2
def test_all(): c = CallbackCounter() RE(stepscan(det, motor), subs={'all': c}) assert c.value == 10 + 1 + 2 # events, descriptor, start and stop c = CallbackCounter() token = RE.subscribe('all', c) RE(stepscan(det, motor)) RE.unsubscribe(token) assert c.value == 10 + 1 + 2
def test_all(RE, hw): c = CallbackCounter() RE(stepscan(hw.det, hw.motor), {"all": c}) assert c.value == 10 + 1 + 2 # events, descriptor, start and stop c = CallbackCounter() token = RE.subscribe(c) RE(stepscan(hw.det, hw.motor)) RE.unsubscribe(token) assert c.value == 10 + 1 + 2
def test_raising_ignored_or_not(RE, hw): RE.ignore_callback_exceptions = True assert RE.ignore_callback_exceptions def cb(name, doc): raise Exception # by default (with ignore... = True) it warns with pytest.warns(UserWarning): RE(stepscan(hw.det, hw.motor), cb) RE.ignore_callback_exceptions = False with pytest.raises(Exception): RE(stepscan(hw.det, hw.motor), cb)
def test_raising_ignored_or_not(fresh_RE): RE = fresh_RE RE.ignore_callback_exceptions = True assert RE.ignore_callback_exceptions def cb(name, doc): raise Exception # by default (with ignore... = True) it warns with pytest.warns(UserWarning): RE(stepscan(det, motor), subs=cb) RE.ignore_callback_exceptions = False with pytest.raises(Exception): RE(stepscan(det, motor), subs={'all': cb})
def test_scan_and_get_data(RE, hw, db): RE.subscribe(db.insert) uid, = RE(stepscan(hw.det, hw.motor), group='foo', beamline_id='testing', config={}) hdr = db[uid] list(hdr.events())
def test_live_plotter(): if skip_mpl: raise nose.SkipTest("matplotlib is not available") my_plotter = LivePlot('det', 'motor') assert_equal(RE.state, 'idle') RE(stepscan(det, motor), subs={'all': my_plotter}) assert_equal(RE.state, 'idle')
def test_post_run(): try: import dataportal except ImportError: raise SkipTest('requires dataportal') from bluesky.broker_callbacks import post_run RE(stepscan(det, motor), subs=post_run(LiveTable()))
def test_raising_ignored_or_not(): assert_true(RE.ignore_callback_exceptions) def cb(name, doc): raise Exception RE(stepscan(det, motor), subs=cb) RE.ignore_callback_exceptions = False _raising_callbacks_helper('all', cb)
def test_average_stream(RE, hw): # Create callback chain avg = AverageStream(10) c = CallbackCounter() d = DocCollector() avg.subscribe(c) avg.subscribe(d.insert) # Run a basic plan RE(stepscan(hw.det, hw.motor), {'all': avg}) assert c.value == 1 + 1 + 2 # events, descriptor, start and stop # See that we made sensible descriptor start_uid = d.start[0]['uid'] assert start_uid in d.descriptor desc_uid = d.descriptor[start_uid][0]['uid'] assert desc_uid in d.event evt = d.event[desc_uid][0] assert evt['seq_num'] == 1 assert all([ key in d.descriptor[start_uid][0]['data_keys'] for key in evt['data'].keys() ]) # See that we returned the correct average assert evt['data']['motor'] == -0.5 # mean of range(-5, 5) assert evt['data']['motor_setpoint'] == -0.5 # mean of range(-5, 5) assert start_uid in d.stop assert d.stop[start_uid]['num_events'] == {'primary': 1}
def test_main_thread_callback_exceptions(): RE(stepscan(det, motor), subs={'start': exception_raiser, 'stop': exception_raiser, 'event': exception_raiser, 'descriptor': exception_raiser, 'all': exception_raiser}, beamline_id='testing', owner='tester')
def test_scan_and_get_data(fresh_RE, db): RE = fresh_RE RE.subscribe('all', db.mds.insert) uid, = RE(stepscan(det, motor), group='foo', beamline_id='testing', config={}) hdr = db[uid] db.fetch_events(hdr)
def test_main_thread_callback_exceptions(): RE(stepscan(det, motor), subs={ 'start': exception_raiser, 'stop': exception_raiser, 'event': exception_raiser, 'descriptor': exception_raiser, 'all': exception_raiser }, beamline_id='testing', owner='tester')
def test_raising_ignored_or_not(): RE.ignore_callback_exceptions = True assert RE.ignore_callback_exceptions def cb(name, doc): raise Exception # by default (with ignore... = True) it warns with pytest.warns(UserWarning): RE(stepscan(det, motor), subs=cb) RE.ignore_callback_exceptions = False _raising_callbacks_helper('all', cb)
def test_scan_and_get_data(RE, hw, db): RE.subscribe(db.insert) rs = RE(stepscan(hw.det, hw.motor), group='foo', beamline_id='testing', config={}) if RE.call_returns_result: uid = rs.run_start_uids[0] else: uid = rs[0] hdr = db[uid] list(hdr.events())
def test_post_run(RE, hw, db): RE.subscribe(db.insert) output = defaultdict(list) def do_nothing(doctype, doc): output[doctype].append(doc) RE(stepscan(hw.det, hw.motor), {'stop': [post_run(do_nothing, db=db)]}) assert len(output) assert len(output['start']) == 1 assert len(output['stop']) == 1 assert len(output['descriptor']) == 1 assert len(output['event']) == 10
def test_subscribe_msg(RE, hw): assert RE.state == 'idle' c = CallbackCounter() def counting_stepscan(det, motor): yield Msg('subscribe', None, c, 'start') yield from stepscan(det, motor) RE(counting_stepscan(hw.det, hw.motor)) # should advance c assert c.value == 1 RE(counting_stepscan(hw.det, hw.motor)) # should advance c assert c.value == 2 RE(stepscan(hw.det, hw.motor)) # should not assert c.value == 2
def test_subscribe_msg(): assert RE.state == 'idle' c = CallbackCounter() def counting_stepscan(det, motor): yield Msg('subscribe', None, 'start', c) yield from stepscan(det, motor) RE(counting_stepscan(det, motor)) # should advance c assert c.value == 1 RE(counting_stepscan(det, motor)) # should advance c assert c.value == 2 RE(stepscan(det, motor)) # should not assert c.value == 2
def test_subscribe_msg(): assert RE.state == 'idle' c = CallbackCounter() def counting_stepscan(det, motor): yield Msg('subscribe', None, 'start', c) yield from stepscan(det, motor) RE(counting_stepscan(det, motor)) # should advance c assert_equal(c.value, 1) RE(counting_stepscan(det, motor)) # should advance c assert_equal(c.value, 2) RE(stepscan(det, motor)) # should not assert_equal(c.value, 2)
def test_subscribe_msg(RE, hw): assert RE.state == "idle" c = CallbackCounter() def counting_stepscan(det, motor): yield Msg("subscribe", None, c, "start") yield from stepscan(det, motor) RE(counting_stepscan(hw.det, hw.motor)) # should advance c assert c.value == 1 RE(counting_stepscan(hw.det, hw.motor)) # should advance c assert c.value == 2 RE(stepscan(hw.det, hw.motor)) # should not assert c.value == 2
def test_scan_and_get_data(): try: import metadatastore except ImportError: raise SkipTest try: from dataportal import DataBroker as db except ImportError: raise SkipTest from bluesky.standard_config import gs uid = gs.RE(stepscan(det, motor), group='foo', beamline_id='testing', config={}) hdr = db[uid] db.fetch_events(hdr)
def test_post_run(fresh_RE, db): RE = fresh_RE RE.subscribe('all', db.mds.insert) output = defaultdict(list) def do_nothing(doctype, doc): output[doctype].append(doc) RE.ignore_callback_exceptions = False RE(stepscan(det, motor), subs={'stop': [post_run(do_nothing, db=db)]}) assert len(output) assert len(output['start']) == 1 assert len(output['stop']) == 1 assert len(output['descriptor']) == 1 assert len(output['event']) == 10
def test_live_plotter(RE, hw): RE.ignore_callback_exceptions = False try: import matplotlib.pyplot as plt del plt except ImportError as ie: pytest.skip("Skipping live plot test because matplotlib is not installed." "Error was: {}".format(ie)) my_plotter = LivePlot('det', 'motor') assert RE.state == 'idle' RE(stepscan(hw.det, hw.motor), {'all': my_plotter}) assert RE.state == 'idle' xlen = len(my_plotter.x_data) assert xlen > 0 ylen = len(my_plotter.y_data) assert xlen == ylen RE.ignore_callback_exceptions = True
def test_scan_and_get_data(): try: import metadatastore del metadatastore except ImportError: raise SkipTest try: from databroker import DataBroker as db except ImportError: raise SkipTest from bluesky.standard_config import gs uid = gs.RE(stepscan(det, motor), group='foo', beamline_id='testing', config={}) hdr = db[uid] db.fetch_events(hdr)
def test_post_run(): try: import databroker del databroker except ImportError: raise SkipTest('requires databroker') from bluesky.standard_config import gs from bluesky.broker_callbacks import post_run output = defaultdict(list) def do_nothing(doctype, doc): output[doctype].append(doc) gs.RE.ignore_callback_exceptions = False gs.RE(stepscan(det, motor), subs={'stop': [post_run(do_nothing)]}) assert len(output) assert_equal(len(output['start']), 1) assert_equal(len(output['stop']), 1) assert_equal(len(output['descriptor']), 1) assert_equal(len(output['event']), 10)
def test_straight_through_stream(RE, hw): # Just a stream that sinks the events it receives ss = NegativeStream() # Create callback chain c = CallbackCounter() d = DocCollector() ss.subscribe(c) ss.subscribe(d.insert) # Run a basic plan RE(stepscan(hw.det, hw.motor), {'all': ss}) # Check that our metadata is there assert c.value == 10 + 1 + 2 # events, descriptor, start and stop assert d.start[0]['stream_level'] == 'boring' desc = d.descriptor[d.start[0]['uid']][0] events = d.event[desc['uid']] print(desc) print([evt['data'] for evt in events]) assert all([ evt['data'][key] <= 0 for evt in events for key in evt['data'].keys() ]) assert all([key in desc['data_keys'] for key in events[0]['data'].keys()])
def test_straight_through_stream(RE, hw): # Just a stream that sinks the events it receives ss = NegativeStream() # Create callback chain c = CallbackCounter() d = DocCollector() ss.subscribe(c) ss.subscribe(d.insert) # Run a basic plan RE(stepscan(hw.det, hw.motor), {'all': ss}) # Check that our metadata is there assert c.value == 10 + 1 + 2 # events, descriptor, start and stop assert d.start[0]['stream_level'] == 'boring' desc = d.descriptor[d.start[0]['uid']][0] events = d.event[desc['uid']] print(desc) print([evt['data'] for evt in events]) assert all([evt['data'][key] <= 0 for evt in events for key in evt['data'].keys()]) assert all([key in desc['data_keys'] for key in events[0]['data'].keys()])
def test_average_stream(RE, hw): # Create callback chain avg = AverageStream(10) c = CallbackCounter() d = DocCollector() avg.subscribe(c) avg.subscribe(d.insert) # Run a basic plan RE(stepscan(hw.det, hw.motor), {'all': avg}) assert c.value == 1 + 1 + 2 # events, descriptor, start and stop # See that we made sensible descriptor start_uid = d.start[0]['uid'] assert start_uid in d.descriptor desc_uid = d.descriptor[start_uid][0]['uid'] assert desc_uid in d.event evt = d.event[desc_uid][0] assert evt['seq_num'] == 1 assert all([key in d.descriptor[start_uid][0]['data_keys'] for key in evt['data'].keys()]) # See that we returned the correct average assert evt['data']['motor'] == -0.5 # mean of range(-5, 5) assert evt['data']['motor_setpoint'] == -0.5 # mean of range(-5, 5) assert start_uid in d.stop assert d.stop[start_uid]['num_events'] == {'primary': 1}
def test_verify_files_saved(fresh_RE, db): RE = fresh_RE RE.subscribe('all', db.mds.insert) vfs = partial(verify_files_saved, db=db) RE(stepscan(det, motor), subs={'stop': vfs})
def test_verify_files_saved(fresh_RE, db): RE = fresh_RE RE.subscribe(db.insert) vfs = partial(verify_files_saved, db=db) RE(stepscan(det, motor), subs={'stop': vfs})
def test_scan_and_get_data(): uid = gs.RE(stepscan(det, motor), group='foo', beamline_id='testing', config={}) hdr = db[uid] db.fetch_events(hdr)
def counting_stepscan(det, motor): yield Msg('subscribe', None, c, 'start') yield from stepscan(det, motor)
def counting_stepscan(det, motor): yield Msg("subscribe", None, c, "start") yield from stepscan(det, motor)
def counting_stepscan(det, motor): yield Msg('subscribe', None, 'start', c) yield from stepscan(det, motor)
def _raising_callbacks_helper(stream_name, callback): RE(stepscan(det, motor), subs={stream_name: callback})
def _raising_callbacks_helper(stream_name, callback): with pytest.raises(Exception): RE(stepscan(det, motor), subs={stream_name: callback})
def test_verify_files_saved(RE, hw, db): RE.subscribe(db.insert) vfs = partial(verify_files_saved, db=db) RE(stepscan(hw.det, hw.motor), {'stop': vfs})