Passed
Push — master ( 74a04b...775f07 )
by Vinicius
04:04 queued 32s
created

TestConnection.test_send()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
"""Test kytos.core.connection module."""
2 1
from socket import error as SocketError
3 1
from unittest.mock import MagicMock
4
5 1
import pytest
6
7 1
from kytos.core.connection import Connection, ConnectionState
8
9
10 1
class TestConnection:
11
    """Connection tests."""
12
13 1
    def setup_method(self):
14
        """Instantiate a Connection."""
15 1
        socket = MagicMock()
16 1
        switch = MagicMock()
17 1
        self.connection = Connection('addr', 123, socket, switch)
18
19 1
        switch.connection = self.connection
20
21 1
    def test__str__(self):
22
        """Test __str__ method."""
23 1
        assert str(self.connection) == "Connection('addr', 123)"
24
25 1
    def test__repr__(self):
26
        """Test __repr__ method."""
27 1
        self.connection.socket = 'socket'
28 1
        self.connection.switch = 'switch'
29
30 1
        expected = "Connection('addr', 123, 'socket', 'switch', " + \
31
                   "<ConnectionState.NEW: 0>)"
32 1
        assert repr(self.connection) == expected
33
34 1
    def test_state(self):
35
        """Test state property."""
36 1
        assert self.connection.state.value == 0
37
38 1
        self.connection.state = ConnectionState.FINISHED
39 1
        assert self.connection.state.value == 4
40
41 1
    def test_state__error(self):
42
        """Test state property to error case."""
43 1
        with pytest.raises(Exception):
44 1
            self.connection.state = 1000
45
46 1
    def test_id(self):
47
        """Test id property."""
48 1
        assert self.connection.id == ('addr', 123)
49
50 1
    def test_send(self):
51
        """Test send method."""
52 1
        self.connection.send(b'data')
53
54 1
        self.connection.socket.sendall.assert_called_with(b'data')
55
56 1
    def test_send_error(self):
57
        """Test send method to error case."""
58 1
        self.connection.socket.sendall.side_effect = SocketError
59
60 1
        with pytest.raises(SocketError):
61 1
            self.connection.send(b'data')
62
63 1
        assert self.connection.socket is None
64
65 1
    def test_close(self):
66
        """Test close method."""
67 1
        self.connection.close()
68
69 1
        assert self.connection.socket is None
70 1
        assert self.connection is not None
71
72 1
    def test_close__os_error(self):
73
        """Test close method to OSError case."""
74 1
        self.connection.socket.shutdown.side_effect = OSError
75
76 1
        with pytest.raises(OSError):
77 1
            self.connection.close()
78
79 1
        assert self.connection.socket is not None
80
81 1
    def test_close__attribute_error(self):
82
        """Test close method to AttributeError case."""
83 1
        self.connection.socket = None
84
85 1
        self.connection.close()
86
87 1
        assert self.connection.socket is None
88
89 1
    def test_is_alive(self):
90
        """Test is_alive method to True and False returns."""
91 1
        assert self.connection.is_alive()
92
93 1
        self.connection.state = ConnectionState.FINISHED
94 1
        assert not self.connection.is_alive()
95
96 1
    def test_is_new(self):
97
        """Test is_new method."""
98 1
        assert self.connection.is_new()
99
100 1
    def test_established_state(self):
101
        """Test set_established_state and is_established methods."""
102 1
        self.connection.set_established_state()
103 1
        assert self.connection.is_established()
104
105 1
    def test_setup_state(self):
106
        """Test set_setup_state and is_during_setup methods."""
107 1
        self.connection.set_setup_state()
108 1
        assert self.connection.is_during_setup()
109
110 1
    def test_update_switch(self):
111
        """Test update_switch method."""
112 1
        switch = MagicMock()
113 1
        self.connection.update_switch(switch)
114
115 1
        assert self.connection.switch == switch
116
        assert switch.connection == self.connection
117