From 6ad99495712a382f682b75d9a6cb9e447a7d3ca7 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Tue, 27 Jan 2026 19:12:43 +1100 Subject: [PATCH] Update tests based on new simpler unit settings code --- src/bonsai/bonsai/bim/prop.py | 4 +- src/bonsai/bonsai/tool/unit.py | 15 +++-- src/bonsai/test/tool/test_unit.py | 96 ++++++++++++------------------- 3 files changed, 51 insertions(+), 64 deletions(-) diff --git a/src/bonsai/bonsai/bim/prop.py b/src/bonsai/bonsai/bim/prop.py index 8e6b8ed7c8..4e93c1c690 100644 --- a/src/bonsai/bonsai/bim/prop.py +++ b/src/bonsai/bonsai/bim/prop.py @@ -627,8 +627,8 @@ class BIMProperties(PropertyGroup): items=[ ("NONE", "None", ""), ("GRAM", "Gram", "Grams"), - ("KILO/KILOGRAM", "Kilogram", "Kilograms"), - ("MEGA/TONNE", "Tonne", "Metric Tons"), + ("KILO/GRAM", "Kilogram", "Kilograms"), + ("MEGA/GRAM", "Tonne", "Metric Tons"), ("pound", "Pound", "Pounds"), ("ounce", "Ounce", "Ounces"), ], diff --git a/src/bonsai/bonsai/tool/unit.py b/src/bonsai/bonsai/tool/unit.py index 823c50bdc3..d7b35e3178 100644 --- a/src/bonsai/bonsai/tool/unit.py +++ b/src/bonsai/bonsai/tool/unit.py @@ -336,11 +336,18 @@ class Unit(bonsai.core.tool.Unit): def get_scene_unit_name(cls, unit_type: UNIT_TYPE) -> str | None: if unit_type == "LENGTHUNIT": name = bpy.context.scene.unit_settings.length_unit - name = {"MILES": "mile", "FEET": "foot", "INCHES": "inch", "THOU": "thou", "ADAPTIVE": "METERS"}.get( - name, name + if name == "ADAPTIVE": + if bpy.context.scene.unit_settings.system == "IMPERIAL": + name = "foot" + else: + name = "METRE" + name = ( + {"MILES": "mile", "FEET": "foot", "INCHES": "inch", "THOU": "thou", "ADAPTIVE": "METERS"} + .get(name, name) + .replace("METERS", "METRE") ) - if len(name) > len("METERS") and name.endswith("METERS"): - return f"{name[:-6]}/METRE" + if len(name) > len("METRE") and name.endswith("METRE"): + return f"{name[:-5]}/METRE" return name bim_props = tool.Blender.get_bim_props() if (name := getattr(bim_props, f"{unit_type[:-4].lower()}_unit")) != "NONE": diff --git a/src/bonsai/test/tool/test_unit.py b/src/bonsai/test/tool/test_unit.py index 6e0e756b67..dcd96c4dea 100644 --- a/src/bonsai/test/tool/test_unit.py +++ b/src/bonsai/test/tool/test_unit.py @@ -131,25 +131,43 @@ class TestGetSceneUnitName(NewFile): assert subject.get_scene_unit_name("AREAUNIT") == "square foot" assert subject.get_scene_unit_name("VOLUMEUNIT") == "cubic inch" + def test_getting_an_si_name(self): + props = tool.Blender.get_bim_props() + bpy.context.scene.unit_settings.system = "METRIC" + bpy.context.scene.unit_settings.length_unit = "METERS" + props.area_unit = "SQUARE_METRE" + props.volume_unit = "CUBIC_METRE" + assert subject.get_scene_unit_name("LENGTHUNIT") == "METRE" + assert subject.get_scene_unit_name("AREAUNIT") == "SQUARE_METRE" + assert subject.get_scene_unit_name("VOLUMEUNIT") == "CUBIC_METRE" + bpy.context.scene.unit_settings.length_unit = "MILLIMETERS" + assert subject.get_scene_unit_name("LENGTHUNIT") == "MILLI/METRE" + bpy.context.scene.unit_settings.length_unit = "ADAPTIVE" + assert subject.get_scene_unit_name("LENGTHUNIT") == "METRE" + def test_getting_a_name_with_no_unit_system(self): assert bpy.context.scene bpy.context.scene.unit_settings.system = "NONE" - assert subject.get_scene_unit_name("LENGTHUNIT") == "foot" + assert subject.get_scene_unit_name("LENGTHUNIT") == "METRE" + bpy.context.scene.unit_settings.length_unit = "ADAPTIVE" + assert subject.get_scene_unit_name("LENGTHUNIT") == "METRE" + assert subject.get_scene_unit_name("AREAUNIT") == "SQUARE_METRE" + assert subject.get_scene_unit_name("VOLUMEUNIT") == "CUBIC_METRE" def test_getting_mass_unit_names(self): """Test getting mass unit names for different systems""" assert bpy.context.scene props = tool.Blender.get_bim_props() props.mass_unit = "GRAM" - assert subject.get_scene_unit_name("MASSUNIT") == "gram" - props.mass_unit = "KILOGRAM" - assert subject.get_scene_unit_name("MASSUNIT") == "kilogram" - props.mass_unit = "POUND" + assert subject.get_scene_unit_name("MASSUNIT") == "GRAM" + props.mass_unit = "KILO/GRAM" + assert subject.get_scene_unit_name("MASSUNIT") == "KILO/GRAM" + props.mass_unit = "MEGA/GRAM" + assert subject.get_scene_unit_name("MASSUNIT") == "MEGA/GRAM" + props.mass_unit = "pound" assert subject.get_scene_unit_name("MASSUNIT") == "pound" - props.mass_unit = "OUNCE" + props.mass_unit = "ounce" assert subject.get_scene_unit_name("MASSUNIT") == "ounce" - props.mass_unit = "TONNE" - assert subject.get_scene_unit_name("MASSUNIT") == "tonne" def test_getting_time_unit_names(self): """Test getting time unit names for different systems""" @@ -157,64 +175,26 @@ class TestGetSceneUnitName(NewFile): props = tool.Blender.get_bim_props() props.time_unit = "SECOND" - assert subject.get_scene_unit_name("TIMEUNIT") == "second" - props.time_unit = "MINUTE" + assert subject.get_scene_unit_name("TIMEUNIT") == "SECOND" + props.time_unit = "minute" assert subject.get_scene_unit_name("TIMEUNIT") == "minute" - props.time_unit = "HOUR" - assert subject.get_scene_unit_name("TIMEUNIT") == "hour" - props.time_unit = "DAY" - assert subject.get_scene_unit_name("TIMEUNIT") == "day" + props.time_unit = "NONE" + assert not subject.get_scene_unit_name("TIMEUNIT") class TestGetSceneUnitSIPrefix: def test_run(self): assert bpy.context.scene - bpy.context.scene.unit_settings.system = "METRIC" - bpy.context.scene.unit_settings.length_unit = "METERS" - assert subject.get_scene_unit_si_prefix("LENGTHUNIT") is None - bpy.context.scene.unit_settings.length_unit = "MICROMETERS" - assert subject.get_scene_unit_si_prefix("LENGTHUNIT") == "MICRO" - bpy.context.scene.unit_settings.length_unit = "MILLIMETERS" - assert subject.get_scene_unit_si_prefix("LENGTHUNIT") == "MILLI" - bpy.context.scene.unit_settings.length_unit = "CENTIMETERS" - assert subject.get_scene_unit_si_prefix("LENGTHUNIT") == "CENTI" - bpy.context.scene.unit_settings.length_unit = "KILOMETERS" - assert subject.get_scene_unit_si_prefix("LENGTHUNIT") == "KILO" - bpy.context.scene.unit_settings.length_unit = "ADAPTIVE" - assert subject.get_scene_unit_si_prefix("LENGTHUNIT") is None - props = tool.Blender.get_bim_props() - props.area_unit = "SQUARE_METRE" - assert subject.get_scene_unit_si_prefix("AREAUNIT") is None - props.area_unit = "MILLI/SQUARE_METRE" - assert subject.get_scene_unit_si_prefix("AREAUNIT") == "MILLI" - props.volume_unit = "CUBIC_METRE" - assert subject.get_scene_unit_si_prefix("VOLUMEUNIT") is None - props.volume_unit = "MILLI/CUBIC_METRE" - assert subject.get_scene_unit_si_prefix("VOLUMEUNIT") == "MILLI" + assert subject.get_scene_unit_si_prefix("METRE") is None + assert subject.get_scene_unit_si_prefix("MICRO/METRE") == "MICRO" + assert subject.get_scene_unit_si_prefix("SQUARE_METRE") is None + assert subject.get_scene_unit_si_prefix("MILLI/SQUARE_METRE") == "MILLI" + assert subject.get_scene_unit_si_prefix("foot") is None def test_mass_and_time_unit_prefixes(self): - assert bpy.context.scene - props = tool.Blender.get_bim_props() - - props.mass_unit = "KILOGRAM" - assert subject.get_scene_unit_si_prefix("MASSUNIT") == "KILO" - props.mass_unit = "GRAM" - assert subject.get_scene_unit_si_prefix("MASSUNIT") is None - props.mass_unit = "POUND" - assert subject.get_scene_unit_si_prefix("MASSUNIT") == "CONVERSION" - props.mass_unit = "OUNCE" - assert subject.get_scene_unit_si_prefix("MASSUNIT") == "CONVERSION" - props.mass_unit = "TONNE" - assert subject.get_scene_unit_si_prefix("MASSUNIT") == "MEGA" - - props.time_unit = "SECOND" - assert subject.get_scene_unit_si_prefix("TIMEUNIT") is None - props.time_unit = "MINUTE" - assert subject.get_scene_unit_si_prefix("TIMEUNIT") == "CONVERSION" - props.time_unit = "HOUR" - assert subject.get_scene_unit_si_prefix("TIMEUNIT") == "CONVERSION" - props.time_unit = "DAY" - assert subject.get_scene_unit_si_prefix("TIMEUNIT") == "CONVERSION" + assert subject.get_scene_unit_si_prefix("KILO/GRAM") == "KILO" + assert subject.get_scene_unit_si_prefix("MEGA/GRAM") == "MEGA" + assert subject.get_scene_unit_si_prefix("GRAM") is None class TestImportUnitAttributes(NewFile):