diff --git a/releasenotes/notes/add-get_run_count-api-43daafc592b929ef.yaml b/releasenotes/notes/add-get_run_count-api-43daafc592b929ef.yaml new file mode 100644 index 0000000..4381899 --- /dev/null +++ b/releasenotes/notes/add-get_run_count-api-43daafc592b929ef.yaml @@ -0,0 +1,4 @@ +--- +features: + - Add a new DB API function get_runs_count() which returns the total number of + runs in the DB diff --git a/subunit2sql/db/api.py b/subunit2sql/db/api.py index 626a3f1..0bf77a0 100644 --- a/subunit2sql/db/api.py +++ b/subunit2sql/db/api.py @@ -1671,6 +1671,17 @@ 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_count(session=None): + """Get the number of runs currently in the database + + :return count: The number of runs in the DB + :rtype: int + """ + session = session or get_session() + query = db_utils.model_query(models.Run, session) + return query.count() + + def get_runs_counts_by_run_metadata(key, value, start_date=None, session=None): """Check runs for a given run metadata pair diff --git a/subunit2sql/tests/db/test_api.py b/subunit2sql/tests/db/test_api.py index d670980..4d8dfe0 100644 --- a/subunit2sql/tests/db/test_api.py +++ b/subunit2sql/tests/db/test_api.py @@ -909,6 +909,15 @@ class TestDatabaseAPI(base.TestCase): self.assertEqual(1, len(results)) self.assertEqual(run_c.id, results[0].id) + def test_get_runs_count(self): + for i in range(0, 100): + api.create_run() + count = api.get_runs_count() + self.assertEqual(100, count) + + def test_get_runs_count_zero(self): + self.assertEqual(0, api.get_runs_count()) + def test_get_runs_counts_by_run_metadata_no_start_date(self): run_a = api.create_run(fails=1) api.create_run()