Example #1
0
    def test_init(self, mpdclient):
        client_mock = mpdclient.return_value

        # default arguments
        client = single.Client(self.basepath)

        expected_calls = [
            mock.call(host=str(self.basepath.absolute()), port=0),
        ]
        assert client_mock.connect.call_args_list == expected_calls
        assert client.muted is False

        # explicit muted state
        muted = True
        client = single.Client(self.basepath, muted=muted)

        assert client.muted == muted

        # with a server object
        server = mock.Mock()
        type(server).socket = mock.PropertyMock(return_value=self.basepath)

        client = single.Client(server)
        assert client.server is server
        assert client.basepath == self.basepath
Example #2
0
    def test_station(self, mpdclient):
        client_mock = mpdclient.return_value

        length = 21
        urls = list(map(str, range(length)))
        station1 = 19
        station2 = 5

        client = single.Client(self.basepath)
        client.urls = urls

        # initial station should be None
        assert client.station is None

        # set the first station
        client.station = station1
        assert client.station == station1

        # set the second station
        client.station = station2
        assert client.station == station2

        # check that play was called appropriately
        expected_calls = [mock.call(station1), mock.call(station2)]
        assert client_mock.play.call_args_list == expected_calls
Example #3
0
    def test_play(self, mpdclient):
        client_mock = mpdclient.return_value

        length = 10
        urls = list(map(str, range(length)))

        client = single.Client(self.basepath)
        client.urls = urls

        # without index
        client.play()

        # succeeding
        succeeding_index = abs(length - 5)
        client.play(succeeding_index)

        # failing
        failing_index = length + 5
        with pytest.raises(RuntimeError):
            client.play(failing_index)

        # check the server state
        expected_calls = [mock.call(), mock.call(succeeding_index)]
        assert client_mock.play.call_args_list == expected_calls

        # with negative index
        failing_negative_index = -5
        with pytest.raises(RuntimeError):
            client.play(failing_negative_index)
Example #4
0
    def test_context(self, mpdclient):
        client_mock = mpdclient.return_value

        client = single.Client(self.basepath)

        with client:
            pass

        assert client_mock.disconnect.call_count == 1
Example #5
0
    def test_connect(self, mpdclient):
        client_mock = mpdclient.return_value

        client = single.Client(self.basepath)
        client_mock.connect.reset_mock()

        client._connect()

        expected_call = mock.call(host=str(self.basepath.absolute()), port=0)
        assert client_mock.connect.call_args_list == [expected_call]
Example #6
0
    def test_clear(self, mpdclient):
        client_mock = mpdclient.return_value

        urls = list(map(str, range(20)))
        client = single.Client(self.basepath)
        client.urls = urls
        client_mock.clear.reset_mock()

        client.clear()
        assert client_mock.clear.call_count == 1
        assert client._urls == []
Example #7
0
    def test_add(self, mpdclient):
        client_mock = mpdclient.return_value

        # urls to add
        urls = list(map(str, range(5)))

        client = single.Client(self.basepath)
        for url in urls:
            client.add(url)

        assert client_mock.add.call_args_list == list(map(mock.call, urls))
        assert client._urls == urls
Example #8
0
    def test_mute(self, mpdclient):
        client = single.Client(self.basepath, muted=False)
        client_mock = mpdclient.return_value

        # mute when unmuted
        client.mute()

        assert client.muted is True

        # mute when muted
        client.mute()
        assert client.muted is True

        # the volume should only have changed once
        assert client_mock.setvol.call_args_list == [mock.call(0)]
Example #9
0
    def test_disconnect(self, mpdclient):
        client_mock = mpdclient.return_value

        client = single.Client(self.basepath)

        # succeeding
        client.disconnect()
        assert client_mock.disconnect.call_count == 1

        client_mock.disconnect.reset_mock()
        # failing
        client_mock.disconnect.side_effect = BrokenPipeError
        client.disconnect()

        assert client_mock.disconnect.call_count == 1
Example #10
0
    def test_unmute(self, mpdclient):
        client_mock = mpdclient.return_value

        volume = 41
        client_mock.status.return_value = {'volume': volume}
        client = single.Client(self.basepath, muted=True)

        # unmute when muted
        client.unmute()
        assert client.muted is False

        # unmute when unmuted
        client.unmute()
        assert client.muted is False

        # the volume should only have changed once
        assert client_mock.setvol.call_args_list == [mock.call(volume)]
Example #11
0
    def test_muted(self, mpdclient):
        client_mock = mpdclient.return_value

        volume1 = 52
        volume2 = 31
        client_mock.status.return_value = {'volume': volume1}
        client = single.Client(self.basepath, muted=False)

        # unmuted to unmuted
        client.muted = False
        assert client.muted is False

        # unmuted to muted
        client.muted = True
        assert client.muted is True

        # muted to muted
        client.muted = True
        assert client.muted is True

        # muted to unmuted
        client.muted = False
        assert client.muted is False

        # check the server state
        expected_calls = [mock.call(0), mock.call(volume1)]
        assert client_mock.setvol.call_args_list == expected_calls

        client_mock.setvol.reset_mock()

        # check that the volume does behave correctly
        # change when unmuted
        client.volume = volume2

        # change when muted
        client.muted = True
        client.volume = volume1

        expected_calls = [mock.call(volume2), mock.call(0)]
        assert client_mock.setvol.call_args_list == expected_calls

        client_mock.setvol.reset_mock()
        # only now when unmuting, the volume should change back again
        client.muted = False
        assert client_mock.setvol.call_args_list == [mock.call(volume1)]
Example #12
0
    def test_toggle_mute(self, mpdclient):
        client_mock = mpdclient.return_value

        volume = 54
        client_mock.status.return_value = {'volume': volume}
        client = single.Client(self.basepath, muted=False)

        # toggle to muted
        client.toggle_mute()
        assert client.muted is True

        # toggle to unmuted
        client.toggle_mute()
        assert client.muted is False

        # the volume should have changed twice
        expected_calls = [mock.call(0), mock.call(volume)]
        assert client_mock.setvol.call_args_list == expected_calls
Example #13
0
    def test_ensure_connection(self, mpdclient):
        client_mock = mpdclient.return_value

        client = single.Client(self.basepath)

        # succeeding
        client_mock.ping.return_value = True
        client.clear()
        assert client_mock.clear.call_count == 1

        client_mock.clear.reset_mock()
        client_mock.connect.reset_mock()
        # failing
        client_mock.ping.side_effect = BrokenPipeError
        client.clear()

        # check that the calls were actually executed
        assert client_mock.clear.call_count == 1
        assert client_mock.connect.call_count == 1
        assert client_mock.disconnect.call_count == 1
Example #14
0
    def test_urls(self, mpdclient):
        client_mock = mpdclient.return_value
        client = single.Client(self.basepath)

        urls1 = list(map(str, range(2, 7)))
        urls2 = list(map(str, range(5, 40)))

        # initial value
        assert client.urls == []

        # reassign once
        client.urls = urls1
        assert client.urls == urls1

        # reassign again
        client.urls = urls2
        assert client.urls == urls2

        # check the server state
        assert client_mock.clear.call_count == 2
        expected_calls = list(map(mock.call, urls1 + urls2))
        assert client_mock.add.call_args_list == expected_calls
Example #15
0
    def test_volume(self, mpdclient):
        client_mock = mpdclient.return_value

        volume1 = 20
        volume2 = 35
        volume3 = 50

        client_mock.status.return_value = {'volume': volume1}
        client = single.Client(self.basepath, muted=False)

        # initial volume
        assert client.volume == volume1

        # first change
        client.volume = volume2
        assert client.volume == volume2

        # second change
        client.volume = volume3
        assert client.volume == volume3

        # assert that the changes took effect on the server
        expected_calls = [mock.call(volume2), mock.call(volume3)]
        assert client_mock.setvol.call_args_list == expected_calls