Enable returning run id in get_test_runs_by_status_for_run_ids()

This commit adds a new option, include_run_id, to the
get_test_runs_by_status_for_run_ids() DB API method for including the
run uuid in the dictionaries returned by the method. This was an
oversight in the method and needed by users in certain cases, however
we can't just add it by default for backwards compat reasons. So a
bool flag is used to enable the additional field being returned.

Change-Id: Icf203668690419c38f6d6be6b6fe4af8462845f3
This commit is contained in:
Matthew Treinish 2016-05-03 21:37:36 -04:00
parent 2828bcc8c2
commit d73db9057d
No known key found for this signature in database
GPG Key ID: FD12A0F214C9E177
3 changed files with 42 additions and 3 deletions

View File

@ -0,0 +1,4 @@
---
features:
- A new option on get_test_runs_by_status_for_run_ids(), include_run_id, to
include the run uuid in the run dicts returned

View File

@ -1583,7 +1583,7 @@ def get_runs_by_status_grouped_by_run_metadata(key, start_date=None,
def get_test_runs_by_status_for_run_ids(status, run_ids, key=None,
session=None):
session=None, include_run_id=False):
"""Get a list of test run dicts by status for all the specified runs
:param str status: The test status to filter the returned test runs on
@ -1593,6 +1593,8 @@ def get_test_runs_by_status_for_run_ids(status, run_ids, key=None,
to the output dict for each test_run
:param session: optional session object if one isn't provided a new session
will be acquired for the duration of this operation
:param bool include_run_id: boolean flag to enable including the run uuid
in the test run dicts returned
:return test_runs: a list of dicts for the test_runs and associated data
:rtype: list
@ -1614,13 +1616,15 @@ def get_test_runs_by_status_for_run_ids(status, run_ids, key=None,
models.TestRun.start_time_microsecond,
models.TestRun.stop_time,
models.TestRun.stop_time_microsecond,
models.RunMetadata.value)
models.RunMetadata.value,
models.Run.uuid)
else:
results = query.values(models.Test.test_id, models.Run.artifacts,
models.TestRun.start_time,
models.TestRun.start_time_microsecond,
models.TestRun.stop_time,
models.TestRun.stop_time_microsecond)
models.TestRun.stop_time_microsecond,
models.Run.uuid)
test_runs = []
for result in results:
test_run = {
@ -1629,6 +1633,8 @@ def get_test_runs_by_status_for_run_ids(status, run_ids, key=None,
'start_time': result.start_time,
'stop_time': result.stop_time,
}
if include_run_id:
test_run['uuid'] = result.uuid
if result.start_time_microsecond is not None:
test_run['start_time'] = test_run['start_time'].replace(
microsecond=result.start_time_microsecond)

View File

@ -641,6 +641,35 @@ class TestDatabaseAPI(base.TestCase):
'a_key': 'b',
}, result[0])
def test_get_test_runs_by_status_for_run_ids_with_run_id(self):
run_b = api.create_run(artifacts='fake_url')
run_a = api.create_run()
run_c = api.create_run()
test_a = api.create_test('fake_test')
api.add_run_metadata({'a_key': 'b'}, run_b.id)
api.add_run_metadata({'a_key': 'a'}, run_a.id)
api.add_run_metadata({'a_key': 'c'}, run_c.id)
start_timestamp = datetime.datetime(1914, 6, 28, 10, 45, 0)
stop_timestamp = datetime.datetime(1914, 6, 28, 10, 50, 0)
api.create_test_run(test_a.id, run_a.id, 'success',
datetime.datetime.utcnow())
api.create_test_run(test_a.id, run_b.id, 'fail',
start_timestamp, stop_timestamp)
api.create_test_run(test_a.id, run_c.id, 'success',
datetime.datetime.utcnow())
result = api.get_test_runs_by_status_for_run_ids(
'fail', [run_a.uuid, run_b.uuid, run_c.uuid], key='a_key',
include_run_id=True)
self.assertEqual(1, len(result))
self.assertEqual({
'test_id': u'fake_test',
'link': u'fake_url',
'start_time': start_timestamp,
'stop_time': stop_timestamp,
'a_key': 'b',
'uuid': run_b.uuid,
}, result[0])
def test_get_all_runs_time_series_by_key_with_overlap(self):
time_a = datetime.datetime(1914, 6, 28, 10, 45, 0)
run_a = api.create_run(run_at=time_a)