diff --git a/polarion/plan.py b/polarion/plan.py index 9f7593a..793ab23 100644 --- a/polarion/plan.py +++ b/polarion/plan.py @@ -29,6 +29,9 @@ def __init__(self, polarion, project, polarion_record=None, uri=None, id=None, n self._id = id if new_plan_id is not None and new_plan_name is not None: + # get the ID from the plan if the ID if the plan is passed + if isinstance(new_plan_parent, Plan): + new_plan_parent = new_plan_parent.id service = self._polarion.getService('Planning') self._uri = service.createPlan(self._project.id, new_plan_name, new_plan_id, new_plan_parent, new_plan_template) @@ -165,6 +168,26 @@ def save(self): service.updatePlan(updated_plan) self._reloadFromPolarion() + def getParent(self): + """ + Get the parent plan + :return: parent Plan + """ + return Plan(self._polarion, self._project, self.parent) + + def getChildren(self): + """ + Get the child plans + :return: List of Plans, or empty list if there are no children. + """ + search_results = self._project.searchPlanFullItem(f'parent.id:{self.id}') + children = [] + for plan in search_results: + if plan.id != self.id: + children.append(plan) + return children + + def _reloadFromPolarion(self): service = self._polarion.getService('Planning') self._polarion_record = service.getPlanByUri(self._polarion_record.uri) diff --git a/polarion/project.py b/polarion/project.py index d0222ab..fc8a1c7 100644 --- a/polarion/project.py +++ b/polarion/project.py @@ -86,6 +86,35 @@ def createPlan(self, new_plan_name, new_plan_id, new_plan_template, new_plan_par return Plan(self.polarion, self, new_plan_name=new_plan_name, new_plan_id=new_plan_id, new_plan_template=new_plan_template, new_plan_parent=new_plan_parent) + def searchPlan(self, query='', order='Created', limit=-1): + """Query for available plans. This will return the polarion data structures. + + :param query: The query to use while searching + :param order: Order by + :param limit: The limit of plans, -1 for no limit + :return: The search results + :rtype: dict[] + """ + query += f' AND project.id:{self.id}' + service = self.polarion.getService('Planning') + return service.searchPlans(query, order, limit) + + def searchPlanFullItem(self, query='', order='Created', limit=-1): + """Query for available plans. This will query for the plans and then fetch all result. May take a while for a big search with many results. + + :param query: The query to use while searching + :param order: Order by + :param limit: The limit of plans, -1 for no limit + :return: The search results + :rtype: Plan[] + """ + return_list = [] + plans = self.searchPlan(query, order, limit) + for plan in plans: + return_list.append(Plan(self.polarion, self, polarion_record=plan)) + return return_list + + def createWorkitem(self, workitem_type: str, new_workitem_fields=None): """ Create a workitem based on the workitem type. diff --git a/polarion/workitem.py b/polarion/workitem.py index 200d2a4..ddcc949 100644 --- a/polarion/workitem.py +++ b/polarion/workitem.py @@ -96,9 +96,14 @@ def _buildWorkitemFromPolarion(self): setattr(self, key, value[key]) self._polarion_test_steps = None try: - service_test = self._polarion.getService('TestManagement') - self._polarion_test_steps = service_test.getTestSteps(self.uri) - except Exception: + # get the custom fields + service = self._polarion.getService('Tracker') + custom_fields = service.getCustomFieldTypes(self.uri) + # check if any of the field has the test steps + if any(field.id == 'testSteps' for field in custom_fields): + service_test = self._polarion.getService('TestManagement') + self._polarion_test_steps = service_test.getTestSteps(self.uri) + except Exception as e: # fail silently as there are probably not test steps for this workitem # todo: logging support pass diff --git a/tests/test_polarion_plan.py b/tests/test_polarion_plan.py index cac0fb0..20779f1 100644 --- a/tests/test_polarion_plan.py +++ b/tests/test_polarion_plan.py @@ -80,6 +80,32 @@ def test_dates(self): self.assertEqual(self.checking_plan.startDate.day, start_date.day, msg='Date is not equal') self.assertEqual(self.checking_plan.startedOn.day, started_on_date.day, msg='Date is not equal') + def test_parent_children(self): + # get checking plan + self.checking_plan = self.executing_project.getPlan(self.executing_plan.id) + + # check that there are no children + self.assertEqual(len(self.checking_plan.getChildren()), 0, + msg='No children should be here') + + # create a child and check that there are children. + child_a = self.executing_project.createPlan('Test plan' + datetime.now().strftime("%d-%m-%Y-%H-%M-%S-%f"), + datetime.now().strftime("%d-%m-%Y-%H-%M-%S-%f"), 'iteration', new_plan_parent=self.executing_plan.id) + + + self.assertEqual(len(self.checking_plan.getChildren()), 1, msg='Added one child, but this is not in checking plan') + self.assertEqual(self.checking_plan.getChildren()[0].id, child_a.id, msg='Child id does not match') + self.assertEqual(child_a.getParent().id, self.executing_plan.id, msg='Parent id should match original parent') + + # also check a sub child + sub_child = self.executing_project.createPlan('Test plan' + datetime.now().strftime("%d-%m-%Y-%H-%M-%S-%f"), + datetime.now().strftime("%d-%m-%Y-%H-%M-%S-%f"), 'iteration', + new_plan_parent=child_a) + + self.assertEqual(child_a.getChildren()[0].id, sub_child.id, msg='Child id does not match') + self.assertEqual(sub_child.getParent().id, child_a.id, msg='Parent id should match original parent') + +