From 56b0fa9f894fbef42c93ea1f5d4f1dae8c359191 Mon Sep 17 00:00:00 2001 From: Alexander Maretskiy Date: Wed, 2 Mar 2016 18:32:49 +0200 Subject: [PATCH] [API] Extend api.Task.get_detailed Extend api.Task.get_detailed with ability to return task data as dict with extended results. This change is backward compatible. Reasons for this change: * current task results format is going to be deprecated after planned DB schema upgrade, so this patch is yet another step forward to this aim * all rally.api methods should return data only of basic python types, that is why task data is of dict dict * This change is also required by further patch Change-Id: I9fadd08db7c3c6ed7cd5a793d4c4990b92f89d6c --- rally/api.py | 16 ++++++++++++++-- tests/unit/test_api.py | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/rally/api.py b/rally/api.py index 24e2625126..32846b2e33 100644 --- a/rally/api.py +++ b/rally/api.py @@ -157,8 +157,20 @@ class Task(object): return objects.Task.get(task_id) @staticmethod - def get_detailed(task_id): - return objects.Task.get_detailed(task_id) + def get_detailed(task_id, extended_results=False): + """Get detailed task data. + + :param task_id: str task UUID + :param extended_results: whether to return task data as dict + with extended results + :returns: rally.common.db.sqlalchemy.models.Task + :returns: dict + """ + task = objects.Task.get_detailed(task_id) + if task and extended_results: + task = dict(task) + task["results"] = objects.Task.extend_results(task["results"]) + return task @classmethod def render_template(cls, task_template, template_dir="./", **kwargs): diff --git a/tests/unit/test_api.py b/tests/unit/test_api.py index 7eb6e9f738..9717692bdf 100644 --- a/tests/unit/test_api.py +++ b/tests/unit/test_api.py @@ -256,6 +256,24 @@ class TaskAPITestCase(test.TestCase): mock_task_delete.assert_called_once_with( self.task_uuid, status=None) + @mock.patch("rally.api.objects.Task") + def test_get_detailed(self, mock_task): + mock_task.get_detailed.return_value = "detailed_task_data" + self.assertEqual("detailed_task_data", + api.Task.get_detailed("task_uuid")) + mock_task.get_detailed.assert_called_once_with("task_uuid") + + @mock.patch("rally.api.objects.Task") + def test_get_detailed_with_extended_results(self, mock_task): + mock_task.get_detailed.return_value = (("uuid", "foo_uuid"), + ("results", "raw_results")) + mock_task.extend_results.return_value = "extended_results" + self.assertEqual({"uuid": "foo_uuid", "results": "extended_results"}, + api.Task.get_detailed("foo_uuid", + extended_results=True)) + mock_task.get_detailed.assert_called_once_with("foo_uuid") + mock_task.extend_results.assert_called_once_with("raw_results") + class BaseDeploymentTestCase(test.TestCase): def setUp(self):