X-Git-Url: http://git.kaliko.me/?a=blobdiff_plain;f=test.py;h=ecf62ffb6c70e70fc28abc8f668644c00003aa3a;hb=a3420f074de70e8cd6f3050e3351fa9b43db0102;hp=5922041619214989740a74aa7416da2a4d0a43b5;hpb=4b44d5f37ad29e54c356f7dcc97194eaabcecc03;p=python-musicpd.git diff --git a/test.py b/test.py index 5922041..ecf62ff 100755 --- a/test.py +++ b/test.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +# pylint: disable=missing-docstring """ Test suite highly borrowed^Wsteal from python-mpd2 [0] project. @@ -35,24 +36,60 @@ TEST_MPD_HOST, TEST_MPD_PORT = ('example.com', 10000) class testEnvVar(unittest.TestCase): def test_envvar(self): - os.environ.pop('MPD_HOST', None) - os.environ.pop('MPD_PORT', None) - client = musicpd.MPDClient() - self.assertEqual(client.host, 'localhost') - self.assertEqual(client.port, '6600') + # mock "os.path.exists" here to ensure there are no socket in + # XDG_RUNTIME_DIR/mpd or /run/mpd since with test defaults fallbacks + # when : + # * neither MPD_HOST nor XDG_RUNTIME_DIR are not set + # * /run/mpd does not expose a socket + with mock.patch('os.path.exists', return_value=False): + os.environ.pop('MPD_HOST', None) + os.environ.pop('MPD_PORT', None) + client = musicpd.MPDClient() + self.assertEqual(client.host, 'localhost') + self.assertEqual(client.port, '6600') + + os.environ.pop('MPD_HOST', None) + os.environ['MPD_PORT'] = '6666' + client = musicpd.MPDClient() + self.assertEqual(client.pwd, None) + self.assertEqual(client.host, 'localhost') + self.assertEqual(client.port, '6666') + # Test password extraction os.environ['MPD_HOST'] = 'pa55w04d@example.org' client = musicpd.MPDClient() self.assertEqual(client.pwd, 'pa55w04d') self.assertEqual(client.host, 'example.org') - self.assertEqual(client.port, '6600') - os.environ.pop('MPD_HOST', None) - os.environ['MPD_PORT'] = '6666' - client = musicpd.MPDClient() + # Test password extraction (no host) + os.environ['MPD_HOST'] = 'pa55w04d@' + with mock.patch('os.path.exists', return_value=False): + client = musicpd.MPDClient() + self.assertEqual(client.pwd, 'pa55w04d') + self.assertEqual(client.host, 'localhost') + + # Test badly formatted MPD_HOST + os.environ['MPD_HOST'] = '@' + with mock.patch('os.path.exists', return_value=False): + client = musicpd.MPDClient() self.assertEqual(client.pwd, None) self.assertEqual(client.host, 'localhost') - self.assertEqual(client.port, '6666') + + # Test unix socket extraction + os.environ['MPD_HOST'] = 'pa55w04d@/unix/sock' + client = musicpd.MPDClient() + self.assertEqual(client.host, '/unix/sock') + + # Test plain abstract socket extraction + os.environ['MPD_HOST'] = '@abstract' + client = musicpd.MPDClient() + self.assertEqual(client.host, '@abstract') + + # Test password and abstract socket extraction + os.environ['MPD_HOST'] = 'pass@@abstract' + client = musicpd.MPDClient() + self.assertEqual(client.pwd, 'pass') + self.assertEqual(client.host, '@abstract') # Test unix socket fallback os.environ.pop('MPD_HOST', None) @@ -61,6 +98,9 @@ class testEnvVar(unittest.TestCase): with mock.patch('os.path.exists', return_value=True): client = musicpd.MPDClient() self.assertEqual(client.host, '/run/mpd/socket') + os.environ['XDG_RUNTIME_DIR'] = '/run/user/1000' + client = musicpd.MPDClient() + self.assertEqual(client.host, '/run/user/1000/mpd/socket') os.environ.pop('MPD_HOST', None) os.environ.pop('MPD_PORT', None) @@ -69,6 +109,20 @@ class testEnvVar(unittest.TestCase): client = musicpd.MPDClient() self.assertEqual(client.host, '/run/user/1000/mpd/socket') + # Test MPD_TIMEOUT + os.environ.pop('MPD_TIMEOUT', None) + client = musicpd.MPDClient() + self.assertEqual(client.mpd_timeout, musicpd.CONNECTION_TIMEOUT) + os.environ['MPD_TIMEOUT'] = 'garbage' + client = musicpd.MPDClient() + self.assertEqual(client.mpd_timeout, + musicpd.CONNECTION_TIMEOUT, + 'Garbage\'s not silently ignore to use default value') + os.environ['MPD_TIMEOUT'] = '42' + client = musicpd.MPDClient() + self.assertEqual(client.mpd_timeout, 42) + + class TestMPDClient(unittest.TestCase): longMessage = True @@ -168,6 +222,10 @@ class TestMPDClient(unittest.TestCase): 'kill': None, 'password': 'nothing', 'ping': 'nothing', + # Partition Commands + 'partition': 'nothing', + 'listpartitions': 'list', + 'newpartition': 'nothing', # Audio Output Commands 'disableoutput': 'nothing', 'enableoutput': 'nothing', @@ -216,6 +274,26 @@ class TestMPDClient(unittest.TestCase): self.client._rfile.readline.side_effect = itertools.chain( lines, itertools.repeat('')) + def MPDWillReturnBinary(self, lines): + data = bytearray(b''.join(lines)) + + def read(amount): + val = bytearray() + while amount > 0: + amount -= 1 + # _ = data.pop(0) + # print(hex(_)) + val.append(data.pop(0)) + return val + + def readline(): + val = bytearray() + while not val.endswith(b'\x0a'): + val.append(data.pop(0)) + return val + self.client._rbfile.readline.side_effect = readline + self.client._rbfile.read.side_effect = read + def assertMPDReceived(self, *lines): self.client._wfile.write.assert_called_with(*lines) @@ -414,6 +492,17 @@ class TestMPDClient(unittest.TestCase): res = self.client.sticker_list('song', 'baz') self.assertEqual(['foo=bar'], res) + def test_albumart(self): + # here is a 34 bytes long data + data = bytes('\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01' + '\x00\x01\x00\x00\xff\xdb\x00C\x00\x05\x03\x04', + encoding='utf8') + read_lines = [b'size: 42\nbinary: 34\n', data, b'\nOK\n'] + self.MPDWillReturnBinary(read_lines) + # Reading albumart / offset 0 should return the data + res = self.client.albumart('muse/Raised Fist/2002-Dedication/', 0) + self.assertEqual(res.get('data'), data) + if __name__ == '__main__': unittest.main()