From 5a883b8273fb5488105e2b33b121a2631b60d61f Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Tue, 10 Jan 2017 14:12:17 -0500 Subject: [PATCH] Add DB API function to get multiple tests by test_id This commit adds a new DB API function, get_tests_by_test_ids(), to get multiple tests by test_id at once. There is already a function to do this for an individual test, but if you need to get multiple tests by tests looping that results in a lot of extra DB traffic when it can be done in a single request. Change-Id: I9db6c050720bd12693ff4fa29ee4f43fe60169be --- ...ests-by-test_ids-api-5f2542d214f9968f.yaml | 4 ++++ subunit2sql/db/api.py | 14 ++++++++++++ subunit2sql/tests/db/test_api.py | 22 +++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 releasenotes/notes/add-batch-tests-by-test_ids-api-5f2542d214f9968f.yaml diff --git a/releasenotes/notes/add-batch-tests-by-test_ids-api-5f2542d214f9968f.yaml b/releasenotes/notes/add-batch-tests-by-test_ids-api-5f2542d214f9968f.yaml new file mode 100644 index 0000000..48ddd36 --- /dev/null +++ b/releasenotes/notes/add-batch-tests-by-test_ids-api-5f2542d214f9968f.yaml @@ -0,0 +1,4 @@ +--- +features: + - Add a new DB API function, get_tests_by_test_ids, to return a list of Test + model objects give an list of test_ids diff --git a/subunit2sql/db/api.py b/subunit2sql/db/api.py index 626a3f1..e789ee5 100644 --- a/subunit2sql/db/api.py +++ b/subunit2sql/db/api.py @@ -723,6 +723,20 @@ def get_test_by_test_id(test_id, session=None): return test +def get_tests_by_test_ids(test_ids, session=None): + """Get tests that match input test_ids + + :param list test_ids: A list of test_ids (aka the test name) for the test + :param session: Optional session object if one isn't provided a new session + will be acquired for the duration of this operation + :return: A list of the specified test objects + :rtype: list + """ + session = session or get_session() + return db_utils.model_query(models.Test, session).filter( + models.Test.test_id.in_(test_ids)).all() + + def get_run_id_from_uuid(uuid, session=None): """Get the id for a run by it's uuid diff --git a/subunit2sql/tests/db/test_api.py b/subunit2sql/tests/db/test_api.py index d670980..fe6275c 100644 --- a/subunit2sql/tests/db/test_api.py +++ b/subunit2sql/tests/db/test_api.py @@ -66,6 +66,28 @@ class TestDatabaseAPI(base.TestCase): res = api.get_test_by_test_id('fake_test') self.assertIsNone(res) + def test_get_tests_by_test_ids(self): + test_a = api.create_test('fake_test1', 2, 1, 1, 1.2) + test_b = api.create_test('fake_test2', 4, 2, 2, 2.3) + test_c = api.create_test('fake_test3', 6, 3, 3, 3.3) + test_d = api.create_test('fake_test4', 8, 4, 4, 4.3) + result = api.get_tests_by_test_ids( + ['fake_test1', 'fake_test2', 'fake_test3']) + result_ids = [x.id for x in result] + self.assertIn(test_a.id, result_ids) + self.assertIn(test_b.id, result_ids) + self.assertIn(test_c.id, result_ids) + self.assertNotIn(test_d.id, result_ids) + + def test_get_tests_by_test_ids_no_matches(self): + api.create_test('fake_test5', 2, 1, 1, 1.2) + api.create_test('fake_test6', 4, 2, 2, 2.3) + api.create_test('fake_test7', 6, 3, 3, 3.3) + api.create_test('fake_test8', 8, 4, 4, 4.3) + result = api.get_tests_by_test_ids( + ['fake_test1', 'fake_test2', 'fake_test3']) + self.assertEqual([], result) + def test_create_run_and_list(self): res = api.create_run() self.assertIsNotNone(res)