diff --git a/subunit2sql/db/api.py b/subunit2sql/db/api.py index 201f4fe..0293d86 100644 --- a/subunit2sql/db/api.py +++ b/subunit2sql/db/api.py @@ -1571,6 +1571,28 @@ def get_recent_failed_runs_by_run_metadata(key, value, num_runs=10, models.Run.run_at.desc()).limit(num_runs).all() +def get_runs_counts_by_run_metadata(key, value, start_date=None, session=None): + """Check runs for a given run metadata pair + + :param str key: The run_metadata key to check runs + :param str value: The run_metadata value to check runs + :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 count: A count for a given run metadata key-value pair + :rtype: int + """ + 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.count() + + 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 23e2789..3724e24 100644 --- a/subunit2sql/tests/db/test_api.py +++ b/subunit2sql/tests/db/test_api.py @@ -862,6 +862,37 @@ class TestDatabaseAPI(base.TestCase): self.assertEqual(1, len(results)) self.assertEqual(run_c.id, results[0].id) + def test_get_runs_counts_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_runs_counts_by_run_metadata('zeon', 'zaku') + self.assertEqual(1, results) + results = api.get_runs_counts_by_run_metadata('zeon', 'dom') + self.assertEqual(0, results) + + def test_get_runs_counts_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_runs_counts_by_run_metadata( + 'zeon', 'zaku', start_date=datetime.date(1970, 1, 1)) + self.assertEqual(1, results) + results = api.get_runs_counts_by_run_metadata( + 'zeon', 'zaku', start_date=datetime.date(1914, 6, 27)) + self.assertEqual(2, results) + results = api.get_runs_counts_by_run_metadata( + 'zeon', 'dom', start_date=datetime.date(1914, 6, 27)) + self.assertEqual(0, results) + def test_get_test_run_metadata(self): run = api.create_run() test = api.create_test('fake_test')