Skip to content

Commit

Permalink
Refactor xlsx exporter to make it easier to test and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pieqq committed Apr 24, 2024
1 parent 427414a commit 72b766c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 17 deletions.
47 changes: 47 additions & 0 deletions checkbox-ng/plainbox/impl/exporter/test_xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
from unittest.mock import MagicMock, ANY

from plainbox.impl.exporter.xlsx import XLSXSessionStateExporter
from plainbox.impl.session import SessionState
from plainbox.impl.unit.job import JobDefinition
from plainbox.impl.unit.category import CategoryUnit


class XLSXSessionStateExporterTests(TestCase):
Expand Down Expand Up @@ -52,3 +55,47 @@ def test_write_job(self):
self_mock.worksheet3.write.assert_any_call(ANY, ANY, "", ANY)
# self_mock.worksheet3.write.assert_called_with(ANY, ANY, "", ANY)
# self.assertEqual(self_mock.worksheet3.write.call_count, 2)

def test_category_map(self):
self_mock = MagicMock()
A = JobDefinition({"id": "A", "category_id": "test"})
B = JobDefinition({"id": "B", "certification-status": "blocker"})
job_list = [A, B]
unit = MagicMock(name="unit", spec_set=CategoryUnit)
unit.id = "test"
unit.tr_name.return_value = "Test"
unit.Meta.name = "category"

state = SessionState(job_list)
state.update_desired_job_list(job_list)
state.unit_list.append(unit)

self.assertEqual(
XLSXSessionStateExporter._category_map(self_mock, state),
{"test": "Test"},
)

def test_write_tp_export(self):
self_mock = MagicMock()
self_mock._category_map.return_value = {
"com.canonical.plainbox::uncategorised": "test"
}
data = MagicMock()
A = JobDefinition({"id": "A"})
B = JobDefinition({"id": "B", "certification-status": "blocker"})
job_list = [A, B]
unit = MagicMock(name="unit", spec_set=CategoryUnit)
unit.Meta.name = "category"

state = SessionState(job_list)
state.update_desired_job_list(job_list)
state.unit_list.append(unit)

data["manager"].default_device_context.state = state
XLSXSessionStateExporter.write_tp_export(self_mock, data)
self_mock.worksheet4.write_row.assert_any_call(
ANY, 0, ["A", "", "A"], ANY
)
self_mock.worksheet4.write_row.assert_any_call(
ANY, 0, ["B", "blocker", "B"], ANY
)
33 changes: 16 additions & 17 deletions checkbox-ng/plainbox/impl/exporter/xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,24 +877,23 @@ def write_results(self, data):
)
self.worksheet3.autofilter(5, max_level, self._lineno, max_level + 3)

def write_tp_export(self, data):
def _category_map(state):
"""Map from category id to their corresponding translated names."""
wanted_category_ids = frozenset(
{
job_state.effective_category_id
for job_state in state.job_state_map.values()
if job_state.job in state.run_list
and job_state.job.plugin not in ("resource", "attachment")
}
)
return {
unit.id: unit.tr_name()
for unit in state.unit_list
if unit.Meta.name == "category"
and unit.id in wanted_category_ids
def _category_map(self, state):
"""Map from category id to their corresponding translated names."""
wanted_category_ids = frozenset(
{
job_state.effective_category_id
for job_state in state.job_state_map.values()
if job_state.job in state.run_list
and job_state.job.plugin not in ("resource", "attachment")
}
)
return {
unit.id: unit.tr_name()
for unit in state.unit_list
if unit.Meta.name == "category" and unit.id in wanted_category_ids
}

def write_tp_export(self, data):
self.worksheet4.set_header(
"&C{}".format(data["manager"].test_plans[0])
)
Expand All @@ -912,7 +911,7 @@ def _category_map(state):
self.worksheet4.repeat_rows(0)
self._lineno = 0
state = data["manager"].default_device_context.state
cat_map = _category_map(state)
cat_map = self._category_map(state)
run_list_ids = [job.id for job in state.run_list]
for cat_id in sorted(cat_map, key=lambda x: cat_map[x].casefold()):
self._lineno += 1
Expand Down

0 comments on commit 72b766c

Please sign in to comment.