def test_snapshot(): station = Station() empty_snapshot = station.snapshot() assert { 'instruments': {}, 'parameters': {}, 'components': {}, 'config': None, } == empty_snapshot instrument = DummyInstrument('instrument', gates=['one']) station.add_component(instrument) instrument_snapshot = instrument.snapshot() parameter = Parameter('parameter', label='Label', unit='m') station.add_component(parameter) parameter_snapshot = parameter.snapshot() excluded_parameter = Parameter('excluded_parameter', snapshot_exclude=True) station.add_component(excluded_parameter) component = DumyPar('component') component.metadata['smth'] = 'in the way she moves' station.add_component(component) component_snapshot = component.snapshot() snapshot = station.snapshot() assert isinstance(snapshot, dict) assert [ 'instruments', 'parameters', 'components', 'config', ] == list(snapshot.keys()) assert ['instrument'] == list(snapshot['instruments'].keys()) assert instrument_snapshot == snapshot['instruments']['instrument'] # the list should not contain the excluded parameter assert ['parameter'] == list(snapshot['parameters'].keys()) assert parameter_snapshot == snapshot['parameters']['parameter'] assert ['component'] == list(snapshot['components'].keys()) assert component_snapshot == snapshot['components']['component']
def test_snapshot(): station = Station() empty_snapshot = station.snapshot() assert {'instruments': {}, 'parameters': {}, 'components': {}, 'config': None, 'default_measurement': [] } == empty_snapshot instrument = DummyInstrument('instrument', gates=['one']) station.add_component(instrument) instrument_snapshot = instrument.snapshot() parameter = Parameter('parameter', label='Label', unit='m') station.add_component(parameter) parameter_snapshot = parameter.snapshot() component = DumyPar('component') component.metadata['smth'] = 'in the way she moves' station.add_component(component) component_snapshot = component.snapshot() snapshot = station.snapshot() assert isinstance(snapshot, dict) assert ['instruments', 'parameters', 'components', 'config', 'default_measurement' ] == list(snapshot.keys()) assert ['instrument'] == list(snapshot['instruments'].keys()) assert instrument_snapshot == snapshot['instruments']['instrument'] assert ['parameter'] == list(snapshot['parameters'].keys()) assert parameter_snapshot == snapshot['parameters']['parameter'] assert ['component'] == list(snapshot['components'].keys()) assert component_snapshot == snapshot['components']['component'] assert [] == snapshot['default_measurement']
def test_station_after_instrument_is_closed(): """ Test that station is aware of the fact that its components could be removed within the lifetime of the station. Here we instantiate an instrument, add it to a station, then close the instrument, and then perform an action on the station to ensure that the closed instrument does not break the work of the station object. """ bob = DummyInstrument('bob', gates=['one']) station = Station(bob) assert bob == station['bob'] bob.close() # 'bob' is closed, but it is still part of the station assert bob == station['bob'] # check that snapshot method executes without exceptions snapshot = station.snapshot() # check that 'bob's snapshot is not here (because 'bob' is closed, # hence it was ignored, and even removed from the station by # `snapshot_base` method) assert { 'instruments': {}, 'parameters': {}, 'components': {}, 'config': None, 'default_measurement': [] } == snapshot # check that 'bob' has been removed from the station with pytest.raises(KeyError, match='bob'): _ = station.components['bob'] # check that 'bob' has been removed from the station, again with pytest.raises(KeyError, match='Component bob is not part of the ' 'station'): station.remove_component('bob')
def test_station_snapshot_during_measurement(experiment, dac, dmm, pass_station): station = Station() station.add_component(dac) station.add_component(dmm, 'renamed_dmm') snapshot_of_station = station.snapshot() if pass_station: measurement = Measurement(experiment, station) else: # in this branch of the `if` we expect that `Measurement` object # will be initialized with `Station.default` which is equal to the # station object that is instantiated above measurement = Measurement(experiment) measurement.register_parameter(dac.ch1) measurement.register_parameter(dmm.v1, setpoints=[dac.ch1]) with measurement.run() as data_saver: data_saver.add_result((dac.ch1, 7), (dmm.v1, 5)) # 1. Test `get_metadata('snapshot')` method json_snapshot_from_dataset = data_saver.dataset.get_metadata('snapshot') snapshot_from_dataset = json.loads(json_snapshot_from_dataset) expected_snapshot = {'station': snapshot_of_station} assert expected_snapshot == snapshot_from_dataset # 2. Test `snapshot_raw` property assert json_snapshot_from_dataset == data_saver.dataset.snapshot_raw # 3. Test `snapshot` property assert expected_snapshot == data_saver.dataset.snapshot