|
1
|
|
|
# pylint: disable=misplaced-comparison-constant,no-self-use,attribute-defined-outside-init |
|
|
|
|
|
|
2
|
|
|
|
|
3
|
|
|
from unittest.mock import Mock |
|
4
|
|
|
|
|
5
|
|
|
import pytest |
|
6
|
|
|
|
|
7
|
|
|
from mine.status import State, Status, ProgramStatus |
|
8
|
|
|
from mine.application import Application |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class TestState: |
|
12
|
|
|
|
|
13
|
|
|
"""Unit tests for the computer state class.""" |
|
14
|
|
|
|
|
15
|
|
|
state1 = State('computer1') |
|
16
|
|
|
state2 = State('computer2') |
|
17
|
|
|
state2.timestamp.started = 42 |
|
18
|
|
|
state3 = State('Computer2') |
|
19
|
|
|
state4 = State('Computer2') |
|
20
|
|
|
|
|
21
|
|
|
str_state = [ |
|
22
|
|
|
("computer1", state1), |
|
23
|
|
|
("computer2", state2), |
|
24
|
|
|
("Computer2", state3), |
|
25
|
|
|
] |
|
26
|
|
|
|
|
27
|
|
|
@pytest.mark.parametrize("string,state", str_state) |
|
28
|
|
|
def test_str(self, string, state): |
|
29
|
|
|
"""Verify states can be converted to strings.""" |
|
30
|
|
|
assert string == str(state) |
|
31
|
|
|
|
|
32
|
|
|
def test_eq(self): |
|
33
|
|
|
"""Verify states can be equated.""" |
|
34
|
|
|
assert self.state3 == self.state4 |
|
35
|
|
|
assert self.state2 != self.state3 |
|
36
|
|
|
assert self.state1 != self.state2 |
|
37
|
|
|
|
|
38
|
|
|
def test_lt(self): |
|
39
|
|
|
"""Verify states can be sorted.""" |
|
40
|
|
|
assert self.state1 < self.state2 |
|
41
|
|
|
assert self.state3 > self.state1 |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
class TestStatus: |
|
45
|
|
|
|
|
46
|
|
|
"""Unit tests for the application status class.""" |
|
47
|
|
|
|
|
48
|
|
|
status1 = Status('app1') |
|
49
|
|
|
status2 = Status('app2') |
|
50
|
|
|
status3 = Status('App2') |
|
51
|
|
|
status4 = Status('App2') |
|
52
|
|
|
|
|
53
|
|
|
str_status = [ |
|
54
|
|
|
("app1", status1), |
|
55
|
|
|
("app2", status2), |
|
56
|
|
|
("App2", status3), |
|
57
|
|
|
] |
|
58
|
|
|
|
|
59
|
|
|
@pytest.mark.parametrize("string,status", str_status) |
|
60
|
|
|
def test_str(self, string, status): |
|
61
|
|
|
"""Verify statuss can be converted to strings.""" |
|
62
|
|
|
assert string == str(status) |
|
63
|
|
|
|
|
64
|
|
|
def test_eq(self): |
|
65
|
|
|
"""Verify statuss can be equated.""" |
|
66
|
|
|
assert self.status3 == self.status4 |
|
67
|
|
|
assert self.status2 != self.status3 |
|
68
|
|
|
assert self.status1 != self.status2 |
|
69
|
|
|
|
|
70
|
|
|
def test_lt(self): |
|
71
|
|
|
"""Verify statuss can be sorted.""" |
|
72
|
|
|
assert self.status1 < self.status2 |
|
73
|
|
|
assert self.status3 > self.status1 |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
class TestProgramStatus: |
|
77
|
|
|
|
|
78
|
|
|
"""Unit tests for the program status class.""" |
|
79
|
|
|
|
|
80
|
|
|
def setup_method(self, _): |
|
81
|
|
|
"""Create an empty program status for all tests.""" |
|
82
|
|
|
self.status = ProgramStatus() |
|
83
|
|
|
self.application = Application('my-application') |
|
84
|
|
|
self.computer = Mock() |
|
85
|
|
|
self.computer.name = 'local' |
|
86
|
|
|
self.computer2 = Mock() |
|
87
|
|
|
self.computer2.name = 'remote' |
|
88
|
|
|
self.computer3 = Mock() |
|
89
|
|
|
self.computer3.name = 'remote2' |
|
90
|
|
|
|
|
91
|
|
|
def test_get_latest(self): |
|
92
|
|
|
"""Verify the latest computer can be determined.""" |
|
93
|
|
|
self.status.start(self.application, self.computer) |
|
94
|
|
|
self.status.start(self.application, self.computer2) |
|
95
|
|
|
self.status.stop(self.application, self.computer3) |
|
96
|
|
|
assert self.computer2.name == self.status.get_latest(self.application) |
|
97
|
|
|
assert 3 == self.status.counter |
|
98
|
|
|
|
|
99
|
|
|
def test_get_latest_empty(self): |
|
100
|
|
|
"""Verify None is returned when there is no latest computer.""" |
|
101
|
|
|
assert None is self.status.get_latest(self.application) |
|
102
|
|
|
assert 0 == self.status.counter |
|
103
|
|
|
|
|
104
|
|
|
def test_is_running_empty(self): |
|
105
|
|
|
"""Verify no app is running when the list is empty.""" |
|
106
|
|
|
assert False is self.status.is_running(self.application, self.computer) |
|
107
|
|
|
assert 0 == self.status.counter |
|
108
|
|
|
|
|
109
|
|
|
def test_queue(self): |
|
110
|
|
|
"""Verify queuing an application sets the next computer.""" |
|
111
|
|
|
self.status.queue(self.application, self.computer) |
|
112
|
|
|
app_status = self.status.find(self.application) |
|
113
|
|
|
assert self.computer.name == app_status.next |
|
114
|
|
|
|
|
115
|
|
|
def test_start(self): |
|
116
|
|
|
"""Verify starting an application adds it to the list.""" |
|
117
|
|
|
self.status.start(self.application, self.computer) |
|
118
|
|
|
names = [status.application for status in self.status.applications] |
|
119
|
|
|
assert self.application.name in names |
|
120
|
|
|
assert 1 == self.status.counter |
|
121
|
|
|
|
|
122
|
|
|
def test_stop(self): |
|
123
|
|
|
"""Verify stopping an application adds it to the list.""" |
|
124
|
|
|
self.status.stop(self.application, self.computer) |
|
125
|
|
|
names = [status.application for status in self.status.applications] |
|
126
|
|
|
assert self.application.name in names |
|
127
|
|
|
assert 1 == self.status.counter |
|
128
|
|
|
|
|
129
|
|
|
def test_start_again(self): |
|
130
|
|
|
"""Verify starting a known application increments the counter.""" |
|
131
|
|
|
self.status.start(self.application, self.computer) |
|
132
|
|
|
self.status.start(self.application, self.computer) |
|
133
|
|
|
self.status.start(self.application, self.computer2) |
|
134
|
|
|
assert self.status.is_running(self.application, self.computer) |
|
135
|
|
|
assert self.status.is_running(self.application, self.computer2) |
|
136
|
|
|
assert 3 == self.status.counter |
|
137
|
|
|
|
|
138
|
|
|
def test_stop_again(self): |
|
139
|
|
|
"""Verify stopping a known application increments the counter.""" |
|
140
|
|
|
self.status.stop(self.application, self.computer) |
|
141
|
|
|
self.status.stop(self.application, self.computer) |
|
142
|
|
|
self.status.stop(self.application, self.computer2) |
|
143
|
|
|
assert not self.status.is_running(self.application, self.computer) |
|
144
|
|
|
assert not self.status.is_running(self.application, self.computer2) |
|
145
|
|
|
assert 3 == self.status.counter |
|
146
|
|
|
|