class TestChannels(TestCase): def setUp(self): # print("setup") self.instrument = DummyChannelInstrument(name='testchanneldummy') def tearDown(self): self.instrument.close() del self.instrument # del self.instrument is not sufficient in general because the __del__ method is # first invoked when there are 0 (non weak) references to the instrument. If a test # fails the unittest framework will keep a reference to the instrument is removed from # the testcase and __del__ is not invoked until all the tests have run. def test_channels_get(self): temperatures = self.instrument.channels.temperature.get() self.assertEqual(len(temperatures), 6) @given(value=hst.floats(0, 300), channel=hst.integers(0, 3)) def test_channel_access_is_identical(self, value, channel): channel_to_label = {0: 'A', 1: 'B', 2: 'C', 3: "D"} label = channel_to_label[channel] channel_via_label = getattr(self.instrument, label) # set via labeled channel channel_via_label.temperature(value) self.assertEqual(channel_via_label.temperature(), value) self.assertEqual(self.instrument.channels[channel].temperature(), value) self.assertEqual(self.instrument.channels.temperature()[channel], value) # reset channel_via_label.temperature(0) self.assertEqual(channel_via_label.temperature(), 0) self.assertEqual(self.instrument.channels[channel].temperature(), 0) self.assertEqual(self.instrument.channels.temperature()[channel], 0) # set via index into list self.instrument.channels[channel].temperature(value) self.assertEqual(channel_via_label.temperature(), value) self.assertEqual(self.instrument.channels[channel].temperature(), value) self.assertEqual(self.instrument.channels.temperature()[channel], value) # it's not possible to set via self.instrument.channels.temperature as this is a multi parameter # that currently does not support set. def test_add_channel(self): n_channels = len(self.instrument.channels) name = 'foo' channel = DummyChannel(self.instrument, 'Chan' + name, name) self.instrument.channels.append(channel) self.instrument.add_submodule(name, channel) self.assertEqual(len(self.instrument.channels), n_channels + 1) self.instrument.channels.lock() # after locking the channels it's not possible to add any more channels with self.assertRaises(AttributeError): name = 'bar' channel = DummyChannel(self.instrument, 'Chan' + name, name) self.instrument.channels.append(channel) self.assertEqual(len(self.instrument.channels), n_channels + 1) def test_add_channels_from_generator(self): n_channels = len(self.instrument.channels) names = ('foo', 'bar', 'foobar') channels = (DummyChannel(self.instrument, 'Chan' + name, name) for name in names) self.instrument.channels.extend(channels) self.assertEqual(len(self.instrument.channels), n_channels + len(names)) def test_add_channels_from_tuple(self): n_channels = len(self.instrument.channels) names = ('foo', 'bar', 'foobar') channels = tuple( DummyChannel(self.instrument, 'Chan' + name, name) for name in names) self.instrument.channels.extend(channels) self.assertEqual(len(self.instrument.channels), n_channels + len(names)) def test_insert_channel(self): n_channels = len(self.instrument.channels) name = 'foo' channel = DummyChannel(self.instrument, 'Chan' + name, name) self.instrument.channels.insert(1, channel) self.instrument.add_submodule(name, channel) self.assertEqual(len(self.instrument.channels), n_channels + 1) self.assertIs(self.instrument.channels[1], channel) self.instrument.channels.lock() # after locking the channels it's not possible to add any more channels with self.assertRaises(AttributeError): name = 'bar' channel = DummyChannel(self.instrument, 'Chan' + name, name) self.instrument.channels.insert(2, channel) self.assertEqual(len(self.instrument.channels), n_channels + 1) def test_remove_channel(self): channels = self.instrument.channels chanA = self.instrument.A original_length = len(channels.temperature()) channels.remove(chanA) with self.assertRaises(AttributeError): getattr(channels, chanA.short_name) self.assertEqual(len(channels), original_length - 1) self.assertEqual(len(channels.temperature()), original_length - 1) def test_remove_locked_channel(self): channels = self.instrument.channels chanA = self.instrument.A channels.lock() with self.assertRaises(AttributeError): channels.remove(chanA) def test_remove_tupled_channel(self): channel_tuple = tuple( DummyChannel(self.instrument, f'Chan{C}', C) for C in ('A', 'B', 'C', 'D', 'E', 'F')) channels = ChannelList(self.instrument, "TempSensorsTuple", DummyChannel, channel_tuple, snapshotable=False) chanA = channels.ChanA with self.assertRaises(AttributeError): channels.remove(chanA) @given(setpoints=hst.lists(hst.floats(0, 300), min_size=4, max_size=4)) def test_combine_channels(self, setpoints): self.assertEqual(len(self.instrument.channels), 6) mychannels = self.instrument.channels[0:2] + self.instrument.channels[ 4:] self.assertEqual(len(mychannels), 4) self.assertIs(mychannels[0], self.instrument.A) self.assertIs(mychannels[1], self.instrument.B) self.assertIs(mychannels[2], self.instrument.E) self.assertIs(mychannels[3], self.instrument.F) for i, chan in enumerate(mychannels): chan.temperature(setpoints[i]) expected = tuple(setpoints[0:2] + [0, 0] + setpoints[2:]) self.assertEquals(self.instrument.channels.temperature(), expected) @given(start=hst.integers(-8, 7), stop=hst.integers(-8, 7), step=hst.integers(1, 7)) def test_access_channels_by_slice(self, start, stop, step): names = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H') channels = tuple( DummyChannel(self.instrument, 'Chan' + name, name) for name in names) chlist = ChannelList(self.instrument, 'channels', DummyChannel, channels) if stop < start: step = -step myslice = slice(start, stop, step) mychans = chlist[myslice] expected_channels = names[myslice] for chan, exp_chan in zip(mychans, expected_channels): assert chan.name == f'testchanneldummy_Chan{exp_chan}' @given(myindexs=hst.lists(elements=hst.integers(-8, 7), min_size=1)) def test_access_channels_by_tuple(self, myindexs): names = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H') mytuple = tuple(myindexs) channels = tuple( DummyChannel(self.instrument, 'Chan' + name, name) for name in names) chlist = ChannelList(self.instrument, 'channels', DummyChannel, channels) mychans = chlist[mytuple] for chan, chanindex in zip(mychans, mytuple): assert chan.name == f'testchanneldummy_Chan{names[chanindex]}' def test_names(self): ex_inst_name = 'testchanneldummy' for channel in self.instrument.channels: sub_channel = DummyChannel(channel, 'subchannel', 'subchannel') channel.add_submodule('somesubchannel', sub_channel) assert self.instrument.name == ex_inst_name assert self.instrument.full_name == ex_inst_name assert self.instrument.short_name == ex_inst_name assert self.instrument.name_parts == [ex_inst_name] # Parameters directly on instrument assert self.instrument.IDN.name == 'IDN' assert self.instrument.IDN.full_name == f"{ex_inst_name}_IDN" for chan, name in zip(self.instrument.channels, ['A', 'B', 'C', 'D', 'E', 'F']): ex_chan_name = f"Chan{name}" ex_chan_full_name = f"{ex_inst_name}_{ex_chan_name}" assert chan.short_name == ex_chan_name assert chan.name == ex_chan_full_name assert chan.full_name == ex_chan_full_name assert chan.name_parts == [ex_inst_name, ex_chan_name] ex_param_name = 'temperature' assert chan.temperature.name == ex_param_name assert chan.temperature.full_name == f'{ex_chan_full_name}_{ex_param_name}' assert chan.temperature.short_name == ex_param_name assert chan.temperature.name_parts == [ ex_inst_name, ex_chan_name, ex_param_name ] ex_subchan_name = f"subchannel" ex_subchan_full_name = f"{ex_chan_full_name}_{ex_subchan_name}" assert chan.somesubchannel.short_name == ex_subchan_name assert chan.somesubchannel.name == ex_subchan_full_name assert chan.somesubchannel.full_name == ex_subchan_full_name assert chan.somesubchannel.name_parts == [ ex_inst_name, ex_chan_name, ex_subchan_name ] assert chan.somesubchannel.temperature.name == ex_param_name assert chan.somesubchannel.temperature.full_name == f'{ex_subchan_full_name}_{ex_param_name}' assert chan.somesubchannel.temperature.short_name == ex_param_name assert chan.somesubchannel.temperature.name_parts == [ ex_inst_name, ex_chan_name, ex_subchan_name, ex_param_name ]
class TestChannels(TestCase): def setUp(self): # print("setup") self.instrument = DummyChannelInstrument(name='testchanneldummy') def tearDown(self): self.instrument.close() del self.instrument # del self.instrument is not sufficient in general because the __del__ method is # first invoked when there are 0 (non weak) references to the instrument. If a test # fails the unittest framework will keep a reference to the instrument is removed from # the testcase and __del__ is not invoked until all the tests have run. def test_channels_get(self): temperatures = self.instrument.channels.temperature.get() self.assertEqual(len(temperatures), 6) @given(value=hst.floats(0, 300), channel=hst.integers(0, 3)) def test_channel_access_is_identical(self, value, channel): channel_to_label = {0: 'A', 1: 'B', 2: 'C', 3: "D"} label = channel_to_label[channel] channel_via_label = getattr(self.instrument, label) # set via labeled channel channel_via_label.temperature(value) self.assertEqual(channel_via_label.temperature(), value) self.assertEqual(self.instrument.channels[channel].temperature(), value) self.assertEqual(self.instrument.channels.temperature()[channel], value) # reset channel_via_label.temperature(0) self.assertEqual(channel_via_label.temperature(), 0) self.assertEqual(self.instrument.channels[channel].temperature(), 0) self.assertEqual(self.instrument.channels.temperature()[channel], 0) # set via index into list self.instrument.channels[channel].temperature(value) self.assertEqual(channel_via_label.temperature(), value) self.assertEqual(self.instrument.channels[channel].temperature(), value) self.assertEqual(self.instrument.channels.temperature()[channel], value) # it's not possible to set via self.instrument.channels.temperature as this is a multi parameter # that currently does not support set. def test_add_channel(self): n_channels = len(self.instrument.channels) name = 'foo' channel = DummyChannel(self.instrument, 'Chan' + name, name) self.instrument.channels.append(channel) self.instrument.add_submodule(name, channel) self.assertEqual(len(self.instrument.channels), n_channels + 1) self.instrument.channels.lock() # after locking the channels it's not possible to add any more channels with self.assertRaises(AttributeError): name = 'bar' channel = DummyChannel(self.instrument, 'Chan' + name, name) self.instrument.channels.append(channel) self.assertEqual(len(self.instrument.channels), n_channels + 1) def test_add_channels_from_generator(self): n_channels = len(self.instrument.channels) names = ('foo', 'bar', 'foobar') channels = (DummyChannel(self.instrument, 'Chan' + name, name) for name in names) self.instrument.channels.extend(channels) self.assertEqual(len(self.instrument.channels), n_channels + len(names)) def test_add_channels_from_tuple(self): n_channels = len(self.instrument.channels) names = ('foo', 'bar', 'foobar') channels = tuple( DummyChannel(self.instrument, 'Chan' + name, name) for name in names) self.instrument.channels.extend(channels) self.assertEqual(len(self.instrument.channels), n_channels + len(names)) def test_insert_channel(self): n_channels = len(self.instrument.channels) name = 'foo' channel = DummyChannel(self.instrument, 'Chan' + name, name) self.instrument.channels.insert(1, channel) self.instrument.add_submodule(name, channel) self.assertEqual(len(self.instrument.channels), n_channels + 1) self.assertIs(self.instrument.channels[1], channel) self.instrument.channels.lock() # after locking the channels it's not possible to add any more channels with self.assertRaises(AttributeError): name = 'bar' channel = DummyChannel(self.instrument, 'Chan' + name, name) self.instrument.channels.insert(2, channel) self.assertEqual(len(self.instrument.channels), n_channels + 1) @given(setpoints=hst.lists(hst.floats(0, 300), min_size=4, max_size=4)) def test_combine_channels(self, setpoints): self.assertEqual(len(self.instrument.channels), 6) mychannels = self.instrument.channels[0:2] + self.instrument.channels[ 4:] self.assertEqual(len(mychannels), 4) self.assertIs(mychannels[0], self.instrument.A) self.assertIs(mychannels[1], self.instrument.B) self.assertIs(mychannels[2], self.instrument.E) self.assertIs(mychannels[3], self.instrument.F) for i, chan in enumerate(mychannels): chan.temperature(setpoints[i]) expected = tuple(setpoints[0:2] + [0, 0] + setpoints[2:]) self.assertEquals(self.instrument.channels.temperature(), expected)