~launchpad-results/launchpad-results/trunk

« back to all changes in this revision

Viewing changes to lib/lpresults/tracker/schema/test/tests/test_testrun.py

  • Committer: Marc Tardif
  • Date: 2012-03-21 22:32:04 UTC
  • Revision ID: marc.tardif@canonical.com-20120321223204-8g7mvzzwmh8ifbrt
Added support for getting systems from a person (LP #899361)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010-2011 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
__metaclass__ = type
 
5
 
 
6
__all__ = []
 
7
 
 
8
from datetime import timedelta
 
9
 
 
10
from unittest import defaultTestLoader
 
11
 
 
12
from lpresults.database.constants import INFINITY_DATETIME
 
13
 
 
14
from lpresults.tracker.schema.test.tests.test_testresult import (
 
15
    TestResultTestCase,
 
16
    )
 
17
 
 
18
 
 
19
class TestRunTestCase(TestResultTestCase):
 
20
 
 
21
    def insertTestResult(self, date_created=None, **kwargs):
 
22
        self.insertTestRun(date_created=date_created)
 
23
        super(TestRunTestCase, self).insertTestResult(
 
24
            date_created=date_created, **kwargs)
 
25
 
 
26
    def getTestRunQuery(self, date_created=None):
 
27
        query = ["project_series = %d" % self.project_series]
 
28
        if date_created is not None:
 
29
            query.append("date_created = '%s'" % date_created)
 
30
 
 
31
        return " AND ".join(query)
 
32
 
 
33
    def insertTestRun(self, date_created=None):
 
34
        if date_created is None:
 
35
            date_created = self.default_date_created
 
36
 
 
37
        self.store.execute(
 
38
            "INSERT INTO testrun (project_series, date_created)"
 
39
            " VALUES (?, ?)",
 
40
            (self.project_series, date_created,),
 
41
            noresult=True,
 
42
            )
 
43
 
 
44
    def deleteTestRun(self, date_created=None):
 
45
        if date_created is None:
 
46
            date_created = self.default_date_created
 
47
 
 
48
        self.store.execute(
 
49
            "DELETE FROM testrun"
 
50
            " WHERE project_series = ? AND date_created = ?",
 
51
            (self.project_series, date_created,),
 
52
            noresult=True,
 
53
            )
 
54
 
 
55
    def getTestRun(self, **kwargs):
 
56
        keys = ["project_series", "date_created"]
 
57
        select_expression = ", ".join(keys)
 
58
        query = self.getTestRunQuery(**kwargs)
 
59
        values = self.store.execute(
 
60
            "SELECT %s FROM testrun WHERE %s"
 
61
            % (select_expression, query)).get_all()
 
62
        if len(values) != 1:
 
63
            self.fail("One test run expected, %d found" % len(values))
 
64
 
 
65
        return dict(zip(keys, values[0]))
 
66
 
 
67
    def countTestRuns(self, **kwargs):
 
68
        query = self.getTestRunQuery(**kwargs)
 
69
        return self.store.execute(
 
70
            "SELECT COUNT(*) FROM testrun WHERE %s" % query).get_one()[0]
 
71
 
 
72
    def assertCountTestRuns(self, count, **kwargs):
 
73
        test_run_count = self.countTestRuns(**kwargs)
 
74
        self.assertEquals(test_run_count, count)
 
75
 
 
76
 
 
77
class TestTestRunInsert(TestRunTestCase):
 
78
 
 
79
    def test_insertTestRun(self):
 
80
        self.insertTestRun()
 
81
        self.assertCountTestRuns(1, date_created=self.default_date_created)
 
82
 
 
83
    def test_insertAfterTestResult(self):
 
84
        """
 
85
        Test results immediately before the test run must have the date
 
86
        deleted set to the date created of the test run.
 
87
        """
 
88
        today = self.default_date_created
 
89
        yesterday = today - timedelta(days=1)
 
90
        self.insertTestResult(date_created=yesterday)
 
91
        self.assertCountTestResults(
 
92
            1, date_created=yesterday, date_deleted=INFINITY_DATETIME)
 
93
 
 
94
        self.insertTestRun()
 
95
        self.assertCountTestResults(
 
96
            1, date_created=yesterday, date_deleted=today)
 
97
 
 
98
    def test_insertBeforeTestResult(self):
 
99
        """
 
100
        Test results immediately after the test run must remain exaclty
 
101
        the same.
 
102
        """
 
103
        today = self.default_date_created
 
104
        tomorrow = today + timedelta(days=1)
 
105
        self.insertTestResult(date_created=tomorrow)
 
106
        self.assertCountTestResults(
 
107
            1, date_created=tomorrow, date_deleted=INFINITY_DATETIME)
 
108
 
 
109
        self.insertTestRun()
 
110
        self.assertCountTestResults(
 
111
            1, date_created=tomorrow, date_deleted=INFINITY_DATETIME)
 
112
 
 
113
    def test_insertBetweenTestResults(self):
 
114
        """
 
115
        Test results immediately before the test run must have the date
 
116
        deleted set to the date created of the test run and test results
 
117
        immediately after the test run must remain exactly the same.
 
118
        """
 
119
        today = self.default_date_created
 
120
        yesterday = today - timedelta(days=1)
 
121
        tomorrow = today + timedelta(days=1)
 
122
        self.insertTestResult(status=0, date_created=yesterday)
 
123
        self.insertTestResult(status=1, date_created=tomorrow)
 
124
        self.assertCountTestResults(
 
125
            1, date_created=yesterday, date_deleted=tomorrow)
 
126
        self.assertCountTestResults(
 
127
            1, date_created=tomorrow, date_deleted=INFINITY_DATETIME)
 
128
 
 
129
        self.insertTestRun()
 
130
        self.assertCountTestResults(
 
131
            1, date_created=yesterday, date_deleted=today)
 
132
        self.assertCountTestResults(
 
133
            1, date_created=tomorrow, date_deleted=INFINITY_DATETIME)
 
134
 
 
135
    def test_insertAfterTestResultBeforeTestRun(self):
 
136
        """
 
137
        Test results immediately before the test run must have the date
 
138
        deleted set to the date created of the test run, another test
 
139
        result must be created after the test run and the other test
 
140
        results must remain the same.
 
141
        """
 
142
        today = self.default_date_created
 
143
        yesterday = today - timedelta(days=1)
 
144
        tomorrow = today + timedelta(days=1)
 
145
        dayafter = today + timedelta(days=2)
 
146
 
 
147
        self.insertTestResult(status=0, date_created=yesterday)
 
148
        self.insertTestResult(status=0, date_created=tomorrow)
 
149
        self.insertTestResult(status=1, date_created=dayafter)
 
150
        self.assertCountTestResults(
 
151
            1, date_created=yesterday, date_deleted=dayafter)
 
152
        self.assertCountTestResults(
 
153
            1, date_created=dayafter, date_deleted=INFINITY_DATETIME)
 
154
 
 
155
        self.insertTestRun()
 
156
        self.assertCountTestResults(
 
157
            1, date_created=yesterday, date_deleted=today)
 
158
        self.assertCountTestResults(
 
159
            1, date_created=tomorrow, date_deleted=dayafter)
 
160
        self.assertCountTestResults(
 
161
            1, date_created=dayafter, date_deleted=INFINITY_DATETIME)
 
162
 
 
163
    def test_insertAfterTestRun(self):
 
164
        """
 
165
        Test results before the test run must have the date deleted
 
166
        set to the date created of the test run.
 
167
        """
 
168
        today = self.default_date_created
 
169
        daybefore = today - timedelta(days=2)
 
170
        yesterday = today - timedelta(days=1)
 
171
 
 
172
        self.insertTestResult(date_created=daybefore)
 
173
        self.insertTestResult(date_created=yesterday)
 
174
        self.assertCountTestResults(
 
175
            1, date_created=daybefore, date_deleted=INFINITY_DATETIME)
 
176
 
 
177
        self.insertTestRun()
 
178
        self.assertCountTestResults(
 
179
            1, date_created=daybefore, date_deleted=today)
 
180
 
 
181
    def test_insertBeforeTestRun(self):
 
182
        """
 
183
        Test results after the test run must remain exaclty the same.
 
184
        """
 
185
        today = self.default_date_created
 
186
        tomorrow = today + timedelta(days=1)
 
187
        dayafter = today + timedelta(days=2)
 
188
 
 
189
        self.insertTestResult(date_created=dayafter)
 
190
        self.assertCountTestResults(
 
191
            1, date_created=dayafter, date_deleted=INFINITY_DATETIME)
 
192
 
 
193
        self.insertTestRun(date_created=tomorrow)
 
194
        self.insertTestRun()
 
195
        self.assertCountTestResults(
 
196
            1, date_created=dayafter, date_deleted=INFINITY_DATETIME)
 
197
 
 
198
    def test_insertAfterTestRunBeforeTestResult(self):
 
199
        """
 
200
        Test results before the test run must have the date deleted set
 
201
        to the date created of the test run the test results immediately
 
202
        after must remain the same.
 
203
        """
 
204
        today = self.default_date_created
 
205
        daybefore = today - timedelta(days=2)
 
206
        yesterday = today - timedelta(days=1)
 
207
        tomorrow = today + timedelta(days=1)
 
208
 
 
209
        self.insertTestResult(status=0, date_created=daybefore)
 
210
        self.insertTestResult(status=0, date_created=yesterday)
 
211
        self.insertTestResult(status=1, date_created=tomorrow)
 
212
        self.assertCountTestResults(
 
213
            1, date_created=daybefore, date_deleted=tomorrow)
 
214
        self.assertCountTestResults(
 
215
            1, date_created=tomorrow, date_deleted=INFINITY_DATETIME)
 
216
 
 
217
        self.insertTestRun()
 
218
        self.assertCountTestResults(
 
219
            1, date_created=daybefore, date_deleted=today)
 
220
        self.assertCountTestResults(
 
221
            1, date_created=tomorrow, date_deleted=INFINITY_DATETIME)
 
222
 
 
223
    def test_insertBetweenTestRuns(self):
 
224
        """
 
225
        Test results before the test run must have the date deleted set
 
226
        to the date created of the test run, another test result must
 
227
        be created after the test run and the other test results must
 
228
        remain the same.
 
229
        """
 
230
        today = self.default_date_created
 
231
        daybefore = today - timedelta(days=2)
 
232
        yesterday = today - timedelta(days=1)
 
233
        tomorrow = today + timedelta(days=1)
 
234
        dayafter = today + timedelta(days=2)
 
235
 
 
236
        self.insertTestResult(status=0, date_created=daybefore)
 
237
        self.insertTestResult(status=0, date_created=yesterday)
 
238
        self.insertTestResult(status=0, date_created=tomorrow)
 
239
        self.insertTestResult(status=1, date_created=dayafter)
 
240
        self.assertCountTestResults(
 
241
            1, date_created=daybefore, date_deleted=dayafter)
 
242
        self.assertCountTestResults(
 
243
            1, date_created=dayafter, date_deleted=INFINITY_DATETIME)
 
244
 
 
245
        self.insertTestRun()
 
246
        self.assertCountTestResults(
 
247
            1, date_created=daybefore, date_deleted=today)
 
248
        self.assertCountTestResults(
 
249
            1, date_created=tomorrow, date_deleted=dayafter)
 
250
        self.assertCountTestResults(
 
251
            1, date_created=dayafter, date_deleted=INFINITY_DATETIME)
 
252
 
 
253
    def test_insertFoo(self):
 
254
        today = self.default_date_created
 
255
        yesterday = today - timedelta(days=1)
 
256
        tomorrow = today + timedelta(days=1)
 
257
        self.insertTestResult(status=0, date_created=yesterday)
 
258
        self.insertTestResult(status=0, date_created=tomorrow)
 
259
        self.insertTestRun()
 
260
 
 
261
 
 
262
class TestTestRunDelete(TestRunTestCase):
 
263
 
 
264
    def test_deleteTestRun(self):
 
265
        self.insertTestResult()
 
266
        self.assertCountTestRuns(1)
 
267
        self.assertCountTestResults(1)
 
268
 
 
269
        self.deleteTestRun()
 
270
        self.assertCountTestRuns(0)
 
271
        self.assertCountTestResults(0)
 
272
 
 
273
    def test_deleteMiddle(self):
 
274
        """
 
275
        Test results in the middle of a test run must remain exactly
 
276
        the same.
 
277
        """
 
278
        today = self.default_date_created
 
279
        yesterday = today - timedelta(days=1)
 
280
        self.insertTestResult(date_created=yesterday)
 
281
        self.insertTestResult()
 
282
        self.assertCountTestResults(
 
283
            1, date_created=yesterday, date_deleted=INFINITY_DATETIME)
 
284
 
 
285
        self.deleteTestRun()
 
286
        self.assertCountTestResults(
 
287
            1, date_created=yesterday, date_deleted=INFINITY_DATETIME)
 
288
 
 
289
    def test_deleteBeginning(self):
 
290
        """
 
291
        Test results at the beginning of a test run must have the date
 
292
        created set to the next test run.
 
293
        """
 
294
        today = self.default_date_created
 
295
        tomorrow = today + timedelta(days=1)
 
296
        self.insertTestResult()
 
297
        self.insertTestResult(date_created=tomorrow)
 
298
        self.assertCountTestResults(
 
299
            1, date_created=today, date_deleted=INFINITY_DATETIME)
 
300
 
 
301
        self.deleteTestRun()
 
302
        self.assertCountTestResults(
 
303
            1, date_created=tomorrow, date_deleted=INFINITY_DATETIME)
 
304
 
 
305
    def test_deleteBefore(self):
 
306
        """
 
307
        Test results after the test run must remain exactly the same.
 
308
        """
 
309
        today = self.default_date_created
 
310
        tomorrow = today + timedelta(days=1)
 
311
        self.insertTestResult(date_created=tomorrow)
 
312
        self.assertCountTestResults(
 
313
            1, date_created=tomorrow, date_deleted=INFINITY_DATETIME)
 
314
 
 
315
        self.deleteTestRun()
 
316
        self.assertCountTestResults(
 
317
            1, date_created=tomorrow, date_deleted=INFINITY_DATETIME)
 
318
 
 
319
    def test_deleteBetweenSame(self):
 
320
        """
 
321
        Test results before and after the test run must be merged if
 
322
        they are the same.
 
323
        """
 
324
        today = self.default_date_created
 
325
        yesterday = today - timedelta(days=1)
 
326
        tomorrow = today + timedelta(days=1)
 
327
        self.insertTestResult(status=0, date_created=yesterday)
 
328
        self.insertTestResult(status=1)
 
329
        self.insertTestResult(status=0, date_created=tomorrow)
 
330
        self.assertCountTestResults(
 
331
            1, date_created=yesterday, date_deleted=today)
 
332
        self.assertCountTestResults(
 
333
            1, date_created=today, date_deleted=tomorrow)
 
334
        self.assertCountTestResults(
 
335
            1, date_created=tomorrow, date_deleted=INFINITY_DATETIME)
 
336
 
 
337
        self.deleteTestRun()
 
338
        self.assertCountTestResults(
 
339
            1, date_created=yesterday, date_deleted=INFINITY_DATETIME)
 
340
 
 
341
 
 
342
def test_suite():
 
343
    return defaultTestLoader.loadTestsFromName(__name__)