def test_load_with_instruments(self):
        with patch(
                'qilib.configuration_helper.instrument_configuration.InstrumentAdapterFactory'
        ):
            instrument_1 = InstrumentConfiguration('DummyClass',
                                                   'fake-address-1',
                                                   self._storage,
                                                   tag=['instrument_1'])
            instrument_2 = InstrumentConfiguration('DummyClass',
                                                   'fake-address-2',
                                                   self._storage,
                                                   tag=['instrument_2'])

            instrument_configuration_set = InstrumentConfigurationSet(
                self._storage,
                instrument_configurations=[instrument_1, instrument_2])
            instrument_configuration_set.store()

            instrument_configuration_set_new = InstrumentConfigurationSet.load(
                instrument_configuration_set.tag, self._storage)

        self.assertEqual(instrument_configuration_set_new.tag,
                         instrument_configuration_set.tag)
        self.assertEqual(
            len(instrument_configuration_set_new.instrument_configurations), 2)
        self.assertEqual(
            instrument_configuration_set_new.instrument_configurations[0].tag,
            instrument_1.tag)
        self.assertEqual(
            instrument_configuration_set_new.instrument_configurations[1].tag,
            instrument_2.tag)
 def test_apply_delta_lazy(self):
     with patch(
             'qilib.configuration_helper.instrument_configuration.InstrumentAdapterFactory'
     ) as mock_factory:
         mock_adapter = MagicMock()
         mock_adapter.read.return_value = PythonJsonStructure(
             param1={'value': 1},
             param2={'value': 2},
             param3={'value': 3},
             param4={'value': 4})
         mock_factory.get_instrument_adapter.return_value = mock_adapter
         instrument_configuration = InstrumentConfiguration(
             'DummyClass',
             'fake-address',
             self._storage,
             configuration=PythonJsonStructure(param1={'value': 1},
                                               param2={'value': 42},
                                               param3={'value': 33}))
         instrument_configuration.apply_delta_lazy()
         mock_adapter.read.assert_called_once_with(update=False)
         mock_adapter.apply.assert_called_once_with({
             'param2': {
                 'value': 42
             },
             'param3': {
                 'value': 33
             }
         })
    def test_snapshot(self):
        with patch(
                'qilib.configuration_helper.instrument_configuration.InstrumentAdapterFactory'
        ) as factory_mock:
            instrument_mock = Mock()
            factory_mock.get_instrument_adapter.return_value = instrument_mock
            instrument_mock.read.return_value = {'hello': 'world'}

            instrument_1 = InstrumentConfiguration('DummyClass',
                                                   'fake-address-1',
                                                   self._storage,
                                                   tag=['instrument_1'])
            instrument_2 = InstrumentConfiguration('DummyClass',
                                                   'fake-address-2',
                                                   self._storage,
                                                   tag=['instrument_2'])

            instrument_configuration_set = InstrumentConfigurationSet(
                self._storage,
                tag=['test'],
                instrument_configurations=[instrument_1, instrument_2])
            tag = instrument_configuration_set.tag
            tag_1 = instrument_1.tag
            tag_2 = instrument_2.tag

            instrument_configuration_set.snapshot()

            self.assertNotEqual(instrument_configuration_set.tag, tag)
            self.assertNotEqual(instrument_1.tag, tag_1)
            self.assertNotEqual(instrument_2.tag, tag_2)
Example #4
0
    def test_configuration_helper_integration(self):
        storage = StorageMemory('some_name')
        configuration_1 = PythonJsonStructure(amper=0.005)
        configuration_2 = PythonJsonStructure(frequency='2.4 GHz')
        with patch('qilib.configuration_helper.instrument_configuration.InstrumentAdapterFactory'):
            instrument_1 = InstrumentConfiguration('DummyClass', 'fake-address-1', storage, tag=['instrument_1'],
                                                   configuration=configuration_1)
            instrument_2 = InstrumentConfiguration('DummyClass', 'fake-address-2', storage, tag=['instrument_2'],
                                                   configuration=configuration_2)
            instrument_configuration_set = InstrumentConfigurationSet(storage, tag=['set'],
                                                                      instrument_configurations=[instrument_1,
                                                                                                 instrument_2])
            instrument_configuration_set.store()

            configuration_helper = ConfigurationHelper(storage)
            configuration_helper.retrieve_inactive_configuration_from_storage(['set'])
        inactive_configuration = configuration_helper.inactive_configuration
        self.assertListEqual(inactive_configuration.tag, ['set'])
        self.assertListEqual(inactive_configuration.instrument_configurations[0].tag, ['instrument_1'])
        self.assertListEqual(inactive_configuration.instrument_configurations[1].tag, ['instrument_2'])
        self.assertDictEqual(configuration_1, inactive_configuration.instrument_configurations[0].configuration)
        self.assertDictEqual(configuration_2, inactive_configuration.instrument_configurations[1].configuration)
        repr_str = r"ConfigurationHelper\(StorageMemory\('some_name'\), InstrumentConfigurationSet\(StorageMemory\(" \
                   r"'some_name'\), \['configuration_set', '.*'\], \[\]\), " \
                   r"InstrumentConfigurationSet\(StorageMemory\('some_name'\), \['set'\], \[InstrumentConfiguration\(" \
                   r"'DummyClass', 'fake-address-1', StorageMemory\('some_name'\), \['instrument_1'\], " \
                   r"\{'amper': 0.005\}, None\), InstrumentConfiguration\('DummyClass', 'fake-address-2', " \
                   r"StorageMemory\('some_name'\), \['instrument_2'\], \{'frequency': '2.4 GHz'\}, None\)\]\)\)"
        self.assertRegex(repr(configuration_helper), repr_str)
 def test_store(self):
     with patch(
             'qilib.configuration_helper.instrument_configuration.InstrumentAdapterFactory'
     ):
         instrument_configuration = InstrumentConfiguration(
             'DummyClass',
             'fake-address',
             self._storage,
             tag=['2019-07-11T00:00:00.424242'])
         instrument_configuration.store()
         self.assertTrue(
             self._storage.tag_in_storage(['2019-07-11T00:00:00.424242']))
    def test_store_with_instruments(self):
        with patch(
                'qilib.configuration_helper.instrument_configuration.InstrumentAdapterFactory'
        ):
            instrument_1 = InstrumentConfiguration('DummyClass',
                                                   'fake-address-1',
                                                   self._storage,
                                                   tag=['instrument_1'])
            instrument_2 = InstrumentConfiguration('DummyClass',
                                                   'fake-address-2',
                                                   self._storage,
                                                   tag=['instrument_2'])
            instrument_1.store()
            instrument_2.store()

            instrument_configuration_set = InstrumentConfigurationSet(
                self._storage,
                instrument_configurations=[instrument_1, instrument_2])

            instrument_configuration_set.store()

            self.assertTrue(
                self._storage.tag_in_storage(instrument_configuration_set.tag))
            self.assertTrue(self._storage.tag_in_storage(instrument_1.tag))
            self.assertTrue(self._storage.tag_in_storage(instrument_2.tag))
 def test_store_raises_error(self):
     with patch(
             'qilib.configuration_helper.instrument_configuration.InstrumentAdapterFactory'
     ):
         instrument_configuration = InstrumentConfiguration(
             'DummyClass',
             'fake-address',
             self._storage,
             tag=['2019-05-09T11:29:51.523636'])
         instrument_configuration.store()
         error = r"InstrumentConfiguration for DummyClass with tag " \
                 r"'\['2019-05-09T11:29:51.523636'\]' already in storage"
         self.assertRaisesRegex(DuplicateTagError, error,
                                instrument_configuration.store)
 def test_apply(self):
     with patch(
             'qilib.configuration_helper.instrument_configuration.InstrumentAdapterFactory'
     ) as mock_factory:
         mock_adapter = MagicMock()
         mock_factory.get_instrument_adapter.return_value = mock_adapter
         instrument_configuration = InstrumentConfiguration(
             'DummyClass',
             'fake-address',
             self._storage,
             configuration=PythonJsonStructure(
                 how_many_instruments='all_the_instruments'))
         instrument_configuration.apply()
     mock_adapter.apply.assert_called_once_with(
         {'how_many_instruments': 'all_the_instruments'})
 def test_refresh(self):
     with patch(
             'qilib.configuration_helper.instrument_configuration.InstrumentAdapterFactory'
     ) as mock_factory:
         mock_adapter = MagicMock()
         instrument_settings = PythonJsonStructure(param1={'value': 1},
                                                   param2={'value': 2},
                                                   param3={'value': 3},
                                                   param4={'value': 4})
         mock_adapter.read.return_value = instrument_settings
         mock_factory.get_instrument_adapter.return_value = mock_adapter
         instrument_configuration = InstrumentConfiguration(
             'DummyClass',
             'fake-address',
             self._storage,
             configuration=PythonJsonStructure(param1={'value': 11},
                                               param2={'value': 22},
                                               param3={'value': 33},
                                               param4={'value': 444}))
         instrument_configuration.refresh()
     self.assertDictEqual(instrument_settings,
                          instrument_configuration.configuration)
 def __create_visitor_with_instrument(self, adapter, instrument_class_name,
                                      instrument_address):
     with patch(
             'qilib.configuration_helper.instrument_configuration.InstrumentAdapterFactory'
     ) as mock_factory:
         mock_factory.get_instrument_adapter.return_value = adapter
         instrument_1 = InstrumentConfiguration(instrument_class_name,
                                                instrument_address,
                                                Mock(),
                                                tag=['instrument_1'])
     instrument_configuration_set = InstrumentConfigurationSet(
         Mock(), instrument_configurations=[instrument_1])
     visitor = InstrumentConfigurationVisitor()
     instrument_configuration_set.accept(visitor)
     return visitor
 def test_constructor(self):
     with patch(
             'qilib.configuration_helper.instrument_configuration.InstrumentAdapterFactory'
     ):
         instrument_configuration = InstrumentConfiguration(
             'DummyClass', 'fake-address', self._storage)
     self.assertIsInstance(instrument_configuration.configuration,
                           PythonJsonStructure)
     self.assertDictEqual({}, instrument_configuration.configuration)
     self.assertEqual('fake-address', instrument_configuration.address)
     self.assertIs(instrument_configuration.storage, self._storage)
     self.assertEqual(instrument_configuration.tag[0], 'configuration')
     self.assertEqual(instrument_configuration.tag[1], 'DummyClass')
     self.assertRegex(instrument_configuration.tag[2],
                      r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{6}')
 def test_constructor_full(self):
     config = PythonJsonStructure(voltage='low',
                                  current='lower',
                                  frequency='high-enough')
     with patch(
             'qilib.configuration_helper.instrument_configuration.InstrumentAdapterFactory'
     ):
         instrument_configuration = InstrumentConfiguration(
             'DummyClass',
             'fake-address',
             self._storage,
             tag=['2019-05-09T11:29:51.523636'],
             configuration=config)
     self.assertDictEqual(config, instrument_configuration.configuration)
     self.assertListEqual(['2019-05-09T11:29:51.523636'],
                          instrument_configuration.tag)
    def load(tag: List[str],
             storage: StorageInterface) -> 'InstrumentConfigurationSet':
        """ A factory that creates a new InstrumentConfigurationSet by loading from database

        Args:
            tag: A unique identifier for a instrument configuration set
            storage: Any storage that implements the StorageInterface

        Returns:
            A new InstrumentConfigurationSet loaded from the storage
        """

        # load the document as a list of instruments tags
        tags = storage.load_data(tag)
        instrument_configurations = [
            InstrumentConfiguration.load(instrument_tag, storage)
            for instrument_tag in tags
        ]

        return InstrumentConfigurationSet(storage, tag,
                                          instrument_configurations)
    def test_load(self):
        some_data = {
            'adapter_class_name': 'Dummy',
            'address': 'dev42',
            'instrument_name': 'name',
            'configuration': {
                'power': 'MAX',
                'other': 'SOME',
                'one_more': 0.42
            }
        }

        self._storage.save_data(some_data, ['2019-05-09T11:29:51.523636'])
        with patch(
                'qilib.configuration_helper.instrument_configuration.InstrumentAdapterFactory'
        ) as mock_factory:
            instrument_configuration = InstrumentConfiguration.load(
                ['2019-05-09T11:29:51.523636'], self._storage)
            mock_factory.get_instrument_adapter.assert_called_with(
                'Dummy', 'dev42', 'name')
            self.assertListEqual(['2019-05-09T11:29:51.523636'],
                                 instrument_configuration.tag)
            self.assertDictEqual(some_data['configuration'],
                                 instrument_configuration.configuration)