diff --git a/checkbox-ng/plainbox/impl/exporter/test_xlsx.py b/checkbox-ng/plainbox/impl/exporter/test_xlsx.py index a276506825..f1a0c8732a 100644 --- a/checkbox-ng/plainbox/impl/exporter/test_xlsx.py +++ b/checkbox-ng/plainbox/impl/exporter/test_xlsx.py @@ -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): @@ -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 + ) diff --git a/checkbox-ng/plainbox/impl/exporter/xlsx.py b/checkbox-ng/plainbox/impl/exporter/xlsx.py index e1d9a11b5f..297f0d7362 100644 --- a/checkbox-ng/plainbox/impl/exporter/xlsx.py +++ b/checkbox-ng/plainbox/impl/exporter/xlsx.py @@ -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]) ) @@ -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