2023-06-23 10:05:24 +02:00
|
|
|
"""Test the `parse_dimensions` function and its helpers"""
|
|
|
|
|
2023-06-23 10:30:07 +02:00
|
|
|
from helpers.dimensions import parse_dimensions, parse_dimensions_measure
|
2023-06-23 10:05:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|