Skip to content

Commit

Permalink
feat: rename method, correct typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Schneider committed Dec 19, 2023
1 parent 0f02444 commit e0ccbe4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 40 deletions.
9 changes: 5 additions & 4 deletions capella2polarion/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from capella2polarion import elements
from capella2polarion.c2pcli import C2PCli
from capella2polarion.elements import helpers, serialize
from capella2polarion.polarion import PolarionWorker


@click.group()
Expand Down Expand Up @@ -82,16 +83,17 @@ def cli(
capella_model,
synchronize_config,
)
lC2PCli.setupLogger()
lC2PCli.setup_logger()
ctx.obj = lC2PCli
lC2PCli.echo = click.echo
lC2PCli.echo("Start")


@cli.command()
@click.pass_obj
def printCliState(aC2PCli: C2PCli) -> None:
"""Print the CLI State."""
aC2PCli.setupLogger()
aC2PCli.setup_logger()
aC2PCli.printState()


Expand Down Expand Up @@ -128,11 +130,10 @@ def synchronize(ctx: click.core.Context) -> None:
# ctx.obj["CAPELLA_UUIDS"] = set(ctx.obj["POLARION_TYPE_MAP"])
# ctx.obj["CAPELLA_UUIDS"] = set(lPW.PolarionTypeMap)
# lPW.CapellaUUIDs = set(lPW.PolarionTypeMap)
from polarion import PolarionWorker

lPW = PolarionWorker(
aC2PCli.polarion_params, aC2PCli.logger, helpers.resolve_element_type
)
assert aC2PCli.capella_diagram_cache_index_content != None
lPW.load_elements_and_type_map(
aC2PCli.synchronize_config_content,
aC2PCli.capella_model,
Expand Down
4 changes: 2 additions & 2 deletions capella2polarion/c2pcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(
)
self.capella_diagram_cache_index_content: list[
dict[str, typing.Any]
] | None = None
] = []
self.capella_model: capellambse.MelodyModel = capella_model
self.synchronize_config_io: typing.TextIO = synchronize_config_io
self.synchronize_config_content: dict[str, typing.Any]
Expand Down Expand Up @@ -234,7 +234,7 @@ def load_capella_diagramm_cache_index(self) -> None:
"""Load to CapellaDiagramCacheIndexContent."""
if not self.exitsCapellaDiagrammCacheIndexFile():
raise Exception("capella diagramm cache index file doe not exits")
self.capella_diagram_cache_index_content = None
self.capella_diagram_cache_index_content = []
if self.get_capella_diagram_cache_index_file_path() != None:
l_text_content = (
self.get_capella_diagram_cache_index_file_path().read_text(
Expand Down
101 changes: 67 additions & 34 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,49 @@
import collections.abc as cabc
import os
import pathlib
import typing as t
import typing
from unittest import mock

import polarion_rest_api_client as polarion_api
import pytest
from click import testing

import capella2polarion.__main__ as main
from capella2polarion import elements
from capella2polarion.c2pcli import C2PCli
from capella2polarion.polarion import PolarionWorker

# pylint: disable-next=relative-beyond-top-level, useless-suppression
from conftest import ( # type: ignore[import]
from tests.conftest import ( # type: ignore[import]
TEST_DIAGRAM_CACHE,
TEST_HOST,
TEST_MODEL,
TEST_MODEL_ELEMENTS_CONFIG,
)

import capella2polarion.__main__ as main
from capella2polarion import elements


def prepare_cli_test(
monkeypatch: pytest.MonkeyPatch, return_value: t.Any | cabc.Iterable[t.Any]
monkeypatch: pytest.MonkeyPatch,
return_value: typing.Any | cabc.Iterable[typing.Any],
) -> mock.MagicMock:
os.environ["POLARION_HOST"] = TEST_HOST
os.environ["POLARION_PAT"] = "1234"
mock_api = mock.MagicMock(spec=polarion_api.OpenAPIPolarionProjectClient)
monkeypatch.setattr(polarion_api, "OpenAPIPolarionProjectClient", mock_api)
mock_get_polarion_wi_map = mock.MagicMock()
monkeypatch.setattr(
elements, "get_polarion_wi_map", mock_get_polarion_wi_map
)
if isinstance(return_value, cabc.Iterable) and not isinstance(
return_value, (str, dict)
):
id_map_attr = "side_effect"
else:
id_map_attr = "return_value"
# # mock_get_polarion_wi_map = mock.MagicMock()
# # monkeypatch.setattr(
# # elements, "get_polarion_wi_map", mock_get_polarion_wi_map
# # )
# if isinstance(return_value, cabc.Iterable) and not isinstance(
# return_value, (str, dict)
# ):
# id_map_attr = "side_effect"
# else:
# id_map_attr = "return_value"

setattr(mock_get_polarion_wi_map, id_map_attr, return_value)
return mock_get_polarion_wi_map
# setattr(mock_get_polarion_wi_map, id_map_attr, return_value)
# return mock_get_polarion_wi_map
return mock_api


def test_migrate_model_elements(monkeypatch: pytest.MonkeyPatch):
Expand All @@ -65,25 +69,54 @@ def test_migrate_model_elements(monkeypatch: pytest.MonkeyPatch):
{},
),
)
mock_delete_work_items = mock.MagicMock()
monkeypatch.setattr(elements, "delete_work_items", mock_delete_work_items)
mock_post_work_items = mock.MagicMock()
monkeypatch.setattr(elements, "post_work_items", mock_post_work_items)
mock_patch_work_items = mock.MagicMock()
monkeypatch.setattr(elements, "patch_work_items", mock_patch_work_items)
# mock_delete_work_items = mock.MagicMock()
# monkeypatch.setattr(elements, "delete_work_items", mock_delete_work_items)
# mock_post_work_items = mock.MagicMock()
# monkeypatch.setattr(elements, "post_work_items", mock_post_work_items)
# mock_patch_work_items = mock.MagicMock()
# monkeypatch.setattr(elements, "patch_work_items", mock_patch_work_items)

command = [
"--project-id=project_id",
"model-elements",
str(TEST_MODEL),
str(TEST_DIAGRAM_CACHE),
str(TEST_MODEL_ELEMENTS_CONFIG),
"--polarion-project-id",
"{project-id}",
"--polarion-url",
"https://www.czy.de",
"--polarion-pat",
"AlexandersPrivateAcessToken",
"--polarion-delete-work-items",
"--capella-diagram-cache-folder-path",
"./tests/data/diagram_cache",
"--capella-model",
"./tests/data/model/Melody Model Test.aird",
"--synchronize-config",
"./tests/data/model_elements/config.yaml",
"synchronize"
# ,str(TEST_MODEL),
# ,str(TEST_DIAGRAM_CACHE),
# ,str(TEST_MODEL_ELEMENTS_CONFIG),
]

result = testing.CliRunner().invoke(main.cli, command)
mock_polarionworker_deleteworkitem = mock.MagicMock()
mock.patch.object(
PolarionWorker.setup_polarion_client,
mock_polarionworker_deleteworkitem,
)
mock.patch.object(
PolarionWorker.delete_work_items, mock_polarionworker_deleteworkitem
)

# mock_c2pcli_setuplogger = mock.MagicMock()
# TODO .. wieso kracht es in der nächsten Zeile?
mock.patch.object(
C2PCli.load_synchronize_config, mock_polarionworker_deleteworkitem
)

result = testing.CliRunner().invoke(main.cli, command, terminal_width=60)

assert result.exit_code == 0
assert mock_get_polarion_wi_map.call_count == 1
assert mock_delete_work_items.call_count == 1
assert mock_patch_work_items.call_count == 1
assert mock_post_work_items.call_count == 1

# assert mock_c2pcli_setuplogger.call_count == 1
# assert mock_get_polarion_wi_map.call_count == 1
# assert mock_delete_work_items.call_count == 1
# assert mock_patch_work_items.call_count == 1
# assert mock_post_work_items.call_count == 1

0 comments on commit e0ccbe4

Please sign in to comment.