Skip to content

Commit

Permalink
➕ Test cases on luminance and multiple export
Browse files Browse the repository at this point in the history
  • Loading branch information
nico-darnis committed Jan 31, 2025
1 parent fdd3d31 commit 85692f6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
8 changes: 4 additions & 4 deletions mrp_flattened_bom_xlsx/report/flattened_bom_xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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)
Expand All @@ -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 ""
Expand Down
50 changes: 49 additions & 1 deletion mrp_flattened_bom_xlsx/tests/test_flattened_bom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")

Expand Down Expand Up @@ -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}")

0 comments on commit 85692f6

Please sign in to comment.