Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
micha91 committed Sep 3, 2024
1 parent 5af1719 commit ae948a0
Show file tree
Hide file tree
Showing 11 changed files with 355 additions and 57 deletions.
2 changes: 1 addition & 1 deletion capella2polarion/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def render_documents(

projects_document_data = renderer.render_documents(configs, documents)
for project, project_data in projects_document_data.items():
polarion_worker.post_documents(project_data.new_docs, project)
polarion_worker.create_documents(project_data.new_docs, project)
polarion_worker.update_documents(project_data.updated_docs, project)


Expand Down
2 changes: 1 addition & 1 deletion capella2polarion/connectors/polarion_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def compare_and_update_work_items(
if uuid in self.polarion_data_repo and data.work_item is not None:
self.compare_and_update_work_item(data)

def post_documents(
def create_documents(
self,
document_datas: list[data_models.DocumentData],
document_project: str | None = None,
Expand Down
38 changes: 0 additions & 38 deletions capella2polarion/converters/polarion_html_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,44 +165,6 @@ def extract_work_items(
return work_items


def insert_text_work_items(
document: polarion_api.Document,
text_work_items: dict[str, polarion_api.WorkItem],
text_work_item_type: str,
):
"""Insert text work items into the given document."""
if not text_work_items:
return

assert document.home_page_content is not None
layout_index = get_layout_index(
"paragraph", document.rendering_layouts, text_work_item_type
)
html_fragments = ensure_fragments(document.home_page_content.value)
new_content = []
last_match = -1
for index, element in enumerate(html_fragments):
if isinstance(element, html.HtmlComment):
continue

if element.tag == "workitem":
new_content += html_fragments[last_match + 1 : index]
last_match = index
if work_item := text_work_items.get(element.get("id")):
new_content.append(
html.fromstring(
POLARION_WORK_ITEM_DOCUMENT.format(
pid=work_item.id, lid=layout_index, custom_info=""
)
)
)

new_content += html_fragments[last_match + 1 :]
document.home_page_content.value = "\n".join(
[html.tostring(element).decode("utf-8") for element in new_content]
)


def get_layout_index(
default_layouter: str,
rendering_layouts: list[polarion_api.RenderingLayout],
Expand Down
1 change: 1 addition & 0 deletions capella2polarion/converters/text_work_item_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def insert_text_work_items(
return

assert document.home_page_content is not None
assert document.rendering_layouts is not None
layout_index = html_helper.get_layout_index(
"paragraph", document.rendering_layouts, self.text_work_item_type
)
Expand Down
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,7 @@ def empty_polarion_worker(monkeypatch: pytest.MonkeyPatch):
delete_work_items=True,
)
yield polarion_worker.CapellaPolarionWorker(polarion_params)


DOCUMENT_TEMPLATES = TEST_DOCUMENT_ROOT / "templates"
DOCUMENT_TEXT_WORK_ITEMS = "document_work_items.html.j2"
1 change: 1 addition & 0 deletions tests/data/documents/sections/section1.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
{{ heading(3, "New Heading", session) }}
<p>{{ global_param }}</p>
<p>{{ local_param }}</p>
<workitem id="id1">TestContent</workitem>
1 change: 1 addition & 0 deletions tests/data/documents/sections/section2.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
{{ heading(3, "Keep Heading", session) }}
<p>Overwritten: {{ global_param }}</p>
<p>{{ local_param }}</p>
<workitem id="id2">TestContent</workitem>
10 changes: 10 additions & 0 deletions tests/data/documents/templates/document_work_items.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{#
Copyright DB InfraGO AG and contributors
SPDX-License-Identifier: Apache-2.0
#}

<workitem id="id1">This is Text in a text workitem</workitem>
<workitem id="id2">
<span>Text</span>
<table><tr><td>1</td><td>2</td></tr></table>
</workitem>
16 changes: 8 additions & 8 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ def test_render_documents(monkeypatch: pytest.MonkeyPatch):
"get_document",
mock_get_document,
)
mock_post_documents = mock.MagicMock()
mock_create_documents = mock.MagicMock()
monkeypatch.setattr(
polarion_worker.CapellaPolarionWorker,
"post_documents",
mock_post_documents,
"create_documents",
mock_create_documents,
)
mock_update_documents = mock.MagicMock()
monkeypatch.setattr(
Expand Down Expand Up @@ -161,11 +161,11 @@ def test_render_documents(monkeypatch: pytest.MonkeyPatch):
"TestProject",
]

assert mock_post_documents.call_count == 2
assert len(mock_post_documents.call_args_list[0].args[0]) == 1
assert len(mock_post_documents.call_args_list[1].args[0]) == 1
assert mock_post_documents.call_args_list[0].args[1] is None
assert mock_post_documents.call_args_list[1].args[1] == "TestProject"
assert mock_create_documents.call_count == 2
assert len(mock_create_documents.call_args_list[0].args[0]) == 1
assert len(mock_create_documents.call_args_list[1].args[0]) == 1
assert mock_create_documents.call_args_list[0].args[1] is None
assert mock_create_documents.call_args_list[1].args[1] == "TestProject"

assert mock_update_documents.call_count == 2
assert len(mock_update_documents.call_args_list[0].args[0]) == 1
Expand Down
159 changes: 150 additions & 9 deletions tests/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@

from capella2polarion import data_models as dm
from capella2polarion.connectors import polarion_worker
from capella2polarion.converters import document_config, document_renderer
from tests.conftest import TEST_COMBINED_DOCUMENT_CONFIG, TEST_DOCUMENT_ROOT
from capella2polarion.converters import (
document_config,
document_renderer,
text_work_item_provider,
)
from tests.conftest import (
DOCUMENT_TEMPLATES,
DOCUMENT_TEXT_WORK_ITEMS,
TEST_COMBINED_DOCUMENT_CONFIG,
TEST_DOCUMENT_ROOT,
)

CLASSES_TEMPLATE = "test-classes.html.j2"
JUPYTER_TEMPLATE_FOLDER = "jupyter-notebooks/document_templates"
Expand Down Expand Up @@ -231,30 +240,162 @@ def test_mixed_authority_document(
"global_param": "Overwrite global param",
},
},
text_work_item_provider=text_work_item_provider.TextWorkItemProvider(
"MyField",
"MyType",
[
polarion_api.WorkItem(
id="EXISTING", additional_attributes={"MyField": "id1"}
)
],
),
)

content: list[etree._Element] = html.fromstring(
document_data.document.home_page_content.value
)

assert len(content) == 15
assert len(document_data.text_work_item_provider.new_text_work_items) == 2
assert (
document_data.text_work_item_provider.new_text_work_items["id1"].id
is None
)
assert (
document_data.text_work_item_provider.new_text_work_items["id2"].id
is None
)
assert len(content) == 17
assert [c.tag for c in content[:3]] == ["h1", "p", "p"]
assert (c4 := content[4]).tag == "h3" and c4.text == "New Heading"
assert content[5].text == "Global Test"
assert content[6].text == "Local Test section 1"
assert content[8].text == "This will be kept."
assert content[10].get("id") == (
assert content[9].text == "This will be kept."
assert content[11].get("id") == (
"polarion_wiki macro name=module-workitem;params=id=ATSY-18305"
)
assert content[10].tag == "h3"
assert content[11].text == "Overwritten: Overwrite global param"
assert content[12].text == "Local Test section 2"
assert content[14].text == "Some postfix stuff"
assert content[11].tag == "h3"
assert content[12].text == "Overwritten: Overwrite global param"
assert content[13].text == "Local Test section 2"
assert content[16].text == "Some postfix stuff"
assert len(document_data.headings) == 1
assert document_data.headings[0].id == "ATSY-18305"
assert document_data.headings[0].title == "Keep Heading"


def test_create_full_authority_document_text_work_items(
empty_polarion_worker: polarion_worker.CapellaPolarionWorker,
model: capellambse.MelodyModel,
):
renderer = document_renderer.DocumentRenderer(
empty_polarion_worker.polarion_data_repo, model
)

document_data = renderer.render_document(
DOCUMENT_TEMPLATES,
DOCUMENT_TEXT_WORK_ITEMS,
"_default",
"TEST-DOC",
text_work_item_provider=text_work_item_provider.TextWorkItemProvider(
"MyField",
"MyType",
),
)

assert len(document_data.text_work_item_provider.new_text_work_items) == 2
assert (
document_data.text_work_item_provider.new_text_work_items["id1"].id
is None
)
assert (
document_data.text_work_item_provider.new_text_work_items["id1"].type
== "MyType"
)
assert (
document_data.text_work_item_provider.new_text_work_items[
"id1"
].additional_attributes["MyField"]
== "id1"
)
assert (
document_data.text_work_item_provider.new_text_work_items["id2"].id
is None
)
assert (
document_data.text_work_item_provider.new_text_work_items["id2"].type
== "MyType"
)
assert (
document_data.text_work_item_provider.new_text_work_items[
"id2"
].additional_attributes["MyField"]
== "id2"
)


def test_update_full_authority_document_text_work_items(
empty_polarion_worker: polarion_worker.CapellaPolarionWorker,
model: capellambse.MelodyModel,
):
renderer = document_renderer.DocumentRenderer(
empty_polarion_worker.polarion_data_repo, model
)
old_doc = polarion_api.Document(
module_folder="_default",
module_name="TEST-DOC",
home_page_content=polarion_api.TextContent(
type="text/html",
value="",
),
)

document_data = renderer.render_document(
DOCUMENT_TEMPLATES,
DOCUMENT_TEXT_WORK_ITEMS,
"_default",
"TEST-DOC",
document=old_doc,
text_work_item_provider=text_work_item_provider.TextWorkItemProvider(
"MyField",
"MyType",
[
polarion_api.WorkItem(
id="EXISTING", additional_attributes={"MyField": "id1"}
)
],
),
)

assert len(document_data.text_work_item_provider.new_text_work_items) == 2
assert (
document_data.text_work_item_provider.new_text_work_items["id1"].id
== "EXISTING"
)
assert (
document_data.text_work_item_provider.new_text_work_items["id1"].type
is None
)
assert (
document_data.text_work_item_provider.new_text_work_items[
"id1"
].additional_attributes["MyField"]
== "id1"
)
assert (
document_data.text_work_item_provider.new_text_work_items["id2"].id
is None
)
assert (
document_data.text_work_item_provider.new_text_work_items["id2"].type
== "MyType"
)
assert (
document_data.text_work_item_provider.new_text_work_items[
"id2"
].additional_attributes["MyField"]
== "id2"
)


def test_render_all_documents_partially_successfully(
empty_polarion_worker: polarion_worker.CapellaPolarionWorker,
model: capellambse.MelodyModel,
Expand Down
Loading

0 comments on commit ae948a0

Please sign in to comment.