def test_get_instance_of_type(): instance1 = SubClass1() instance2 = SubClass2() relayer = Relayer(BaseClass, [Protocol.MRP, Protocol.DMAP, Protocol.AirPlay]) relayer.register(instance1, Protocol.MRP) relayer.register(instance2, Protocol.DMAP) assert relayer.get(Protocol.MRP) == instance1 assert relayer.get(Protocol.DMAP) == instance2 assert relayer.get(Protocol.AirPlay) is None
def test_main_instance(): instance2 = SubClass2() relayer = Relayer(BaseClass, [Protocol.MRP, Protocol.DMAP, Protocol.AirPlay]) relayer.register(SubClass1(), Protocol.DMAP) relayer.register(SubClass3(), Protocol.AirPlay) relayer.register(instance2, Protocol.MRP) assert relayer.main_instance == instance2
def test_relay_override_priority(): relayer = Relayer(BaseClass, [Protocol.MRP, Protocol.DMAP]) relayer.register(SubClass1(), Protocol.DMAP) relayer.register(SubClass2(), Protocol.MRP) assert relayer.relay("with_args", [Protocol.MRP, Protocol.DMAP])(3) == 3 assert relayer.relay("with_args", [Protocol.DMAP, Protocol.MRP])(3) == 6
def __init__(self, config: conf.AppleTV, session_manager: ClientSessionManager): """Initialize a new FacadeAppleTV instance.""" super().__init__() self._config = config self._session_manager = session_manager self._protocol_handlers: Dict[Protocol, SetupData] = {} self._push_updates = Relayer( interface.PushUpdater, DEFAULT_PRIORITIES # type: ignore ) self._features = FacadeFeatures(self._push_updates) self.interfaces = { interface.Features: self._features, interface.RemoteControl: FacadeRemoteControl(), interface.Metadata: FacadeMetadata(), interface.Power: FacadePower(), interface.PushUpdater: self._push_updates, interface.Stream: FacadeStream(), interface.Apps: FacadeApps(), interface.Audio: FacadeAudio(), }
def __init__(self): """Initialize a new FacadePower instance.""" # This is border line, maybe need another structure to support this Relayer.__init__(self, interface.Power, DEFAULT_PRIORITIES) interface.Power.__init__(self)
def test_relay_missing_instance_ignored_and_raises_not_found(): relayer = Relayer(BaseClass, [Protocol.MRP]) with pytest.raises(exceptions.NotSupportedError): relayer.relay("no_args")
def relay_base_only(): relayer = Relayer(BaseClass, [Protocol.MRP]) relayer.register(SubClass1(), Protocol.MRP) yield relayer
def test_class_priority(): relayer = Relayer(BaseClass, [Protocol.MRP, Protocol.DMAP, Protocol.AirPlay]) relayer.register(SubClass1(), Protocol.AirPlay) relayer.register(SubClass3(), Protocol.MRP) relayer.register(SubClass2(), Protocol.DMAP) assert relayer.relay("no_args")() == "subclass1" assert relayer.relay("with_args")(3) == 3 assert relayer.relay("with_kwargs")(a=4, b=1) == 3
def test_main_instance_missing_instance_for_priority(): relayer = Relayer(BaseClass, [Protocol.MRP]) with pytest.raises(exceptions.NotSupportedError): relayer.main_instance
def test_add_instance_not_in_priority_list_raises(): relayer = Relayer(BaseClass, [Protocol.MRP]) with pytest.raises(RuntimeError): relayer.register(SubClass1(), Protocol.DMAP)
def test_relay_method_not_in_interface_raises(): relayer = Relayer(BaseClass, [Protocol.MRP]) relayer.register(SubClass2(), Protocol.MRP) with pytest.raises(RuntimeError): relayer.relay("missing_method")
def test_relay_missing_target_raises(): relayer = Relayer(BaseClass, [Protocol.MRP]) relayer.register(SubClass2(), Protocol.MRP) with pytest.raises(exceptions.NotSupportedError): relayer.relay("no_args")