From 5f4003b31d97bc0b60855f2b294e9ff271e5a73f Mon Sep 17 00:00:00 2001 From: Matthew Treinish Date: Mon, 8 Aug 2016 12:28:16 -0400 Subject: [PATCH] Add option to ensure get_test_runs_by_test_id() is ordered by date This commit adds a new option to the get_test_runs_by_test_id() DB API function, most_recent_first, that when set True will order the response list by date with the most recent being first. This is useful if you want to look at a recent subset of the response list on the consumer side. Change-Id: Ia94f8066eb36deb33391162920b748707630e420 --- ...pt-get_test_runs_by_test_test_id-ba65e97d27fb7dd8.yaml | 4 ++++ subunit2sql/db/api.py | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/add-date-sort-opt-get_test_runs_by_test_test_id-ba65e97d27fb7dd8.yaml diff --git a/releasenotes/notes/add-date-sort-opt-get_test_runs_by_test_test_id-ba65e97d27fb7dd8.yaml b/releasenotes/notes/add-date-sort-opt-get_test_runs_by_test_test_id-ba65e97d27fb7dd8.yaml new file mode 100644 index 0000000..f376604 --- /dev/null +++ b/releasenotes/notes/add-date-sort-opt-get_test_runs_by_test_test_id-ba65e97d27fb7dd8.yaml @@ -0,0 +1,4 @@ +--- +features: + - A new option get_test_runs_by_test_test_id, most_recent_first, which ensures + the response list is ordered by date in descending order. diff --git a/subunit2sql/db/api.py b/subunit2sql/db/api.py index a19c337..feffd35 100644 --- a/subunit2sql/db/api.py +++ b/subunit2sql/db/api.py @@ -788,7 +788,8 @@ def get_test_runs_by_test_id(test_id, session=None): def get_test_runs_by_test_test_id(test_id, start_date=None, stop_date=None, - session=None, key=None, value=None): + session=None, key=None, value=None, + most_recent_first=False): """Get all test runs for a specific test by the test'stest_id column :param str test_id: The test's test_id (the test_id column in the test @@ -805,6 +806,8 @@ def get_test_runs_by_test_test_id(test_id, start_date=None, stop_date=None, :param str value: An optional value for run metadata to filter the test runs on. Must be specified with a key otherwise it does nothing. + :param bool most_recent_first: If true order the results list by date of + test_run start_time in descending order. :return list: The list of test run objects for the specified test :rtype: subunit2sql.models.TestRun @@ -827,6 +830,9 @@ def get_test_runs_by_test_test_id(test_id, start_date=None, stop_date=None, models.TestRun.run_id == models.RunMetadata.run_id).filter( models.RunMetadata.key == key, models.RunMetadata.value == value) + if most_recent_first: + test_runs_query = test_runs_query.order_by( + models.TestRun.start_time.desc()) test_runs = test_runs_query.all() return test_runs