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 5a6cc41..f09ef34 100644 --- a/subunit2sql/db/api.py +++ b/subunit2sql/db/api.py @@ -1607,7 +1607,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 @@ -1617,6 +1617,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 @@ -1638,13 +1640,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 = { @@ -1653,6 +1657,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 d19debd..23e2789 100644 --- a/subunit2sql/tests/db/test_api.py +++ b/subunit2sql/tests/db/test_api.py @@ -658,6 +658,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)