Completed
Push — master ( f46766...a9fd17 )
by Jace
8s
created

TestComputers   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 47
rs 10
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A test_generate_name_duplicates() 0 6 2
A test_get_current_by_matching_hostname() 0 4 2
A test_generate_name() 0 6 2
A test_match() 0 4 2
A test_get_current_by_matching_address() 0 4 2
A test_get() 0 4 2
A test_get_missing() 0 4 2
1
# pylint: disable=misplaced-comparison-constant,no-self-use
0 ignored issues
show
introduced by
Bad option value 'misplaced-comparison-constant'
Loading history...
2
3
from unittest.mock import Mock, patch
4
5
import pytest
6
7
from mine.computer import Computer, Computers
8
9
10
@patch('uuid.getnode', Mock(return_value=0))
11
@patch('socket.gethostname', Mock(return_value='Sample.local'))
12
class TestComputer:
13
14
    """Unit tests for the computer classes."""
15
16
    def test_init(self):
17
        """Verify the correct computer information is loaded."""
18
        computer = Computer('my-sample')
19
        assert 'my-sample' == computer.name
20
        assert '00:00:00:00:00:00' == computer.address
21
        assert 'Sample.local' == computer.hostname
22
23
    def test_init_defaults(self):
24
        """Verify the correct computer information can be overridden."""
25
        computer = Computer('name', 'hostname', 'address')
26
        assert 'name' == computer.name
27
        assert 'address' == computer.address
28
        assert 'hostname' == computer.hostname
29
30
    def test_eq(self):
31
        """Verify computers and strings can be equated."""
32
        assert Computer('mac1') == Computer('mac1')
33
        assert Computer('mac1') != Computer('mac2')
34
        assert Computer('mac1') == 'mac1'
35
        assert 'mac1' != Computer('mac2')
36
37
    def test_lt(self):
38
        """Verify computers can be sorted."""
39
        assert Computer('mac1') < Computer('mac2')
40
        assert Computer('def') > Computer('ABC')
41
42
    def test_get_match_none(self):
43
        """Verify a computer is added when missing."""
44
        other = Computer('name', 'hostname', 'address')
45
        computers = Computers([other])
46
        this = computers.get_current()
47
        assert 'sample' == this.name
48
        assert '00:00:00:00:00:00' == this.address
49
        assert 'Sample.local' == this.hostname
50
        assert 2 == len(computers)
51
52
    def test_get_match_all(self):
53
        """Verify a computer can be matched exactly."""
54
        other = Computer('all', 'Sample.local', '00:00:00:00:00:00')
55
        computers = Computers([other])
56
        this = computers.get_current()
57
        assert 'all' == this.name
58
        assert '00:00:00:00:00:00' == this.address
59
        assert 'Sample.local' == this.hostname
60
        assert 1 == len(computers)
61
62
63
@patch('uuid.getnode', Mock(return_value=0))
64
@patch('socket.gethostname', Mock(return_value='Sample.local'))
65
class TestComputers:
66
67
    """Unit tests for lists of computers."""
68
69
    computers = Computers([Computer('abc', 'abc.local', 1),
70
                           Computer('def', 'def.local', 2)])
71
72
    def test_get(self):
73
        """Verify a computer can be found in a list."""
74
        computer = self.computers.get('ABC')
75
        assert 'abc' == computer.name
76
77
    def test_get_missing(self):
78
        """Verify an invalid names raise an assertion."""
79
        with pytest.raises(AssertionError):
80
            self.computers.get('fake')
81
82
    def test_match(self):
83
        """Verify a similar computer can be found."""
84
        computer = self.computers.match('AB')
85
        assert 'abc' == computer.name
86
87
    def test_generate_name(self):
88
        """Verify a computer name is generated correctly."""
89
        computers = Computers()
90
        computer = Computer(None, hostname='Jaces-iMac.local')
91
        name = computers.generate_name(computer)
92
        assert 'jaces-imac' == name
93
94
    def test_generate_name_duplicates(self):
95
        """Verify a computer name is generated correctly with duplicates."""
96
        computers = Computers([Computer('jaces-imac')])
97
        computer = Computer(None, hostname='Jaces-iMac.local')
98
        name = computers.generate_name(computer)
99
        assert 'jaces-imac-2' == name
100
101
    def test_get_current_by_matching_address(self):
102
        computers = Computers([Computer('abc', 'foobar', '00:00:00:00:00:00')])
103
        computer = computers.get_current()
104
        assert 'abc' == computer.name
105
106
    def test_get_current_by_matching_hostname(self):
107
        computers = Computers([Computer('abc', 'Sample.local', 'foobar')])
108
        computer = computers.get_current()
109
        assert 'abc' == computer.name
110