diff --git a/docs/workitem.rst b/docs/workitem.rst index d442965..9d4f278 100644 --- a/docs/workitem.rst +++ b/docs/workitem.rst @@ -176,6 +176,13 @@ Workitems can be linked together using :func:`~Workitem.addLinkedItem`. In this new_workitem_2.addLinkedItem(new_workitem_1, 'relates_to') +Links can be retrieved either with or without link roles: + +.. code:: python + + print(workitem_1.getLinkedItem()) # [PYTH-540: None] + print(workitem_1.getLinkedItemWithRoles()) # [('follow_up', PYTH-540: None)] + Custom fields ^^^^^^^^^^^^^ diff --git a/polarion/workitem.py b/polarion/workitem.py index 3ea9d81..e5cf38f 100644 --- a/polarion/workitem.py +++ b/polarion/workitem.py @@ -456,6 +456,31 @@ def removeLinkedItem(self, workitem, role=None): self._reloadFromPolarion() workitem._reloadFromPolarion() + def getLinkedItemWithRoles(self): + """ + Get linked workitems both linked and back linked item will show up. Will include link roles. + + @return: Array of tuple ('link type', Workitem) + """ + linked_items = [] + service = self._polarion.getService('Tracker') + if self.linkedWorkItems is not None: + for linked_item in self.linkedWorkItems.LinkedWorkItem: + linked_items.append((linked_item.role.id, Workitem(self._polarion, self._project, uri=linked_item.workItemURI))) + if self.linkedWorkItemsDerived is not None: + for linked_item in self.linkedWorkItemsDerived.LinkedWorkItem: + linked_items.append((linked_item.role.id, Workitem(self._polarion, self._project, uri=linked_item.workItemURI))) + return linked_items + + def getLinkedItem(self): + """ + Get linked workitems both linked and back linked item will show up. + + @return: Array of Workitem + @return: + """ + return [item[1] for item in self.getLinkedItemWithRoles()] + def hasAttachment(self): """ Checks if the workitem has attachments diff --git a/tests/test_polarion_workitem.py b/tests/test_polarion_workitem.py index 18d1aaa..ef0db72 100644 --- a/tests/test_polarion_workitem.py +++ b/tests/test_polarion_workitem.py @@ -645,4 +645,35 @@ def test_testcase_update_steps(self): for i in range(3): self.assertEqual(f'{10 + i}', executed_workitem_1.getTestSteps()[i]['step'], msg='Value in first column did not change') self.assertEqual(f'new {i + 20}', executed_workitem_1.getTestSteps()[i]['description'], msg='Value in second column did not change') - self.assertEqual(f'last {i + 20}', executed_workitem_1.getTestSteps()[i]['expectedResult'], msg='Value in third column did not change') \ No newline at end of file + self.assertEqual(f'last {i + 20}', executed_workitem_1.getTestSteps()[i]['expectedResult'], msg='Value in third column did not change') + + def test_get_linked_items(self): + executed_workitem_1 = self.executing_project.createWorkitem('task') + executed_workitem_2 = self.executing_project.createWorkitem('task') + + # check empty linked items + self.assertEqual(0, len(executed_workitem_1.getLinkedItemWithRoles()), msg='Linked workitem not 0 in length') + self.assertEqual(0, len(executed_workitem_1.getLinkedItem()), msg='Linked workitem not 0 in length') + + # add link + executed_workitem_1.addLinkedItem(executed_workitem_2, 'follow_up') + + # check non empty linked items + self.assertEqual(1, len(executed_workitem_1.getLinkedItemWithRoles()), msg='Linked workitem not 1 in length') + self.assertEqual(1, len(executed_workitem_1.getLinkedItem()), msg='Linked workitem not 1 in length') + self.assertEqual(1, len(executed_workitem_2.getLinkedItemWithRoles()), msg='Linked workitem not 1 in length') + self.assertEqual(1, len(executed_workitem_2.getLinkedItem()), msg='Linked workitem not 1 in length') + + self.assertEqual(executed_workitem_2, executed_workitem_1.getLinkedItemWithRoles()[0][1], + msg='Check workitem') + self.assertEqual('follow_up', executed_workitem_1.getLinkedItemWithRoles()[0][0], + msg='Check link type') + self.assertEqual(executed_workitem_2, executed_workitem_1.getLinkedItem()[0], + msg='Check workitem') + + self.assertEqual(executed_workitem_1, executed_workitem_2.getLinkedItemWithRoles()[0][1], + msg='Check workitem') + self.assertEqual('follow_up', executed_workitem_2.getLinkedItemWithRoles()[0][0], + msg='Check link type') + self.assertEqual(executed_workitem_1, executed_workitem_2.getLinkedItem()[0], + msg='Check workitem') \ No newline at end of file