File: test_utils.py

package info (click to toggle)
mopidy-mpd 3.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 680 kB
  • sloc: python: 7,641; makefile: 3
file content (89 lines) | stat: -rw-r--r-- 2,962 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import socket
import unittest
from unittest.mock import Mock, patch, sentinel

from mopidy_mpd import network


class FormatHostnameTest(unittest.TestCase):
    @patch("mopidy_mpd.network.has_ipv6", True)
    def test_format_hostname_prefixes_ipv4_addresses_when_ipv6_available(self):
        network.has_ipv6 = True
        assert network.format_hostname("0.0.0.0") == "::ffff:0.0.0.0"
        assert network.format_hostname("1.0.0.1") == "::ffff:1.0.0.1"

    @patch("mopidy_mpd.network.has_ipv6", False)
    def test_format_hostname_does_nothing_when_only_ipv4_available(self):
        network.has_ipv6 = False
        assert network.format_hostname("0.0.0.0") == "0.0.0.0"


class FormatAddressTest(unittest.TestCase):
    def test_format_address_ipv4(self):
        address = (sentinel.host, sentinel.port)
        assert (
            network.format_address(address)
            == f"[{sentinel.host}]:{sentinel.port}"
        )

    def test_format_address_ipv6(self):
        address = (sentinel.host, sentinel.port, sentinel.flow, sentinel.scope)
        assert (
            network.format_address(address)
            == f"[{sentinel.host}]:{sentinel.port}"
        )

    def test_format_address_unix(self):
        address = (sentinel.path, None)
        assert network.format_address(address) == f"[{sentinel.path}]"


class GetSocketAddress(unittest.TestCase):
    def test_get_socket_address(self):
        host = str(sentinel.host)
        port = sentinel.port
        assert network.get_socket_address(host, port) == (host, port)

    def test_get_socket_address_unix(self):
        host = str(sentinel.host)
        port = sentinel.port
        assert network.get_socket_address(("unix:" + host), port) == (
            host,
            None,
        )


class TryIPv6SocketTest(unittest.TestCase):
    @patch("socket.has_ipv6", False)
    def test_system_that_claims_no_ipv6_support(self):
        assert not network.try_ipv6_socket()

    @patch("socket.has_ipv6", True)
    @patch("socket.socket")
    def test_system_with_broken_ipv6(self, socket_mock):
        socket_mock.side_effect = IOError()
        assert not network.try_ipv6_socket()

    @patch("socket.has_ipv6", True)
    @patch("socket.socket")
    def test_with_working_ipv6(self, socket_mock):
        socket_mock.return_value = Mock()
        assert network.try_ipv6_socket()


class CreateSocketTest(unittest.TestCase):
    @patch("mopidy_mpd.network.has_ipv6", False)
    @patch("socket.socket")
    def test_ipv4_socket(self, socket_mock):
        network.create_tcp_socket()
        assert socket_mock.call_args[0] == (socket.AF_INET, socket.SOCK_STREAM)

    @patch("mopidy_mpd.network.has_ipv6", True)
    @patch("socket.socket")
    def test_ipv6_socket(self, socket_mock):
        network.create_tcp_socket()
        assert socket_mock.call_args[0] == (socket.AF_INET6, socket.SOCK_STREAM)

    @unittest.SkipTest
    def test_ipv6_only_is_set(self):
        pass