def test_takeover_with_missing_implementation(): relayer = Relayer(BaseClass, [Protocol.AirPlay, Protocol.DMAP]) relayer.register(SubClass1(), Protocol.AirPlay) relayer.register(BaseClass(), Protocol.DMAP) relayer.takeover(Protocol.DMAP) assert relayer.relay("no_args")() == "subclass1"
def __init__(self, core_dispatcher: CoreStateDispatcher): """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) self._is_playing: Optional[bool] = None core_dispatcher.listen_to( UpdatedState.Playing, self._playing_changed, message_filter=lambda message: message.protocol == self. main_protocol, )
def test_takeover_while_takeover_raises(): relayer = Relayer(BaseClass, [Protocol.AirPlay]) relayer.register(SubClass4("airplay"), Protocol.AirPlay) relayer.takeover(Protocol.DMAP) with pytest.raises(exceptions.InvalidStateError): relayer.takeover(Protocol.DMAP)
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_takeover_overrides_main_protocol(): relayer = Relayer(BaseClass, [Protocol.MRP, Protocol.DMAP]) relayer.register(SubClass4("mrp"), Protocol.MRP) relayer.register(SubClass4("dmap"), Protocol.DMAP) relayer.takeover(Protocol.DMAP) assert relayer.main_protocol == Protocol.DMAP
def test_takeover_overrides_main_instance(): relayer = Relayer(BaseClass, [Protocol.MRP, Protocol.DMAP]) relayer.register(SubClass4("mrp"), Protocol.MRP) relayer.register(SubClass4("dmap"), Protocol.DMAP) relayer.takeover(Protocol.DMAP) assert relayer.main_instance.no_args() == "dmap"
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 test_main_protocol(): relayer = Relayer(BaseClass, [Protocol.MRP, Protocol.DMAP, Protocol.AirPlay]) relayer.register(SubClass1(), Protocol.AirPlay) assert relayer.main_protocol == Protocol.AirPlay relayer.register(SubClass1(), Protocol.DMAP) assert relayer.main_protocol == Protocol.DMAP relayer.register(SubClass1(), Protocol.MRP) assert relayer.main_protocol == Protocol.MRP
def test_get_all_instances(): mrp = SubClass4("mrp") dmap = SubClass4("dmap") airplay = SubClass4("airplay") relayer = Relayer(BaseClass, [Protocol.MRP, Protocol.DMAP, Protocol.AirPlay]) relayer.register(mrp, Protocol.MRP) relayer.register(dmap, Protocol.DMAP) relayer.register(airplay, Protocol.AirPlay) instances = relayer.instances assert len(instances) == 3 assert mrp in instances assert dmap in instances assert airplay in instances
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 relay_base_only(): relayer = Relayer(BaseClass, [Protocol.MRP]) relayer.register(SubClass1(), Protocol.MRP) yield relayer
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_takeover_overrides_manual_priority(): relayer = Relayer(BaseClass, [Protocol.MRP, Protocol.DMAP, Protocol.AirPlay]) relayer.register(SubClass4("airplay"), Protocol.AirPlay) relayer.register(SubClass4("mrp"), Protocol.MRP) relayer.register(SubClass4("dmap"), Protocol.DMAP) relayer.takeover(Protocol.AirPlay) assert (relayer.relay( "no_args", [Protocol.DMAP, Protocol.MRP, Protocol.AirPlay])() == "airplay")
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_takeover_and_release(): relayer = Relayer(BaseClass, [Protocol.MRP, Protocol.DMAP, Protocol.AirPlay]) relayer.register(SubClass4("airplay"), Protocol.AirPlay) relayer.register(SubClass4("mrp"), Protocol.MRP) relayer.register(SubClass4("dmap"), Protocol.DMAP) assert relayer.relay("no_args")() == "mrp" relayer.takeover(Protocol.AirPlay) assert relayer.relay("no_args")() == "airplay" relayer.release() assert relayer.relay("no_args")() == "mrp"
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")
def test_main_protocol_with_no_instance(): relayer = Relayer(BaseClass, [Protocol.MRP]) assert relayer.main_protocol is None
def test_main_instance_missing_instance_for_priority(): relayer = Relayer(BaseClass, [Protocol.MRP]) with pytest.raises(exceptions.NotSupportedError): relayer.main_instance
def __init__(self): """Initialize a new PushUpdater.""" # TODO: python 3.6 seems to have problem with this sometimes Relayer.__init__( # pylint: disable=non-parent-init-called self, interface.PushUpdater, DEFAULT_PRIORITIES) interface.PushUpdater.__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")