Skip to content

Commit

Permalink
Update multiple parts of wi. (#129)
Browse files Browse the repository at this point in the history
* refactor: Rename _skip_save attribute to _post_pone.

This indicates better what is happening.

* feat: Implement contextmanager for Workitem.

* docs: Add tiny example how to use contextmanager with Workitem.
  • Loading branch information
therealevahill authored Aug 16, 2023
1 parent fb39d30 commit 21b43bf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
14 changes: 14 additions & 0 deletions docs/workitem.rst
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,20 @@ The workitem class implements the __eq__ method allowing it to be compared.
if workitem1 == workitem2:
#...
Context Manager
^^^^^^^^^^^^^^^

It is possible to use the context manager with a workitem.
This is useful, when updating many attributes of it.
Execution speed increases, because the workitem is only updated / saved once, when exiting the context manager.

.. code:: python
with workitem as wi:
wi.addAttachment(path_to_file, 'file title')
wi.addComment('comment')
# any many more
List of available attributes
----------------------------
Expand Down
8 changes: 4 additions & 4 deletions polarion/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ def __init__(self, polarion, test_run, polarion_record, index):
self._polarion_record = polarion_record
self._index = index

self._skip_save = False
self._postpone_save = False

self._buildWorkitemFromPolarion()

def __enter__(self):
self._skip_save = True
self._postpone_save = True
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self._skip_save = False
self._postpone_save = False
self.save()

def _buildWorkitemFromPolarion(self):
Expand Down Expand Up @@ -301,7 +301,7 @@ def save(self):
"""
Saves the current test record
"""
if self._skip_save:
if self._postpone_save:
return

new_item = {}
Expand Down
11 changes: 11 additions & 0 deletions polarion/workitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(self, polarion, project, id=None, uri=None, new_workitem_type=None,
self._project = project
self._id = id
self._uri = uri
self._postpone_save = False

service = self._polarion.getService('Tracker')

Expand Down Expand Up @@ -88,6 +89,14 @@ def __init__(self, polarion, project, id=None, uri=None, new_workitem_type=None,

self._buildWorkitemFromPolarion()

def __enter__(self):
self._postpone_save = True
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self._postpone_save = False
self.save()

def _buildWorkitemFromPolarion(self):
if self._polarion_item is not None and not self._polarion_item.unresolvable:
self._original_polarion = copy.deepcopy(self._polarion_item)
Expand Down Expand Up @@ -774,6 +783,8 @@ def save(self):
"""
Update the workitem in polarion
"""
if self._postpone_save:
return
updated_item = {}

for attr, value in self._polarion_item.__dict__.items():
Expand Down

0 comments on commit 21b43bf

Please sign in to comment.