From 4516b368df646b84977b15febb4f5308bf9f1033 Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Mon, 18 Apr 2016 14:52:20 -0400 Subject: [PATCH] Add optional start_date parameter to get_recent*() methods This commit adds an optional start_date parameter to the get_recent* methods that will only return results after a specified date. Change-Id: Ib1c899701fdec6d86ced6edca7aa7a8385cefc92 --- ...recent-list-commands-146b22b742c7a94a.yaml | 7 ++++ subunit2sql/db/api.py | 35 +++++++++++----- subunit2sql/tests/db/test_api.py | 40 +++++++++++++++++++ 3 files changed, 72 insertions(+), 10 deletions(-) create mode 100644 releasenotes/notes/add-start-time-to-recent-list-commands-146b22b742c7a94a.yaml diff --git a/releasenotes/notes/add-start-time-to-recent-list-commands-146b22b742c7a94a.yaml b/releasenotes/notes/add-start-time-to-recent-list-commands-146b22b742c7a94a.yaml new file mode 100644 index 0000000..7d8c399 --- /dev/null +++ b/releasenotes/notes/add-start-time-to-recent-list-commands-146b22b742c7a94a.yaml @@ -0,0 +1,7 @@ +--- +features: + - An optional `start_date` parameter was added to all DB API functions that + returned a recent runs list. This includes get_recent_successful_runs(), + get_recent_failed_runs(), and get_recent_runs_by_key_value_metadata(). This + is to set a starting point for the list of recent runs returned by these + methods. diff --git a/subunit2sql/db/api.py b/subunit2sql/db/api.py index 6a3b5b6..9a4b7da 100644 --- a/subunit2sql/db/api.py +++ b/subunit2sql/db/api.py @@ -1049,53 +1049,68 @@ def get_test_status_time_series(test_id, session=None): return status_series -def get_recent_successful_runs(num_runs=10, session=None): +def get_recent_successful_runs(num_runs=10, session=None, start_date=None): """Return a list of run uuid strings for the most recent successful runs :param int num_runs: The number of runs to return in the list - :param session: Optional session object if one isn't provided a new session + :param datetime start_date: An optional date to use as the starting point + for getting recent runs. Only runs after this + date will be returned. :return list: A list of run uuid strings (the id column in the runs table) for the most recent runs. """ session = session or get_session() - results = db_utils.model_query(models.Run, session).order_by( + results = db_utils.model_query(models.Run, session) + results = _filter_runs_by_date(results, start_date) + results = results.order_by( models.Run.run_at.desc()).filter_by(fails=0).limit(num_runs).all() return list(map(lambda x: x.uuid, results)) -def get_recent_failed_runs(num_runs=10, session=None): +def get_recent_failed_runs(num_runs=10, session=None, start_date=None): """Return a list of run uuid strings for the most recent failed runs :param int num_runs: The number of runs to return in the list :param session: Optional session object if one isn't provided a new session + :param datetime start_date: An optional date to use as the starting point + for getting recent runs. Only runs after this + date will be returned. :return list: A list of run uuid strings (the id column in the runs table) for the most recent runs. """ session = session or get_session() - results = db_utils.model_query(models.Run, session).order_by( + results = db_utils.model_query(models.Run, session) + results = _filter_runs_by_date(results, start_date) + results = results.order_by( models.Run.run_at.desc()).filter( models.Run.fails > 0).limit(num_runs).all() return list(map(lambda x: x.uuid, results)) def get_recent_runs_by_key_value_metadata(key, value, num_runs=10, - session=None): + session=None, start_date=None): """Get a list of runs for recent runs with a key value metadata pair :param int num_runs: The number of runs to return in the list :param session: Optional session object if one isn't provided a new session + :param datetime start_date: An optional date to use as the starting point + for getting recent runs. Only runs after this + date will be returned. + :return list: A list of run objects for the most recent runs. :rtype subunit2sql.db.models.Run """ session = session or get_session() results = 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).order_by( - models.Run.run_at.desc()).limit(num_runs).all() + models.Run.id == models.RunMetadata.run_id) + results = _filter_runs_by_date(results, start_date) + results = results.filter( + models.RunMetadata.key == key, + models.RunMetadata.value == value).order_by( + models.Run.run_at.desc()).limit(num_runs).all() return results diff --git a/subunit2sql/tests/db/test_api.py b/subunit2sql/tests/db/test_api.py index 04dbaea..d19debd 100644 --- a/subunit2sql/tests/db/test_api.py +++ b/subunit2sql/tests/db/test_api.py @@ -577,6 +577,22 @@ class TestDatabaseAPI(base.TestCase): self.assertNotIn(run_b.id, [x.id for x in result]) self.assertIn(run_c.id, [x.id for x in result]) + def test_get_recent_runs_by_key_value_metadata_with_start_date(self): + run_a = api.create_run(run_at=datetime.datetime( + 1914, 6, 28, 10, 45, 0)) + run_b = api.create_run() + run_c = api.create_run() + api.add_run_metadata({'a_key': 'a_value'}, run_a.id) + api.add_run_metadata({'a_key': 'a_value'}, run_c.id) + api.add_run_metadata({'a_key': 'b_value'}, run_b.id) + result = api.get_recent_runs_by_key_value_metadata( + 'a_key', 'a_value', start_date=datetime.datetime( + 1918, 11, 11, 11, 11, 11)) + self.assertEqual(1, len(result)) + self.assertNotIn(run_a.id, [x.id for x in result]) + self.assertNotIn(run_b.id, [x.id for x in result]) + self.assertIn(run_c.id, [x.id for x in result]) + def test_get_recent_runs_by_key_value_metadata_one_run(self): timestamp = datetime.datetime(1914, 6, 28, 10, 45, 0) run_a = api.create_run(run_at=timestamp) @@ -939,6 +955,18 @@ class TestDatabaseAPI(base.TestCase): self.assertIn(run_c.uuid, res) self.assertIn(run_d.uuid, res) + def test_get_recent_failed_runs_with_start_date(self): + api.create_run(fails=1, run_at=datetime.datetime( + 1914, 6, 28, 10, 45, 0)) + api.create_run() + run_c = api.create_run(fails=2) + run_d = api.create_run(fails=1) + res = api.get_recent_failed_runs(start_date=datetime.datetime( + 1918, 11, 11, 11, 11, 11)) + self.assertEqual(2, len(res)) + self.assertIn(run_c.uuid, res) + self.assertIn(run_d.uuid, res) + def test_get_recent_successful_runs(self): run_a = api.create_run(passes=1) run_b = api.create_run() @@ -950,6 +978,18 @@ class TestDatabaseAPI(base.TestCase): self.assertIn(run_b.uuid, res) self.assertIn(run_c.uuid, res) + def test_get_recent_successful_runs_with_start_date(self): + api.create_run(passes=1, run_at=datetime.datetime( + 1914, 6, 28, 10, 45, 0)) + run_b = api.create_run() + run_c = api.create_run(passes=2) + api.create_run(fails=1) + res = api.get_recent_successful_runs(start_date=datetime.datetime( + 1918, 11, 11, 11, 11, 11)) + self.assertEqual(2, len(res)) + self.assertIn(run_b.uuid, res) + self.assertIn(run_c.uuid, res) + def test_get_test_counts_in_date_range_as_str(self): timestamp_str_a = 'Dec 01 2015' timestamp_str_b = 'Dec 20 2015'