1
# Copyright 2010-2011 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
8
from datetime import timedelta
10
from unittest import defaultTestLoader
12
from lpresults.database.constants import INFINITY_DATETIME
14
from lpresults.tracker.schema.test.tests.test_testresult import (
19
class TestRunTestCase(TestResultTestCase):
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)
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)
31
return " AND ".join(query)
33
def insertTestRun(self, date_created=None):
34
if date_created is None:
35
date_created = self.default_date_created
38
"INSERT INTO testrun (project_series, date_created)"
40
(self.project_series, date_created,),
44
def deleteTestRun(self, date_created=None):
45
if date_created is None:
46
date_created = self.default_date_created
50
" WHERE project_series = ? AND date_created = ?",
51
(self.project_series, date_created,),
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()
63
self.fail("One test run expected, %d found" % len(values))
65
return dict(zip(keys, values[0]))
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]
72
def assertCountTestRuns(self, count, **kwargs):
73
test_run_count = self.countTestRuns(**kwargs)
74
self.assertEquals(test_run_count, count)
77
class TestTestRunInsert(TestRunTestCase):
79
def test_insertTestRun(self):
81
self.assertCountTestRuns(1, date_created=self.default_date_created)
83
def test_insertAfterTestResult(self):
85
Test results immediately before the test run must have the date
86
deleted set to the date created of the test run.
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)
95
self.assertCountTestResults(
96
1, date_created=yesterday, date_deleted=today)
98
def test_insertBeforeTestResult(self):
100
Test results immediately after the test run must remain exaclty
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)
110
self.assertCountTestResults(
111
1, date_created=tomorrow, date_deleted=INFINITY_DATETIME)
113
def test_insertBetweenTestResults(self):
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.
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)
130
self.assertCountTestResults(
131
1, date_created=yesterday, date_deleted=today)
132
self.assertCountTestResults(
133
1, date_created=tomorrow, date_deleted=INFINITY_DATETIME)
135
def test_insertAfterTestResultBeforeTestRun(self):
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.
142
today = self.default_date_created
143
yesterday = today - timedelta(days=1)
144
tomorrow = today + timedelta(days=1)
145
dayafter = today + timedelta(days=2)
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)
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)
163
def test_insertAfterTestRun(self):
165
Test results before the test run must have the date deleted
166
set to the date created of the test run.
168
today = self.default_date_created
169
daybefore = today - timedelta(days=2)
170
yesterday = today - timedelta(days=1)
172
self.insertTestResult(date_created=daybefore)
173
self.insertTestResult(date_created=yesterday)
174
self.assertCountTestResults(
175
1, date_created=daybefore, date_deleted=INFINITY_DATETIME)
178
self.assertCountTestResults(
179
1, date_created=daybefore, date_deleted=today)
181
def test_insertBeforeTestRun(self):
183
Test results after the test run must remain exaclty the same.
185
today = self.default_date_created
186
tomorrow = today + timedelta(days=1)
187
dayafter = today + timedelta(days=2)
189
self.insertTestResult(date_created=dayafter)
190
self.assertCountTestResults(
191
1, date_created=dayafter, date_deleted=INFINITY_DATETIME)
193
self.insertTestRun(date_created=tomorrow)
195
self.assertCountTestResults(
196
1, date_created=dayafter, date_deleted=INFINITY_DATETIME)
198
def test_insertAfterTestRunBeforeTestResult(self):
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.
204
today = self.default_date_created
205
daybefore = today - timedelta(days=2)
206
yesterday = today - timedelta(days=1)
207
tomorrow = today + timedelta(days=1)
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)
218
self.assertCountTestResults(
219
1, date_created=daybefore, date_deleted=today)
220
self.assertCountTestResults(
221
1, date_created=tomorrow, date_deleted=INFINITY_DATETIME)
223
def test_insertBetweenTestRuns(self):
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
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)
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)
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)
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)
262
class TestTestRunDelete(TestRunTestCase):
264
def test_deleteTestRun(self):
265
self.insertTestResult()
266
self.assertCountTestRuns(1)
267
self.assertCountTestResults(1)
270
self.assertCountTestRuns(0)
271
self.assertCountTestResults(0)
273
def test_deleteMiddle(self):
275
Test results in the middle of a test run must remain exactly
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)
286
self.assertCountTestResults(
287
1, date_created=yesterday, date_deleted=INFINITY_DATETIME)
289
def test_deleteBeginning(self):
291
Test results at the beginning of a test run must have the date
292
created set to the next test run.
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)
302
self.assertCountTestResults(
303
1, date_created=tomorrow, date_deleted=INFINITY_DATETIME)
305
def test_deleteBefore(self):
307
Test results after the test run must remain exactly the same.
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)
316
self.assertCountTestResults(
317
1, date_created=tomorrow, date_deleted=INFINITY_DATETIME)
319
def test_deleteBetweenSame(self):
321
Test results before and after the test run must be merged if
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)
338
self.assertCountTestResults(
339
1, date_created=yesterday, date_deleted=INFINITY_DATETIME)
343
return defaultTestLoader.loadTestsFromName(__name__)