diff --git a/capella2polarion/connectors/polarion_worker.py b/capella2polarion/connectors/polarion_worker.py index 0bbee57..3d9bf49 100644 --- a/capella2polarion/connectors/polarion_worker.py +++ b/capella2polarion/connectors/polarion_worker.py @@ -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 diff --git a/tests/conftest.py b/tests/conftest.py index e08ec98..a4d97ba 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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( @@ -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") @@ -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) diff --git a/tests/test_elements.py b/tests/test_elements.py index ed297ec..ea9e294 100644 --- a/tests/test_elements.py +++ b/tests/test_elements.py @@ -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] @@ -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] diff --git a/tests/test_polarion_worker_documents.py b/tests/test_polarion_worker_documents.py index c7ddb12..3313aeb 100644 --- a/tests/test_polarion_worker_documents.py +++ b/tests/test_polarion_worker_documents.py @@ -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, ): @@ -46,7 +41,6 @@ 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]) @@ -54,7 +48,7 @@ def test_update_document( '
\n' '' + 'params=id=AUTO-0|layout=0|external=true">' ) assert client.documents.update.call_count == 1 assert client.documents.update.call_args.args[0] == [document] @@ -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( '\n' + 'params=id=AUTO-0|layout=0|external=true">\n' '' + 'params=id=AUTO-1|layout=0|external=true">' ) assert client.documents.update.call_count == 1 assert client.documents.update.call_args.args[0] == [document] diff --git a/tests/test_worker.py b/tests/test_worker.py index 052fa9c..3ddf2bd 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -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(): @@ -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 + )