From 5d254e2bda50d821a8554535a5d5c7a832bf6a76 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Thu, 23 Jun 2016 10:56:09 -0400 Subject: [PATCH] Add new db api method to get recent successes by metadata This commit adds a new function to the DB API, get_recent_successful_runs_by_run_metadata(), which is used to return a list of recent Run objects for all runs with a provided key value run_metadata pair. Change-Id: Id6ca7e7a4cd1f913d9f84efae5f48d247dd9ed64 --- ...cessful-runs-by-meta-560c35bcd21ac98e.yaml | 5 ++++ subunit2sql/db/api.py | 26 +++++++++++++++++++ subunit2sql/tests/db/test_api.py | 26 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 releasenotes/notes/add-recent-successful-runs-by-meta-560c35bcd21ac98e.yaml diff --git a/releasenotes/notes/add-recent-successful-runs-by-meta-560c35bcd21ac98e.yaml b/releasenotes/notes/add-recent-successful-runs-by-meta-560c35bcd21ac98e.yaml new file mode 100644 index 0000000..90cb899 --- /dev/null +++ b/releasenotes/notes/add-recent-successful-runs-by-meta-560c35bcd21ac98e.yaml @@ -0,0 +1,5 @@ +--- +features: + - A new DB API function, get_recent_successful_runs_by_run_metadata, was + added. This enabled returning a list of recent successful runs with a key + value run_metadata pair. diff --git a/subunit2sql/db/api.py b/subunit2sql/db/api.py index 0293d86..a19c337 100644 --- a/subunit2sql/db/api.py +++ b/subunit2sql/db/api.py @@ -1733,3 +1733,29 @@ def get_all_test_run_metadata_keys(session=None): session = session or get_session() keys = session.query(models.TestRunMetadata.key).distinct().all() return [key[0] for key in keys] + + +def get_recent_successful_runs_by_run_metadata(key, value, num_runs=10, + start_date=None, session=None): + """Get a list of recent successful runs for a given run metadata pair + + :param str key: The run_metadata key to get successful runs + :param str value: The run_metadata value to get successful runs + :param int num_runs: The number of results to fetch, defaults to 10 + :param datetime start_date: The optional starting dates to get runs from. + Nothing older than this date will be returned + :param session: Optional session object if one isn't provided a new session + will be acquired for the duration of this operation + + :return list: The list of recent failed Run objects + :rtype: subunit2sql.models.Run + """ + session = session or get_session() + query = db_utils.model_query(models.Run, session).join( + models.RunMetadata, models.Run.id == models.RunMetadata.run_id).filter( + models.RunMetadata.key == key, + models.RunMetadata.value == value) + query = _filter_runs_by_date(query, start_date) + return query.filter(models.Run.passes >= 1).filter( + models.Run.fails == 0).order_by( + models.Run.run_at.desc()).limit(num_runs).all() diff --git a/subunit2sql/tests/db/test_api.py b/subunit2sql/tests/db/test_api.py index 3724e24..47b2dc8 100644 --- a/subunit2sql/tests/db/test_api.py +++ b/subunit2sql/tests/db/test_api.py @@ -1149,3 +1149,29 @@ class TestDatabaseAPI(base.TestCase): self.assertIn((test_c.id,), res) self.assertIn((test_b.id,), res) self.assertIn((test_a.id,), res) + + def test_get_recent_successful_runs_by_run_metadata_no_start_date(self): + run_a = api.create_run(fails=0, passes=1) + api.create_run() + run_c = api.create_run(fails=0, passes=2) + api.add_run_metadata({'fake_key': 'fake_value'}, run_a.id) + api.add_run_metadata({'zeon': 'zaku'}, run_c.id) + results = api.get_recent_successful_runs_by_run_metadata('zeon', + 'zaku') + self.assertEqual(1, len(results)) + self.assertEqual(run_c.id, results[0].id) + + def test_get_recent_successful_runs_by_run_metadata_with_start_date(self): + run_a = api.create_run(fails=0, passes=1) + api.create_run() + run_c = api.create_run(fails=0, passes=2) + run_d = api.create_run(fails=0, passes=3, + run_at=datetime.datetime(1914, 6, 28, + 10, 45, 0)) + api.add_run_metadata({'fake_key': 'fake_value'}, run_a.id) + api.add_run_metadata({'zeon': 'zaku'}, run_c.id) + api.add_run_metadata({'zeon': 'zaku'}, run_d.id) + results = api.get_recent_successful_runs_by_run_metadata( + 'zeon', 'zaku', start_date=datetime.date(1970, 1, 1)) + self.assertEqual(1, len(results)) + self.assertEqual(run_c.id, results[0].id)