~allanlesage/qakit/jibel-report

« back to all changes in this revision

Viewing changes to qakit/kpi/tests/test_practitest_tests.py

  • Committer: Allan LeSage
  • Date: 2015-07-31 03:42:24 UTC
  • Revision ID: allan.lesage@canonical.com-20150731034224-tq77xl6qsqo9ryob
Updated PractiTest tests slurping.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# UEQA KPI Utilities
 
2
# Copyright (C) 2015 Canonical
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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/>.
 
16
 
 
17
from bson import ObjectId
 
18
import unittest
 
19
import unittest.mock as mock
 
20
 
 
21
import qakit.kpi.practitest.tests as tests
 
22
 
 
23
 
 
24
UPDATED_PRACTITEST_TEST = {
 
25
    'last_run': '26-Feb-2015  11:44',
 
26
    'display_id': 94,
 
27
    'name': ('Complete wizard successfully '
 
28
             '(English, SIM inserted, Wi-Fi connected)'),
 
29
    'run_status': 'PASSED',
 
30
    'updated_at': '25-Feb-2015  23:15',
 
31
    'steps_count': 9,
 
32
    'system_id': 371857
 
33
}
 
34
 
 
35
UNUPDATED_PRACTITEST_TEST = {
 
36
    'last_run': '26-Feb-2015  11:44',
 
37
    'display_id': 94,
 
38
    'name': ('Complete wizard successfully '
 
39
             '(English, SIM inserted, Wi-Fi connected)'),
 
40
    'run_status': 'PASSED',
 
41
    'updated_at': '25-Feb-2015  23:14',
 
42
    'steps_count': 9,
 
43
    'system_id': 371857
 
44
}
 
45
 
 
46
 
 
47
NEW_PRACTITEST_TEST = {
 
48
    'last_run': '26-Feb-2015  11:44',
 
49
    'display_id': 94,
 
50
    'name': ('Complete wizard successfully '
 
51
             '(English, SIM inserted, Wi-Fi connected)'),
 
52
    'run_status': 'PASSED',
 
53
    'updated_at': '25-Feb-2015  23:15',
 
54
    'steps_count': 9,
 
55
    'system_id': 371858
 
56
}
 
57
 
 
58
 
 
59
DB_TEST = {
 
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'},
 
72
    'system_id': 371857,
 
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',
 
79
               'name': 'Author'},
 
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'},
 
83
    'id': 94
 
84
}
 
85
 
 
86
 
 
87
TESTS = [
 
88
    {'last_run': '09-Feb-2015  11:55',
 
89
     'display_id': 6,
 
90
     'name': 'adb to locked device',
 
91
     'run_status': 'NO RUN',
 
92
     'updated_at': '24-Jan-2015  10:16',
 
93
     'steps_count': 5,
 
94
     'system_id': 362866},
 
95
    {'last_run': '09-Feb-2015  11:55',
 
96
     'display_id': 7,
 
97
     'name': 'adb to unlocked device',
 
98
     'run_status': 'NO RUN',
 
99
     'updated_at': '24-Jan-2015  10:18',
 
100
     'steps_count': 5,
 
101
     'system_id': 362867},
 
102
    {'last_run': '09-Feb-2015  11:55',
 
103
     'display_id': 8,
 
104
     'name': 'adb to locked device - shell enabled',
 
105
     'run_status': 'NO RUN',
 
106
     'updated_at': '24-Jan-2015  10:28',
 
107
     'steps_count': 6,
 
108
     'system_id': 362868},
 
109
    {'last_run': '26-Feb-2015  11:44',
 
110
     'display_id': 94,
 
111
     'name': ('Complete wizard successfully '
 
112
              '(English, SIM inserted, Wi-Fi connected)'),
 
113
     'run_status': 'PASSED',
 
114
     'updated_at': '25-Feb-2015  16:14',
 
115
     'steps_count': 9,
 
116
     'system_id': 371857},
 
117
    {'run_status': 'FAILED',
 
118
     'test_status': 'Ready',
 
119
     'display_id': 109,
 
120
     'name': 'Discover Bluetooth devices',
 
121
     'system_id': 371872,
 
122
     'updated_at': '03-Mar-2015  16:07',
 
123
     'last_run': '03-Mar-2015  17:22',
 
124
     'steps_count': 3},
 
125
]
 
126
 
 
127
UPSERT_UPDATED_ONE = {'nModified': 1, 'updatedExisting': True, 'n': 1, 'ok': 1}
 
128
 
 
129
 
 
130
class IsUpdatedTestCase(unittest.TestCase):
 
131
 
 
132
    def test_is_updated(self):
 
133
        db = mock.Mock(
 
134
            tests=mock.Mock(
 
135
                find_one=mock.Mock(return_value=DB_TEST)))
 
136
        self.assertTrue(tests.is_updated(db, UPDATED_PRACTITEST_TEST))
 
137
 
 
138
    def test_is_not_updated(self):
 
139
        db = mock.Mock(
 
140
            tests=mock.Mock(
 
141
                find_one=mock.Mock(return_value=DB_TEST)))
 
142
        self.assertFalse(tests.is_updated(db, UNUPDATED_PRACTITEST_TEST))
 
143
 
 
144
    def test_new_test_throws(self):
 
145
        db = mock.Mock(
 
146
            tests=mock.Mock(
 
147
                find_one=mock.Mock(side_effect=ValueError)))
 
148
        with self.assertRaises(ValueError):
 
149
            tests.is_updated(db, NEW_PRACTITEST_TEST)
 
150
 
 
151
 
 
152
@mock.patch('qakit.kpi.practitest.tests.is_updated')
 
153
class IsNewOrIsUpdatedTestCase(unittest.TestCase):
 
154
 
 
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'))
 
159
 
 
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'))
 
164
 
 
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'))
 
169
 
 
170
 
 
171
class RetrieveTestsInDetailTestCase(unittest.TestCase):
 
172
 
 
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,
 
176
                                 mock_retrieve_test,
 
177
                                 mock_sum_results_queue):
 
178
        """Large test of retrieval.
 
179
 
 
180
        Don't touch anything external but let all the multiprocess
 
181
        stuff play out.
 
182
 
 
183
        """
 
184
        mock_sum_results_queue.return_value = {'updated': 1,
 
185
                                               'inserted': 5}
 
186
        mock_retrieve_test.return_value = UPSERT_UPDATED_ONE
 
187
        tests.retrieve_tests_in_detail('fake_db',
 
188
                                       'fake_session',
 
189
                                       TESTS)
 
190
        # FIXME: multiprocessing incompatible with nose?
 
191
        # self.assertEqual(5, mock_retrieve_test.call_count)
 
192
 
 
193
 
 
194
class RetrieveTestsTestCase(unittest.TestCase):
 
195
 
 
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,
 
199
                       mock_retrieve,
 
200
                       mock_filter_tests):
 
201
        # TODO: more assertions pls
 
202
        mock_session = mock.Mock()
 
203
        mock_filter_tests.return_value = TESTS
 
204
        mock_retrieve.return_value = {'updated': 1,
 
205
                                      'inserted': 5}
 
206
        tests.retrieve_tests('fake_db', mock_session)
 
207
        mock_retrieve.assert_called()
 
208
 
 
209
 
 
210
class SumResultsTestCase(unittest.TestCase):
 
211
 
 
212
    def test_sum_results_returns_results(self):
 
213
        results = [{'nModified': 1, 'updatedExisting': True, 'n': 1, 'ok': 1},
 
214
                   {'nModified': 0,
 
215
                    'updatedExisting': False,
 
216
                    'n': 1,
 
217
                    'upserted': ObjectId('55ba80888f4a674823d435fd'),
 
218
                    'ok': 1}]
 
219
        self.assertEqual({'inserted': 1, 'updated': 1},
 
220
                         tests._sum_results(results))
 
221
 
 
222
    def test_sum_results_returns_nothing(self):
 
223
        self.assertEqual({'inserted': 0, 'updated': 0},
 
224
                         tests._sum_results([]))
 
225
 
 
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))