#!/usr/bin/env python3
-# -*- coding: utf-8 -*-
+# coding: utf-8
+# SPDX-FileCopyrightText: 2012-2021 kaliko <kaliko@azylum.org>
+# SPDX-License-Identifier: LGPL-3.0-or-later
# pylint: disable=missing-docstring
"""
Test suite highly borrowed^Wsteal from python-mpd2 [0] project.
import itertools
import os
-import sys
import types
import unittest
+import unittest.mock as mock
import warnings
import musicpd
-try:
- import unittest.mock as mock
-except ImportError:
- try:
- import mock
- except ImportError:
- print("Please install mock from PyPI to run tests!")
- sys.exit(1)
# show deprecation warnings
warnings.simplefilter('default')
client = musicpd.MPDClient()
self.assertEqual(client.mpd_timeout,
musicpd.CONNECTION_TIMEOUT,
- 'Garbage\'s not silently ignore to use default value')
+ 'Garbage is silently ignore to use default value')
os.environ['MPD_TIMEOUT'] = '42'
client = musicpd.MPDClient()
self.assertEqual(client.mpd_timeout, 42)
self.client.command_list_end()
self.assertMPDReceived('command_list_end\n')
+ def test_two_word_commands(self):
+ self.MPDWillReturn('OK\n')
+ self.client.tagtypes_clear()
+ self.assertMPDReceived('tagtypes clear\n')
+ self.MPDWillReturn('OK\n')
+ with self.assertRaises(AttributeError):
+ self.client.foo_bar()
+
+class testConnection(unittest.TestCase):
+
+ def test_exposing_fileno(self):
+ with mock.patch('musicpd.socket') as socket_mock:
+ sock = mock.MagicMock(name='socket')
+ socket_mock.socket.return_value = sock
+ cli = musicpd.MPDClient()
+ cli.connect()
+ cli.fileno()
+ cli._sock.fileno.assert_called_with()
+
+ def test_connect_abstract(self):
+ os.environ['MPD_HOST'] = '@abstract'
+ with mock.patch('musicpd.socket') as socket_mock:
+ sock = mock.MagicMock(name='socket')
+ socket_mock.socket.return_value = sock
+ cli = musicpd.MPDClient()
+ cli.connect()
+ sock.connect.assert_called_with('\0abstract')
+
+ def test_connect_unix(self):
+ os.environ['MPD_HOST'] = '/run/mpd/socket'
+ with mock.patch('musicpd.socket') as socket_mock:
+ sock = mock.MagicMock(name='socket')
+ socket_mock.socket.return_value = sock
+ cli = musicpd.MPDClient()
+ cli.connect()
+ sock.connect.assert_called_with('/run/mpd/socket')
+
+
+class testException(unittest.TestCase):
+
+ def test_CommandError_on_newline(self):
+ os.environ['MPD_HOST'] = '/run/mpd/socket'
+ with mock.patch('musicpd.socket') as socket_mock:
+ sock = mock.MagicMock(name='socket')
+ socket_mock.socket.return_value = sock
+ cli = musicpd.MPDClient()
+ cli.connect()
+ with self.assertRaises(musicpd.CommandError):
+ cli.find('(album == "foo\nbar")')
+
+class testContextManager(unittest.TestCase):
+
+ def test_enter_exit(self):
+ os.environ['MPD_HOST'] = '@abstract'
+ with mock.patch('musicpd.socket') as socket_mock:
+ sock = mock.MagicMock(name='socket')
+ socket_mock.socket.return_value = sock
+ cli = musicpd.MPDClient()
+ with cli as c:
+ sock.connect.assert_called_with('\0abstract')
+ sock.close.assert_not_called()
+ sock.close.assert_called()
if __name__ == '__main__':
unittest.main()