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
This commit is contained in:
Matthew Treinish 2016-06-23 10:56:09 -04:00
parent e857ffb98c
commit 5d254e2bda
No known key found for this signature in database
GPG Key ID: FD12A0F214C9E177
3 changed files with 57 additions and 0 deletions

View File

@ -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.

View File

@ -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()

View File

@ -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)