Skip to content

Commit

Permalink
test: Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ewuerger committed Dec 12, 2024
1 parent 64d0692 commit 182ce81
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 88 deletions.
16 changes: 7 additions & 9 deletions tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
TEST_ACTOR_UUID = "08e02248-504d-4ed8-a295-c7682a614f66"
TEST_PHYS_COMP = "b9f9a83c-fb02-44f7-9123-9d86326de5f1"
TEST_PHYS_NODE = "8a6d68c8-ac3d-4654-a07e-ada7adeed09f"
TEST_PHYS_FNC = "11906f7b-3ae9-4343-b998-95b170be2e2b"
TEST_SCENARIO = "afdaa095-e2cd-4230-b5d3-6cb771a90f51"
TEST_CAP_REAL = "b80b3141-a7fc-48c7-84b2-1467dcef5fce"
TEST_CONSTRAINT = "95cbd4af-7224-43fe-98cb-f13dda540b8e"
Expand Down Expand Up @@ -1861,24 +1862,23 @@ def test_generic_work_item(

@staticmethod
def test_add_context_diagram(model: capellambse.MelodyModel):
uuid = "11906f7b-3ae9-4343-b998-95b170be2e2b"
type_config = converter_config.CapellaTypeConfig(
"test", "add_context_diagram", []
)
serializer = element_converter.CapellaWorkItemSerializer(
model,
polarion_repo.PolarionDataRepository(),
{
uuid: data_session.ConverterData(
TEST_PHYS_FNC: data_session.ConverterData(
"pa",
type_config,
model.by_uuid(uuid),
model.by_uuid(TEST_PHYS_FNC),
)
},
True,
)

work_item = serializer.serialize(uuid)
work_item = serializer.serialize(TEST_PHYS_FNC)

assert work_item is not None
assert "context_diagram" in work_item.additional_attributes
Expand Down Expand Up @@ -2154,7 +2154,6 @@ def test_read_config_tree_view_with_params(
with mock.patch.object(
context.ContextDiagram, "render"
) as wrapped_render:

wis = serializer.serialize_all()
_ = wis[0].attachments[0].content_bytes

Expand Down Expand Up @@ -2204,25 +2203,24 @@ def test_read_config_links(caplog: pytest.LogCaptureFixture):

@staticmethod
def test_add_context_diagram_with_caption(model: capellambse.MelodyModel):
uuid = "11906f7b-3ae9-4343-b998-95b170be2e2b"
type_config = converter_config.CapellaTypeConfig(
"test", "add_context_diagram", []
)
serializer = element_converter.CapellaWorkItemSerializer(
model,
polarion_repo.PolarionDataRepository(),
{
uuid: data_session.ConverterData(
TEST_PHYS_FNC: data_session.ConverterData(
"pa",
type_config,
model.by_uuid(uuid),
model.by_uuid(TEST_PHYS_FNC),
)
},
True,
generate_figure_captions=True,
)

work_item = serializer.serialize(uuid)
work_item = serializer.serialize(TEST_PHYS_FNC)

assert work_item is not None
assert "context_diagram" in work_item.additional_attributes
Expand Down
129 changes: 50 additions & 79 deletions tests/test_workitem_attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

# pylint: disable=relative-beyond-top-level, useless-suppression
from .conftest import TEST_DIAGRAM_CACHE
from .test_elements import TEST_DIAG_DESCR
from .test_elements import TEST_DIAG_DESCR, TEST_PHYS_FNC

DIAGRAM_WI_CHECKSUM = (
"76fc1f7e4b73891488de7e47de8ef75fc24e85fc3cdde80661503201e70b1733"
"1239ced17306dc92213cd1b729e3652a1cbc9b07997683ed36033a9d05adcb75"
)
WI_CONTEXT_DIAGRAM_CHECKSUM = (
"0ed1417e8e4717524bc91162dcf8633afca686e93f8b036d0bc48d81f0444f56"
Expand Down Expand Up @@ -62,6 +62,21 @@
)


# pylint: disable=redefined-outer-name
@pytest.fixture
def converter(
model: capellambse.MelodyModel,
) -> model_converter.ModelConverter:
diag = model.diagrams.by_uuid(TEST_DIAG_UUID)
converter = model_converter.ModelConverter(model, "TEST")
converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData(
model_converter.get_layer_name(diag),
converter_config.CapellaTypeConfig("diagram", "diagram", []),
diag,
)
return converter


@pytest.fixture
def worker(monkeypatch: pytest.MonkeyPatch):
mock_api_client = mock.MagicMock(spec=polarion_api.PolarionClient)
Expand Down Expand Up @@ -97,35 +112,23 @@ def set_attachment_ids(attachments: list[polarion_api.WorkItemAttachment]):
counter += 1


def test_diagram_no_attachments(model: capellambse.MelodyModel):
diag = model.diagrams.by_uuid(TEST_DIAG_UUID)
converter = model_converter.ModelConverter(model, "TEST")
converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData(
model_converter.get_layer_name(diag),
converter_config.CapellaTypeConfig("diagram", "diagram", []),
diag,
)

def test_diagram_no_attachments(converter: model_converter.ModelConverter):
converter.generate_work_items(
polarion_repo.PolarionDataRepository(), False, False
polarion_repo.PolarionDataRepository(),
generate_links=False,
generate_attachments=False,
)

work_item = converter.converter_session[TEST_DIAG_UUID].work_item
assert work_item is not None
assert work_item.attachments == []


def test_diagram_has_attachments(model: capellambse.MelodyModel):
diag = model.diagrams.by_uuid(TEST_DIAG_UUID)
converter = model_converter.ModelConverter(model, "TEST")
converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData(
model_converter.get_layer_name(diag),
converter_config.CapellaTypeConfig("diagram", "diagram", []),
diag,
)

def test_diagram_has_attachments(converter: model_converter.ModelConverter):
converter.generate_work_items(
polarion_repo.PolarionDataRepository(), False, True
polarion_repo.PolarionDataRepository(),
generate_links=False,
generate_attachments=True,
)

work_item = converter.converter_session[TEST_DIAG_UUID].work_item
Expand All @@ -135,11 +138,9 @@ def test_diagram_has_attachments(model: capellambse.MelodyModel):

# pylint: disable=redefined-outer-name
def test_diagram_attachments_new(
model: capellambse.MelodyModel,
converter: model_converter.ModelConverter,
worker: polarion_worker.CapellaPolarionWorker,
):
diag = model.diagrams.by_uuid(TEST_DIAG_UUID)
converter = model_converter.ModelConverter(model, "TEST")
worker.polarion_data_repo = polarion_repo.PolarionDataRepository(
[data_model.CapellaWorkItem(WORKITEM_ID, uuid_capella=TEST_DIAG_UUID)]
)
Expand All @@ -150,11 +151,6 @@ def test_diagram_attachments_new(
worker.project_client.work_items.attachments.create.side_effect = (
set_attachment_ids
)
converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData(
model_converter.get_layer_name(diag),
converter_config.CapellaTypeConfig("diagram", "diagram", []),
diag,
)

converter.generate_work_items(worker.polarion_data_repo, False, True)
worker.compare_and_update_work_item(
Expand Down Expand Up @@ -187,11 +183,9 @@ def test_diagram_attachments_new(

# pylint: disable=redefined-outer-name
def test_new_diagram(
model: capellambse.MelodyModel,
converter: model_converter.ModelConverter,
worker: polarion_worker.CapellaPolarionWorker,
):
diag = model.diagrams.by_uuid(TEST_DIAG_UUID)
converter = model_converter.ModelConverter(model, "TEST")
checksum = json.dumps({"__C2P__WORK_ITEM": DIAGRAM_WI_CHECKSUM})
worker.polarion_data_repo = polarion_repo.PolarionDataRepository(
[
Expand All @@ -209,11 +203,6 @@ def test_new_diagram(
worker.project_client.work_items.attachments.create.side_effect = (
set_attachment_ids
)
converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData(
model_converter.get_layer_name(diag),
converter_config.CapellaTypeConfig("diagram", "diagram", []),
diag,
)

converter.generate_work_items(worker.polarion_data_repo, False, True)
worker.compare_and_update_work_item(
Expand All @@ -233,11 +222,9 @@ def test_new_diagram(


def test_diagram_attachments_updated(
model: capellambse.MelodyModel,
converter: model_converter.ModelConverter,
worker: polarion_worker.CapellaPolarionWorker,
):
diag = model.diagrams.by_uuid(TEST_DIAG_UUID)
converter = model_converter.ModelConverter(model, "TEST")
worker.polarion_data_repo = polarion_repo.PolarionDataRepository(
[data_model.CapellaWorkItem(WORKITEM_ID, uuid_capella=TEST_DIAG_UUID)]
)
Expand Down Expand Up @@ -266,11 +253,6 @@ def test_diagram_attachments_updated(
worker.project_client.work_items.attachments.get_all.return_value = (
existing_attachments
)
converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData(
model_converter.get_layer_name(diag),
converter_config.CapellaTypeConfig("diagram", "diagram", []),
diag,
)

converter.generate_work_items(worker.polarion_data_repo, False, True)
worker.compare_and_update_work_item(
Expand All @@ -293,11 +275,9 @@ def test_diagram_attachments_updated(


def test_diagram_attachments_unchanged_work_item_changed(
model: capellambse.MelodyModel,
converter: model_converter.ModelConverter,
worker: polarion_worker.CapellaPolarionWorker,
):
diag = model.diagrams.by_uuid(TEST_DIAG_UUID)
converter = model_converter.ModelConverter(model, "TEST")
diagram_work_item = data_model.CapellaWorkItem(
WORKITEM_ID,
uuid_capella=TEST_DIAG_UUID,
Expand Down Expand Up @@ -330,11 +310,6 @@ def test_diagram_attachments_unchanged_work_item_changed(
worker.project_client.work_items.attachments.get_all.return_value = (
diagram_work_item.attachments
)
converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData(
model_converter.get_layer_name(diag),
converter_config.CapellaTypeConfig("diagram", "diagram", []),
diag,
)

converter.generate_work_items(worker.polarion_data_repo, False, True)
worker.compare_and_update_work_item(
Expand All @@ -358,11 +333,9 @@ def test_diagram_attachments_unchanged_work_item_changed(


def test_diagram_attachments_fully_unchanged(
model: capellambse.MelodyModel,
converter: model_converter.ModelConverter,
worker: polarion_worker.CapellaPolarionWorker,
):
diag = model.diagrams.by_uuid(TEST_DIAG_UUID)
converter = model_converter.ModelConverter(model, "TEST")
worker.polarion_data_repo = polarion_repo.PolarionDataRepository(
[
data_model.CapellaWorkItem(
Expand All @@ -372,11 +345,6 @@ def test_diagram_attachments_fully_unchanged(
)
]
)
converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData(
model_converter.get_layer_name(diag),
converter_config.CapellaTypeConfig("diagram", "diagram", []),
diag,
)

converter.generate_work_items(worker.polarion_data_repo, False, True)
worker.compare_and_update_work_item(
Expand All @@ -393,16 +361,15 @@ def test_add_context_diagram(
model: capellambse.MelodyModel,
worker: polarion_worker.CapellaPolarionWorker,
):
uuid = "11906f7b-3ae9-4343-b998-95b170be2e2b"
converter = model_converter.ModelConverter(model, "TEST")
worker.polarion_data_repo = polarion_repo.PolarionDataRepository(
[data_model.CapellaWorkItem(WORKITEM_ID, uuid_capella=uuid)]
[data_model.CapellaWorkItem(WORKITEM_ID, uuid_capella=TEST_PHYS_FNC)]
)

converter.converter_session[uuid] = data_session.ConverterData(
"",
converter.converter_session[TEST_PHYS_FNC] = data_session.ConverterData(
"pa",
converter_config.CapellaTypeConfig("test", "add_context_diagram", []),
model.by_uuid(uuid),
model.by_uuid(TEST_PHYS_FNC),
)

worker.project_client.work_items.attachments.create = mock.MagicMock()
Expand All @@ -412,7 +379,9 @@ def test_add_context_diagram(

converter.generate_work_items(worker.polarion_data_repo, False, True)

worker.compare_and_update_work_item(converter.converter_session[uuid])
worker.compare_and_update_work_item(
converter.converter_session[TEST_PHYS_FNC]
)

assert worker.project_client.work_items.update.call_count == 1
assert worker.project_client.work_items.attachments.create.call_count == 1
Expand Down Expand Up @@ -445,13 +414,12 @@ def test_update_context_diagram_no_changes(
model: capellambse.MelodyModel,
worker: polarion_worker.CapellaPolarionWorker,
):
uuid = "11906f7b-3ae9-4343-b998-95b170be2e2b"
converter = model_converter.ModelConverter(model, "TEST")
worker.polarion_data_repo = polarion_repo.PolarionDataRepository(
[
data_model.CapellaWorkItem(
WORKITEM_ID,
uuid_capella=uuid,
uuid_capella=TEST_PHYS_FNC,
checksum=json.dumps(
{
"__C2P__WORK_ITEM": WI_CONTEXT_DIAGRAM_CHECKSUM,
Expand All @@ -462,15 +430,17 @@ def test_update_context_diagram_no_changes(
]
)

converter.converter_session[uuid] = data_session.ConverterData(
"",
converter.converter_session[TEST_PHYS_FNC] = data_session.ConverterData(
"pa",
converter_config.CapellaTypeConfig("test", "add_context_diagram", []),
model.by_uuid(uuid),
model.by_uuid(TEST_PHYS_FNC),
)

with mock.patch.object(context.ContextDiagram, "render") as wrapped_render:
converter.generate_work_items(worker.polarion_data_repo, False, True)
worker.compare_and_update_work_item(converter.converter_session[uuid])
worker.compare_and_update_work_item(
converter.converter_session[TEST_PHYS_FNC]
)

assert worker.project_client.work_items.update.call_count == 0
assert worker.project_client.work_items.attachments.update.call_count == 0
Expand All @@ -481,13 +451,12 @@ def test_update_context_diagram_with_changes(
model: capellambse.MelodyModel,
worker: polarion_worker.CapellaPolarionWorker,
):
uuid = "11906f7b-3ae9-4343-b998-95b170be2e2b"
converter = model_converter.ModelConverter(model, "TEST")
worker.polarion_data_repo = polarion_repo.PolarionDataRepository(
[
data_model.CapellaWorkItem(
WORKITEM_ID,
uuid_capella=uuid,
uuid_capella=TEST_PHYS_FNC,
checksum=json.dumps(
{
"__C2P__WORK_ITEM": WI_CONTEXT_DIAGRAM_CHECKSUM,
Expand All @@ -498,10 +467,10 @@ def test_update_context_diagram_with_changes(
]
)

converter.converter_session[uuid] = data_session.ConverterData(
converter.converter_session[TEST_PHYS_FNC] = data_session.ConverterData(
"",
converter_config.CapellaTypeConfig("test", "add_context_diagram", []),
model.by_uuid(uuid),
model.by_uuid(TEST_PHYS_FNC),
)
worker.project_client.work_items.attachments.get_all.return_value = [
polarion_api.WorkItemAttachment(
Expand Down Expand Up @@ -532,7 +501,9 @@ def test_update_context_diagram_with_changes(
'width="100" height="100"></svg>'
)
converter.generate_work_items(worker.polarion_data_repo, False, True)
worker.compare_and_update_work_item(converter.converter_session[uuid])
worker.compare_and_update_work_item(
converter.converter_session[TEST_PHYS_FNC]
)

assert worker.project_client.work_items.update.call_count == 1
assert worker.project_client.work_items.attachments.update.call_count == 2
Expand Down

0 comments on commit 182ce81

Please sign in to comment.