Skip to content

Commit

Permalink
merge: Merge pull request #133 from DSD-DBS/fix-deleted-workitems-update
Browse files Browse the repository at this point in the history
Fix deleted WorkItems update
  • Loading branch information
micha91 authored Nov 21, 2024
2 parents 74dea46 + 3c92328 commit 05470be
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 31 deletions.
3 changes: 3 additions & 0 deletions capella2polarion/connectors/polarion_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ def compare_and_update_work_item(
assert old is not None
assert old.id is not None

if old.status == self.project_client.work_items.delete_status:
old.checksum = None

new.calculate_checksum()
if not self.force_update and new.checksum == old.checksum:
return
Expand Down
35 changes: 24 additions & 11 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,27 @@ class BaseObjectContainer(t.NamedTuple):
mc: model_converter.ModelConverter


def _set_work_item_id(work_items: list[polarion_api.WorkItem]):
for index, work_item in enumerate(work_items):
work_item.id = f"AUTO-{index}"


def _setup_polarion_worker(
monkeypatch: pytest.MonkeyPatch,
params: polarion_worker.PolarionWorkerParams,
) -> polarion_worker.CapellaPolarionWorker:
mock_api_client = mock.MagicMock(spec=polarion_api.PolarionClient)
monkeypatch.setattr(polarion_api, "PolarionClient", mock_api_client)
mock_project_client = mock.MagicMock(spec=polarion_api.ProjectClient)
monkeypatch.setattr(polarion_api, "ProjectClient", mock_project_client)
pw = polarion_worker.CapellaPolarionWorker(params)
pw.project_client.work_items.create.side_effect = _set_work_item_id
pw.project_client.work_items.delete_status = (
None if params.delete_work_items else "deleted"
)
return pw


# pylint: disable=redefined-outer-name
@pytest.fixture
def base_object(
Expand All @@ -132,10 +153,6 @@ def base_object(
)

c2p_cli.setup_logger()
mock_api_client = mock.MagicMock(spec=polarion_api.PolarionClient)
monkeypatch.setattr(polarion_api, "PolarionClient", mock_api_client)
mock_project_client = mock.MagicMock(spec=polarion_api.ProjectClient)
monkeypatch.setattr(polarion_api, "ProjectClient", mock_project_client)
c2p_cli.config = mock.Mock(converter_config.ConverterConfig)

fake = FakeModelObject("uuid1", name="Fake 1")
Expand Down Expand Up @@ -166,21 +183,17 @@ def base_object(
),
}

pw = polarion_worker.CapellaPolarionWorker(c2p_cli.polarion_params)
pw = _setup_polarion_worker(monkeypatch, c2p_cli.polarion_params)
pw.polarion_data_repo = polarion_repo.PolarionDataRepository([work_item])
return BaseObjectContainer(c2p_cli, pw, mc)


@pytest.fixture
def empty_polarion_worker(monkeypatch: pytest.MonkeyPatch):
mock_api_client = mock.MagicMock(spec=polarion_api.PolarionClient)
monkeypatch.setattr(polarion_api, "PolarionClient", mock_api_client)
mock_project_client = mock.MagicMock(spec=polarion_api.ProjectClient)
monkeypatch.setattr(polarion_api, "ProjectClient", mock_project_client)
polarion_params = polarion_worker.PolarionWorkerParams(
project_id=TEST_PROJECT_ID,
url=TEST_HOST,
pat="PrivateAccessToken",
delete_work_items=True,
delete_work_items=False,
)
yield polarion_worker.CapellaPolarionWorker(polarion_params)
yield _setup_polarion_worker(monkeypatch, polarion_params)
14 changes: 4 additions & 10 deletions tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,6 @@
DIAGRAM_CONFIG = converter_config.CapellaTypeConfig("diagram", "diagram")


def _set_work_item_id(work_items: list[polarion_api.WorkItem]):
for index, work_item in enumerate(work_items):
work_item.id = f"AUTO-{index}"


class GroupedLinksBaseObject(t.TypedDict):
link_serializer: link_converter.LinkSerializer
work_items: dict[str, data_model.CapellaWorkItem]
Expand Down Expand Up @@ -899,17 +894,16 @@ def test_create_new_work_item(base_object: BaseObjectContainer):
base_object.pw.project_client.work_items.get_all = (
polarion_api_get_all_work_items
)
polarion_api_create_work_items = mock.MagicMock()
polarion_api_create_work_items.side_effect = _set_work_item_id
base_object.pw.project_client.work_items.create = (
polarion_api_create_work_items
)

base_object.pw.load_polarion_work_item_map()
base_object.pw.create_missing_work_items(
base_object.mc.converter_session
)

polarion_api_create_work_items = (
base_object.pw.project_client.work_items.create
)

assert polarion_api_create_work_items.call_count == 1
assert len(polarion_api_create_work_items.call_args[0][0]) == 1
work_item = polarion_api_create_work_items.call_args[0][0][0]
Expand Down
13 changes: 3 additions & 10 deletions tests/test_polarion_worker_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
from .conftest import DOCUMENT_TEMPLATES, DOCUMENT_TEXT_WORK_ITEMS


def _set_work_item_id(work_items: list[polarion_api.WorkItem]):
for index, work_item in enumerate(work_items):
work_item.id = f"id{index}"


def test_update_document(
empty_polarion_worker: polarion_worker.CapellaPolarionWorker,
):
Expand Down Expand Up @@ -46,15 +41,14 @@ def test_update_document(
document.home_page_content.value
)
client = empty_polarion_worker.project_client
client.work_items.create.side_effect = _set_work_item_id

empty_polarion_worker.update_documents([document_data])

assert document.home_page_content.value.endswith(
'<div id="polarion_wiki macro name=module-workitem;'
'params=id=EXISTING|layout=0|external=true"></div>\n'
'<div id="polarion_wiki macro name=module-workitem;'
'params=id=id0|layout=0|external=true"></div>'
'params=id=AUTO-0|layout=0|external=true"></div>'
)
assert client.documents.update.call_count == 1
assert client.documents.update.call_args.args[0] == [document]
Expand Down Expand Up @@ -90,15 +84,14 @@ def test_create_document(
document.home_page_content.value
)
client = empty_polarion_worker.project_client
client.work_items.create.side_effect = _set_work_item_id

empty_polarion_worker.update_documents([document_data])

assert document.home_page_content.value.endswith(
'<div id="polarion_wiki macro name=module-workitem;'
'params=id=id0|layout=0|external=true"></div>\n'
'params=id=AUTO-0|layout=0|external=true"></div>\n'
'<div id="polarion_wiki macro name=module-workitem;'
'params=id=id1|layout=0|external=true"></div>'
'params=id=AUTO-1|layout=0|external=true"></div>'
)
assert client.documents.update.call_count == 1
assert client.documents.update.call_args.args[0] == [document]
Expand Down
36 changes: 36 additions & 0 deletions tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
from unittest import mock

from capella2polarion.connectors import polarion_worker
from capella2polarion.converters import converter_config, data_session
from capella2polarion.data_model import work_items

# pylint: disable-next=relative-beyond-top-level, useless-suppression
from .conftest import FakeModelObject # type: ignore[import]


def test_polarion_worker_non_delete_mode():
Expand Down Expand Up @@ -34,3 +39,34 @@ def test_polarion_worker_delete_mode():
)
)
assert worker.project_client.work_items.delete_status is None


def test_polarion_worker_reuse_deleted_work_item(
empty_polarion_worker: polarion_worker.CapellaPolarionWorker,
):

new_work_item = work_items.CapellaWorkItem(
"ID", title="Test", status="open", uuid_capella="123", type="test"
)
old_work_item = work_items.CapellaWorkItem(
"ID",
status="deleted",
type="test",
uuid_capella="123",
checksum=new_work_item.calculate_checksum(),
)
empty_polarion_worker.polarion_data_repo.update_work_items([old_work_item])
empty_polarion_worker.compare_and_update_work_items(
{
"123": data_session.ConverterData(
"la",
converter_config.CapellaTypeConfig("test"),
FakeModelObject("123"),
new_work_item,
)
}
)

assert (
empty_polarion_worker.project_client.work_items.update.call_count == 1
)

0 comments on commit 05470be

Please sign in to comment.