diff --git a/pipeline/beam_etl/helpers/dimensions.py b/pipeline/beam_etl/helpers/dimensions.py index ec735b8..95692de 100644 --- a/pipeline/beam_etl/helpers/dimensions.py +++ b/pipeline/beam_etl/helpers/dimensions.py @@ -6,6 +6,8 @@ import logging from typing import Dict, Optional import re +from helpers.misc import convert_units + UNIT_CONVERSIONS = {"inches": 2.54, "feet": 30.48, "cm": 1} @@ -31,17 +33,6 @@ def parse_dimensions_measure(dimensions: str, measure: str) -> Optional[Dict]: return None -def units_to_cm(value: float, unit: str) -> Optional[float]: - """Convert a given dimension unit into centimeters. - If unrecognized unit, return None""" - try: - conversion = UNIT_CONVERSIONS[unit] - except KeyError: - logging.error("unrecognized unit: %s", unit) - return None - return value * conversion - - def parse_dimensions(dimensions: Optional[str]) -> Dict[str, Optional[float]]: """Parse a string representing dimensions""" if dimensions is None: @@ -63,5 +54,5 @@ def parse_dimensions(dimensions: Optional[str]) -> Dict[str, Optional[float]]: if value is None: result[key] = value else: - result[key] = units_to_cm(**value) + result[key] = convert_units(**value, unit_conversions=UNIT_CONVERSIONS) return result diff --git a/pipeline/beam_etl/helpers/misc.py b/pipeline/beam_etl/helpers/misc.py new file mode 100644 index 0000000..fdf9f34 --- /dev/null +++ b/pipeline/beam_etl/helpers/misc.py @@ -0,0 +1,17 @@ +"""Misc helper functions""" + +import logging +from typing import Dict, Optional + + +def convert_units( + value: float, unit: str, unit_conversions: Dict[str, float] +) -> Optional[float]: + """Convert a given value and unit into a different unit. + If unrecognized unit, return None""" + try: + conversion = unit_conversions[unit] + except KeyError: + logging.error("unrecognized unit: %s", unit) + return None + return value * conversion diff --git a/pipeline/beam_etl/tests/test_convert_units.py b/pipeline/beam_etl/tests/test_convert_units.py new file mode 100644 index 0000000..cee3f4e --- /dev/null +++ b/pipeline/beam_etl/tests/test_convert_units.py @@ -0,0 +1,24 @@ +"""Test the `convert_units`""" + +from helpers.dimensions import UNIT_CONVERSIONS as dimension_unit_conversions +from helpers.misc import convert_units + + +def test_units_to_cm_inches(): + """Test `convert_units` to cm from inches""" + assert convert_units(0.5, "inches", dimension_unit_conversions) == 1.27 + + +def test_units_to_cm_feet(): + """Test `convert_units` to cm from feet""" + assert convert_units(0.5, "feet", dimension_unit_conversions) == 15.24 + + +def test_units_to_cm_cm(): + """Test `convert_units` to cm from cm""" + assert convert_units(0.5, "cm", dimension_unit_conversions) == 0.5 + + +def test_units_to_cm_unrecognized(): + """Test `convert_units` to cm from unrecognized unit""" + assert convert_units(0.5, "yard", dimension_unit_conversions) is None diff --git a/pipeline/beam_etl/tests/test_dimensions.py b/pipeline/beam_etl/tests/test_dimensions.py index 11514b6..37821f5 100644 --- a/pipeline/beam_etl/tests/test_dimensions.py +++ b/pipeline/beam_etl/tests/test_dimensions.py @@ -1,6 +1,6 @@ """Test the `parse_dimensions` function and its helpers""" -from helpers.dimensions import parse_dimensions, parse_dimensions_measure, units_to_cm +from helpers.dimensions import parse_dimensions, parse_dimensions_measure def test_none(): @@ -91,23 +91,3 @@ def test_parse_dimensions_missing(): "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