From 474983a7536fe8826b30ce05bdd9f9368484224f Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Mon, 8 Aug 2016 14:49:46 -0400 Subject: [PATCH] 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 --- .../add_get_runs_by_ids-ffdfc2f86d60c799.yaml | 4 ++++ subunit2sql/db/api.py | 16 ++++++++++++++++ subunit2sql/tests/db/test_api.py | 9 +++++++++ 3 files changed, 29 insertions(+) create mode 100644 releasenotes/notes/add_get_runs_by_ids-ffdfc2f86d60c799.yaml diff --git a/releasenotes/notes/add_get_runs_by_ids-ffdfc2f86d60c799.yaml b/releasenotes/notes/add_get_runs_by_ids-ffdfc2f86d60c799.yaml new file mode 100644 index 0000000..d3896a8 --- /dev/null +++ b/releasenotes/notes/add_get_runs_by_ids-ffdfc2f86d60c799.yaml @@ -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. diff --git a/subunit2sql/db/api.py b/subunit2sql/db/api.py index feffd35..626d17c 100644 --- a/subunit2sql/db/api.py +++ b/subunit2sql/db/api.py @@ -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. diff --git a/subunit2sql/tests/db/test_api.py b/subunit2sql/tests/db/test_api.py index 47b2dc8..d07ab6a 100644 --- a/subunit2sql/tests/db/test_api.py +++ b/subunit2sql/tests/db/test_api.py @@ -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()