~launchpad-results/launchpad-results/trunk

« back to all changes in this revision

Viewing changes to lib/lpresults/tracker/schema/test/tests/test_testresult.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:
33
33
        self.store.execute(
34
34
            "INSERT INTO projectseriestwin VALUES (?, ?)",
35
35
            (self.project_series, self.project),
 
36
            noresult=True,
36
37
            )
37
38
 
38
39
        name = self.factory.getUniqueString(u"test-name")
49
50
            (self.project_series,),
50
51
            )
51
52
 
52
 
    def getWhereCondition(self, status=None, output=None, date_created=None):
53
 
        where = ["test_case = %d" % self.test_case]
 
53
    def getTestResultQuery(
 
54
        self, status=None, output=None, date_created=None, date_deleted=None):
 
55
        query = ["test_case = %d" % self.test_case]
54
56
        if status is not None:
55
 
            where.append("status = %d" % status)
 
57
            query.append("status = %d" % status)
56
58
        if output is not None:
57
 
            where.append("output = '%s'" % output)
 
59
            query.append("output = '%s'" % output)
58
60
        if date_created is not None:
59
 
            where.append("date_created = '%s'" % date_created)
 
61
            query.append("date_created = '%s'" % date_created)
 
62
        if date_deleted is not None:
 
63
            query.append("date_deleted = '%s'" % date_deleted)
60
64
 
61
 
        return " AND ".join(where)
 
65
        return " AND ".join(query)
62
66
 
63
67
    def insertTestResult(self, status=None, output=None, date_created=None):
64
68
        if status is None:
71
75
        self.store.execute(
72
76
            "INSERT INTO testresult VALUES (?, ?, ?, ?)",
73
77
            (self.test_case, status, output, date_created,),
 
78
            noresult=True,
74
79
            )
75
80
 
76
81
    def removeTestResult(self, **kwargs):
77
 
        where_condition = self.getWhereCondition(**kwargs)
78
 
        self.store.execute(
79
 
            "DELETE FROM testresult WHERE %s"
80
 
            % where_condition)
 
82
        query = self.getTestResultQuery(**kwargs)
 
83
        self.store.execute("DELETE FROM testresult WHERE %s" % query)
81
84
 
82
85
    def countTestResults(self, **kwargs):
83
 
        where_condition = self.getWhereCondition(**kwargs)
 
86
        query = self.getTestResultQuery(**kwargs)
84
87
        return self.store.execute(
85
 
            "SELECT COUNT(*) FROM testresult WHERE %s"
86
 
            % where_condition).get_one()[0]
 
88
            "SELECT COUNT(*) FROM testresult WHERE %s" % query).get_one()[0]
87
89
 
88
 
    def searchTestResults(self, **kwargs):
 
90
    def getTestResult(self, **kwargs):
89
91
        keys = [
90
 
            "test_case",
91
 
            "status",
92
 
            "output",
93
 
            "date_created",
94
 
            "date_deleted"]
 
92
            "test_case", "status", "output", "date_created", "date_deleted"]
95
93
        select_expression = ", ".join(keys)
96
 
        where_condition = self.getWhereCondition(**kwargs)
 
94
        query = self.getTestResultQuery(**kwargs)
97
95
        values = self.store.execute(
98
 
            "SELECT %s FROM testresult WHERE %s"
99
 
            % (select_expression, where_condition)).get_all()
 
96
            "SELECT %s FROM testresult WHERE %s ORDER BY date_created"
 
97
            % (select_expression, query)).get_all()
 
98
        if len(values) != 1:
 
99
            self.fail("One test result expected, %d found" % len(values))
100
100
 
101
 
        return [dict(zip(keys, value)) for value in values]
 
101
        return dict(zip(keys, values[0]))
102
102
 
103
103
    def assertCountTestResults(self, count, **kwargs):
104
104
        test_result_count = self.countTestResults(**kwargs)
112
112
        self.insertTestResult()
113
113
        self.assertCountTestResults(1)
114
114
 
115
 
        test_result = self.searchTestResults()[0]
 
115
        # Only the first test result should exist
 
116
        test_result = self.getTestResult()
116
117
        self.assertEquals(test_result["test_case"], self.test_case)
117
118
        self.assertEquals(test_result["status"], self.default_status)
118
119
        self.assertEquals(test_result["output"], self.default_output)
119
120
        self.assertEquals(
120
 
            test_result["date_created"],
121
 
            self.default_date_created)
 
121
            test_result["date_created"], self.default_date_created)
122
122
        self.assertTrue(
123
123
            test_result["date_deleted"] > self.default_date_created)
124
124
 
134
134
        self.assertCountTestResults(2)
135
135
 
136
136
        # Both test results should exist
137
 
        first_result = self.searchTestResults(date_created=first_date)[0]
 
137
        first_result = self.getTestResult(date_created=first_date)
138
138
        self.assertNotEquals(first_result, None)
139
 
        second_result = self.searchTestResults(date_created=second_date)[0]
 
139
        second_result = self.getTestResult(date_created=second_date)
140
140
        self.assertNotEquals(second_result, None)
141
141
 
142
142
        # The date_deleted of the first test result
154
154
        # There should be two rows after creating different test results
155
155
        self.insertTestResult(status=0, date_created=first_date)
156
156
        self.insertTestResult(status=0, date_created=second_date)
157
 
        self.assertCountTestResults(1)
158
157
 
159
158
        # Only the first test result should exist
160
 
        test_results = self.searchTestResults(date_created=first_date)
161
 
        self.assertEquals(len(test_results), 1)
162
 
        test_result = test_results[0]
 
159
        test_result = self.getTestResult(date_created=first_date)
163
160
        self.assertEquals(test_result["status"], 0)
164
161
        self.assertEquals(test_result["date_created"], first_date)
165
162
 
166
 
        test_results = self.searchTestResults(date_created=second_date)
167
 
        self.assertEquals(len(test_results), 0)
 
163
        self.assertCountTestResults(0, date_created=second_date)
168
164
 
169
165
    def test_insertTwoSimilarResultsReversed(self):
170
166
        first_date = datetime(2010, 1, 1)
175
171
        # Add test results in reversed order
176
172
        self.insertTestResult(status=0, date_created=second_date)
177
173
        self.insertTestResult(status=0, date_created=first_date)
178
 
        self.assertCountTestResults(1)
179
174
 
180
175
        # Only the first test result should exist
181
 
        test_results = self.searchTestResults(date_created=first_date)
182
 
        self.assertEquals(len(test_results), 1)
183
 
        test_result = test_results[0]
 
176
        test_result = self.getTestResult(date_created=first_date)
184
177
        self.assertEquals(test_result["status"], 0)
185
178
        self.assertEquals(test_result["date_created"], first_date)
186
179
 
187
 
        test_results = self.searchTestResults(date_created=second_date)
188
 
        self.assertEquals(len(test_results), 0)
189
 
 
190
 
 
191
 
class TestTestResultRemove(TestResultTestCase):
192
 
 
193
 
    def test_deleteOneResult(self):
194
 
        self.assertCountTestResults(0)
195
 
        self.insertTestResult()
196
 
        self.assertCountTestResults(1)
197
 
        self.removeTestResult()
198
 
        self.assertCountTestResults(0)
199
 
 
200
 
    def test_deleteFirstOfTwoResults(self):
201
 
        first_date = datetime(2010, 1, 1)
202
 
        self.assertCountTestResults(0, date_created=first_date)
203
 
        second_date = datetime(2010, 1, 2)
204
 
        self.assertCountTestResults(0, date_created=second_date)
205
 
 
206
 
        # There should be two rows after creating different test results
207
 
        self.insertTestResult(status=0, date_created=first_date)
208
 
        self.insertTestResult(status=1, date_created=second_date)
209
 
        self.assertCountTestResults(2)
210
 
 
211
 
        # There should only be one test result
212
 
        # after removing the first one
213
 
        self.removeTestResult(date_created=first_date)
214
 
        self.assertCountTestResults(1)
215
 
 
216
 
        # The date_created of the second test result should no longer exist
217
 
        removed_results = self.searchTestResults(date_created=second_date)
218
 
        self.assertEquals(removed_results, [])
219
 
 
220
 
        # Instead, the date_created of the second test result should now
221
 
        # be the same as the first test result
222
 
        remaining_results = self.searchTestResults(date_created=first_date)
223
 
        self.assertNotEquals(remaining_results, [])
224
 
 
225
 
        # There should no longer be any test results
226
 
        # after removing the last one
227
 
        self.removeTestResult(date_created=first_date)
228
 
        self.assertCountTestResults(0)
229
 
 
230
 
    def test_deleteSecondOfTwoResults(self):
231
 
        first_date = datetime(2010, 1, 1)
232
 
        self.assertCountTestResults(0, date_created=first_date)
233
 
        second_date = datetime(2010, 1, 2)
234
 
        self.assertCountTestResults(0, date_created=second_date)
235
 
 
236
 
        # There should be two test results after creating different ones
237
 
        self.insertTestResult(status=0, date_created=first_date)
238
 
        self.insertTestResult(status=1, date_created=second_date)
239
 
        self.assertCountTestResults(2)
240
 
 
241
 
        # The date_deleted of the first test result
242
 
        # should be the same as the date_created of the second test result
243
 
        first_result = self.searchTestResults(date_created=first_date)[0]
244
 
        second_result = self.searchTestResults(date_created=second_date)[0]
245
 
        self.assertEquals(
246
 
            first_result["date_deleted"],
247
 
            second_result["date_created"])
248
 
 
249
 
        # There should only be one test result
250
 
        # after removing the second one
251
 
        self.removeTestResult(date_created=second_date)
252
 
        self.assertCountTestResults(1)
253
 
 
254
 
        # The date_deleted of the first test result
255
 
        # should be adjusted to the same as the second test result
256
 
        remaining_result = self.searchTestResults(date_created=first_date)[0]
257
 
        self.assertEquals(
258
 
            remaining_result["date_deleted"],
259
 
            second_result["date_deleted"])
260
 
 
261
 
        # There should no longer be any test results
262
 
        # after removing the last one
263
 
        self.removeTestResult(date_created=first_date)
264
 
        self.assertCountTestResults(0)
265
 
 
266
 
    def test_deleteSecondOfThreeDifferentResults(self):
267
 
        first_date = datetime(2010, 1, 1)
268
 
        self.assertCountTestResults(0, date_created=first_date)
269
 
        second_date = datetime(2010, 1, 2)
270
 
        self.assertCountTestResults(0, date_created=second_date)
271
 
        third_date = datetime(2010, 1, 3)
272
 
        self.assertCountTestResults(0, date_created=third_date)
273
 
 
274
 
        # There should be three test results after creating different ones
275
 
        self.insertTestResult(status=0, date_created=first_date)
276
 
        self.insertTestResult(status=1, date_created=second_date)
277
 
        self.insertTestResult(status=2, date_created=third_date)
278
 
        self.assertCountTestResults(3)
279
 
 
280
 
        # There should only be two test results
281
 
        # after removing the second one
282
 
        self.removeTestResult(date_created=second_date)
283
 
        self.assertCountTestResults(2)
284
 
 
285
 
        # The date_deleted of the first test result
286
 
        # should be the same as the date_created of the third test result
287
 
        first_result = self.searchTestResults(date_created=first_date)[0]
288
 
        self.assertEquals(first_result["date_deleted"], third_date)
289
 
 
290
 
        # The date_created of the third test result
291
 
        # should be the same as before
292
 
        third_result = self.searchTestResults(date_created=third_date)[0]
293
 
        self.assertEquals(third_result["date_created"], third_date)
294
 
 
295
 
    def test_deleteSecondOfThreeSimilarResults(self):
296
 
        first_date = datetime(2010, 1, 1)
297
 
        self.assertCountTestResults(0, date_created=first_date)
298
 
        second_date = datetime(2010, 1, 2)
299
 
        self.assertCountTestResults(0, date_created=second_date)
300
 
        third_date = datetime(2010, 1, 3)
301
 
        self.assertCountTestResults(0, date_created=third_date)
302
 
 
303
 
        # There should be three test results after creating different ones
304
 
        self.insertTestResult(status=0, date_created=first_date)
305
 
        self.insertTestResult(status=1, date_created=second_date)
306
 
        self.insertTestResult(status=0, date_created=third_date)
307
 
        self.assertCountTestResults(3)
308
 
 
309
 
        third_result = self.searchTestResults(date_created=third_date)[0]
310
 
 
311
 
        # There should only be one test result
312
 
        # after removing the second one
313
 
        self.removeTestResult(date_created=second_date)
314
 
        self.assertCountTestResults(1)
315
 
 
316
 
        # The date_created of the remaining test result
317
 
        # should be the same as the date_created of the first test result
318
 
        remaining_result = self.searchTestResults(date_created=first_date)[0]
319
 
        self.assertNotEquals(remaining_result, None)
320
 
        self.assertEquals(remaining_result["date_created"], first_date)
321
 
 
322
 
        # The date_deleted of the remaining test result
323
 
        # should be the same as the date_deleted of the third test result
324
 
        self.assertEquals(
325
 
            remaining_result["date_deleted"],
326
 
            third_result["date_deleted"])
 
180
        self.assertCountTestResults(0, date_created=second_date)
327
181
 
328
182
 
329
183
def test_suite():