From 59da1e221b4d9a9294efae007d7df0e0710ff77c Mon Sep 17 00:00:00 2001 From: Aleksey Maksimov Date: Thu, 10 Apr 2014 23:26:35 +0800 Subject: [PATCH] Fix for issue #268 and unit tests for it --- jenkinsapi/queue.py | 18 +++++++++++------- jenkinsapi_tests/unittests/test_queue.py | 21 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/jenkinsapi/queue.py b/jenkinsapi/queue.py index 4ed5de3..d0efc5b 100644 --- a/jenkinsapi/queue.py +++ b/jenkinsapi/queue.py @@ -57,11 +57,13 @@ class Queue(JenkinsBase): else: raise UnknownQueueItem(item_id) - def get_queue_items_for_job(self, job_name): + def get_queue_items_for_job(self, job_name=''): if not job_name: - return [QueueItem(self.jenkins, **item) for item in self._data['items']] + return [QueueItem(self.jenkins, **item) + for item in self._data['items']] else: - return [QueueItem(self.jenkins, **item) for item in self._data['items'] + return [QueueItem(self.jenkins, **item) + for item in self._data['items'] if item['task']['name'] == job_name] def delete_item(self, queue_item): @@ -74,8 +76,8 @@ class Queue(JenkinsBase): class QueueItem(object): """ - Flexible class to handle queue items. If the Jenkins API changes this support - those changes + Flexible class to handle queue items. + If the Jenkins API changes this support those changes """ def __init__(self, jenkins, **kwargs): @@ -94,11 +96,13 @@ class QueueItem(object): for action in actions: if type(action) is dict and 'parameters' in action: parameters = action['parameters'] - return dict([(x['name'], x['value']) for x in parameters]) + return dict([(x['name'], x.get('value', None)) + for x in parameters]) return [] def __repr__(self): - return "<%s.%s %s>" % (self.__class__.__module__, self.__class__.__name__, str(self)) + return "<%s.%s %s>" % (self.__class__.__module__, + self.__class__.__name__, str(self)) def __str__(self): return "%s #%i" % (self.task['name'], self.id) diff --git a/jenkinsapi_tests/unittests/test_queue.py b/jenkinsapi_tests/unittests/test_queue.py index 8afaea1..0f043be 100644 --- a/jenkinsapi_tests/unittests/test_queue.py +++ b/jenkinsapi_tests/unittests/test_queue.py @@ -144,5 +144,26 @@ class TestQueue(unittest.TestCase): j = item40.get_job() self.assertIsInstance(j, Job) + @mock.patch.object(JenkinsBase, 'get_data', mockGetData) + def test_get_queue_item_for_job(self): + item40 = self.q.get_queue_items_for_job('klscuimkqo') + self.assertIsInstance(item40, list) + self.assertEquals(len(item40), 1) + self.assertIsInstance(item40[0], QueueItem) + + item40 = self.q.get_queue_items_for_job() + self.assertIsInstance(item40, list) + self.assertEquals(len(item40), 3) + + def test_qi_get_parameters(self): + act = [{'parameters': + [{'name': 'name1', 'value': 'value1'}, + {'name': 'node'}]}] + qi = QueueItem(jenkins=None, actions=act) + + self.assertEquals(qi.get_parameters(), {'name1': 'value1', + 'node': None}) + + if __name__ == '__main__': unittest.main() -- 2.7.4