def merge_gadgets(self, old_gadget: Gadget, new_gadget: Gadget) -> Gadget: """ Merges two gadgets (typically one 'original' and a new one with update information) into a new gadget containing with name, client and class of the old and characteristics of the new one. :param old_gadget: Original gadget to base merged gadget on :param new_gadget: Gadget wth update information :return: The emrged gadget :raise GadgetMergeError: If anything goes wrong during merges """ encoder = ApiEncoder() if not isinstance( new_gadget, AnyGadget) and old_gadget.__class__ != new_gadget.__class__: raise GadgetMergeError( f"Cannot merge gadgets with different classes {old_gadget.__class__.__name__} " f"and {new_gadget.__class__.__name__}") try: gadget_type = encoder.encode_gadget_identifier(old_gadget) except IdentifierEncodeError as err: raise GadgetMergeError(err.args[0]) try: merged_gadget = self.create_gadget( gadget_type, old_gadget.get_name(), old_gadget.get_host_client(), new_gadget.get_characteristics()) except GadgetCreationError as err: raise GadgetMergeError(err.args[0]) return merged_gadget
def test_api_gadget_de_serialization(f_validator: Validator, encoder: ApiEncoder, decoder: ApiDecoder, f_any_gadget: Gadget, f_dummy_gadget: Gadget): serialized_data = encoder.encode_gadget(f_any_gadget) f_validator.validate(serialized_data, "api_gadget_data") with pytest.raises(NotImplementedError): deserialized_gadget = decoder.decode_gadget( serialized_data, f_any_gadget.get_host_client()) # assert deserialized_gadget.equals(f_any_gadget) with pytest.raises(GadgetEncodeError): encoder.encode_gadget(f_dummy_gadget)