2
# Copyright (C) 2015 Canonical
4
# This program is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
from bson import ObjectId
19
import unittest.mock as mock
21
import qakit.kpi.practitest.tests as tests
24
UPDATED_PRACTITEST_TEST = {
25
'last_run': '26-Feb-2015 11:44',
27
'name': ('Complete wizard successfully '
28
'(English, SIM inserted, Wi-Fi connected)'),
29
'run_status': 'PASSED',
30
'updated_at': '25-Feb-2015 23:15',
35
UNUPDATED_PRACTITEST_TEST = {
36
'last_run': '26-Feb-2015 11:44',
38
'name': ('Complete wizard successfully '
39
'(English, SIM inserted, Wi-Fi connected)'),
40
'run_status': 'PASSED',
41
'updated_at': '25-Feb-2015 23:14',
47
NEW_PRACTITEST_TEST = {
48
'last_run': '26-Feb-2015 11:44',
50
'name': ('Complete wizard successfully '
51
'(English, SIM inserted, Wi-Fi connected)'),
52
'run_status': 'PASSED',
53
'updated_at': '25-Feb-2015 23:15',
60
'last_run': {'value': '26-Feb-2015 18:44', 'name': 'Last Run'},
61
'name': 'Complete wizard successfully (English, SIM inserted, Wi-Fi connected)', # NOQA
62
'run_status': {'value': 'PASSED', 'name': 'Run Status'},
63
'___f_9063': {'value': 'no', 'name': 'Automated'},
64
'___f_9061': {'value': '1-High', 'name': 'Priority'},
65
'___f_8660': {'value': None, 'name': 'Devices'},
66
'___f_9068': {'value': None, 'name': 'Subsystem'},
67
'___f_9069': {'value': None, 'name': 'Component'},
68
'description': 'This test requires that the Welcome Wizard is enabled. The ideal way to enable the wizard is to reset the device, but if this is not possible you can run phablet-config welcome-wizard --enable and restart lightdm. This is a change', # NOQA
69
'tags': {'value': '', 'name': 'Tags (comma separated)'},
70
'___f_9070': {'value': None, 'name': 'Release'},
71
'___f_8658': {'value': None, 'name': 'Execution Duration'},
73
'test_status': {'value': 'Ready', 'name': 'Status'},
74
'updated_at': {'value': '25-Feb-2015 23:14', 'name': 'Last Modified'},
75
'___f_9136': {'value': None, 'name': 'Pre-requisites and setup'},
76
'___f_9062': {'value': '["Sanity"]', 'name': 'Test Level'},
77
'___f_9065': {'value': None, 'name': 'Whiteboard'},
78
'author': {'value': 'jean-baptiste.lallement@canonical.com',
80
'___f_9064': {'value': 'ubuntu_sanity_tests.tests.test_settings_wizard.SettingsWizardTestCase.test_complete_settings_wizard', # NOQA
81
'name': 'External ID'},
82
'created_at': {'value': '09-Feb-2015 18:40', 'name': 'Created'},
88
{'last_run': '09-Feb-2015 11:55',
90
'name': 'adb to locked device',
91
'run_status': 'NO RUN',
92
'updated_at': '24-Jan-2015 10:16',
95
{'last_run': '09-Feb-2015 11:55',
97
'name': 'adb to unlocked device',
98
'run_status': 'NO RUN',
99
'updated_at': '24-Jan-2015 10:18',
101
'system_id': 362867},
102
{'last_run': '09-Feb-2015 11:55',
104
'name': 'adb to locked device - shell enabled',
105
'run_status': 'NO RUN',
106
'updated_at': '24-Jan-2015 10:28',
108
'system_id': 362868},
109
{'last_run': '26-Feb-2015 11:44',
111
'name': ('Complete wizard successfully '
112
'(English, SIM inserted, Wi-Fi connected)'),
113
'run_status': 'PASSED',
114
'updated_at': '25-Feb-2015 16:14',
116
'system_id': 371857},
117
{'run_status': 'FAILED',
118
'test_status': 'Ready',
120
'name': 'Discover Bluetooth devices',
122
'updated_at': '03-Mar-2015 16:07',
123
'last_run': '03-Mar-2015 17:22',
127
UPSERT_UPDATED_ONE = {'nModified': 1, 'updatedExisting': True, 'n': 1, 'ok': 1}
130
class IsUpdatedTestCase(unittest.TestCase):
132
def test_is_updated(self):
135
find_one=mock.Mock(return_value=DB_TEST)))
136
self.assertTrue(tests.is_updated(db, UPDATED_PRACTITEST_TEST))
138
def test_is_not_updated(self):
141
find_one=mock.Mock(return_value=DB_TEST)))
142
self.assertFalse(tests.is_updated(db, UNUPDATED_PRACTITEST_TEST))
144
def test_new_test_throws(self):
147
find_one=mock.Mock(side_effect=ValueError)))
148
with self.assertRaises(ValueError):
149
tests.is_updated(db, NEW_PRACTITEST_TEST)
152
@mock.patch('qakit.kpi.practitest.tests.is_updated')
153
class IsNewOrIsUpdatedTestCase(unittest.TestCase):
155
def test_is_updated(self, mock_is_updated):
156
mock_is_updated.return_value = True
157
self.assertTrue(tests.is_new_or_is_updated(
158
'fake_db', 'updated_practitest_test'))
160
def test_is_not_updated(self, mock_is_updated):
161
mock_is_updated.return_value = False
162
self.assertFalse(tests.is_new_or_is_updated(
163
'fake_db', 'unupdated_practitest_test'))
165
def test_is_new(self, mock_is_updated):
166
mock_is_updated.side_effect = ValueError
167
self.assertTrue(tests.is_new_or_is_updated(
168
'fake_db', 'new_practitest_test'))
171
class RetrieveTestsInDetailTestCase(unittest.TestCase):
173
@mock.patch('qakit.kpi.practitest.tests._sum_results_queue')
174
@mock.patch('qakit.kpi.practitest.tests.retrieve_test')
175
def test_retrieval_in_detail(self,
177
mock_sum_results_queue):
178
"""Large test of retrieval.
180
Don't touch anything external but let all the multiprocess
184
mock_sum_results_queue.return_value = {'updated': 1,
186
mock_retrieve_test.return_value = UPSERT_UPDATED_ONE
187
tests.retrieve_tests_in_detail('fake_db',
190
# FIXME: multiprocessing incompatible with nose?
191
# self.assertEqual(5, mock_retrieve_test.call_count)
194
class RetrieveTestsTestCase(unittest.TestCase):
196
@mock.patch('qakit.kpi.practitest.tests.filter_tests')
197
@mock.patch('qakit.kpi.practitest.tests.retrieve_tests_in_detail')
198
def test_retrieval(self,
201
# TODO: more assertions pls
202
mock_session = mock.Mock()
203
mock_filter_tests.return_value = TESTS
204
mock_retrieve.return_value = {'updated': 1,
206
tests.retrieve_tests('fake_db', mock_session)
207
mock_retrieve.assert_called()
210
class SumResultsTestCase(unittest.TestCase):
212
def test_sum_results_returns_results(self):
213
results = [{'nModified': 1, 'updatedExisting': True, 'n': 1, 'ok': 1},
215
'updatedExisting': False,
217
'upserted': ObjectId('55ba80888f4a674823d435fd'),
219
self.assertEqual({'inserted': 1, 'updated': 1},
220
tests._sum_results(results))
222
def test_sum_results_returns_nothing(self):
223
self.assertEqual({'inserted': 0, 'updated': 0},
224
tests._sum_results([]))
226
def test_sum_results_returns_multi(self):
227
results = [{'nModified': 1, 'updatedExisting': True, 'n': 1, 'ok': 1},
228
{'nModified': 1, 'updatedExisting': True, 'n': 1, 'ok': 1},
229
{'nModified': 1, 'updatedExisting': True, 'n': 1, 'ok': 1},
230
{'nModified': 1, 'updatedExisting': True, 'n': 1, 'ok': 1},
231
{'nModified': 1, 'updatedExisting': True, 'n': 1, 'ok': 1}]
232
self.assertEqual({'inserted': 0, 'updated': 5},
233
tests._sum_results(results))