Exemple #1
0
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"
Exemple #2
0
 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,
     )
Exemple #3
0
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)
Exemple #4
0
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
Exemple #5
0
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
Exemple #6
0
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"
Exemple #7
0
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
Exemple #8
0
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
Exemple #9
0
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
Exemple #10
0
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
Exemple #11
0
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
Exemple #12
0
def relay_base_only():
    relayer = Relayer(BaseClass, [Protocol.MRP])
    relayer.register(SubClass1(), Protocol.MRP)
    yield relayer
Exemple #13
0
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")
Exemple #14
0
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")
Exemple #15
0
def test_add_instance_not_in_priority_list_raises():
    relayer = Relayer(BaseClass, [Protocol.MRP])

    with pytest.raises(RuntimeError):
        relayer.register(SubClass1(), Protocol.DMAP)
Exemple #16
0
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"
Exemple #17
0
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")
Exemple #18
0
def test_main_protocol_with_no_instance():
    relayer = Relayer(BaseClass, [Protocol.MRP])
    assert relayer.main_protocol is None
Exemple #19
0
def test_main_instance_missing_instance_for_priority():
    relayer = Relayer(BaseClass, [Protocol.MRP])
    with pytest.raises(exceptions.NotSupportedError):
        relayer.main_instance
Exemple #20
0
 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)
Exemple #21
0
def test_relay_missing_instance_ignored_and_raises_not_found():
    relayer = Relayer(BaseClass, [Protocol.MRP])

    with pytest.raises(exceptions.NotSupportedError):
        relayer.relay("no_args")