class RepetitionPulseTemplateSerializationTests(unittest.TestCase):
    def setUp(self) -> None:
        self.serializer = DummySerializer(deserialize_callback=lambda x: x["name"])
        self.body = DummyPulseTemplate()

    def test_get_serialization_data_constant(self) -> None:
        repetition_count = 3
        template = RepetitionPulseTemplate(self.body, repetition_count)
        expected_data = dict(
            type=self.serializer.get_type_identifier(template),
            body=str(id(self.body)),
            repetition_count=repetition_count,
        )
        data = template.get_serialization_data(self.serializer)
        self.assertEqual(expected_data, data)

    def test_get_serialization_data_declaration(self) -> None:
        repetition_count = ParameterDeclaration("foo")
        template = RepetitionPulseTemplate(self.body, repetition_count)
        expected_data = dict(
            type=self.serializer.get_type_identifier(template),
            body=str(id(self.body)),
            repetition_count=str(id(repetition_count)),
        )
        data = template.get_serialization_data(self.serializer)
        self.assertEqual(expected_data, data)

    def test_deserialize_constant(self) -> None:
        repetition_count = 3
        data = dict(repetition_count=repetition_count, body=dict(name=str(id(self.body))), identifier="foo")
        # prepare dependencies for deserialization
        self.serializer.subelements[str(id(self.body))] = self.body
        # deserialize
        template = RepetitionPulseTemplate.deserialize(self.serializer, **data)
        # compare!
        self.assertEqual(self.body, template.body)
        self.assertEqual(repetition_count, template.repetition_count)

    def test_deserialize_declaration(self) -> None:
        repetition_count = ParameterDeclaration("foo")
        data = dict(repetition_count=dict(name="foo"), body=dict(name=str(id(self.body))), identifier="foo")
        # prepare dependencies for deserialization
        self.serializer.subelements[str(id(self.body))] = self.body
        self.serializer.subelements["foo"] = repetition_count
        # deserialize
        template = RepetitionPulseTemplate.deserialize(self.serializer, **data)
        # compare!
        self.assertEqual(self.body, template.body)
        self.assertEqual(repetition_count, template.repetition_count)
    def setUp(self) -> None:
        self.serializer = DummySerializer()

        self.table_foo = TablePulseTemplate(identifier='foo')
        self.table_foo.add_entry('hugo', 2)
        self.table_foo.add_entry(ParameterDeclaration('albert', max=9.1), 'voltage')

        self.table = TablePulseTemplate(measurement=True)
        self.foo_mappings = dict(hugo='ilse', albert='albert', voltage='voltage')
class ParameterDeclarationSerializationTests(unittest.TestCase):

    def setUp(self) -> None:
        self.serializer = DummySerializer()
        self.declaration = ParameterDeclaration('foo')
        self.expected_data = dict(name='foo', type=self.serializer.get_type_identifier(self.declaration))

    def test_get_serialization_data_all_default(self) -> None:
        self.expected_data['min_value'] = float('-inf')
        self.expected_data['max_value'] = float('+inf')
        self.expected_data['default_value'] = None
        self.assertEqual(self.expected_data, self.declaration.get_serialization_data(self.serializer))

    def test_get_serialization_data_all_floats(self) -> None:
        self.declaration = ParameterDeclaration('foo', min=-3.1, max=4.3, default=0.2)
        self.expected_data['min_value'] = -3.1
        self.expected_data['max_value'] = 4.3
        self.expected_data['default_value'] = 0.2
        self.assertEqual(self.expected_data, self.declaration.get_serialization_data(self.serializer))

    def test_get_serialization_data_min_max_references(self) -> None:
        bar_min = ParameterDeclaration('bar_min')
        bar_max = ParameterDeclaration('bar_max')
        self.declaration.min_value = bar_min
        self.declaration.max_value = bar_max
        self.expected_data['min_value'] = 'bar_min'
        self.expected_data['max_value'] = 'bar_max'
        self.expected_data['default_value'] = None
        self.assertEqual(self.expected_data, self.declaration.get_serialization_data(self.serializer))

    def test_deserialize_all_default(self) -> None:
        data = dict(min_value=float('-inf'), max_value=float('+inf'), default_value=None, name='foo')
        declaration = ParameterDeclaration.deserialize(self.serializer, **data)
        self.assertEqual(data['min_value'], declaration.min_value)
        self.assertEqual(data['max_value'], declaration.max_value)
        self.assertEqual(data['default_value'], declaration.default_value)
        self.assertEqual(data['name'], declaration.name)
        self.assertIsNone(declaration.identifier)

    def test_deserialize_all_floats(self) -> None:
        data = dict(min_value=33.3, max_value=44, default_value=41.1, name='foo')
        declaration = ParameterDeclaration.deserialize(self.serializer, **data)
        self.assertEqual(data['min_value'], declaration.min_value)
        self.assertEqual(data['max_value'], declaration.max_value)
        self.assertEqual(data['default_value'], declaration.default_value)
        self.assertEqual(data['name'], declaration.name)
        self.assertIsNone(declaration.identifier)

    def test_deserialize_min_max_references(self) -> None:
        data = dict(min_value='bar_min', max_value='bar_max', default_value=-23.5, name='foo')
        declaration = ParameterDeclaration.deserialize(self.serializer, **data)
        self.assertEqual(float('-inf'), declaration.min_value)
        self.assertEqual(float('+inf'), declaration.max_value)
        self.assertEqual(data['default_value'], declaration.default_value)
        self.assertEqual(data['name'], declaration.name)
        self.assertIsNone(declaration.identifier)
class SequencePulseTemplateSerializationTests(unittest.TestCase):

    def setUp(self) -> None:
        self.serializer = DummySerializer()

        self.table_foo = TablePulseTemplate(identifier='foo')
        self.table_foo.add_entry('hugo', 2)
        self.table_foo.add_entry(ParameterDeclaration('albert', max=9.1), 'voltage')

        self.table = TablePulseTemplate(measurement=True)
        self.foo_mappings = dict(hugo='ilse', albert='albert', voltage='voltage')

    def test_get_serialization_data(self) -> None:
        sequence = SequencePulseTemplate([(self.table_foo, self.foo_mappings), (self.table, {})],
                                         ['ilse', 'albert', 'voltage'],
                                         identifier='foo')

        expected_data = dict(
            type=self.serializer.get_type_identifier(sequence),
            external_parameters=['albert', 'ilse', 'voltage'],
            is_interruptable=True,
            subtemplates = [
                dict(template=str(id(self.table_foo)), mappings=self.foo_mappings),
                dict(template=str(id(self.table)), mappings=dict())
            ]
        )
        data = sequence.get_serialization_data(self.serializer)
        self.assertEqual(expected_data, data)

    def test_deserialize(self) -> None:
        data = dict(
            external_parameters={'ilse', 'albert', 'voltage'},
            is_interruptable=True,
            subtemplates = [
                dict(template=str(id(self.table_foo)), mappings=self.foo_mappings),
                dict(template=str(id(self.table)), mappings=dict())
            ],
            identifier='foo'
        )

        # prepare dependencies for deserialization
        self.serializer.subelements[str(id(self.table_foo))] = self.table_foo
        self.serializer.subelements[str(id(self.table))] = self.table

        # deserialize
        sequence = SequencePulseTemplate.deserialize(self.serializer, **data)

        # compare!
        self.assertEqual(data['external_parameters'], sequence.parameter_names)
        self.assertEqual({ParameterDeclaration('ilse'), ParameterDeclaration('albert'), ParameterDeclaration('voltage')},
                         sequence.parameter_declarations)
        self.assertIs(self.table_foo, sequence.subtemplates[0][0])
        self.assertIs(self.table, sequence.subtemplates[1][0])
        self.assertEqual(self.foo_mappings, {k: m.string for k,m in sequence.subtemplates[0][1].items()})
        self.assertEqual(dict(), sequence.subtemplates[1][1])
        self.assertEqual(data['identifier'], sequence.identifier)
 def test_get_serialization_data(self) -> None:
     constant_parameter = ConstantParameter(-0.2)
     serializer = DummySerializer()
     data = constant_parameter.get_serialization_data(serializer)
     self.assertEqual(dict(type=serializer.get_type_identifier(constant_parameter), constant=-0.2), data)
 def setUp(self) -> None:
     self.serializer = DummySerializer()
     self.declaration = ParameterDeclaration('foo')
     self.expected_data = dict(name='foo', type=self.serializer.get_type_identifier(self.declaration))
 def setUp(self) -> None:
     self.serializer = DummySerializer(deserialize_callback=lambda x: x["name"])
     self.body = DummyPulseTemplate()