1
# Copyright 2017 Canonical Ltd.
3
# This program is free software: you can redistribute it and/or modify it
4
# under the terms of the GNU General Public License version 3, as published
5
# by the Free Software Foundation.
7
# This program is distributed in the hope that it will be useful, but
8
# WITHOUT ANY WARRANTY; without even the implied warranties of
9
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
10
# PURPOSE. See the GNU General Public License for more details.
12
# You should have received a copy of the GNU General Public License along
13
# with this program. If not, see <http://www.gnu.org/licenses/>.
16
from unittest import TestCase
17
from libertine.service import operations_monitor, download
20
class TestOperationsMonitor(TestCase):
22
self._connection = unittest.mock.Mock()
24
def test_new_operation_returns_some_id(self):
25
with unittest.mock.patch('dbus.service.Object'):
26
monitor = operations_monitor.OperationsMonitor(self._connection)
27
monitor._connection = self._connection
29
with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
30
self.assertIsNotNone(monitor.new_operation())
32
def test_remove_connection(self):
33
with unittest.mock.patch('dbus.service.Object'):
34
monitor = operations_monitor.OperationsMonitor(self._connection)
35
monitor._connection = self._connection
37
with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
38
monitor.remove_from_connection(monitor.new_operation())
39
MockDownload.return_value.remove_from_connection.assert_called_once_with()
41
def test_returns_done_for_operation(self):
42
with unittest.mock.patch('dbus.service.Object'):
43
monitor = operations_monitor.OperationsMonitor(self._connection)
44
monitor._connection = self._connection
46
with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
47
MockDownload.return_value.done = True
48
self.assertTrue(monitor.done(monitor.new_operation()))
50
MockDownload.return_value.done = False
51
self.assertFalse(monitor.done(monitor.new_operation()))
53
# non-existent operation
54
self.assertFalse(monitor.done("123456"))
56
def test_running_returns_download_state(self):
57
with unittest.mock.patch('dbus.service.Object'):
58
monitor = operations_monitor.OperationsMonitor(self._connection)
59
monitor._connection = self._connection
61
with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
62
MockDownload.return_value.done = True
63
self.assertFalse(monitor.running(monitor.new_operation()))
65
MockDownload.return_value.done = False
66
self.assertTrue(monitor.running(monitor.new_operation()))
68
# non-existent operation
69
self.assertFalse(monitor.running("123456"))
71
def test_result_returns_download_results(self):
72
with unittest.mock.patch('dbus.service.Object'):
73
monitor = operations_monitor.OperationsMonitor(self._connection)
74
monitor._connection = self._connection
76
with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
77
MockDownload.return_value.result = "pokemongus"
78
self.assertEqual("pokemongus", monitor.result(monitor.new_operation()))
80
# non-existent operation
81
self.assertEqual("", monitor.result("123456"))
83
def test_last_error_returns_download_last_errors(self):
84
with unittest.mock.patch('dbus.service.Object'):
85
monitor = operations_monitor.OperationsMonitor(self._connection)
86
monitor._connection = self._connection
88
with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
89
MockDownload.return_value.last_error = "pokemongus"
90
self.assertEqual("pokemongus", monitor.last_error(monitor.new_operation()))
92
# non-existent operation
93
self.assertEqual("", monitor.last_error("123456"))
95
def test_forwards_finished(self):
96
with unittest.mock.patch('dbus.service.Object'):
97
monitor = operations_monitor.OperationsMonitor(self._connection)
98
monitor._connection = self._connection
99
monitor._locations = []
101
with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
102
path = monitor.new_operation()
103
monitor.finished(path)
104
MockDownload.return_value.finished.assert_called_once_with(path)
106
# test does not crash on empty
107
MockDownload.reset_mock()
108
monitor.finished("some/junk")
109
MockDownload.return_value.finished.assert_not_called()
111
def test_forwards_error(self):
112
with unittest.mock.patch('dbus.service.Object'):
113
monitor = operations_monitor.OperationsMonitor(self._connection)
114
monitor._connection = self._connection
115
monitor._locations = []
117
with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
118
path = monitor.new_operation()
119
monitor.error(path, "something messed up")
120
MockDownload.return_value.error.assert_called_once_with("something messed up")
122
# test does not crash on empty
123
MockDownload.reset_mock()
124
monitor.error("some/junk", "something messed up")
125
MockDownload.return_value.error.assert_not_called()
127
def test_forwards_data(self):
128
with unittest.mock.patch('dbus.service.Object'):
129
monitor = operations_monitor.OperationsMonitor(self._connection)
130
monitor._connection = self._connection
131
monitor._locations = []
133
with unittest.mock.patch('libertine.service.operations_monitor.download.Download') as MockDownload:
134
path = monitor.new_operation()
135
monitor.data(path, "some of that gud data")
136
MockDownload.return_value.data.assert_called_once_with("some of that gud data")
138
# test does not crash on empty
139
MockDownload.reset_mock()
140
monitor.data("some/junk", "some of that gud data")
141
MockDownload.return_value.data.assert_not_called()
145
if __name__ == '__main__':