"""Test the `parse_dimensions` function and its helpers""" from helpers.dimensions import parse_dimensions, parse_dimensions_measure, units_to_cm def test_none(): """Test None value""" assert parse_dimensions(None) == {"height": None, "width": None, "depth": None} def test_parse_dimensions(): """Test a normal example from the sample file""" dimensions_str = "23 inches (H) x 1 inches (W) x 23 inches (D)" assert parse_dimensions_measure(dimensions_str, "W") == { "unit": "inches", "value": 1.0, } assert parse_dimensions_measure(dimensions_str, "H") == { "unit": "inches", "value": 23.0, } assert parse_dimensions_measure(dimensions_str, "D") == { "unit": "inches", "value": 23.0, } assert parse_dimensions(dimensions_str) == { "depth": 58.42, "height": 58.42, "width": 2.54, } def test_parse_dimensions_comma(): """Test a normal example from the sample file, but the separator is a comma""" dimensions_str = "23 inches (H), 1 inches (W), 23 inches (D)" assert parse_dimensions_measure(dimensions_str, "W") == { "unit": "inches", "value": 1.0, } assert parse_dimensions_measure(dimensions_str, "H") == { "unit": "inches", "value": 23.0, } assert parse_dimensions_measure(dimensions_str, "D") == { "unit": "inches", "value": 23.0, } assert parse_dimensions(dimensions_str) == { "depth": 58.42, "height": 58.42, "width": 2.54, } def test_parse_dimensions_feet(): """Test a normal example from the sample file, but the units is feet""" dimensions_str = "23 feet (H) x 1 feet (W) x 23 feet (D)" assert parse_dimensions_measure(dimensions_str, "W") == { "unit": "feet", "value": 1.0, } assert parse_dimensions_measure(dimensions_str, "H") == { "unit": "feet", "value": 23.0, } assert parse_dimensions_measure(dimensions_str, "D") == { "unit": "feet", "value": 23.0, } assert parse_dimensions(dimensions_str) == { "depth": 701.04, "height": 701.04, "width": 30.48, } def test_parse_dimensions_missing(): """Test a normal example from the sample file but some measurement is missing""" dimensions_str = "23 inches (H) x 23 inches (D)" assert parse_dimensions_measure(dimensions_str, "W") is None assert parse_dimensions_measure(dimensions_str, "H") == { "unit": "inches", "value": 23.0, } assert parse_dimensions_measure(dimensions_str, "D") == { "unit": "inches", "value": 23.0, } assert parse_dimensions(dimensions_str) == { "depth": 58.42, "height": 58.42, "width": None, } def test_units_to_cm_inches(): """Test `units_to_cm` where the units is inches""" assert units_to_cm(value=0.5, unit="inches") == 1.27 def test_units_to_cm_feet(): """Test `units_to_cm` where the units is inches""" assert units_to_cm(value=0.5, unit="feet") == 15.24 def test_units_to_cm_cm(): """Test `units_to_cm` where the units is already cm""" assert units_to_cm(value=0.5, unit="cm") == 0.5 def test_units_to_cm_unrecognized(): """Test `units_to_cm` where the units are not recognized""" assert units_to_cm(value=0.5, unit="yard") is None