Add DB API function to get list of runs from ids

This commit adds a new function to the DB api for getting a list of
multiple runs from an input list of run_ids. This avoids the need to
loop over a list and call get_run_by_id().

Change-Id: I5df793ec5417245101a6e016def3bddfe7fb25e7
This commit is contained in:
Matthew Treinish 2016-08-08 14:49:46 -04:00
parent 5f4003b31d
commit 474983a753
No known key found for this signature in database
GPG Key ID: FD12A0F214C9E177
3 changed files with 29 additions and 0 deletions

View File

@ -0,0 +1,4 @@
---
features:
- A new DB API function, get_runs_by_ids(), was added. This function returns
a list of Run objects from a list of run ids.

View File

@ -753,6 +753,22 @@ def get_run_by_id(id, session=None):
return run
def get_runs_by_ids(ids, session=None):
"""Get a list of runs by their ids.
:param list id: A list of run ids for the run
:param session: Optional session object if one isn't provided a new session
will be acquired for the duration of this operation
:return: The list of the specified run objects
:rtype: list
"""
session = session or get_session()
run = db_utils.model_query(models.Run, session).filter(
models.Run.id.in_(ids)).all()
return run
def get_test_run_by_id(test_run_id, session=None):
"""Get an individual test run by it's id.

View File

@ -809,6 +809,15 @@ class TestDatabaseAPI(base.TestCase):
self.assertEqual(res.id, run.id)
self.assertEqual(res.uuid, run.uuid)
def test_get_runs_by_ids(self):
run_a = api.create_run()
run_b = api.create_run()
res = api.get_runs_by_ids([run_a.id, run_b.id])
self.assertIn(run_a.id, [x.id for x in res])
self.assertIn(run_b.id, [x.id for x in res])
self.assertIn(run_a.uuid, [x.uuid for x in res])
self.assertIn(run_b.uuid, [x.uuid for x in res])
def test_get_test_runs_by_test_id(self):
run_a = api.create_run()
run_b = api.create_run()