From 85692f6c8245ee267d98a640d2c8db5936db6abd Mon Sep 17 00:00:00 2001 From: Nico Darnis Date: Fri, 31 Jan 2025 16:19:36 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=20Test=20cases=20on=20luminance=20and?= =?UTF-8?q?=20multiple=20export?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../report/flattened_bom_xlsx.py | 8 +-- .../tests/test_flattened_bom.py | 50 ++++++++++++++++++- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.py b/mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.py index 7ae2a32f..593eb50b 100644 --- a/mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.py +++ b/mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.py @@ -16,7 +16,7 @@ def get_text_color(self, hex_color): linear_rgb = [] for i in range(0, 6, 2): - value = int(hex_color[i:i+2], 16) / 255.0 + value = int(hex_color[i : i + 2], 16) / 255.0 linear_rgb.append( value / 12.92 if value <= 0.04045 else ((value + 0.055) / 1.055) ** 2.4 ) @@ -58,11 +58,11 @@ def generate_xlsx_report(self, workbook, data, objects): "bold": True, "bg_color": bg_color, "font_color": text_color, - "bottom": 1 + "bottom": 1, } ) - sheet_name = bom.code or _("BOM") + sheet_name = f"{bom.id}/{bom.code}"[:31] sheet = workbook.add_worksheet(sheet_name[:31]) sheet.set_landscape() sheet.fit_to_pages(1, 0) @@ -72,7 +72,7 @@ def generate_xlsx_report(self, workbook, data, objects): sheet.set_column(3, 4, 10) # Company Info - company_name = ( + company_name = ( bom.company_id.partner_id.contact_address_inline or bom.company_id.name or "" diff --git a/mrp_flattened_bom_xlsx/tests/test_flattened_bom.py b/mrp_flattened_bom_xlsx/tests/test_flattened_bom.py index 255ded05..f702dc0e 100644 --- a/mrp_flattened_bom_xlsx/tests/test_flattened_bom.py +++ b/mrp_flattened_bom_xlsx/tests/test_flattened_bom.py @@ -2,7 +2,9 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). from odoo.tests.common import TransactionCase - +import base64 +import zipfile +import io class TestFlattenedBom(TransactionCase): @classmethod @@ -12,6 +14,7 @@ def setUpClass(cls): cls.product_obj = cls.env["product.product"] cls.bom_obj = cls.env["mrp.bom"] cls.bom_line_obj = cls.env["mrp.bom.line"] + cls.report_obj = cls.env.ref("mrp_flattened_bom_xlsx.flattened_bom_xlsx") cls.uom_dozen = cls.env.ref("uom.product_uom_dozen") @@ -113,3 +116,48 @@ def test_02_different_uom(self): self.assertAlmostEqual(flat_tot.get(self.component_2), 10 / 12) # Component 3 = 5*3 = 15 units -> 15/12 dozens self.assertAlmostEqual(flat_tot.get(self.component_3), 15 / 12) + + + def test_03_multiple_export(self): + """Test XLSX report generation with separate sheets for each BoM selected""" + report = self.report_obj + bom_records = self.bom_top | self.bom_sub_1 | self.bom_sub_2 + + # Generate the report + report_data = report._render_xlsx("mrp_flattened_bom_xlsx.flattened_bom_xlsx", bom_records.ids, data={}) + report_bytes = report_data[0] + + with zipfile.ZipFile(io.BytesIO(report_bytes), "r") as xlsx_zip: + sheet_files = [name for name in xlsx_zip.namelist() if name.startswith("xl/worksheets/sheet")] + + # Ensure the correct number of sheets exists + self.assertEqual(len(bom_records), len(sheet_files)) + + + def test_get_text_color(self): + """Test that text color is correctly determined based on background luminance.""" + + # {background_color: expected_text_color} + test_cases = { + "#FFFFFF": "#000000", # White background -> Black text + "#000000": "#FFFFFF", # Black background -> White text + "#FF0000": "#000000", # Red background -> Black text + "#00FF00": "#000000", # Green background -> Black text + "#0000FF": "#FFFFFF", # Blue background -> White text + "#FFFF00": "#000000", # Yellow background -> Black text + "#00FFFF": "#000000", # Cyan background -> Black text + "#FF00FF": "#000000", # Magenta background -> Black text + "#808080": "#000000", # Gray background -> Black text + "#C0C0C0": "#000000", # Light gray -> Black text + "#101010": "#FFFFFF", # Dark gray -> White text + } + + # Instantiate the model class + model_instance = self.env["report.mrp_flattened_bom_xlsx.flattened_bom_xlsx"] + + # Run tests + for bg_color, expected_text_color in test_cases.items(): + with self.subTest(bg_color=bg_color): + result = model_instance.get_text_color(bg_color) + + self.assertEqual(result, expected_text_color, f"Failed for {bg_color}")