"""Test the `parse_dimensions` function and its helpers""" from helpers.dimensions import parse_dimensions, parse_dimensions_measure 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, }