Skip to content

Commit a4e879a

Browse files
committed
Use hypothesis for __mul__ Percentage tests
We make the tests more generic, testing with all constructors, and use hypothesis to have a better (and more random) coverage. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 7c754a6 commit a4e879a

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

tests/timeseries/test_quantities.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -544,31 +544,37 @@ def test_abs() -> None:
544544
assert abs(-pct) == Percentage.from_fraction(30)
545545

546546

547-
def test_quantity_multiplied_with_precentage() -> None:
547+
@pytest.mark.parametrize("quantity_ctor", _QUANTITY_CTORS + [Quantity])
548+
# Use a small amount to avoid long running tests, we have too many combinations
549+
@hypothesis.settings(max_examples=10)
550+
@hypothesis.given(
551+
quantity_value=st.floats(
552+
allow_infinity=False,
553+
allow_nan=False,
554+
allow_subnormal=False,
555+
# We need to set this because otherwise constructors with big exponents will
556+
# cause the value to be too big for the float type, and the test will fail.
557+
max_value=1e298,
558+
min_value=-1e298,
559+
),
560+
percent=st.floats(allow_infinity=False, allow_nan=False, allow_subnormal=False),
561+
)
562+
def test_quantity_multiplied_with_precentage(
563+
quantity_ctor: type[Quantity], quantity_value: float, percent: float
564+
) -> None:
548565
"""Test the multiplication of all quantities with percentage."""
549-
percentage = Percentage.from_percent(50)
550-
power = Power.from_watts(1000.0)
551-
voltage = Voltage.from_volts(230.0)
552-
current = Current.from_amperes(2)
553-
energy = Energy.from_kilowatt_hours(12)
554-
percentage_ = Percentage.from_percent(50)
555-
556-
assert power * percentage == Power.from_watts(500.0)
557-
assert voltage * percentage == Voltage.from_volts(115.0)
558-
assert current * percentage == Current.from_amperes(1)
559-
assert energy * percentage == Energy.from_kilowatt_hours(6)
560-
assert percentage_ * percentage == Percentage.from_percent(25)
561-
562-
power *= percentage
563-
assert power == Power.from_watts(500.0)
564-
voltage *= percentage
565-
assert voltage == Voltage.from_volts(115.0)
566-
current *= percentage
567-
assert current == Current.from_amperes(1)
568-
energy *= percentage
569-
assert energy == Energy.from_kilowatt_hours(6)
570-
percentage_ *= percentage
571-
assert percentage_ == Percentage.from_percent(25)
566+
percentage = Percentage.from_percent(percent)
567+
quantity = quantity_ctor(quantity_value)
568+
expected_value = quantity.base_value * (percent / 100.0)
569+
print(f"{quantity=}, {percentage=}, {expected_value=}")
570+
571+
product = quantity * percentage
572+
print(f"{product=}")
573+
assert product.base_value == expected_value
574+
575+
quantity *= percentage
576+
print(f"*{quantity=}")
577+
assert quantity.base_value == expected_value
572578

573579

574580
def test_invalid_multiplications() -> None:

0 commit comments

Comments
 (0)