From d73db9057dec3c68fc23f163c295cdf988309d9d Mon Sep 17 00:00:00 2001
From: Matthew Treinish <mtreinish@kortar.org>
Date: Tue, 3 May 2016 21:37:36 -0400
Subject: [PATCH] Enable returning run id in
 get_test_runs_by_status_for_run_ids()

This commit adds a new option, include_run_id, to the
get_test_runs_by_status_for_run_ids() DB API method for including the
run uuid in the dictionaries returned by the method. This was an
oversight in the method and needed by users in certain cases, however
we can't just add it by default for backwards compat reasons. So a
bool flag is used to enable the additional field being returned.

Change-Id: Icf203668690419c38f6d6be6b6fe4af8462845f3
---
 ...y_status_for_run_ids-c763ca4d90f8f0ae.yaml |  4 +++
 subunit2sql/db/api.py                         | 12 ++++++--
 subunit2sql/tests/db/test_api.py              | 29 +++++++++++++++++++
 3 files changed, 42 insertions(+), 3 deletions(-)
 create mode 100644 releasenotes/notes/add-run-uuid-opt-to-get_test_runs_by_status_for_run_ids-c763ca4d90f8f0ae.yaml

diff --git a/releasenotes/notes/add-run-uuid-opt-to-get_test_runs_by_status_for_run_ids-c763ca4d90f8f0ae.yaml b/releasenotes/notes/add-run-uuid-opt-to-get_test_runs_by_status_for_run_ids-c763ca4d90f8f0ae.yaml
new file mode 100644
index 0000000..d449ee0
--- /dev/null
+++ b/releasenotes/notes/add-run-uuid-opt-to-get_test_runs_by_status_for_run_ids-c763ca4d90f8f0ae.yaml
@@ -0,0 +1,4 @@
+---
+features:
+  - A new option on get_test_runs_by_status_for_run_ids(), include_run_id, to
+    include the run uuid in the run dicts returned
diff --git a/subunit2sql/db/api.py b/subunit2sql/db/api.py
index 0bfa7b2..1b4e897 100644
--- a/subunit2sql/db/api.py
+++ b/subunit2sql/db/api.py
@@ -1583,7 +1583,7 @@ def get_runs_by_status_grouped_by_run_metadata(key, start_date=None,
 
 
 def get_test_runs_by_status_for_run_ids(status, run_ids, key=None,
-                                        session=None):
+                                        session=None, include_run_id=False):
     """Get a list of test run dicts by status for all the specified runs
 
     :param str status: The test status to filter the returned test runs on
@@ -1593,6 +1593,8 @@ def get_test_runs_by_status_for_run_ids(status, run_ids, key=None,
                     to the output dict for each test_run
     :param session: optional session object if one isn't provided a new session
                     will be acquired for the duration of this operation
+    :param bool include_run_id: boolean flag to enable including the run uuid
+                    in the test run dicts returned
 
     :return test_runs: a list of dicts for the test_runs and associated data
     :rtype: list
@@ -1614,13 +1616,15 @@ def get_test_runs_by_status_for_run_ids(status, run_ids, key=None,
                                models.TestRun.start_time_microsecond,
                                models.TestRun.stop_time,
                                models.TestRun.stop_time_microsecond,
-                               models.RunMetadata.value)
+                               models.RunMetadata.value,
+                               models.Run.uuid)
     else:
         results = query.values(models.Test.test_id, models.Run.artifacts,
                                models.TestRun.start_time,
                                models.TestRun.start_time_microsecond,
                                models.TestRun.stop_time,
-                               models.TestRun.stop_time_microsecond)
+                               models.TestRun.stop_time_microsecond,
+                               models.Run.uuid)
     test_runs = []
     for result in results:
         test_run = {
@@ -1629,6 +1633,8 @@ def get_test_runs_by_status_for_run_ids(status, run_ids, key=None,
             'start_time': result.start_time,
             'stop_time': result.stop_time,
         }
+        if include_run_id:
+            test_run['uuid'] = result.uuid
         if result.start_time_microsecond is not None:
             test_run['start_time'] = test_run['start_time'].replace(
                 microsecond=result.start_time_microsecond)
diff --git a/subunit2sql/tests/db/test_api.py b/subunit2sql/tests/db/test_api.py
index 92843a7..8cb53d0 100644
--- a/subunit2sql/tests/db/test_api.py
+++ b/subunit2sql/tests/db/test_api.py
@@ -641,6 +641,35 @@ class TestDatabaseAPI(base.TestCase):
             'a_key': 'b',
         }, result[0])
 
+    def test_get_test_runs_by_status_for_run_ids_with_run_id(self):
+        run_b = api.create_run(artifacts='fake_url')
+        run_a = api.create_run()
+        run_c = api.create_run()
+        test_a = api.create_test('fake_test')
+        api.add_run_metadata({'a_key': 'b'}, run_b.id)
+        api.add_run_metadata({'a_key': 'a'}, run_a.id)
+        api.add_run_metadata({'a_key': 'c'}, run_c.id)
+        start_timestamp = datetime.datetime(1914, 6, 28, 10, 45, 0)
+        stop_timestamp = datetime.datetime(1914, 6, 28, 10, 50, 0)
+        api.create_test_run(test_a.id, run_a.id, 'success',
+                            datetime.datetime.utcnow())
+        api.create_test_run(test_a.id, run_b.id, 'fail',
+                            start_timestamp, stop_timestamp)
+        api.create_test_run(test_a.id, run_c.id, 'success',
+                            datetime.datetime.utcnow())
+        result = api.get_test_runs_by_status_for_run_ids(
+            'fail', [run_a.uuid, run_b.uuid, run_c.uuid], key='a_key',
+            include_run_id=True)
+        self.assertEqual(1, len(result))
+        self.assertEqual({
+            'test_id': u'fake_test',
+            'link': u'fake_url',
+            'start_time': start_timestamp,
+            'stop_time': stop_timestamp,
+            'a_key': 'b',
+            'uuid': run_b.uuid,
+        }, result[0])
+
     def test_get_all_runs_time_series_by_key_with_overlap(self):
         time_a = datetime.datetime(1914, 6, 28, 10, 45, 0)
         run_a = api.create_run(run_at=time_a)