def test_serialization_and_deserialization_combined(self) -> None: table_foo = TablePulseTemplate( identifier='foo', entries={'default': [('hugo', 2), ('albert', 'voltage')]}, parameter_constraints=['albert<9.1']) table = TablePulseTemplate({'default': [('t', 0)]}) foo_mappings = dict(hugo='ilse', albert='albert', voltage='voltage') sequence = SequencePulseTemplate( (table_foo, foo_mappings, dict()), (table, dict(t=0), dict()), external_parameters=['ilse', 'albert', 'voltage'], identifier=None) storage = DummyStorageBackend() serializer = Serializer(storage) serializer.serialize(sequence) serialized_foo = storage.stored_items['foo'] serialized_sequence = storage.stored_items['main'] deserialized_sequence = serializer.deserialize('main') storage.stored_items = dict() serializer.serialize(deserialized_sequence) self.assertEqual(serialized_foo, storage.stored_items['foo']) self.assertEqual(serialized_sequence, storage.stored_items['main'])
def test_serializer_integration(self): serializer = Serializer(DummyStorageBackend()) serializer.serialize(self.template) template = serializer.deserialize('foo') self.assertIsInstance(template, TablePulseTemplate) self.assertEqual(template.entries, self.template.entries) self.assertEqual(template.measurement_declarations, self.template.measurement_declarations) self.assertEqual(template.parameter_constraints, self.template.parameter_constraints)
def test_serializer_integration(self): before = FunctionPulseTemplate(expression=self.s, duration_expression=self.s2, channel='A', measurements=self.meas_list, parameter_constraints=self.constraints, identifier='my_tpt') serializer = Serializer(DummyStorageBackend()) serializer.serialize(before) after = serializer.deserialize('my_tpt') self.assertIsInstance(after, FunctionPulseTemplate) self.assertEqual(before.expression, after.expression) self.assertEqual(before.duration, after.duration) self.assertEqual(before.defined_channels, after.defined_channels) self.assertEqual(before.measurement_declarations, after.measurement_declarations) self.assertEqual(before.parameter_constraints, after.parameter_constraints)
def test_serialization_and_deserialization_combined(self) -> None: table_foo = TablePulseTemplate(identifier='foo') table_foo.add_entry('hugo', 2) table_foo.add_entry(ParameterDeclaration('albert', max=9.1), 'voltage') table = TablePulseTemplate(measurement=True) foo_mappings = dict(hugo='ilse', albert='albert', voltage='voltage') sequence = SequencePulseTemplate([(table_foo, foo_mappings), (table, {})], ['ilse', 'albert', 'voltage'], identifier=None) storage = DummyStorageBackend() serializer = Serializer(storage) serializer.serialize(sequence) serialized_foo = storage.stored_items['foo'] serialized_sequence = storage.stored_items['main'] deserialized_sequence = serializer.deserialize('main') storage.stored_items = dict() serializer.serialize(deserialized_sequence) self.assertEqual(serialized_foo, storage.stored_items['foo']) self.assertEqual(serialized_sequence, storage.stored_items['main'])
class SerializerTests(unittest.TestCase): def setUp(self) -> None: self.backend = DummyStorageBackend() self.serializer = Serializer(self.backend) self.deserialization_data = dict( data='THIS IS DARTAA!', type=self.serializer.get_type_identifier(DummySerializable())) def test_serialize_subpulse_no_identifier(self) -> None: serializable = DummySerializable(data='bar') serialized = self.serializer.dictify(serializable) expected = serializable.get_serialization_data(self.serializer) expected['type'] = self.serializer.get_type_identifier(serializable) self.assertEqual(expected, serialized) def test_serialize_subpulse_identifier(self) -> None: serializable = DummySerializable(identifier='bar') serialized = self.serializer.dictify(serializable) self.assertEqual(serializable.identifier, serialized) def test_serialize_subpulse_duplicate_identifier(self) -> None: serializable = DummySerializable(identifier='bar') self.serializer.dictify(serializable) self.serializer.dictify(serializable) serializable = DummySerializable(data='this is other data than before', identifier='bar') with self.assertRaises(Exception): self.serializer.dictify(serializable) def test_collection_dictionaries_no_identifier(self) -> None: serializable = DummySerializable(data='bar') dictified = self.serializer._Serializer__collect_dictionaries( serializable) expected = {'': serializable.get_serialization_data(self.serializer)} expected['']['type'] = self.serializer.get_type_identifier( serializable) self.assertEqual(expected, dictified) def test_collection_dictionaries_identifier(self) -> None: serializable = DummySerializable(data='bar', identifier='foo') dicified = self.serializer._Serializer__collect_dictionaries( serializable) expected = { serializable.identifier: serializable.get_serialization_data(self.serializer) } expected[serializable.identifier][ 'type'] = self.serializer.get_type_identifier(serializable) self.assertEqual(expected, dicified) def test_dicitify_no_identifier_one_nesting_no_identifier(self) -> None: inner_serializable = DummySerializable(data='bar') serializable = NestedDummySerializable(data=inner_serializable) dicitified = self.serializer._Serializer__collect_dictionaries( serializable) expected = {'': serializable.get_serialization_data(self.serializer)} expected['']['type'] = self.serializer.get_type_identifier( serializable) self.assertEqual(expected, dicitified) def test_collection_dictionaries_no_identifier_one_nesting_identifier( self) -> None: inner_serializable = DummySerializable(data='bar', identifier='foo') serializable = NestedDummySerializable(data=inner_serializable) dicitified = self.serializer._Serializer__collect_dictionaries( serializable) expected = { '': serializable.get_serialization_data(self.serializer), inner_serializable.identifier: inner_serializable.get_serialization_data(self.serializer) } expected['']['type'] = self.serializer.get_type_identifier( serializable) expected[inner_serializable.identifier][ 'type'] = self.serializer.get_type_identifier(inner_serializable) self.assertEqual(expected, dicitified) def test_collection_dictionaries_identifier_one_nesting_no_identifier( self) -> None: inner_serializable = DummySerializable(data='bar') serializable = NestedDummySerializable(data=inner_serializable, identifier='outer_foo') dicitified = self.serializer._Serializer__collect_dictionaries( serializable) expected = { serializable.identifier: serializable.get_serialization_data(self.serializer) } expected[serializable.identifier][ 'type'] = self.serializer.get_type_identifier(serializable) self.assertEqual(expected, dicitified) def test_collection_dictionaries_identifier_one_nesting_identifier( self) -> None: inner_serializable = DummySerializable(data='bar', identifier='foo') serializable = NestedDummySerializable(data=inner_serializable, identifier='outer_foo') dicitified = self.serializer._Serializer__collect_dictionaries( serializable) expected = { inner_serializable.identifier: inner_serializable.get_serialization_data(self.serializer), serializable.identifier: serializable.get_serialization_data(self.serializer) } expected[serializable.identifier][ 'type'] = self.serializer.get_type_identifier(serializable) expected[inner_serializable.identifier][ 'type'] = self.serializer.get_type_identifier(inner_serializable) self.assertEqual(expected, dicitified) def __serialization_test_helper(self, serializable: Serializable, expected: Dict[str, str]) -> None: self.serializer.serialize(serializable) expected = { k: json.dumps(v, indent=4, sort_keys=True) for k, v in expected.items() } self.assertEqual(expected, self.backend.stored_items) def test_serialize_no_identifier(self) -> None: serializable = DummySerializable(data='bar') expected = { 'main': serializable.get_serialization_data(self.serializer) } expected['main']['type'] = self.serializer.get_type_identifier( serializable) self.__serialization_test_helper(serializable, expected) def test_serialize_identifier(self) -> None: serializable = DummySerializable(data='bar', identifier='foo') expected = { serializable.identifier: serializable.get_serialization_data(self.serializer) } expected[serializable.identifier][ 'type'] = self.serializer.get_type_identifier(serializable) self.__serialization_test_helper(serializable, expected) def test_serialize_no_identifier_one_nesting_no_identifier(self) -> None: inner_serializable = DummySerializable(data='bar') serializable = NestedDummySerializable(data=inner_serializable) expected = { 'main': serializable.get_serialization_data(self.serializer) } expected['main']['type'] = self.serializer.get_type_identifier( serializable) self.__serialization_test_helper(serializable, expected) def test_serialize_no_identifier_one_nesting_identifier(self) -> None: inner_serializable = DummySerializable(data='bar', identifier='foo') serializable = NestedDummySerializable(data=inner_serializable) expected = { 'main': serializable.get_serialization_data(self.serializer), inner_serializable.identifier: inner_serializable.get_serialization_data(self.serializer) } expected['main']['type'] = self.serializer.get_type_identifier( serializable) expected[inner_serializable.identifier][ 'type'] = self.serializer.get_type_identifier(inner_serializable) self.__serialization_test_helper(serializable, expected) def test_serialize_identifier_one_nesting_no_identifier(self) -> None: inner_serializable = DummySerializable(data='bar') serializable = NestedDummySerializable(data=inner_serializable, identifier='outer_foo') expected = { serializable.identifier: serializable.get_serialization_data(self.serializer) } expected[serializable.identifier][ 'type'] = self.serializer.get_type_identifier(serializable) self.__serialization_test_helper(serializable, expected) def test_serialize_identifier_one_nesting_identifier(self) -> None: inner_serializable = DummySerializable(data='bar', identifier='foo') serializable = NestedDummySerializable(data=inner_serializable, identifier='outer_foo') expected = { serializable.identifier: serializable.get_serialization_data(self.serializer), inner_serializable.identifier: inner_serializable.get_serialization_data(self.serializer) } expected[serializable.identifier][ 'type'] = self.serializer.get_type_identifier(serializable) expected[inner_serializable.identifier][ 'type'] = self.serializer.get_type_identifier(inner_serializable) self.__serialization_test_helper(serializable, expected) def test_deserialize_dict(self) -> None: deserialized = self.serializer.deserialize(self.deserialization_data) self.assertIsInstance(deserialized, DummySerializable) self.assertEqual(self.deserialization_data['data'], deserialized.data) def test_deserialize_identifier(self) -> None: jsonized_data = json.dumps(self.deserialization_data, indent=4, sort_keys=True) identifier = 'foo' self.backend.put(identifier, jsonized_data) deserialized = self.serializer.deserialize(identifier) self.assertIsInstance(deserialized, DummySerializable) self.assertEqual(self.deserialization_data['data'], deserialized.data) def test_serialization_and_deserialization_combined(self) -> None: table_foo = TablePulseTemplate( identifier='foo', entries={'default': [('hugo', 2), ('albert', 'voltage')]}, parameter_constraints=['albert<9.1']) table = TablePulseTemplate({'default': [('t', 0)]}) foo_mappings = dict(hugo='ilse', albert='albert', voltage='voltage') sequence = SequencePulseTemplate( (table_foo, foo_mappings, dict()), (table, dict(t=0), dict()), external_parameters=['ilse', 'albert', 'voltage'], identifier=None) storage = DummyStorageBackend() serializer = Serializer(storage) serializer.serialize(sequence) serialized_foo = storage.stored_items['foo'] serialized_sequence = storage.stored_items['main'] deserialized_sequence = serializer.deserialize('main') storage.stored_items = dict() serializer.serialize(deserialized_sequence) self.assertEqual(serialized_foo, storage.stored_items['foo']) self.assertEqual(serialized_sequence, storage.stored_items['main'])
class SerializerTests(unittest.TestCase): def setUp(self) -> None: self.backend = DummyStorageBackend() self.serializer = Serializer(self.backend) self.deserialization_data = dict(data='THIS IS DARTAA!', type=self.serializer.get_type_identifier(DummySerializable())) def test_serialize_subpulse_no_identifier(self) -> None: serializable = DummySerializable(data='bar') serialized = self.serializer._serialize_subpulse(serializable) self.assertEqual(serializable.get_serialization_data(self.serializer), serialized) def test_serialize_subpulse_identifier(self) -> None: serializable = DummySerializable(identifier='bar') serialized = self.serializer._serialize_subpulse(serializable) self.assertEqual(serializable.identifier, serialized) def test_serialize_subpulse_duplicate_identifier(self) -> None: serializable = DummySerializable(identifier='bar') self.serializer._serialize_subpulse(serializable) self.serializer._serialize_subpulse(serializable) serializable = DummySerializable(data='this is other data than before', identifier='bar') with self.assertRaises(Exception): self.serializer._serialize_subpulse(serializable) def test_dictify_no_identifier(self) -> None: serializable = DummySerializable(data='bar') dictified = self.serializer.dictify(serializable) self.assertEqual({'': serializable.get_serialization_data(self.serializer)}, dictified) def test_dictify_identifier(self) -> None: serializable = DummySerializable(data='bar', identifier='foo') dicified = self.serializer.dictify(serializable) self.assertEqual({serializable.identifier: serializable.get_serialization_data(self.serializer)}, dicified) def test_dicitify_no_identifier_one_nesting_no_identifier(self) -> None: inner_serializable = DummySerializable(data='bar') serializable = NestedDummySerializable(data=inner_serializable) dicitified = self.serializer.dictify(serializable) self.assertEqual({'': serializable.get_serialization_data(self.serializer)}, dicitified) def test_dictify_no_identifier_one_nesting_identifier(self) -> None: inner_serializable = DummySerializable(data='bar', identifier='foo') serializable = NestedDummySerializable(data=inner_serializable) dicitified = self.serializer.dictify(serializable) self.assertEqual({'': serializable.get_serialization_data(self.serializer), inner_serializable.identifier: inner_serializable.get_serialization_data(self.serializer)}, dicitified) def test_dictify_identifier_one_nesting_no_identifier(self) -> None: inner_serializable = DummySerializable(data='bar') serializable = NestedDummySerializable(data=inner_serializable, identifier='outer_foo') dicitified = self.serializer.dictify(serializable) self.assertEqual({serializable.identifier: serializable.get_serialization_data(self.serializer)}, dicitified) def test_dictify_identifier_one_nesting_identifier(self) -> None: inner_serializable = DummySerializable(data='bar', identifier='foo') serializable = NestedDummySerializable(data=inner_serializable, identifier='outer_foo') dicitified = self.serializer.dictify(serializable) self.assertEqual({inner_serializable.identifier: inner_serializable.get_serialization_data(self.serializer), serializable.identifier: serializable.get_serialization_data(self.serializer)}, dicitified) def __serialization_test_helper(self, serializable: Serializable, expected: Dict[str, str]) -> None: self.serializer.serialize(serializable) expected = {k: json.dumps(v, indent=4, sort_keys=True) for k,v in expected.items()} self.assertEqual(expected, self.backend.stored_items) def test_serialize_no_identifier(self) -> None: serializable = DummySerializable(data='bar') expected = {'main': serializable.get_serialization_data(self.serializer)} self.__serialization_test_helper(serializable, expected) def test_serialize_identifier(self) -> None: serializable = DummySerializable(data='bar', identifier='foo') expected = {serializable.identifier: serializable.get_serialization_data(self.serializer)} self.__serialization_test_helper(serializable, expected) def test_serialize_no_identifier_one_nesting_no_identifier(self) -> None: inner_serializable = DummySerializable(data='bar') serializable = NestedDummySerializable(data=inner_serializable) expected = {'main': serializable.get_serialization_data(self.serializer)} self.__serialization_test_helper(serializable, expected) def test_serialize_no_identifier_one_nesting_identifier(self) -> None: inner_serializable = DummySerializable(data='bar', identifier='foo') serializable = NestedDummySerializable(data=inner_serializable) expected = {'main': serializable.get_serialization_data(self.serializer), inner_serializable.identifier: inner_serializable.get_serialization_data(self.serializer)} self.__serialization_test_helper(serializable, expected) def test_serialize_identifier_one_nesting_no_identifier(self) -> None: inner_serializable = DummySerializable(data='bar') serializable = NestedDummySerializable(data=inner_serializable, identifier='outer_foo') expected = {serializable.identifier: serializable.get_serialization_data(self.serializer)} self.__serialization_test_helper(serializable, expected) def test_serialize_identifier_one_nesting_identifier(self) -> None: inner_serializable = DummySerializable(data='bar', identifier='foo') serializable = NestedDummySerializable(data=inner_serializable, identifier='outer_foo') expected = {serializable.identifier: serializable.get_serialization_data(self.serializer), inner_serializable.identifier: inner_serializable.get_serialization_data(self.serializer)} self.__serialization_test_helper(serializable, expected) def test_deserialize_dict(self) -> None: deserialized = self.serializer.deserialize(self.deserialization_data) self.assertIsInstance(deserialized, DummySerializable) self.assertEqual(self.deserialization_data['data'], deserialized.data) def test_deserialize_identifier(self) -> None: jsonized_data = json.dumps(self.deserialization_data, indent=4, sort_keys=True) identifier = 'foo' self.backend.put(identifier, jsonized_data) deserialized = self.serializer.deserialize(identifier) self.assertIsInstance(deserialized, DummySerializable) self.assertEqual(self.deserialization_data['data'], deserialized.data) def test_serialization_and_deserialization_combined(self) -> None: table_foo = TablePulseTemplate(identifier='foo') table_foo.add_entry('hugo', 2) table_foo.add_entry(ParameterDeclaration('albert', max=9.1), 'voltage') table = TablePulseTemplate(measurement=True) foo_mappings = dict(hugo='ilse', albert='albert', voltage='voltage') sequence = SequencePulseTemplate([(table_foo, foo_mappings), (table, {})], ['ilse', 'albert', 'voltage'], identifier=None) storage = DummyStorageBackend() serializer = Serializer(storage) serializer.serialize(sequence) serialized_foo = storage.stored_items['foo'] serialized_sequence = storage.stored_items['main'] deserialized_sequence = serializer.deserialize('main') storage.stored_items = dict() serializer.serialize(deserialized_sequence) self.assertEqual(serialized_foo, storage.stored_items['foo']) self.assertEqual(serialized_sequence, storage.stored_items['main'])