Skip to content

Commit

Permalink
feat: additional enhancements and proper testing for the changes
Browse files Browse the repository at this point in the history
  • Loading branch information
micha91 committed Aug 29, 2024
1 parent 43eb988 commit e9d5527
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 57 deletions.
12 changes: 5 additions & 7 deletions capella2polarion/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,11 @@ def render_documents(
overwrite_layouts,
)

new_documents, updated_documents, work_items = renderer.render_documents(
configs, documents
)

polarion_worker.post_documents(new_documents)
polarion_worker.update_documents(updated_documents)
polarion_worker.update_headings(work_items)
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.update_documents(project_data.updated_docs, project)
polarion_worker.update_headings(project_data.work_items, project)


if __name__ == "__main__":
Expand Down
56 changes: 30 additions & 26 deletions capella2polarion/converters/document_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ class RenderingSession:
)


@dataclasses.dataclass
class ProjectData:
"""A class holding data of a project which documents are rendered for."""

new_docs: list[polarion_api.Document] = dataclasses.field(
default_factory=list
)
updated_docs: list[polarion_api.Document] = dataclasses.field(
default_factory=list
)
work_items: list[polarion_api.WorkItem] = dataclasses.field(
default_factory=list
)


class DocumentRenderer(polarion_html_helper.JinjaRendererMixin):
"""A Renderer class for Polarion documents."""

Expand All @@ -52,6 +67,7 @@ def __init__(
self.jinja_envs: dict[str, jinja2.Environment] = {}
self.overwrite_heading_numbering = overwrite_heading_numbering
self.overwrite_layouts = overwrite_layouts
self.projects: dict[str | None, ProjectData] = {}

def setup_env(self, env: jinja2.Environment):
"""Add globals and filters to the environment."""
Expand Down Expand Up @@ -318,36 +334,23 @@ def render_documents(
existing_documents: dict[
tuple[str | None, str, str], polarion_api.Document | None
],
) -> tuple[
list[polarion_api.Document],
list[polarion_api.Document],
list[polarion_api.WorkItem],
]:
) -> dict[str | None, ProjectData]:
"""Render all documents defined in the given config.
Returns a list new documents followed by updated documents and
work items, which need to be updated
"""

new_docs: list[polarion_api.Document] = []
updated_docs: list[polarion_api.Document] = []
work_items: list[polarion_api.WorkItem] = []
self._render_full_authority_documents(
configs.full_authority,
existing_documents,
new_docs,
updated_docs,
work_items,
)

self._render_mixed_authority_documents(
configs.mixed_authority,
existing_documents,
updated_docs,
work_items,
configs.mixed_authority, existing_documents
)

return new_docs, updated_docs, work_items
return self.projects

def _check_document_status(
self,
Expand Down Expand Up @@ -377,13 +380,14 @@ def _render_mixed_authority_documents(
existing_documents: dict[
tuple[str | None, str, str], polarion_api.Document | None
],
updated_docs: list[polarion_api.Document],
work_items: list[polarion_api.WorkItem],
):
for config in mixed_authority_configs:
rendering_layouts = document_config.generate_work_item_layouts(
config.work_item_layouts
)
project_data = self.projects.setdefault(
config.project_id, ProjectData()
)
for instance in config.instances:
old_doc = self._get_and_customize_doc(
config.project_id,
Expand Down Expand Up @@ -424,23 +428,23 @@ def _render_mixed_authority_documents(
)
continue

updated_docs.append(new_doc)
work_items.extend(wis)
project_data.updated_docs.append(new_doc)
project_data.work_items.extend(wis)

def _render_full_authority_documents(
self,
full_authority_configs,
existing_documents: dict[
tuple[str | None, str, str], polarion_api.Document | None
],
new_docs: list[polarion_api.Document],
updated_docs: list[polarion_api.Document],
work_items: list[polarion_api.WorkItem],
):
for config in full_authority_configs:
rendering_layouts = document_config.generate_work_item_layouts(
config.work_item_layouts
)
project_data = self.projects.setdefault(
config.project_id, ProjectData()
)
for instance in config.instances:
if old_doc := self._get_and_customize_doc(
config.project_id,
Expand Down Expand Up @@ -471,8 +475,8 @@ def _render_full_authority_documents(
)
continue

updated_docs.append(new_doc)
work_items.extend(wis)
project_data.updated_docs.append(new_doc)
project_data.work_items.extend(wis)
else:
try:
new_doc, _ = self.render_document(
Expand All @@ -495,7 +499,7 @@ def _render_full_authority_documents(
)
continue

new_docs.append(new_doc)
project_data.new_docs.append(new_doc)

def _extract_section_areas(self, html_elements: list[etree._Element]):
section_areas = {}
Expand Down
26 changes: 26 additions & 0 deletions tests/data/documents/combined_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ mixed_authority:
section_params:
section1:
param_1: Test
- template_directory: jupyter-notebooks/document_templates
sections:
section1: test-icd.html.j2
section2: test-icd.html.j2
heading_numbering: True
project_id: TestProject
status_allow_list:
- draft
- open
instances:
- polarion_space: _default
polarion_name: id1239
section_params:
section1:
param_1: Test
full_authority:
- template_directory: jupyter-notebooks/document_templates
template: test-icd.html.j2
Expand Down Expand Up @@ -66,3 +81,14 @@ full_authority:
instances:
- polarion_space: _default
polarion_name: id1238
- template_directory: jupyter-notebooks/document_templates
template: test-icd.html.j2
project_id: TestProject
status_allow_list:
- draft
- open
instances:
- polarion_space: _default
polarion_name: id1240
params:
interface: 2681f26a-e492-4e5d-8b33-92fb00a48622
36 changes: 29 additions & 7 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,32 @@ def test_render_documents(monkeypatch: pytest.MonkeyPatch):

assert result.exit_code == 0
assert mock_get_polarion_wi_map.call_count == 1
assert mock_get_document.call_count == 6
assert mock_post_documents.call_count == 1
assert len(mock_post_documents.call_args.args[0]) == 1
assert mock_update_documents.call_count == 1
assert len(mock_update_documents.call_args.args[0]) == 1
assert mock_update_headings.call_count == 1
assert len(mock_update_headings.call_args.args[0]) == 1
assert mock_get_document.call_count == 8
assert [call.args[2] for call in mock_get_document.call_args_list] == [
None,
None,
None,
"TestProject",
None,
None,
None,
"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_update_documents.call_count == 2
assert len(mock_update_documents.call_args_list[0].args[0]) == 1
assert len(mock_update_documents.call_args_list[1].args[0]) == 0
assert mock_update_documents.call_args_list[0].args[1] is None
assert mock_update_documents.call_args_list[1].args[1] == "TestProject"

assert mock_update_headings.call_count == 2
assert len(mock_update_headings.call_args_list[0].args[0]) == 1
assert len(mock_update_headings.call_args_list[1].args[0]) == 0
assert mock_update_headings.call_args_list[0].args[1] is None
assert mock_update_headings.call_args_list[1].args[1] == "TestProject"
58 changes: 41 additions & 17 deletions tests/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def existing_documents() -> (
(None, "_default", "id123"): polarion_api.Document(
module_folder="_default",
module_name="id123",
status="draft",
home_page_content=polarion_api.TextContent(
type="text/html",
value=MIXED_AUTHORITY_DOCUMENT.read_text("utf-8"),
Expand All @@ -39,6 +40,25 @@ def existing_documents() -> (
(None, "_default", "id1237"): polarion_api.Document(
module_folder="_default",
module_name="id1237",
status="draft",
home_page_content=polarion_api.TextContent(
type="text/html",
value=MIXED_AUTHORITY_DOCUMENT.read_text("utf-8"),
),
),
("TestProject", "_default", "id1239"): polarion_api.Document(
module_folder="_default",
module_name="id1239",
status="in_review",
home_page_content=polarion_api.TextContent(
type="text/html",
value=MIXED_AUTHORITY_DOCUMENT.read_text("utf-8"),
),
),
("TestProject", "_default", "id1240"): polarion_api.Document(
module_folder="_default",
module_name="id1240",
status="draft",
home_page_content=polarion_api.TextContent(
type="text/html",
value=MIXED_AUTHORITY_DOCUMENT.read_text("utf-8"),
Expand Down Expand Up @@ -233,24 +253,27 @@ def test_render_all_documents_partially_successfully(
empty_polarion_worker.polarion_data_repo, model
)

new_docs, updated_docs, work_items = renderer.render_documents(
conf, existing_documents()
)
projects_data = renderer.render_documents(conf, existing_documents())

# There are 6 documents in the config, we expect 3 rendering to fail
assert len(caplog.records) == 3
# There are 8 documents in the config, we expect 4 rendering to fail
assert len(caplog.records) == 4
# The first tree documents weren't rendered due to an error, the fourth
# wasn't rendered because of status restrictions, which is a just warning
assert [lr.levelno for lr in caplog.records] == [40, 40, 40, 30]
# For one valid config we did not pass a document, so we expect a new one
assert len(new_docs) == 1
# And two updated documents
assert len(updated_docs) == 2
# In both existing documents we had 2 headings. In full authority mode
assert len(projects_data[None].new_docs) == 1
# And three updated documents
assert len(projects_data[None].updated_docs) == 2
assert len(projects_data["TestProject"].updated_docs) == 1
# In all existing documents we had 2 headings. In full authority mode
# both should be updated and in mixed authority mode only one of them as
# the other is outside the rendering area
assert len(work_items) == 3
assert len(updated_docs[0].rendering_layouts) == 0
assert len(updated_docs[1].rendering_layouts) == 1
assert updated_docs[0].outline_numbering is None
assert updated_docs[1].outline_numbering is None
assert len(projects_data[None].work_items) == 3
assert len(projects_data["TestProject"].work_items) == 2
assert len(projects_data[None].updated_docs[0].rendering_layouts) == 0
assert len(projects_data[None].updated_docs[1].rendering_layouts) == 1
assert projects_data[None].updated_docs[0].outline_numbering is None
assert projects_data[None].updated_docs[1].outline_numbering is None


def test_render_all_documents_overwrite_headings_layouts(
Expand All @@ -264,7 +287,8 @@ def test_render_all_documents_overwrite_headings_layouts(
empty_polarion_worker.polarion_data_repo, model, True, True
)

_, updated_docs, _ = renderer.render_documents(conf, existing_documents())
projects_data = renderer.render_documents(conf, existing_documents())
updated_docs = projects_data[None].updated_docs

assert len(updated_docs[0].rendering_layouts) == 2
assert len(updated_docs[1].rendering_layouts) == 2
Expand Down Expand Up @@ -331,8 +355,8 @@ def test_combined_config():
with open(TEST_COMBINED_DOCUMENT_CONFIG, "r", encoding="utf-8") as f:
conf = document_config.read_config_file(f)

assert len(conf.full_authority) == 2
assert len(conf.mixed_authority) == 2
assert len(conf.full_authority) == 3
assert len(conf.mixed_authority) == 3


def test_rendering_config():
Expand Down

0 comments on commit e9d5527

Please sign in to comment.