diff --git a/releasenotes/notes/add-get-recent-failed-runs-by-run-meta-f36c5197f3fe869f.yaml b/releasenotes/notes/add-get-recent-failed-runs-by-run-meta-f36c5197f3fe869f.yaml new file mode 100644 index 0000000..4b76418 --- /dev/null +++ b/releasenotes/notes/add-get-recent-failed-runs-by-run-meta-f36c5197f3fe869f.yaml @@ -0,0 +1,5 @@ +--- +features: + - Adds a new DB API method get_recent_failed_runs_by_run_metadata() that + will return a list of models.Run objects for recent failures on runs + with a specified run_metadata key value pair diff --git a/subunit2sql/db/api.py b/subunit2sql/db/api.py index ad3fbe8..c064bf9 100644 --- a/subunit2sql/db/api.py +++ b/subunit2sql/db/api.py @@ -1522,6 +1522,31 @@ def add_test_run_attachments(attach_dict, test_run_id, session=None): return attachments +def get_recent_failed_runs_by_run_metadata(key, value, num_runs=10, + start_date=None, session=None): + """Get a list of recent failed runs for a given run metadata pair + + :param str key: the run_metadata key to get failed runs + :param str value: The run_metadata value to get failed 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.fails > 0).order_by( + models.Run.run_at.desc()).limit(num_runs).all() + + def get_runs_by_status_grouped_by_run_metadata(key, start_date=None, stop_date=None, session=None): session = session or get_session() diff --git a/subunit2sql/tests/db/test_api.py b/subunit2sql/tests/db/test_api.py index 766cc3f..92843a7 100644 --- a/subunit2sql/tests/db/test_api.py +++ b/subunit2sql/tests/db/test_api.py @@ -790,3 +790,28 @@ class TestDatabaseAPI(base.TestCase): for meta in test_metadata: self.assertIn(meta.key, test_meta.keys()) self.assertIn(meta.value, test_meta.values()) + + def test_get_recent_failed_runs_by_run_metadata_no_start_date(self): + run_a = api.create_run(fails=1) + api.create_run() + run_c = api.create_run(fails=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_failed_runs_by_run_metadata('zeon', 'zaku') + self.assertEqual(1, len(results)) + self.assertEqual(run_c.id, results[0].id) + + def test_get_recent_failed_runs_by_run_metadata_with_start_date(self): + run_a = api.create_run(fails=1) + api.create_run() + run_c = api.create_run(fails=2) + run_d = api.create_run(fails=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_failed_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)